// CSS special Explorer 6 *****************************************
if (document.all) {
var pos = navigator.userAgent.indexOf('MSIE');
	if (pos > -1) {
		version = navigator.userAgent.substring(pos + 5);
		var pos = version.indexOf(';');
		var version = version.substring(0,pos);
	}
	if (version < 7) {
		document.write('<link media="screen" type="text/css" rel="stylesheet" href="css/ie6.css" />');
	} else {
		document.write('<link media="screen" type="text/css" rel="stylesheet" href="css/ie7.css" />');
	}
}
// Fonctions D.O.M. **********************************************
function getElementsByClassName(className) {
	var arr = new Array();
	var elems = document.getElementsByTagName("*");
	for(var i = 0; i < elems.length; i++) {
		var elem = elems[i];
		var id = elem.getAttribute("id");
		var cls = elem.getAttribute("class");
		if(cls == className) {
			arr[arr.length] = id;
		}
	}
	return arr;
}

function rssAlert() {
	$('content').innerHTML = 'chargement ...';
	var ajax = new Ajax.Request('rss/rss_alertes.php', {method:'get', onComplete:showResponse});

	function showResponse(request){
		var liste = request.responseText.split('|');
		showContent(0,liste.length);
	
		function showContent(index,end){
			new Effect.Fade('content',{duration:0.5,afterFinishInternal:function(){$('content').innerHTML = liste[index];new Effect.Appear('content',{duration:0.5});}
	});
	
	setTimeout(function(){if(index == (end-1)){index = 0;showContent(index,end);}else{showContent(index+1,end);}},8000);}}
}

// Dimensions de la fenetre *************************************

// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll() {

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize() {
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// Pour centrer les div dans la fenetre *************************************
function centerDiv(divid, intWidth, intHeight) {
	// pour chaque variable / tableau : 0 -> largeur , 1 -> hauteur
	var PageDim = getPageSize();
	var ScrollDim = getPageScroll();

	var layerTop = (PageDim[3]/2) + ScrollDim[1] - (intHeight/2);
	var layerLeft = (PageDim[2]/2) - (intWidth/2);
	document.getElementById(divid).style.left = layerLeft + 'px';
	if (PageDim[3] <= intHeight) {
		document.getElementById(divid).style.top = 205 + 'px';
	}
	else {
		document.getElementById(divid).style.top = layerTop +'px';
	}
	
	document.getElementById(divid).style.width= intWidth +'px';
	document.getElementById(divid).style.height= intHeight +'px';
}

// les popups *****************************************************

function popclose() {
	document.getElementById("popcontenu").innerHTML = '';
	if (document.getElementById("obligatoire")) {
		if (document.getElementById("overlay")) {
			new Effect.Fade("overlay", { duration: 0.2});
		}
		Element.hide("obligatoire");
	}
	if (document.getElementById("dragdrop")) {
		Element.hide("dragdrop");
	}
	return false;
}

function truePopup(url, intWidth, intHeight){
	if (!intWidth) { intWidth = '500';}
	if (!intHeight) { intHeight = '500';}
	window.open(url,"","toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width="+ intWidth +",height="+ intHeight);
}

function setPopupDiv(myDiv) {
/* this write the following in the <body>, without line breaks
  <div class="popcadre">
    <div class="popbloc">
      <div id="popentete" class="popentete">
        <div class="popclosediv" onClick="popclose()"><span>fermer</span></div>
      </div>
      <div id="popcontenu" class="popcontenu">
      </div>
    </div>
  </div>
*/
	element = Builder.node('div',{id:myDiv,className:'popupdiv',style:'display:none'},[
		Builder.node('div',{className:'popcadre'},[
		  Builder.node('div',{className:'popbloc'},[
			Builder.node('div',{id:'popentete',className:'popentete'},[
			  Builder.node('div',{className:'popclosediv',onclick:'popclose()'},[
				Builder.node('span','fermer')
			  ]),
			]),
		  Builder.node('div',{id:'popcontenu',className:'popcontenu'})
		]),
	  ]),
	]);
	//	$('page').appendChild(element);
	document.getElementsByTagName("body").item(0).appendChild(element);
}

// popup drag and drop
function popDrag(strURL, myDiv, intWidth, intHeight) {

	setPopupDiv(myDiv);

	document.getElementById(myDiv).onmousedown = function() {
		new Draggable('dragdrop', {revert:false});
	}
	
	centerDiv(myDiv, intWidth, intHeight);
	
	function showResponse(request){
		document.getElementById(myDiv).childNodes[0].childNodes[0].childNodes[1].innerHTML = request.responseText; // rempli popcontenu
	}
	new Ajax.Request(strURL, { method:'get', onSuccess:showResponse, onFailure:truePopup });

}
// popup obligatoire
function popExclu(strURL, myDiv, intWidth, intHeight) {

	// grey background
	objOverlay = Builder.node('div',{id:'overlay',style:'display:none',onclick:'popclose()'});
	document.getElementsByTagName("body").item(0).appendChild(objOverlay);

	var arrayPageSize = getPageSize();
	objOverlay.style.height = arrayPageSize[1] + "px";

	// popup window
	setPopupDiv(myDiv);

	document.getElementById(myDiv).childNodes[0].childNodes[0].childNodes[1].style.height = (intHeight - 34).toString() + "px"; // set du height de popcontenu pour avoir le scroll

	new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.4 });	
	
	centerDiv(myDiv, intWidth, intHeight);

	function showResponse(request){
		document.getElementById(myDiv).childNodes[0].childNodes[0].childNodes[1].innerHTML = request.responseText; // rempli popcontenu
	}
	new Ajax.Request(strURL, { method:'get', onSuccess:showResponse, onFailure:truePopup });

}

