
$(document).ready(function(){
	/*Ajaxed links*/
	var $ie6 = false;
	if( $.browser.msie ){
	    var versionParts = jQuery.browser.version.split('.');
	    if( parseInt(versionParts[0])==6 ){
		$ie6 = true;
	    }
	}
	if(!$ie6){
	    $('a').each(replaceLinks);
	    initHistory();
	}
	$('#main-menu li a').each(function(){
		$(this).parent().attr('current-status','off');
	});
	$('#main-menu li a.menu-trigger').mouseover(function(){
		var parentLi=$(this).parent();
		var status=parentLi.attr('current-status');
		var bgPos=parentLi.attr('bgPos');
		if (status == 'off') {
			$(this).css(
			   {backgroundPosition: '0px 30px'}
			).animate({backgroundPosition: '(0px 0px)'},800,'easeOutBack');
			parentLi.attr('current-status', 'on');
		}
		if (status == 'on') {
			/*
			var to = parentLi.attr('current_to');
			clearTimeout(to);
			parentLi.attr('current_to',null);
			*/
		}
	});
	$('#main-menu li a').mouseout(function(){
		var parentLi=$(this).parent();
		var status=parentLi.attr('current-status');
		if (status == 'on') {
			/*var to=setTimeout('hideMenuElements("#'+parentLi.attr('id')+'")',10);
			parentLi.attr('current_to',to);*/
			hideMenuElements("#"+parentLi.attr('id'))
		}
	});
	/*$('#main-menu li ul a').mouseout(function(){
		var parentLi=$(this).parent().parent().parent();
		var parentUl=$(this).parent().parent();
		var status=parentLi.attr('current-status');
		if (status == 'on') {
			var to=setTimeout('hideMenuElements("#'+parentLi.attr('id')+'")',10);
			parentLi.attr('current_to',to);
		}
	});
	$('#main-menu li ul a').mouseover(function(){
		var parentLi=$(this).parent().parent().parent();
		var parentUl=$(this).parent().parent();
		var status=parentLi.attr('current-status');
		if (status == 'on') {
			var to = parentLi.attr('current_to');
			clearTimeout(to);
			parentLi.attr('current_to',null);
		}
	});*/
});
function hideMenuElements(elemId){
	$(elemId).children('a').animate({backgroundPosition: '(0px 100px)'},1000,'easeInBack');
	$(elemId).attr('current-status','off');
}

function replaceLinks(){
	var hr = $(this).attr('href');
	var tg = $(this).attr('target');
	var rel = $(this).attr('rel');
	if (hr != undefined
		&& hr.slice(0, 7) != "http://"
		&& hr.slice(0, 7) != "mailto:"
		&& hr.slice(0, 11) != "javascript:"
		&& tg!="_blank"
		&& rel==undefined||rel=="internal"
	){
		/*$.ajax({
			url: this.href,
			type: 'GET',
			dataType: 'html',
			timeout: 10000,
			error: function(){
				$('#content').load('/error404');
			},
			success: function(xml){
				$('#content').html(xml);
				$('#content a').click(replaceLinks);
			}
		});*/

		$(this).attr('href','#'+hr);
		$(this).attr('rel','history');
	}else{
		return true;
	}
	return true;
}

function pageload(hash) {
	// hash doesn't contain the first # character.

	if(hash) {
		// restore ajax loaded state
		$('#visual_feedback').fadeIn('fast');
		$("#ajax_content").load(hash,function(){
			replaceContentLinks();
			$("a[@rel='history']").unbind('click');
			$("a[@rel='history']").click(function(){
		        $.history.load(this.href.replace(/^.*#/, ''));
		        return false;
		    });
			$('#visual_feedback').fadeOut('fast');
			//$.history.init(pageload);
		});
	} else {
		// start page
		//$("#load").empty();
	}
}
function replaceContentLinks(){
	$('#ajax_content a').each(replaceLinks);
}
function initHistory(){
	// Initialize history plugin.
	// The callback is called at once by present location.hash.
	$.history.init(pageload);

	// set onlick event for buttons
	$("a[@rel='history']").click(function(){
		//
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		// moves to a new page.
		// pageload is called at once.
		$.history.load(hash);
		return false;
	});
}

