var objOverlay = null;
var objPopup = null;
var objPopupTitle = null;
var objPopupContent = null;
var popup_ajax = new Ajax();
var col = 0;
var popup_width = 600;
var popup_height = (screen.height<768)?380:500;
var objPopupCloseFooter;

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;
}

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;
}

function initPopup() {
	var objBody = document.getElementsByTagName("body").item(0);
	objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = hidePopup;
	objOverlay.style.cursor = 'pointer';
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);

	objPopup = document.createElement("div");
	objPopup.setAttribute('id','rssPopup');
	objPopup.style.display = 'none';
	objPopup.style.position = 'absolute';
	objPopup.style.zIndex = '500';	
	objPopup.style.width = popup_width + 'px';
	objPopup.style.height = popup_height + 'px';
	objBody.insertBefore(objPopup, objBody.firstChild);

	objPopupTitle = document.createElement("div");
	objPopupTitle.className = 'boxHeader';
	//objPopupTitle.setAttribute('id','rssPopupTitle');
	objPopupTitle.innerHTML = '...';
	objPopup.appendChild(objPopupTitle);

	var objClose = document.createElement("a");
	objClose.innerHTML = '<img src="/images/closeButton.gif" alt="" />';
	objClose.setAttribute('href','#');
	objClose.setAttribute('title','Click to close');
	objClose.className = 'closePopup';
	objClose.onclick = function () {hidePopup(); return false;}
	objPopup.appendChild(objClose); 

	objPopupContent = document.createElement("div");
	objPopupContent.style.height = (popup_height-75)+'px'; 
	objPopupContent.setAttribute('id','rssPopupContent');
	objPopup.appendChild(objPopupContent);

	var objPopupFooter = document.createElement("div");
	objPopupFooter.setAttribute('id','rssPopupFooter');
	objPopupFooter.innerHTML = 'Jaide.ca a été créé et est géré par <B><A HREF="http://www.ebb.com">EBB HEC Montréal</A></B><BR>Le tremplin pour réussir en affaires sur Internet';
	objPopup.appendChild(objPopupFooter);

	objPopupCloseFooter=document.createElement("UL");
	objPopupCloseFooter.setAttribute('id','rssPopupCloseFooter');
	objPopupFooter.insertBefore(objPopupCloseFooter,objPopupFooter.firstChild);

}

function showContent(ok) {
		var html = ok.responseXML;
		var body = html.getElementsByTagName('body')[0];
		
		if (!body && window.ActiveXObject) { 
			var xmldoc;
			try { xmldoc = new ActiveXObject("MSXML2.XmlDom"); } catch (e) {}
			if (!xmldoc) try { xmldoc = new ActiveXObject("Microsoft.XmlDom"); } catch (e) {}
			if (!xmldoc) try { xmldoc = new ActiveXObject("MSXML.XmlDom"); } catch (e) {}
			if (!xmldoc) try { xmldoc = new ActiveXObject("MSXML3.XmlDom"); } catch (e) {}
			xmldoc.validateOnParse = false;
			if (xmldoc.loadXML(this.xmlhttp.responseText)) { // recovery seems possible
				html = xmldoc.documentElement;
				body = html.getElementsByTagName('body')[0];
			} 
		}

		if(!body) {
			objPopupTitle.innerHTML = 'Erreur 404 - Not Found';
		}
		
		objPopupTitle.innerHTML = (html.getElementsByTagName('title')[0]).firstChild.nodeValue;
		objPopupContent.innerHTML = (body.innerHTML)?body.innerHTML:body.xml;

		var JM_ONLOAD = null;
		var scripts = html.getElementsByTagName('script');
		for (var i=0; i<scripts.length; i++) {
			var src = scripts[i].getAttribute('src');
			if(!scripts[i].getAttribute('src')) {
		       eval(scripts[i].firstChild.nodeValue);
			}
		}
		if (JM_ONLOAD) window.setTimeout(JM_ONLOAD, 100);


}

function showWaitMessage() {
	objPopupContent.innerHTML = '<BR><BR><BR><BR><CENTER><IMG SRC="images/loading.gif" alt=""><BR>Chargement en cours...</CENTER>';
}

function showPopup(url,msg) {
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var closepopup = msg || 'Sauvegarder et quitter';
	objPopupCloseFooter.innerHTML='<LI><A HREF="javascript:hidePopup();" style="background-image: url(\'images/icons/disk.gif\');">'+closepopup+'</A></LI>';

	if (objPopup) {
		objPopup.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 15 - popup_height) / 2) + 'px');
		objPopup.style.left = (((arrayPageSize[0] - popup_width) / 2) + 'px');
		objPopup.style.display = 'block';
	}
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	popup_ajax.onLoading = showWaitMessage;	
	popup_ajax.onsuccess = showContent;
	popup_ajax.request(url, {method: 'GET'});
}

function hidePopup() {
	saveCook();
	objPopup.style.display = 'none';
	objOverlay.style.display = 'none';
	showWaitMessage();
}

function focus() { document.g.q.focus(); }

function initPage() {
	document.getElementById('startpage').onclick = function() { HomePage(this);	}
	/*
	document.getElementById('friendpage').onclick = function() {
		Fenetre=window.open('/amis/',"Fenetre","resizable=0,scrollable=yes,scrollbars=yes ,top=60,left=60,height=435,width=500");
        setTimeout("Fenetre.focus()",10)	
	};
	*/
	document.getElementById('friendpage').onclick = function() {
		setCookie('LIGHT','1',3600);
		document.location.href = document.location.href;
	};
	document.getElementById('about').onclick = function() { showPopup('/about.php','Quitter'); };
}

function HomePage(obj) {
	try {
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage('http://www.jaide.ca');
	} catch (e) {
		alert('La version de votre navigateur ne permet pas l\'ajout de ce site en page de demarrage à partir de ce lien.\nVous devez donc l\'ajouter manuellement. Merci pour votre aide !'); 
	}
}

addLoadEvent(initPopup);
addLoadEvent(initPage);
addLoadEvent(focus);