/*
 * Script by Pâris MEULEMAN
 * Copyright © 2010 EUROBEND S.A. 
 */
var ajaxquery;
var hash = location.hash;
var hashwait = false;
if(!window.location.href.match('.*(edit|write).*'))
{
window.addEvent('domready', function(){
	loadlinks();
	loadajaxlink();
	parseurl();
	setInterval(function()
	{
		if (location.hash != hash && !hashwait)
		{
			parseurl();
		}
	}, 100)
});
}
function loadajaxlink()
{
	jQuery(".loadajaxlink").each(addclicktocontent);
}
var addclicktocontent= function(ind,ele){
	jQuery(ele).unbind("click");
	jQuery(ele).bind("click",		function(){
		if(jQuery(ele).parent().is('li') && !jQuery(ele).parent().is('.parent'))
			{collapsesiblings(ele); }
		jQuery("#accordionMenu .active").removeClass("active");	
		loadjacontentwrap(ele.href);	
		return false;			
	});
}		
function bindclick(ele){
	jQuery(ele).unbind("click");
	jQuery(ele).bind("click",
		function(){
			loadContainerWrap2(ele.href);
			return false;
		}
	);
}


function collapsesiblings(ele){
	try{
		$(jQuery(ele).parent().parent().attr('id')).Accordion.display(-1);
		jQuery(ele).parent().parent().children('.parent').children('ul').each(
			function(id,ele){
				$(jQuery(ele).attr('id')).Accordion.display(-1);	
			});
	}catch(e){}
}
function parseurl(){
	if(window.location.hash.length > 0 && !window.location.href.match(new RegExp( "^(.*index\.php)$")	))
	{
		target="http://"+window.location.host+window.location.hash.substring(1,window.location.hash.length);
		jQuery('#ja-mainbody-fr').hide();
		jQuery("#ja-mainnav .menu .active").removeClass("active");
		jQuery("#accordionMenu .active").removeClass("active");
		jQuery("#ja-container2").css({height: '140px'});
		loadContainerWrap2(target);
	}
	else
	{
		if(window.location.href.match(new RegExp(	"^(http://"
													+window.location.host.replace(".","\.")
													+")(/)?([A-Za-z]{2})?(/)?(index\.php.*)?$"
		)))
		{
			jQuery("#ja-mainnav .menu .active").removeClass("active");
			setTimeout(function(){
				if(!(window.location.hash.length > 0) || window.location.href.match(new RegExp( "^(.*index\.php)$")))
				{
					jQuery("#ja-mainnav .menu .item1 a").trigger("click");
				}
			}, 13500);
		}
		else
		{
			activateurl(window.location.href,false);	
		}
	}
}
function loadlinks(){
	jQuery("a").not(".loadajaxlink").not('.noajax').not('#lbNextLink').not('#lbPrevLink').not('#lbCloseLink').not(".rawimages a").not('.item25 a').not('.item15 a').not('.item6 a').not(".logo a").each(
		function(ind,ele){
			if(ele.target != "_blank" && !ele.href.match(new RegExp("^.*(print=|mailto|\.pdf).*$"))  && ele.href.match(new RegExp("^(http://"+window.location.host.replace(".","\.")+").*")))
			{
				bindclick(this);
			}
	});
}
var activateurl = function(url,doexpand){
	var doexpand = doexpand || false;
	jQuery(".active").removeClass("active");		
	jQuery(".menuajaxlink").each(function(ind,ele)
	{
		if(url.match("^.*("+ele.href+")(/.*)?$"))
		{
			jQuery(ele).parent("li").addClass("active");
		}
	});
	jQuery(".loadajaxlink").each(function(ind,ele)
	{
		if(url.match("^.*("+ele.href+")(/.*)?$"))
		{
			jQuery(ele).addClass("active");
			if(jQuery(this).parent('li.parent') !== undefined)
			{
				if(doexpand){
					jQuery(this).trigger('onclick');
				}
				jQuery(ele).parent().parent().siblings('.separator').addClass("active");
				jQuery(ele).parent().parent().parent().parent().siblings('.separator').addClass("active");
			}
		}
	});
}
function loadSomething(target,toptop,top, processedid, processed, reloadall){
	tar=window.location.href.split('#')[0];
	if(tar.match(new RegExp(	
			"^(http://"
			+window.location.host.replace(".","\.")
			+")(/)([A-Za-z]{2})(/.+)$"))){
			tar=tar.substring(0,window.location.host.length+10);
		}
	
	if(target.match(".*(contact|edit).*")){
		window.location=target;
		return;
	}	
	hashwait=true;
	window.location = tar+'#'+target.substring(target.split( '/' )[2].length+7, target.length);	
	hash = location.hash;
	hashwait=false;
	jQuery.ajax({type: 'GET', url: target, timeout: 10000,success: function(data){
		jQuery(top).queue('fx', function(){
			if(ajaxquery==target)
			{
				jQuery(processed).remove();
				if(jQuery(data).find(processed).is(processed))
					{jQuery(top).append(jQuery(data).find(processed));}
				else
				{
					jQuery(top).append(
						"<div id='"+processedid.substr(1,processedid.length)+"' class='"+processed.substr(1,processed.length)+"'>"
						+'	<dl id="system-message">'
						+'		<dt class="error">Error</dt>'
						+'		<dd class="error message fade">'
						+'			<ul>'
						+'				<li>There was an error while loading the requested page.</li>'
						+'			</ul>'
						+'		</dd>'
						+'	</dl>'
						+"</div>"
					);
				}
				if(reloadall){
					loadAccordionMenu();
				}
				jQuery(" #jflanguageselection .rawimages").remove();
				jQuery("#jflanguageselection").append(jQuery(data).find(" #jflanguageselection .rawimages"));
				loadajaxlink();
				loadlinks();
				if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {	jQuery(function($) {		$("a[rel^=\'lightbox\']").slimbox({/* Put custom options here */  /* BEGIN */   loop: 1, overlayOpacity: 0.8,	overlayFadeDuration: 400,resizeDuration: 400, initialWidth: 250, initialHeight: 250 , imageFadeDuration: 400, captionAnimationDuration: 400 , closeKeys: [27, 88, 67] , previousKeys: [37, 80] , nextKeys: [39, 78] , counterText: "Image {x} of {y}" /* END */ }, null, function(el) {			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));		});	});}
				//Shadowbox.clearCache();
				//Shadowbox.setup();
//jQuery("#ja-contentwrap").slideDown(1000);
			}
			if(ajaxquery==target)
			{
				if(reloadall){
					jQuery(toptop).animate({height: jQuery(top).height()},700,function(){
						jQuery(top).fadeIn(200);
						jQuery(".menuloading").fadeOut(200,function(){jQuery(this).remove()});
						jQuery(toptop).css({height: 'auto'});
						jQuery("#ja-footerwrap").hide().show();
						activateurl(target,reloadall);
					});
				}else{
					jQuery(toptop).animate({height: Math.max(jQuery(top).outerHeight(true), jQuery('#ja-col1').outerHeight(true))},400,function(){
						jQuery(top).fadeIn(200);
						jQuery(".menuloading").fadeOut(200,function(){jQuery(this).remove()});
						jQuery(toptop).css({height: 'auto'});
						activateurl(target,reloadall);
					});
				}
			}
			if(ajaxquery==target)
			{ 
				ajaxquery=undefined;
			}
			jQuery(top).dequeue();
		});
	},
	error: function(){
		jQuery(top).queue('fx', function(){
			if(ajaxquery==target)
			{
				jQuery(processed).remove();
				jQuery(top).append(
					"<div id='"+processedid.substr(1,processedid.length)+"' class='"+processed.substr(1,processed.length)+"'>"
					+'	<dl id="system-message">'
					+'		<dt class="error">Error</dt>'
					+'		<dd class="error message fade">'
					+'			<ul>'
					+'				<li>There was an error while loading the requested page.</li>'
					+'			</ul>'
					+'		</dd>'
					+'	</dl>'
					+"</div>"
				);
				if(reloadall){
					jQuery(toptop).animate({height: jQuery(top).height()},700,function(){
						jQuery(top).fadeIn(200);
						jQuery(".menuloading").fadeOut(200,function(){jQuery(this).remove()});
						jQuery(toptop).css({height: 'auto'});
						jQuery("#ja-footerwrap").hide().show();
					});
				}else{
					jQuery(toptop).animate({height: Math.max(jQuery(top).outerHeight(true), jQuery('#ja-col1').outerHeight(true))},400,function(){
						jQuery(top).fadeIn(200);
						jQuery(".menuloading").fadeOut(200,function(){jQuery(this).remove()});
						jQuery(toptop).css({height: 'auto'});
					});
				}				
			}
			jQuery(top).dequeue();
		});
	}});	
	if(reloadall){
		jQuery(toptop).css({height: Math.max(jQuery(toptop).height(), 140)});
		jQuery(top).fadeOut(300, function(){
	});
	}else{
		jQuery(top).fadeOut(300);
	}
	//~ try{
		//~ jQuery(".menuLoading").remove();
	//~ }catch(e){/* no problem */}
	ajaxquery=target;
	activateurl(target);
	jQuery(toptop).prepend("<img alt='Loading' src='/templates/ja_purity/images/loading.gif' class='menuloading' style='position: absolute; top: "+(jQuery("#ja-headerwrap").height()+jQuery("#ja-mainnavwrap").height()+30)+"px; left: 40%;'/>");

}	
function loadContainerWrap2(target){
	loadSomething(target,'#ja-container2',"#ja-container3","#ja-mainbody-fr",".ja-containerwrapclass",true);
	if(jQuery.browser.msie && parseFloat(jQuery.browser.version) < 7){
//~ //do other stuff
//~ return;
	}
}
function loadjacontentwrap(target){
	loadSomething(target,"#ja-container3","#ja-contentwrap","#ja-content",".ja-contentwrapclass",false);
}

