/**
 * SOME USEFUL FUNCTIONS
 **/
(function($) {
	$.fn.extend({
		isChildOf: function(filter_string) {
			var parents = $(this).parents().get();
			for (j = 0; j < parents.length; j++) {
				if ($(parents[j]).is(filter_string))
					return true;
			}
			return false;
		}
	});
})(jQuery); 

function stripUrl(url) {
	url = url.replace("http:\/\/","");
	url = url.replace(document.domain,''); 
	return url;
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString()+
	";path=/");
}

function ReadCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function clickExternalLink() {
	if (_gaq) _gaq.push(['_trackEvent','outgoing',this.href.replace("http:\/\/","")]);
}



/**
 * DOCUMENT READY
 **/
$(document).ready(function () {
	initPage();

	// ajax
	enableAjax();

	// settings
	$('div#menu div#settings_btn').click(clickSettings);
	
	// track outgoing links
	$("div#content a[href^='http://']").click(clickExternalLink); 
});



/**
 * AJAX
 **/
var hash;
var title;

function enableAjax() {
	if (ReadCookie('ajax') != '0')
		$('a[rel=ajax]').live('click',clickAjax);
}

function disableAjax() {
	$('a[rel=ajax]').die('click',clickAjax);
}

function initPage() {   
	hash = stripUrl(new String(document.location))

	var hash_path = hash.split('/');
	if (hash_path.length >= 2) {
		hash_path = hash_path[1];
		if (hash_path.length > 0)
			hash_path += '/';
	
		//highlight the selected link	
		$('a[href=/' + hash_path + ']').addClass('selected');
	}

}

function clickAjax() {
	hash = stripUrl(this.href.replace(/^.*#/,''));

	//clear the selected class and add the class class to the selected link
 	if ($(this).isChildOf('div#menu')) {
 		$('div#menu a[rel=ajax]').removeClass('selected');
 		$(this).addClass('selected');
 	}

 	//hide the content and show the progress bar
 	$('#content_holder').hide();
 	$('#loading').fadeIn('fast');

	//history.pushState({}, title, hash);

	loadPage();

	//cancel the anchor tag behaviour
	return false;	
}
	
function loadPage() {
	$.ajax({
		url: '/ajax' + hash,	
		type: "GET",
		success: function (html) {
			$('#loading').fadeOut('fast');	
			$('#content').html(html);
			$('#content_holder').fadeIn('slow');

			// google analytics.
			if (hash) _gaq.push(['_trackPageview',hash]);
		},
		error:function (xhr,ajaxOptions,thrownError) {
			$('#loading').hide();	
			alert('An error occured (error ' + xhr.status + ')');			
		}			
	});
}

function getFlashPage(url) {
	trackPageview(url);
}

function trackPageview(url) {
	_gaq.push(['_trackPageview',url]);
}


/**
 * SETTINGS
 **/
function clickSettings() {
	$('#settings_btn').toggleClass('btn_open');
	$('#settings').slideToggle('normal',settingsState);
	return false;
}

function settingsState() {
	if ($(this).is(':hidden')) {
		// disable settings listeners
		$('a[rel=switchStyle]').unbind('click',clickSettingStyle);
		$('input#setting_ajax').unbind('change',changeSettingAjax);		
	} else {
		// enable settings listeners
		$('a[rel=switchStyle]').click(clickSettingStyle);
		$('input#setting_ajax').change(changeSettingAjax);						
	}
}
 
function changeSettingAjax() {
	if (this.checked) {
		setCookie('ajax','1',365);
		enableAjax();		
	} else {
		setCookie('ajax','0',365);
		disableAjax();			
	}
}

function clickSettingStyle() {
	setCookie("style",this.title,365);	

	if (hash)
		location.href = stripUrl(hash);

	return false;	
}
