//Load <a> with 'rel="external"' attributes in another page
//Made to made page validate with having the target attributes in the <a> tag
function externalLinks() {
	if (!document.getElementsByTagName) return;
	var alla = document.getElementsByTagName("a");
	for (var i=0; i<alla.length; i++) {
		var a = alla[i];
		if(a.getAttribute("href") && a.getAttribute("rel") == "external")
			a.target="_blank";
	}
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

$(function(){

	if($.client.browser == 'Firefox'){
		if($.client.os == 'Mac'){
			$('#content button span,#content .nice_form button span').css({'left':'-15px','top':'-7px'});
		}
		else{
			$('#content button span,#content .nice_form button span').css({'left':'-15px','top':'-6px'});
		}
	}

	var api = $("#overlay").overlay({
		api:true,
		expose: '#97A0A7',
		finish:{
			top: 'center',
			left: 'center',
			width: 0,
			absolute: true
		}
	});

	// define function that opens the overlay
	window.openOverlay = function() {
		api.load();
	}
	// define function that close the overlay
	window.closeOverlay = function() {
		api.close();
		window.setTimeout(function() {
 			$("#overlay").hide();
			},
		250);
	}

	//Add Active class to the "active" section <li> in Main Nav
	if(self.location.href.indexOf('index.php') != -1){
		var url = self.location.href.substr((self.location.href.indexOf('index.php')+9));
		if(url != false && url != ''){
			if(url.indexOf('/societies') != -1){
				$('#nav_main a[href*=societies]').parent('li').addClass('active');
			}
			else if(url.indexOf('/users/register') != -1){
				$('#nav_main a[href*=register]').parent('li').addClass('active');
			}
			else if(url.indexOf('/users/edit') != -1){
				$('#nav_main a[href*=edit]').parent('li').addClass('active');
			}
			else if(url.indexOf('/contacts') != -1){
				$('#nav_main a[href*=contact]').parent('li').addClass('active');
			}
			else if(url == '/' || url.indexOf('/users') != -1 || url.indexOf('/search') != -1 || url.indexOf('/#Users') != -1 && (url.indexOf('/') == -1 && url.indexOf('/edit') == -1 && url.indexOf('/login') == -1 && url.indexOf('/xml') == -1 && url.indexOf('/register') == -1 && url.indexOf('/advanced_search') == -1)){
				$('#nav_main li:first').addClass('active');
			}
		}
	}
	else{
		$('#nav_main li:first').addClass('active');
	}

	//Render the a href with "rel=external" with "target=_blank"
	//this make "_blank" link validate w3c
	externalLinks();
/*
	$(".nice_form :input").tooltip({

		// place tooltip on the right edge
		position: ['center', 'right'],

		// a little tweaking of the position
		offset: [-2, -80],

		// use a simple show/hide effect
		effect: 'toggle',

		// custom opacity setting
		opacity: 0.7
	});
*/

});
