var domain = "http://"+window.location.hostname;
var index = 0;
var windowWidth=1024;
var windowHeight=768;
var nbrPages = 1;
var hash = window.location.hash;
var speed = 5000;
var arrayChildren;
var arrayChildrenG;
var moverIndex = 0;
var movable = true;


$(document).ready(function(){//on attend que la page soit charg�e
	// alert(hash);
	windowWidth=$(window).width();
	windowHeight=$(window).height();
	
	if ($('#canvas').length>0){
		
		nbrPages = $('.page').length;
		
		$('#menu').width((windowWidth*1.3)*nbrPages);
		$('#menu').bind("moveReady",function(){
			//if (movable)
			//{
				movable=false;
				moveTo(moverIndex);	
			//}
			
		});
		
		
		$('.fancy').fancybox();
		$('.ul_gal').wslide({width: 590,height: 350,horiz: false,pos:1,duration:1000});
		train();
		onResize();
		ManageWindowSize();
		ballons();
		autoNav();
	
		loaded();
		
		$(window).trigger('resize');
	}
	
	//FUNCTION DE NAVIGATION
	$('.menuLi a').click(function(){
		moverIndex = $('.menuLi').index($(this).parent('li'))%8;
		var i = 0;
		$('.page').each(function(){
			// alert($(this).attr('title')+i);
			if (i<8){
				if ($(this).find('.content').html().length<3){
					ajaxMe($(this).attr('title'),i);	
					// alert(i+"sended");
				}else{
					
				}
			}
			i++;
		});
		$('#menu').trigger('moveReady');
		
	});
	if (hash=="" && $('#nav').hasClass('starter')){
		moveTo(1);
		window.location.hash=$('#menu01 .menuLi:first').next('li').find('a').attr('title');
	}
	$('.partLink').click(function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	fixResolution();
	
	
	
});


function fixResolution()
{
	if ($(window).width()<1170)
	{
		$('#nav')
			.css
			({
				'right':'80%'	
			})
		$('#menuContainer')
			.css
			({
				'left':'-150px'
			})
	}
}

function loaded(){
	
	$('#loading').fadeOut('slow');
	// $('#canvas').show();
	$('body').css({backgroundColor:'#c7b299'});
}

function train(){
	
	$('#train').sprite({fps: 9, no_of_frames: 8}).spToggle();
	$('#train_reverse').sprite({fps: 9, no_of_frames: 8}).spToggle();
	$( "#train" ).draggable({ axis: 'x' });
	$('#train').bind( "dragstop", function(event, ui) {
		// alert(event);
		$('#train_reverse').css({'left':event.clientX-300+"px"});
	});
	
	/* $('#train').bind("moved",function(){
		$('#train').bind( "drag", function(event, ui) {
			
			if (event.clientX>2*windowWidth/3){
				$('#train').unbind( "drag");
				if(index<$('.page').length-1){
					
					$('#train').trigger({type:"dragstop",'event':event,'ui':ui});
					$('#train').css({'left':event.clientX-300+"px"});
					moveTo(index+1);
				}
			}else if (event.clientX<windowWidth/4){
				$('#train').unbind( "drag");
				if(index>0){
					// $('#train').unbind( "drag");
					$('#train').trigger({type:"dragstop",'event':event,'ui':ui});
					$('#train').css({'left':event.clientX-300+"px"});
					moveTo(index-1);
				}
			}
		});
	}); */
	$('#train').trigger("moved");


}

function ballons(){

	$('.ballon').draggable()
				.each(function(){
					$(this).css({'left': Math.floor(Math.random()*(windowWidth-400))+200});
				});
				
	$('.ballon').bind( "dragstop", function(event, ui) {
		$(this).animate({'top':0+"px"}, { "duration": 7000, "easing": "easeOutBounce" });
	});
	

}

function autoNav(){
	if (hash){
		var fauxHash=hash;
		// alert(hash);
		
	}else{
		if (substr(window.location,-4) == "html"){
			loc = loc=substr(window.location,0,strrpos(window.location,'/'));
			var fauxHash ="#"+substr(loc,strrpos(loc,'/')+1);	
			
		}else{
			if (substr(window.location,-1)=='/'){
				loc=substr(window.location,0,strrpos(window.location,'/'));
			}else{
				loc=window.location;
			}
			var fauxHash ="#"+substr(loc,strrpos(loc,'/')+1);
		}
	}
	// alert(fauxHash);
	
	var i =0;
		$('.page').each(function(){
			if (fauxHash=='#'+$(this).attr('title')){
				// alert(i);
				ajaxMe($(this).attr('title'),i);
				goTo(i);
				index=i;
			}
			i++;
		});

}

function ManageWindowSize(){
	
	if (windowWidth<=1024){
		$('.pageCenter').width(windowWidth+"px");
		$('.pageLeft,.pageRight').css({'width':0+"px"});
	}else{
		$('.pageLeft,.pageRight').css({'width':(windowWidth-1024)/2+"px"});
	}
	// alert($(window).height());
	
	$('.page').css({'height':windowHeight+'px','width':windowWidth+'px'});
	
	
	$('#canvas').css({'height':windowHeight+'px','width':windowWidth+'px'});
	
	
	$('#sousSol').css({width:windowWidth*(nbrPages+1)+"px"});
	
	$('#tunnel').css({'height':windowHeight+'px'});
	$('#collines').width($('#canvas').width());
	// $('#lights').css({'left':windowWidth-300+"px"});
	$('#lights').css({'left':(windowWidth-300)-(windowWidth*index)+"px"});
	
	$('.ballons').css({'top':0});
	
	var i = 1;
	$('.menuUl').each(function(){
		$(this).css({'left':(windowWidth*1.3)*i+(300)+'px'});
		i++;
	});
	
	$('.page').width(windowWidth);
	i=0;
	$('.page').each(function(){
		$(this).css({'position':'absolute','left':windowWidth*i+'px'});
		i++;	
	});
	
	
	
	//log($(window).height());
	 //log(windowHeight);
	if (windowHeight>=1000){
		
		$('#menuContainer').css({'bottom':windowHeight-1000+"px"});
		$('#credits').css({'bottom':windowHeight-1000+"px"});
		$('#train,#train_reverse').css({'bottom':60+windowHeight-1000+"px"});
		$('#sousSol').css({'top':1000+"px",height:windowHeight-1000+"px"});
		
		$('#contentBack').css({'height':1000+'px','width':(windowWidth/1.2)+4619+"px"});
		$('#contentFront').css({'height':1000+'px','width':windowWidth*nbrPages,'top':0+"px"});
		$('.pageLeft,.pageRight,.pageCenter').css({'height':windowHeight+'px','top':windowHeight-1000+"px"});
		$('#tunnel,#tunnelLeft').css({'bottom':20+windowHeight-1000+'px'});
		$('#nav').css({'bottom':75+windowHeight-1000+'px'});
		$('#mur').height(1000+"px");
		$('#page07 .content').css({'top':0+"px"});
		$('#lights').css({'bottom':25+(windowHeight-1000)+"px"});
	
	}else{
		
		//$('#menu ul').css({'top':60+"px"});
		$('#menuContainer').css({'bottom':20+"px"});
		$('#credits').css({'bottom':0+"px"});
		$('#train,#train_reverse').css({'bottom':60+"px"});
		$('#contentBack,#contentFront').css({'height':windowHeight+'px'});
		$('#contentBack').css({'height':windowHeight+'px',width:(windowWidth/1.2)+4619+"px"});
		$('#contentFront').css({'height':windowHeight+'px',width:windowWidth*nbrPages+"px"});
		$('.pageLeft,.pageRight,.pageCenter').css({top:-(1000-windowHeight)/5+"px",height:windowHeight+(1000-windowHeight)/5+"px"});
		$('#sousSol').css({'bottom':-469+"px"});
		$('#tunnel,#tunnelLeft').css({'bottom':20+'px'});
		$('#page07 .content').css({'top':(1000-windowHeight)/5+"px"});
		$('#lights').css({'bottom':25+"px"});
		// $('.ballon').css({'top':(1000-windowHeight)/5});
			
		
	}
	
	
	// alert(windowHeig
	
	

}
function onResize(){
$(window).resize(function(){
		windowWidth=$(window).width();
		windowHeight=$(window).height();
		ManageWindowSize();
		
	});
}


function goTo(Nindex){
		index=Nindex;
		$('#lights').css({'left':(windowWidth-300)-(windowWidth*Nindex)+"px"});
		$('.depth1').css({left:-(windowWidth*1.3)*Nindex+"px"});
		
		if (Nindex==1){
			$('#contentFront').css({left:-windowWidth+"px"});
			$('#contentBack').css({left:-(windowWidth/1.2)+"px"});
		}else{
			$('#contentBack').css({left:-(windowWidth/1.2)-((4619-windowWidth)/7)*(Nindex)+"px"});
			$('#contentFront').css({left:-windowWidth*Nindex+"px"});
		}
}

//FUNCTION DE DEPLACEMENT-NAVIGATION
function moveTo(Nindex){
	
	var diffTrain=500;
	
	if(Nindex>index){
		
		diffTrain=100;
		
	}else{
		$('#train').hide();
		$('#train_reverse').show();
	}
	// alert(Nindex);
	if (index==0){
		var widthCorrector = 0;
		var speedCorrector = 0;
		if ($(window).width()<1070)
		{
			widthCorrector = -150;
			speedCorrector = 2;
		}
		
		$('#train,#train_reverse').spStart();
		index=1;
		$('.depth1,#sousSol').animate({left:-windowWidth*1.3+"px"},speed/Nindex,function() {
					$('#train,#train_reverse').spStop();
		});
		$('#train,#train_reverse').animate({left:diffTrain+'px'},speed/3,function() {
					$('#train,#train_reverse').animate({left:300+widthCorrector+'px'},(2*speedCorrector)*speed/3);
		});
		$('#contentFront').animate({left:-windowWidth+"px"},speed/Nindex);
		$('#contentBack').animate({left:-(windowWidth/1.2)+"px"},speed/Nindex);
		$('#lights').animate({'left':-300+"px"},speed/Nindex);
		
	}else if (Nindex!=index && Nindex>=0){
		log(index);
		//if (index==1)
		index=Nindex;
		$('#train,#train_reverse').spStart();
		
		var widthCorrector = 0;
		var speedCorrector = 0;
		if ($(window).width()<1070)
		{
			widthCorrector = -200;
			speedCorrector = 2;
		}
		log(Nindex);
		
		
		$('.depth1,#sousSol').animate({left:-(windowWidth*1.3)*Nindex+"px"},speed,function() {
					$('#train,#train_reverse').spStop();
					$('#train').show();
					$('#train_reverse').hide();
					$('#train').trigger("moved");
					
		});
		$('#train,#train_reverse').animate({left:diffTrain+'px'},speed/3,function() {
					$('#train,#train_reverse').animate({left:300+widthCorrector+'px'},(2*speedCorrector)*speed/3,
						function()
						{
							movable=true;
						}
					);
		});
		// if (Nindex==0){
		// $('#lights').animate({'left':windowWidth-300+"px"},speed/Nindex);
		// }else{
			$('#lights').animate({'left':(windowWidth-300)-(windowWidth*Nindex)+"px"},speed);
		// }
		if (Nindex==1){
			$('#contentFront').animate({left:-windowWidth+"px"},speed);
			$('#contentBack').animate({left:-(windowWidth/1.2)+"px"},speed);
		}else{
			$('#contentBack').animate({left:-(windowWidth/1.2)-((4619-windowWidth)/7)*((Nindex))+"px"},speed);
			$('#contentFront').animate({left:-windowWidth*(Nindex)+"px"},speed);
		}
	}
		
}

function log(msg)
{
	try{
		console.log(msg);
	}
	catch(e)
	{
		
	}
}

function ajaxMe(alias,index){
	// alert('ajax'+index);
	// if (index==7){
				// alert(index);
			// }
	$.ajax({
	   type: "POST",
	   url: domain+"/do/pageLoader.php",
	   data: {docAlias:alias,lang:$('#mur').attr('rel')},
	    success: function(msg){
			// alert(msg);
			var doc = jQuery.parseJSON(msg);
			var contenuAdd="";
			if (doc.additionalContent!=""){
				contenuAdd ='<div class="hidden" id="'+doc.aliasPath+'"><h1>'+doc.title+'</h1>'+doc.additionalContent+'</div>';
			}
			// if (index==7){
				// alert(index);
			// }
			var draft ="";
			if (doc.draft==1){
				draft = "<h2>DRAFT !</h2>"
			}
			
			switch (index){
				case 2:
					var contenu = '<div id="decoLeft"></div><div class="innerContent"><h1>'+doc.title+'</h1> '+draft+' '+edit(doc)+' '+doc.content;
					if (doc.additionalContent){
						contenu+='<a href="#'+doc.aliasPath+'" class="readMore fancy">'+doc.readMore+'</a>';
					}
					contenu+='</div><div id="decoRight"></div>';
				break;
				case 3 :
					var contenu ='<div class="innerContent"><h1>'+doc.title+'</h1> '+draft+' '+edit(doc)+' '+doc.content;
					if (doc.additionalContent){
						contenu+='<a href="#'+doc.aliasPath+'" class="readMore fancy">'+doc.readMore+'</a>';
					}
					contenu+='</div>'
				break;
				case 4:
					var contenu ='<div id="evenements"><div class="innerContent"><h1>'+doc.title+'</h1> '+draft+' '+edit(doc)+' '+doc.content;
					if (doc.additionalContent){
						contenu+='<a href="#'+doc.aliasPath+'" class="readMore fancy">'+doc.readMore+'</a>';
					}
					contenu+='</div></div>';
					$('#page01').bind('childrenLoaded',function(){
						if (arrayChildren.length>0){
						var contenuPlus='<div id="partenaires"><div class="innerContent"><h1>'+doc.partenaires+'</h1> '+draft;
						if (doc.edit=="true"){
							contenuPlus+=' <a href="'+domain+'/admin/manage/edit?parentId='+doc.id+'&typeId=3&lan='+doc.language+'">[Add]</a>';
						}
						// contenuPlus+=add();
							var i =0;
							while (i<arrayChildren.length){
								child=jQuery.parseJSON(arrayChildren[i]);
								// alert(arrayChildren[i]);
								contenuPlus+='<div class="partenaire"><h2><a href="'+child.externalLink+'" class="partLink">'+child.title+'</a></h2> '+draft+' '+edit(child)+' '+child.intro+'</div>';
								i++;
							}
							contenuPlus+="</div></div>";
							// alert(contenuPlus);
							$('#page0'+index+' .content').append(contenuPlus);
							$('#page01').unbind('childrenLoaded');
							$('.partLink').click(function(){
								window.open($(this).attr('href'));
								return false;
							});
						}
					});
					getChildren(doc.id,0);
				break;
				case 5 :
					var contenu = '<div class="innerContent"><div class="topDeco"></div><h1>'+doc.title+'</h1> '+draft+' '+edit(doc)+' '+doc.content+'</div>';
					if (doc.additionalContent){
							contenu+='<a href="#'+doc.aliasPath+'" class="readMore fancy">'+doc.readMore+'</a>';
						}
				break;
				case 6:
					// alert(doc.aliasPath);
					$('#page01').bind('childrenLoadedG',function(){
						if (arrayChildrenG.length>0){
							child=jQuery.parseJSON(arrayChildrenG[0]);
							var contenu = '<div class="innerContent">';
							contenu+='<h1>'+child.title+'</h1> '+draft+' '+edit(child)+' '+'<ul class="ul_gal">';
							var imgs=child.photos.split(',');
							var alts=child.alt.split(',');
							var minis=child.mini.split(',');
							var i = 0;
							while (i<imgs.length){
								var img = imgs[i];
								var alt=alts[i] ;
								var mini = minis[i];
								if (i==0 || i%12==0){
									contenu+= '<li class="li_gal">';
								}
								contenu+='<a href="'+domain+img+'" class="fancy" rel="gal'+child.id+'" title="'+alt+'"><img class="img_gal" src="'+domain+mini+'" /></a>\n'
								i++;
								if (i%12==0 || i==imgs.length){
									contenu+= '</li>';
								}
							}
														
						}
						$('#page01').unbind('childrenLoadedG');
						$('#page0'+index+' .content').html(contenu);
						$('.fancy').fancybox();
						$('.ul_gal').wslide({width: 590,height: 350,horiz: false,pos:1,duration:1000});
					});
					getChildren(doc.id,1);
					contenu+='</ul></div>';
				break;
				case 7:
					// alert('contact');
					var contenu ='<div class="innerContent"><h1>'+doc.title+'</h1> '+draft+' '+edit(doc)+' '+doc.content+'</div>';
				break;
			
			}
			// alert('#page0'+index+' .content');
			$('#page0'+index+' .content').html(contenu);
			if (contenuAdd.length>2){
				$('#canvas').append(contenuAdd);
				
				$('.readMore').fancybox({'hideOnContentClick':false,'onComplete':fancyComplete()});
				
			}
			
	   },
	  error : function(XMLHttpRequest, textStatus, errorThrown){
		 alert(XMLHttpRequest+'///'+textStatus+'///'+errorThrown+'///'+index);
	  }
	  
	 
 });
}

function edit(document){
	// alert(document.edit);
	if (document.edit=="true"){
		return '<a href="'+domain+'/fr/admin/manage/edit?docId='+document.id+'&amp;lan='+document.language+'" class="edit">[Edit]</a>';
	}else{
		return '';
	}
}
function fancyComplete(){
// $.fancybox.resize();
}
 function getChildren(parentAlias,nombre){
		var i = 0;
		arrayChildren = new Array();
		arrayChildrenG = new Array();
		if (nombre>0){
			while (i<nombre){
				ajaxChildren(parentAlias,i,false,nombre-1);
				i++;
			}
		}else{
			ajaxChildren(parentAlias,i,true,0,0);
			
		}
		
		
}
	  
function ajaxChildren(parentId,index,loop,max){
	$.ajax({
	   type: "POST",
	   url: domain+"/do/pageLoader.php",
	   data: {children:parentId,ind : index,lang:$('#mur').attr('rel')},
	    success: function(msg){
		// alert(msg);
			if (msg!=""){
				// alert(parentId);
				if (parentId=="24" || parentId=="30"){
					// alert('inner');
					arrayChildren.push(msg);
				}else{
					arrayChildrenG.push(msg);
				}
				if (loop=true){
					ajaxChildren(parentId,index+1,loop);
				}else{
					if (max==index){
						// alert('trigger1');
						if (parentId=="24" || parentId=="30"){
							$('#page01').trigger('childrenLoaded');
						}else{
							$('#page01').trigger('childrenLoadedG');
						}
						
					}
				}
				// alert(msg);
				return msg;	
			}else{
				// alert("empty"+msg);
				if (parentId=="24" || parentId=="30"){
					// alert('trigger2');
							$('#page01').trigger('childrenLoaded');
						}else{
						// alert('trigger3');
							$('#page01').trigger('childrenLoadedG');
						}
						
			}
		}
		});
}
		
	
