var onglets=new Array;
var ongletWidth=175;
var logoWidth=95;
var attribute="";
var ongletsUrl=new Array;


function initScript(){
	init_navigator();
	if(Ostart) {Oinit(Ostart);ongletsUrl[Ostart]=document.location.href.split(domain)[1];}
	else {Oinit(0,1);ongletsUrl[0]='/';}

	reHeight();
	$(window).resize(function(){reHeight();});
	$('.titre_onglet').click(function(){
		moveOnglet($(this).parent().parent());
		return false;
	});
	initAjax();
	do_refresh();
}

function reHeight(){
	$(".fullHeight").height($(window).height());
	$(".onglet").height($(window).height());

	if(onglets.length==1){
		oWidth=($(window).width()-3*ongletWidth-logoWidth);
		Oopen(onglets[0],oWidth,1);
	} else if(onglets.length!=0) {
		oWidth=($(window).width()-3*ongletWidth-logoWidth)/2;
		Oopen(onglets[0],oWidth,1);
		Oopen(onglets[1],oWidth,1);
	}
	

	$(".content", ".fullHeight").height($(window).height()-40).each(function(){
		api=$(this).data('jsp');
		api.reinitialise();
	});
	
	$('#logo').unbind('click').click(function(){moveOnglet($('#home'));});
}

function initScroll(Elmt){
	$(Elmt).width($(Elmt).width()+20);
	$(Elmt).parent().css('overflow','hidden');
	$(Elmt).jScrollPane();
}

function moveOnglet(Elmt){
	// Détection si ouvert ou fermé
	if($(Elmt).attr('id')=='home') openedHome=1;
	else openedHome=0;

	for (var key in onglets) {
	    if (onglets[key] == $(Elmt).attr('id')) {
	        onglets.splice(key, 1);
	        	Oclose($(Elmt).attr('id'));
	        	if(onglets.length && onglets[0]!='home'){
	    			oWidth=$(window).width()-3*ongletWidth-logoWidth;
	    	    	Oopen(onglets[0],oWidth);
	        	} else {
	        		onglets.pop();
	    			oWidth=$(window).width()-3*ongletWidth-logoWidth;
	    			Oopen('home',oWidth-20);
	        	}
	        	return;
	    } else if(onglets[key]=='home') openedHome=1;
	}
	if(onglets.length>1){
		Oclose(onglets[1]);
		onglets.pop();
	}
	onglets.unshift($(Elmt).attr('id'));
	if(onglets.length==1){
		if(!openedHome){
			oWidth=$(window).width()-3*ongletWidth-logoWidth;
			Oopen($(Elmt).attr('id'), oWidth);
		} else {
			onglets.pop();
			oWidth=$(window).width()-3*ongletWidth-logoWidth;
			Oopen("home", oWidth-20);	
		}
	} else {
		oWidth=($(window).width()-3*ongletWidth-logoWidth)/2;
		for (var key in onglets) {
        	/*$('.content',"#"+onglets[key]).animate({
    			width:oWidth+'px',
    		},500,'easeOutCirc',function(){$(this).jScrollPane();});*/
		    if (onglets[key] != 'home') {
		    	Oopen(onglets[key], oWidth);
		    } else {
		    	Oopen("home", oWidth-10);
		    }
		}
	}
	if(!openedHome) Oclose('home');
	return false;
}

function Oinit(nmb, Otime){
	oWidth=($(window).width()-3*ongletWidth-logoWidth);
	if(!nmb){
		oWidth+=-20;
		$(".content", ".fullHeight").each(function(){ initScroll(this);});
		$('.container').each(function(current){
			if(current==3) $(this).animate({width:oWidth+'px'},Otime,'easeOutCirc');
			else $(this).animate({width:10+'px'},Otime,'easeOutCirc');
		});
		$('.jspPane','#home').animate({width:oWidth-20+'px'},Otime,'easeOutCirc');
		$('.jspContainer','#home').animate({width:oWidth-20+'px'},Otime,'easeOutCirc');
		$('.content',"#home").animate({width:oWidth-20+'px'},Otime,'easeOutCirc',function(){
			var api=$('.content',"#home").data('jsp');
			api.reinitialise();
		});
		onglets[0]='';
		onglets[1]="";
	}
	else {
		var elmtId=(nmb==3?'#chroniques':(nmb==2?'#empreintes':'#cabinet'));
		$(".content", ".fullHeight").each(function(){ initScroll(this);});
		$('.container','.fullHeight').each(function(current){
			if((3-current)==nmb) $(this).animate({width:oWidth+'px'},Otime,'easeOutCirc');
			else $(this).animate({width:10+'px'},Otime,'easeOutCirc');
		});
		$('.jspPane',elmtId).animate({width:oWidth-20+'px'},Otime,'easeOutCirc');
		$('.jspContainer',elmtId).animate({width:oWidth-20+'px'},Otime,'easeOutCirc');
		$('.content',elmtId).animate({width:oWidth-20+'px'},Otime,'easeOutCirc',function(){
			var api=$('.content',elmtId).data('jsp');
			api.reinitialise();
		});
		onglets[0]=elmtId.replace('#','');
		onglets[1]="";
		onglets.pop();
	}
	
}

function Oclose(Elmt){
	$('.container',"#"+Elmt).animate({
		width:'10px'
	},800,'easeOutCirc');
}
function Oopen(Elmt, oWidth, speed){
	if(!Elmt) return;
	if(speed!=1) speed=700;
	// Si l'onglet est fermé, recalibrage de la largeur
	if($('.container',"#"+Elmt).width()==10 || $('.container',"#"+Elmt).width()===0){
		$('.jspPane',"#"+Elmt).width(oWidth-20);
		$('.jspContainer',"#"+Elmt).width(oWidth-20);
		$('.content',"#"+Elmt).width(oWidth-20);
		var api=$('.content',"#"+Elmt).data('jsp');
		api.reinitialise();
	} else {
		$('.content',"#"+Elmt).animate({width:oWidth-20+'px'},speed,'easeOutCirc',function(){
			var api=$('.content',"#"+Elmt).data('jsp');
			api.reinitialise();
		});
		$('.jspPane',"#"+Elmt).animate({width:oWidth-20+'px'},Math.max(speed-10,1),'easeOutCirc');
		$('.jspContainer',"#"+Elmt).animate({width:oWidth-20+'px'},Math.max(speed-10,1),'easeOutCirc');
	}

	//#cabinet .jspPane, #cabinet .jspContainer {width:300px !important;}
	$('.container',"#"+Elmt).animate({width:oWidth+'px'},speed,'easeOutCirc');
}

function init_navigator() {
	if (navigator.userAgent.indexOf('Firefox',0)!=-1) attribute='firefox';
	if (navigator.userAgent.indexOf('Iceweasel',0)!=-1) attribute='linux';
	if (navigator.userAgent.indexOf('Safari',0)!=-1) attribute='safari';
	if (navigator.userAgent.indexOf('MSIE 6',0)!=-1) attribute='explorer6';
	if (navigator.userAgent.indexOf('MSIE 7',0)!=-1) attribute='explorer7';
	if (navigator.userAgent.indexOf('Opera',0)!=-1) attribute='opera';
	if (attribute!='explorer6'&& attribute!='explorer7'&&navigator.userAgent.indexOf('MSIE',0)!=-1) attribute='explorer';
	feuilles=document.getElementsByTagName('link');
	for (var i=0; i<feuilles.length;i++) {
	if((feuilles[i].getAttribute('rev')!=attribute)&&(feuilles[i].getAttribute('rev'))) feuilles[i].disabled='true'; 
	}
	if(attribute=='explorer6') {animFadeMot=0;animFadeText=0;}
}


function initAjax(){
	$('a').each(function(){
		if($(this).attr('href').indexOf('/')<4){
			if(!$(this).attr('onclick')) $(this).unbind('click').click(function(){
				var Onmb=parseInt($(this).attr('rel'));
				if(!isNaN(Onmb)){
					var Otarget=(Onmb==3 ? "#chroniques" : (Onmb==2 ? "#empreintes" : (Onmb==1 ? "#cabinet" : "#home")));
					if("#"+onglets[0]!=Otarget && "#"+onglets[1]!=Otarget) Oinit(Onmb, 700);
				}
				if(ongletsUrl[Onmb]!=$(this).attr('href')) return openAjax($(this).attr('href'));
				else return false;
			});
		}
	});
	$('.onglet').unbind('click').click(function(){moveOnglet($(this).parent());});
}

function updateUrl(href){
	var doc_url=document.location.href;
	tmp=doc_url.split('#');
	if(doc_url.indexOf('#')==-1){tmp[1]='';tmp[0]=doc_url;}
	//suffix=href.split(domain);
	current_page=href;
	document.location=tmp[0]+'#'+href;
}

function do_refresh(){
	setTimeout('do_refresh();',50);
	doc_url=document.location.href;

	if(doc_url.indexOf('#')==-1) {
		tmp=doc_url.split(domain);
		detected=tmp[1];
	}
	else {
		tmp=doc_url.split('#');
		detected=tmp[1];
	}
	if(current_page==detected) return;
	current_page=detected;
	openAjax(detected);
}

function openAjax(href){
	updateUrl(href);
	$('body').css('cursor','progress');
	$.ajax({
		url:(href.indexOf('http://')==-1 ? domain : "")+href+"?ajax=1",
		cache:false,
		timeout:4000,
		error: function(){openAjax(href);},
		success: function(htmlTxt){
			var objPage=eval("("+htmlTxt+")");
			var Otarget=(objPage.onglet==3 ? "#chroniques" : (objPage.onglet==2 ? "#empreintes" : (objPage.onglet==1 ? "#cabinet" : "#home")));
			document.title=objPage.title;
			$('.similaires',Otarget).html(objPage.similaires);
			$('.jspPane',Otarget).html(objPage.page);
			api=$('.content',Otarget).data('jsp');
			api.reinitialise();
			$('.content',Otarget).hide().fadeIn(300);
			$("body").css({cursor:'default'});
			ongletsUrl[objPage.onglet]=href;
			initAjax();
			if("#"+onglets[0]!=Otarget && "#"+onglets[1]!=Otarget) Oinit(objPage.onglet, 700);
		}
	});
	return false;
}

initScript();
