function Sky_Accueil()
{
	SetupEvents();
	SetupNewsStack();
	//SetupFlash();
	
}


function SetupEvents()
{
	var datesFound = window.document.getElementsByName('eventDate');
	var regp = new RegExp("<SPAN class='KDate'>(.*)</SPAN>", "g");
	
	
	for(i=0;i<datesFound.length;i++)
	{
		var obj = document.createElement("div");
		obj.className="eventDate";
		var innerDate = "";
		innerDate = datesFound[i].value;
		innerDate = innerDate.replace(regp, "$1");
		var parsedDated = innerDate.split(" ");
		if(parsedDated.length==3)
		{
			var objMois = document.createElement("div");
			objMois.className="eventDateDay";
			objMois.appendChild(document.createTextNode(parsedDated[1].substring(0,3)));
			obj.appendChild(objMois);
			
			var objChif = document.createElement("div");
			objChif.className="eventDateMonth";
			objChif.appendChild(document.createTextNode(parsedDated[0]));
			obj.appendChild(objChif);
		}
		else
		{
			continue;
		}
		var parent = datesFound[i].parentNode;
		parent.insertBefore(obj,datesFound[i]);
	}

}
var baseIndex=200;
function SetupNewsStack()
{
	var divSource = $g("c_acc_mn_news");
	
	var divsInSource = $in(divSource,"DIV");
	var news = new Array();
	for(i=0,dl=divsInSource.length;i<dl;i++)
	{
		if(divsInSource[i].className)
		{
			if(divsInSource[i].className=="KItem")
			{
				news.push(divsInSource[i]);
				
			}
		}
	}
	
	if(news.lenght==0)
		return;
	
	var container =  $make("DIV");
	container.id="newsContainer";
	
	var leftPart = $make("DIV");
	leftPart.id="newsLeft";
	var rightPart = $make("DIV");
	rightPart.id="newsRight";
	
	
	var titles=$classIn(divSource,"SPAN","KTitle");
	for(i=0,nl=news.length;i<nl;i++)
	{
		var newImg = $in(news[i],"IMG")[0];
		var imgContainer = $make("DIV");
		imgContainer.appendChild(newImg);
		imgContainer.className="newsImgContainer";
		imgContainer.style.zIndex =baseIndex - (i*10);
		imgContainer.id="imgNews"+i;
		leftPart.appendChild(imgContainer);
		
		var newsTitleContainer = $make("DIV");
		newsTitleContainer.className= "newsTitleContainer";
		if(i===0)
		newsTitleContainer.className+= " activeItem";
		if(i===1)
		newsTitleContainer.className+= " noborder";
		//newsTitleContainer.innerHTML = '<table width="100%" height="100%" border="0"><tr><td valign="center">'+titles[i].innerHTML +'</td></tr></table>';
		titles[i].getElementsByTagName("A")[0].innerHTML = '<table width="100%" height="100%" border="0"><tr><td valign="center" onclick="window.location=\''+titles[i].getElementsByTagName("A")[0].href+'\'">'+titles[i].getElementsByTagName("A")[0].innerHTML+'</td></tr></table>';
		newsTitleContainer.innerHTML = titles[i].innerHTML;
		newsTitleContainer.id="linkNews"+i;
		newsTitleContainer.onmouseover=function(a){return function(){(SelectActiveView(a,true))};}(i);
		newsTitleContainer.onmouseout=function(a){return function(){if(nbMoved<4)automaticSwitch = setTimeout(function(){SelectActiveView(a,false);},automaticSwitchDelay)};}((i+1>3)?0:i+1);
		if(i===nl-1)
			newsTitleContainer.className+=" lastItem";
		rightPart.appendChild(newsTitleContainer);
		
	}
	
	
	
	container.appendChild(leftPart);
	container.appendChild(rightPart);
	divSource.appendChild(container);
	
	var lnkAllCont = $make("DIV");
	var lnkAll = $make("A");
	lnkAll.innerHTML = "voir toutes les nouvelles";
	lnkAll.href = "/?F54AF53C-5865-4732-874F-6ED967053824";
	lnkAllCont.appendChild(lnkAll);
	lnkAllCont.id="c_acc_mn_news_a";
	divSource.appendChild(lnkAllCont);
	automaticSwitch = setTimeout(function(){SelectActiveView(1,false);},automaticSwitchDelay);
}
var automaticSwitchDelay = 2000;
var tweenOp = null;
var isWorkingTweenOp = false;
var lastMoved = 0;
var automaticSwitch = null;
var nbMoved = 0;
function SelectActiveView(pos,noTimer)
{	
	if(lastMoved==pos)
	{
		clearTimeout(automaticSwitch);
		return;	
	}
	if(automaticSwitch!=null || noTimer)
	{
		clearTimeout(automaticSwitch);
	}
	if(automaticSwitch!=null && !noTimer)
		nbMoved++;
	
	ClearTweenOp(true);
	if(lastMoved!=null)
	{
		$g("imgNews0").style.zIndex=100;
		$g("imgNews1").style.zIndex=100;
		$g("imgNews2").style.zIndex=100;;
		$g("imgNews3").style.zIndex=100;
		$g("imgNews"+lastMoved).style.zIndex=200;
		toggleClass($g("linkNews"+lastMoved),"activeItem",false);
		toggleClass($g("linkNews"+(lastMoved+1)),"noborder",false);
	}
	
	var img = $g("imgNews"+pos);
	var nextPos=(pos+1>3)?0:(pos+1);
	img.style.zIndex=300;
	tweenOp = new Tween(img.style,'opacity',Tween.regularEaseOut,0,100,1,'');
	if(!noTimer && nbMoved<4)
		tweenOp.onMotionFinished = function () {ClearTweenOp(false);automaticSwitch=setTimeout(function(){SelectActiveView(nextPos,false);},automaticSwitchDelay);};
	else
		tweenOp.onMotionFinished = function () {ClearTweenOp(false);};
	isWorkingTweenOp = true;
	
	tweenOp.start();
	
	toggleClass($g("linkNews"+pos),"activeItem",true);
	toggleClass($g("linkNews"+(pos+1)),"noborder",true);
	if(pos===3)
		toggleClass($g("linkNews"+pos),"lastItem",false);
	if(lastMoved===3)
		toggleClass($g("linkNews3"),"lastItem",true);
	lastMoved = pos;
}

function ClearTweenOp(stopCurrent)
{
	if(isWorkingTweenOp && tweenOp!=null)
	{
		tweenOp.stop();
		isWorkingTweenOp = false;
		tweenOp = null;
	}
}

function to(obj, isMenuDisplayed)
{
	//var img = $in(obj,"IMG")[0];
	
	//var strSrc= img.src.substr(0,img.src.length-6);
	
	//KMenu.menus.items("3908223C-D0FD-4205-9C5D-E547AC948E2B")
	/* console.log(KMenu.menus.count());
	 
	 
	for (var i = 0; i < KMenu.menus.items("3908223C-D0FD-4205-9C5D-E547AC948E2B").menus.count(); i++) {
		if(KMenu.menus.items("3908223C-D0FD-4205-9C5D-E547AC948E2B").menus.items(i).style.display == "block")
			img.src = strSrc.replace("over_","") + img.src.substring(img.src.length-6,img.src.length)
		else
			img.src = strSrc + "over_" + img.src.substring(img.src.length-6,img.src.length);
	}
	return false;	
	
	/*
	if(img.src.indexOf("over_")>-1)
	{
		if(isMenuDisplayed)
			img.src = strSrc.replace("over_","") + img.src.substring(img.src.length-6,img.src.length);
	}
	else
	{
		img.src = strSrc + "over_" + img.src.substring(img.src.length-6,img.src.length);
	}
	**/
	
}

function SetupRotation_Accueil()
{
	var path = LoadXMLImage("/Modules/KModThumbnails/galleries.asp?site=AD60489A-7AFC-4581-B544-1FE92655B16C&path=V2/Rotations/Accueil");
	$g("c_head_rot").style.background="transparent url('"+path+"') no-repeat top center";
	
}
function SetupRotation_Form()
{
	var path = LoadXMLImage("/Modules/KModThumbnails/galleries.asp?site=AD60489A-7AFC-4581-B544-1FE92655B16C&path=V2/Rotations/Formation");
	$in($g("bie_head_rotate"),"IMG")[0].src=path;
}
function SetupRotation_CAP()
{
	var path = LoadXMLImage("/Modules/KModThumbnails/galleries.asp?site=AD60489A-7AFC-4581-B544-1FE92655B16C&path=V2/Rotations/CAP");
	$in($g("bie_head_rotate"),"IMG")[0].src=path;
}
function SetupRotation_Entreprise()
{
	var path = LoadXMLImage("/Modules/KModThumbnails/galleries.asp?site=AD60489A-7AFC-4581-B544-1FE92655B16C&path=V2/Rotations/Entreprise");
	$in($g("bie_head_rotate"),"IMG")[0].src=path;
}
function SetupRotation_Etudiant()
{
	var path;
	if(estdRotation && estdRotation!="")
	{
		path  = LoadXMLImage("/Modules/KModThumbnails/galleries.asp?site=AD60489A-7AFC-4581-B544-1FE92655B16C&path=V2/Rotations/Etudiant/"+estdRotation);
	}
	else
	{
		path  = LoadXMLImage("/Modules/KModThumbnails/galleries.asp?site=AD60489A-7AFC-4581-B544-1FE92655B16C&path=V2/Rotations/Etudiant");
	}
	$in($g("bie_head_rotate"),"IMG")[0].src=path;
}

	
var xmld= null;

function LoadXMLImage(url)
{
	var arrValid = new Array();
	var xmlhttp = new XMLHttpRequest();  
	xmlhttp.open("GET", url, false);  
	xmlhttp.send('');
	xmld = xmlhttp.responseXML;
	arrValid = xmlhttp.responseXML.getElementsByTagName('photo');
	var rd = Math.floor(Math.random() * arrValid.length);
	var path = arrValid[rd].childNodes[2].lastChild.nodeValue;
	return path;
}



function SetupFlash()
{
	var flashvars = {};
	flashvars.xmlPath = "/templates/00968512-E642-44A9-9518-47E953D31AD2/data.xml";
	
	var params = {};
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";
	params.width = "431";
	params.height = "279";
	params.menu = "false";
	params.wmode = "transparent";
	
	var attributes = {};
	
	swfobject.embedSWF("/templates/00968512-E642-44A9-9518-47E953D31AD2/banner.swf", "flashBanner", "431", "279", "9.0.0", false, flashvars, params, attributes);

}
function SetupFlashTwo()
{
	var flashvars = {};
	flashvars.xmlPath = "/templates/00968512-E642-44A9-9518-47E953D31AD2/news.xml";
	
	var params = {};
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";
	params.width = "431";
	params.height = "279";
	params.menu = "false";
	params.wmode = "transparent";
	
	var attributes = {};
	
	swfobject.embedSWF("/templates/00968512-E642-44A9-9518-47E953D31AD2/preview.swf", "flashBanner", "431", "279", "9.0.0", false, flashvars, params, attributes);

}


/* quick utils */
function $g(id)
{
	return document.getElementById(id);
}
function $in(obj,name)
{
	return obj.getElementsByTagName(name);
}
function $make(name)
{
	return document.createElement(name);
}

function $classIn(obj,type,cls)
{
	var temp = new Array();
	var lst = $in(obj,type);
	for (iL=0,ll=lst.length;iL<ll;iL++)
	{
		if(lst[iL].className)
		{
			if(lst[iL].className==cls)
			{
				temp.push(lst[iL]);
			}
		}
	}
	return temp;
}
function toggleClass(obj,toggleName,add)
{
	if(obj && obj.className)
	{
		if(obj.className.indexOf(toggleName)>-1 && !add)
		{
			obj.className = obj.className.replace(" "+toggleName,"");
		}
		else if(add)
		{
			obj.className += " "+toggleName;
		}
	}
}

function resizeImages()
{
	//utilisé dans liste de nouvelles
	var div = document.getElementById("zNews");
	var imgs = div.getElementsByTagName("img");
	var maxH = 116;
	for(var i=0; i<imgs.length; i++)
	{
		if(imgs[i].height > maxH)
		{
			imgs[i].height = maxH;
			imgs[i].width = (maxH * imgs[i].width) / imgs[i].height;
		}
	}
}
