var clientSections;
var clientHash;

var currentSection=null;


// Ready
$(document).ready(siteReady);

function siteReady() {
	
}

// Loaded
$(window).load(siteLoaded);

function siteLoaded() {
	// Location change event (jquery.location.js)
	$(window.location).bind("change", handleLocation);
	handleLocation(null, {currentHash:window.location.hash})
}



// Ajax URLs

function handleLocation(e, data) {
	var url = data.currentHash;
	url = url.substring(url.indexOf("#")+1, url.length);
	// Remove first and trailing slashes
	if(url.indexOf("/")==0) url = url.substring(1,url.length);
	if(url.lastIndexOf("/")==url.length) url = url.substring(0,url.length-1);
	
	if(clientHash==url) return;
	
	clientSections = url.split("/");
	
	var s1 = clientSections[0];
	if(s1=="") return;
	
	switch(s1) {
		case "videos": 
			if(clientSections[1] && clientSections[1] != "") {
				goVideo(clientSections[1], "");
			} else {
				goSection(s1);
			}
			break;
		default: goSection(s1); break;
	}
	
	// Urchin Tracking
	if(pageTracker) pageTracker._trackPageview();
	
	clientHash=url;
}

function updateLocation(newHash) {
	if(newHash.indexOf("/")==0) newHash = newHash.substring(1,newHash.length);
	window.location = window.location.href.substring(0, window.location.href.indexOf("#")) + "#" + newHash;
	//window.location.hash = newHash;
}



// Events

function keyPressed(e) {
	if(e.type=="keypress") {
		switch(e.keyCode) {
			case 37: break; // left
			case 38: break; // up
			case 39: break; // right
			case 40: break; // down
		}
	}
	
}


// Navigation

function goSection(vId, callback) {
	var vY = yGetCurrentScroll();
		
	if (vId=="top") {
		scrollTo(0,0,Tween.strongEaseOut,1);
	} else {
		var xAnchor = xGetObject("a_"+vId);
		if(!xAnchor) return;
		var vYd = xAnchor.offsetTop;
		if (!window.scrollY) vYd -= 30;
		if (vY<= 50) {
			scrollTo(0,vYd,Tween.strongEaseInOut,1.5);
		} else {
			scrollTo(0,vYd,Tween.strongEaseInOut,1);
		}
	}
	
	currentSection=vId;
	if(!callback) callback = goSectionDone;
	scrollDone = callback;
	if(currentSection!="videos") stopVideo_frame();
	//hideSplash();
	
}

function goSectionDone() {
	//top.location="#"+currentSection;
	//if (!window.scrollY) window.scrollBy(0,-30);
	
}


function hideSplash() {
	goSection('portfolio');
}

function goSplash() {
	showNewsItem('99');
	goSection('nouvelles');
}



/*ThickBox*/
if(xLg == "fr"){ var tb_closeCaption = "Fermer"; var tb_closeCaption2 = "";}

function showMedia(vFile,vTitre,vWidth,vHeight) {	
	if(!vWidth) vWidth= 450;
	if(!vHeight) vHeight= 340;
	if(!vTitre) vTitre = "";
	//vWidth-=30;
	//vHeight-=10;
	tb_show(vTitre,"/medias/player.aspx?file="+vFile+"&TB_iframe=true&width="+vWidth+"&height="+vHeight+"&modal=false&scrolling=no");
}

function showFrame(vUrl,vTitre,vWidth,vHeight) {
	var paramSep="?";
	if(vUrl.indexOf("?")>0) paramSep="&";
	tb_show(vTitre,vUrl+paramSep+"s=1&TB_iframe=true&width="+vWidth+"&height="+vHeight+"&modal=false&scrolling=no");
}
function showImage(imageUrl,imageTitre) {
    if(!imageTitre) imageTitre = top.document.title;
	tb_show(imageTitre,imageUrl+"?width");
}

/* VIDEOS */


video_section="";

function goVideo(video,titre) {
	video_file = video;
	video_titre = titre;
	
	// Previous section
	var vs=currentSection;
	//if(vs=="") vs="portfolio";
	currentVideo=null;

	
	if(vs=="videos") {
		goVideoDone();
	} else {
		goSection('videos', goVideoDone);
	}
	
	video_section=vs;
}

function goVideoDone(){
   playVideo_frame(video_file,video_titre,"");
}


function showVideosMenu() {
	// Return to previous section
	if(video_section!="") {
		var vs=video_section;
		video_section="";
		goSection(vs);
	}
	
	$("#videosHome").show();
	$("#videosPager").show();
	hideVideoFrame();
	//updateLocation("videos");
}

function hideVideosMenu() {
	$("#videosHome").hide();
	$("#videosPager").hide();
}

var currentVideo;

function playVideo_frame(video,title,date,width,height) {
	var fileExt = ".flv";
	if(video.indexOf(".mp4")>-1) fileExt=".mp4";
	if(video.indexOf(".")>-1) video = video.substring(0, video.indexOf("."));
	
	if(video == currentVideo) return;
	
	width=780;
	height=439;
	var langue=xLg;
	var url = "/medias/player.aspx?file=/videos/"+video+fileExt+"&width="+width+"&height="+height+"&langue="+langue;
	
	var xiFrame = $("#ifVideos");
	xiFrame.attr("src",url);
	xiFrame.show();
	
	hideVideosMenu();
	$("#videosPlayer").show();
	
	$("#video_titre").html("<span class='title'>"+title+"</span> &nbsp; <span class='date'>"+date+"</span>");
	currentVideo = video;
	
	updateLocation("videos/"+video);
}

function stopVideo_frame() {
	currentVideo=null;
	showVideosMenu();
}

function hideVideoFrame() {
	var url = "/medias/noPlayer.html";
	var xiFrame = $("#ifVideos");
	xiFrame.attr("src",url);
	xiFrame.hide();
	$("#videosPlayer").hide();
	currentVideo=null;
}

function playVideo(video,nomVideo,langue) {
	if(!langue) langue=xLg;
	showFrame("/medias/player.aspx?file=/videos/"+video+"&langue="+langue, nomVideo, 610, 349); //469
	stopVideo_frame();
}



/* Show / Hide sections */
function showSection_static(vNum,vCode,vId) {
	var vBase=sectionClientPath+vNum+"_section_Panel";
	
	xShowSection(vBase,vCode,vId);
}

function showSection(vCode,vId) {
	xShowSection("s_"+vCode+"_",vCode,vId);
}

function xShowSection(vBase,vCode,vId) {
	if (!aSections[vCode]) aSections[vCode]=1;
	if(aSections[vCode]==vId) return;	
	
	var xSection = xGetObject(vBase+vId);
	if(xSection) xSetDisplay(xSection,"block");
	var divMenu = xGetObject("sMenu_"+vCode+"_"+vId);
	if(divMenu) divMenu.className = "item_a";
	
	var vLast = aSections[vCode];
	var xLastSection = xGetObject(vBase+vLast)
	if (xLastSection)  xSetDisplay(xLastSection,"none");
	var lastMenu = xGetObject("sMenu_"+vCode+"_"+vLast);
	if(lastMenu) lastMenu.className = "item";
	
	aSections[vCode]=vId;
}



/* News */
currentNewsItem = null;

function showNewsItem(vId) {
	// close last item
	if(currentNewsItem!=null) {
		var divData_last = xGetObject("news_"+currentNewsItem+"_data");
		xSetDisplay(divData_last,"none");
	}
	// open new item
	if (vId!=null) {
		var divData = xGetObject("news_"+vId+"_data");
		xSetDisplay(divData,"block");
		
		//xSetDisplay(xGetObject("news_retour"),"block");
		//xSetDisplay(xGetObject("news_pager"),"none");
	}
	currentNewsItem=vId;
}
function closeNewsItem() {
	showNewsItem(null);
	//xSetDisplay(xGetObject("news_retour"),"none");
	//xSetDisplay(xGetObject("news_pager"),"block");
	
}
