/*
************************
BASIC  SCRIPTS
************************
*/

var origY;
var origX;

function $(id) {
  return document.getElementById(id);
}
	
function OpenDiv(id) {
 $(id).style.visibility = "visible";	
 $(id).style.display = "block"
}

function closeDiv(id) {
 $(id).style.visibility = "hidden";	
 $(id).style.display = "none"
}
	

function SetDefValue(value, defvalue) {
	var ret = null;
	if (typeof(value) == 'undefined')
			ret = defvalue
	else
			ret = value
	return ret;
}
	
function AttachEvent(element, eventName, callback)
{ 
  if(typeof(element) == "string")
    element = document.getElementById(element);
  if(element == null)
    return;
  if(element.addEventListener)
    element.addEventListener(eventName, callback, false);
  else if(element.attachEvent)
    element.attachEvent("on" + eventName, callback);
}

	
/*
************************
VALIDATION SCRIPTS
************************
*/

//checks if string is empty
function isEmpty(str){
	if(str.replace(/^\s+/,"").replace(/\s+$/,"")==""){
		return true;
	}
	return false;
}

/*

function isnumeric(value){
	var testresult;
	var anum=/(^\d+$)|(^\d+\(.|,)\d+$/; 
	if (anum.test(value))
		testresult=true;
	else{
		//alert("Please input a valid number on this field");
		testresult=false;
		//obj.select();
		}
return (testresult);
}
*/


/*
************************
AJAX SCRIPTS
************************
*/

function getXMLHttpRequestObject()
{ 
  var req = false;
  if(window.XMLHttpRequest && !(window.ActiveXObject)) {
  	try {req = new XMLHttpRequest();} 
	  catch(e) {req = false; }}
  // branch for IE/Windows ActiveX version
  else if(window.ActiveXObject) {
     try {	req = new ActiveXObject("Msxml2.XMLHTTP");	} 
		 catch(e) {
      	try { req = new ActiveXObject("Microsoft.XMLHTTP"); 	} 
			  catch(e) {req = false; 	} }  }
	if (req) 	
		return req;
	else
		{
			alert("Sorry your browser doesnt support AJAX.");
			return false;
		}
}


function getData(url, divID, loadImg)
{	var XMLHttpRequestObject = getXMLHttpRequestObject();
	if(XMLHttpRequestObject) {
	var obj = document.getElementById(divID);
	// **** some text to put while waiting for contents to load
	//obj.innerHTML = "Loading...";
	if (loadImg==1) {
	    obj.innerHTML = "<img src='http://img.aujourdhui.com/blogs/progressimgred.gif'>";
	}
	window.status = url;
	XMLHttpRequestObject.open("GET", url);
	XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 &&	XMLHttpRequestObject.status == 200) {
				obj.innerHTML = XMLHttpRequestObject.responseText;
				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;}
		}
	XMLHttpRequestObject.send(null);
	}
	return false;
}

//ajax to load a url on div, then executes a function when the contents are received
function getDataExec(url, divID, func)
{	var XMLHttpRequestObject = getXMLHttpRequestObject();
	if(XMLHttpRequestObject) {
	var obj = document.getElementById(divID);
	// **** some text to put while waiting for contents to load
	obj.innerHTML = "<div id=ajaxLoader></div>";
	//window.status = url;
	XMLHttpRequestObject.open("GET", url);
	XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 &&	XMLHttpRequestObject.status == 200) {
				obj.innerHTML = XMLHttpRequestObject.responseText;
				func();				
				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;}
		}
	XMLHttpRequestObject.send(null);
	}
	return false;
}


function getDataExecFunc(url, func)
{	var XMLHttpRequestObject = getXMLHttpRequestObject();
	if(XMLHttpRequestObject) {
	//window.status = url;
	XMLHttpRequestObject.open("GET", url);
	XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 &&	XMLHttpRequestObject.status == 200) {
				var text = XMLHttpRequestObject.responseText;
				//var xml = XMLHttpRequestObject.responseXML;
				func(text);
				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;}
		}
	XMLHttpRequestObject.send(null);
	}
	return false;
}


function getXML(url, func)
{	var XMLHttpRequestObject = getXMLHttpRequestObject();
	if(XMLHttpRequestObject) {
	//window.status = url;
	XMLHttpRequestObject.open("GET", url);
	XMLHttpRequestObject.onreadystatechange = function()
		{
			if (XMLHttpRequestObject.readyState == 4 &&	XMLHttpRequestObject.status == 200) {
				//obj.innerHTML = XMLHttpRequestObject.responseText;
				var xml = XMLHttpRequestObject.responseXML;
				func(xml.documentElement);
				delete XMLHttpRequestObject;
				XMLHttpRequestObject = null;}
		}
	XMLHttpRequestObject.send(null);
	}
	return false;
}


function getMsgAndData(url, divID, options)
{
	var XMLHttpRequestObject = getXMLHttpRequestObject();
	if(XMLHttpRequestObject) {
		var obj = document.getElementById(divID);				
		XMLHttpRequestObject.open("GET", url);
		XMLHttpRequestObject.onreadystatechange = function()
			{
				if (XMLHttpRequestObject.readyState == 4 &&	XMLHttpRequestObject.status == 200) {				
					var xmlDoc=XMLHttpRequestObject.responseXML.documentElement;
					//for data 1; replacement of html display
					 var data = xmlDoc.childNodes[0].firstChild.nodeValue;	
					 var msg = xmlDoc.childNodes[1].firstChild.nodeValue;
				//	alert(options.arrowY);		
					obj.innerHTML = data;
					var delay = SetDefValue(options.delay, 10000);
					Bubble.PopMsg(obj,msg, options, delay);
					delete XMLHttpRequestObject;
					XMLHttpRequestObject = null;}
			}
		XMLHttpRequestObject.send(null);
	}
return false;
}

/*xml separates to 2 div*/
function get2Data(url, divID, divID2)
{
	var XMLHttpRequestObject = getXMLHttpRequestObject();
	if(XMLHttpRequestObject) {
		var obj = document.getElementById(divID);	
		var obj2 = document.getElementById(divID2);	
		XMLHttpRequestObject.open("GET", url);
		XMLHttpRequestObject.onreadystatechange = function()
			{
				if (XMLHttpRequestObject.readyState == 4 &&	XMLHttpRequestObject.status == 200) {				
					var xmlDoc=XMLHttpRequestObject.responseXML.documentElement;
					//for data 1; replacement of html display
					 var msg1 = xmlDoc.childNodes[0].firstChild.nodeValue;	
					 var msg2 = xmlDoc.childNodes[1].firstChild.nodeValue;
				//	alert(options.arrowY);		
					obj.innerHTML = msg1;
					obj2.innerHTML = msg2;
					
					//var delay = SetDefValue(options.delay, 10000);
					//Bubble.PopMsg(obj,msg, options, delay);
					delete XMLHttpRequestObject;
					XMLHttpRequestObject = null;}
			}
		XMLHttpRequestObject.send(null);
	}
return false;
}

/*
************************
GET OBJECT POSITION SCRIPT
************************
*/
function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}
	

/*
************************
BUBBLE SCRIPT
************************
*/

var Bubble = {};
Bubble.ismouseover=0;
Bubble.id = "popUpDiv"
Bubble.msgid = "popUpMsg";
Bubble.init = false;
Bubble.currObj = null;
Bubble.enableMouseout = true;
Bubble.hidden = true;
//var ctr = 0


Bubble.build = function(){
		document.write("<div id=popUpDiv  style=\"position:absolute; left:100px; top:100px; display:none; z-index:200\">");
		//document.write("<IFRAME id=iframe width=100% height=100% frameborder=0 name=iframe1 src=\"\" scroll=none style=\"position:absolute; z-index:5;\"></iframe>")
		//document.write("<div style=\"position:relative; z-index:6;\">");
		//document.write("<div style=\"position:relative\"></div>");
		document.write("<table border=0 cellspacing=0 cellpadding=0>");
		document.write("<tr>");
		document.write("	<td class=cor1></td>");
		document.write("	<td class=top><img src=\"http://img.aujourdhui.com/s.gif\"/></td>");
		document.write("	<td class=cor2></td>");
		document.write("</tr>");
		document.write("<tr>");
		document.write("	<td class=left><img src=\"http://img.aujourdhui.com/s.gif\" width=\"8\"/></td>");
		document.write("	<td>");
		document.write("	<div align=right class=popUpMsg style=\"padding:1px 5px 5px; \"><a href=\"javascr" + "ipt: void(0)\" onclick=\"Bubble.Hide(1);\" style=\"color:gray; font-size:12px\">fermer</a></div>");
		document.write("	<div id=popUpMsg class=popUpMsg>");
		document.write("	</div>");
		document.write("	</td>");
		document.write("	<td class=right><img src=\"http://img.aujourdhui.com/s.gif\"/></td>");
		document.write("</tr>");
		document.write("<tr>");
		document.write("	<td class=cor3></td>");
		document.write("	<td class=bottom><img src=\"http://img.aujourdhui.com/s.gif\"/></td>");
		document.write("	<td class=cor4></td>");
		document.write("</tr>");
		document.write("</table>");
		document.write("<div class=arrow style=\"position:absolute; left:-28px; top:50px\" id=\"popArrow\"></div>");
		document.write("</div>");
		document.write("</div>");
}
Bubble.build();


//function to run when mouse over
Bubble.MouseOverGetData = function(obj, url, options, delay){
	//Bubble.mouseover(1);
	Bubble.enableMouseout = true;
	if(!obj.added)
		{	AttachEvent(obj, "mouseout",function(){obj.mousein=0; Bubble.mouseover(0) }) ;
		 // fixOnMouseOut(this, event, function(){obj.mousein=0; Bubble.mouseover(0)});
			obj.added = true;
		}
	
	obj.mousein = 1;
	Bubble.mouseover(1);
	if (Bubble.currObj != obj || Bubble.hidden)
	{	
		Bubble.currObj = obj;
		//$('temp').innerHTML = obj.id;
		if (delay) //if there was a delay, I will wait for it first before to show the popup
			window.setTimeout(function(){if (obj.mousein==1) Bubble.Show(obj, options, url); }, delay);
		else
			Bubble.Show(obj, options, url);	
			
	}
	
	
	if (!Bubble.init )
	{
		AttachEvent($(Bubble.id), "mouseover", function(){Bubble.mouseover(1)});
		AttachEvent($(Bubble.id), "mouseout", function(){Bubble.mouseover(0)});
	}
}

//function to show popup programmatically or on click.
//this disabled mouse out function
Bubble.PopGetData = function(obj, url, options){
	Bubble.enableMouseout = false;
	Bubble.Show(obj, options);	
	$(Bubble.msgid).innerHTML = "<img src='http://img.aujourdhui.com/elements/compte/ajax-loader.gif' style='margin:50px' align='center' />"
	getData(url,Bubble.msgid );
	return false;
}


//function to show the popup, with the msg as parameter (not from url)
//additional parameter is the delay to hide the popup
Bubble.PopMsg = function(obj, msg, options, delay){
	Bubble.enableMouseout = false;
	Bubble.Show(obj, options);	
	$(Bubble.msgid).innerHTML = msg;
	//Bubble.ismouseover==0;
	Bubble.mouseover(0);
	if (delay)
		Bubble.preClose(delay);
	return false;
}


Bubble.Show = function(obj, options, url) {
    //ctr = ctr + 1;
    //$("temp").innerHTML = ctr;

    var x = SetDefValue(options.x, "center");
    var y = SetDefValue(options.y, "center");
    var arrowY = parseInt(SetDefValue(options.arrowY, 50)); //position of arrow from top of box
    //var boxY = SetDefValue(options.boxY, 60  ); //box distance from obj
    var boxY = SetDefValue(options.boxY, (arrowY + 10)); //box distance from obj


    var objpos = getPosition(obj);
    var objHeight = obj.offsetHeight;
    var objWidth = obj.offsetWidth;


    if (x == "center")
        x = objpos.x + objWidth / 2 + 30;
    else if (x == "right")
        x = objpos.x + objWidth + 20;
    else if (x == "same")
        x = origX;    
    else
        x = objpos.x + 30;

    if (y == "center")
        y = objpos.y + objHeight / 2 - boxY;
    else if (y == "top")
        y = objpos.y - boxY;
    else if (y == "same")
        y = origY;
    else
        y = objpos.y - boxY;

    $("popArrow").style.top = arrowY + 'px';
    $(Bubble.id).style.left = x + 'px';
    $(Bubble.id).style.top = y + 'px';
    OpenDiv(Bubble.id);
    Bubble.mouseover(1);
    Bubble.hidden = false;
    //document.body.className = "hideObject";
    hideObjects();

    if (url) {
        $(Bubble.msgid).innerHTML = "<img src='http://img.aujourdhui.com/elements/compte/ajax-loader.gif' style='margin:50px' align='center' />"
        getData(url, Bubble.msgid);
    }

    origY = y;
    origX = x;

}

Bubble.mouseover = function(yn)
{ 
	Bubble.ismouseover=yn;
	//$('temp').innerHTML = yn;
	if (yn==0 && Bubble.enableMouseout)
		Bubble.preClose(3000);
}


var bubbleclosetimer;


Bubble.preClose = function(t)
{// alert(t);
  window.clearTimeout(bubbleclosetimer);
	bubbleclosetimer = window.setTimeout('Bubble.Hide(0)', t);
	
}


Bubble.Hide = function(force)
{//	alert(Bubble.ismouseover);
	if (bubbleclosetimer) window.clearTimeout(bubbleclosetimer);
	if (Bubble.ismouseover==0 || force)
	{	Bubble.ismouseover = 0;
	  Bubble.hidden = true;
		closeDiv(Bubble.id);
		//document.body.className = "";
		showObjects();
	}
}


/*
************************
end BUBBLE SCRIPT
************************
*/



/*
************************
FUNCTION TO center the div object
************************
*/

function CenterObject(divID)
{
 var divObj = $(divID);
 var x= parseInt(document.body.scrollWidth/2 - divObj.offsetWidth/2);
// var y= parseInt(document.body.scrollHeight/2 - divObj.offsetHeight/2) ;
  var y= parseInt(document.body.clientHeight/2 - divObj.offsetHeight/2)  + document.body.scrollTop;

 
 divObj.style.left = x + 'px';
 divObj.style.top = y + 'px';

}

/*
************************
FUNCTION TO center the div object Horizontally
************************
*/

function CenterHObject(divID)
{
 var divObj = $(divID);
 var x= 10;
var y= 10;
 
 divObj.style.left = x + 'px';
 divObj.style.top = y + 'px';

}

/*
************************
MODAL POPUP SCRIPT
************************
*/

function ajaxModal()
{ // this.modalID = "modalPage";
	 this.divBg = "modalBackground";
	 this.id = "modalBox"; 
	 this.divPlainBox = "modalPlainBox"; 
	 this.visible = false;
	 
	 this.init = function()
	 {
		document.write("<div id=\""+ this.divBg +"\"></div>");
		document.write("<div id=\""+ this.id +"\"><center>");
		document.write("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		document.write("<tr class=\"modalHeader\"><td id=\"modalTitle\"></td><td align=right><a href=\"javascr" + "ipt: modal.hide()\"><img src=\"http://img.aujourdhui.com/w_close.gif\" border=\"0\"/> fermer</a></td></tr>");
		document.write("<tr><td colspan=\"2\"><div id=\"modalBody\"></div></td></tr>");
		document.write("</table>");
		document.write("</center></div>");	
		document.write("<div id=\""+ this.divPlainBox +"\"><center>");
		document.write("<div id=\"modalPlainBody\"></div>");
		document.write("</center></div></div>");	
		
	 }
	
	/* 
	 this.showData = function(url, title, height, width)
	 {	var divBg = this.divBg;
	 		OpenDiv(divBg);
			$(divBg).style.height = document.body.scrollHeight;
			window.onscroll = function () {$(divBg).style.height = document.body.scrollHeight};
			
			$('modalBody').innerHTML = "<div id=ajaxLoader></div>";
			$(this.id).style.top = document.body.scrollTop + 200;
			
			getData(url,'modalBody');
			if (title) 
			  $('modalTitle').innerHTML = title;
			
			OpenDiv(this.id);
			return false;
	 }*/
	
	  //display modal popup and adjust the position based on its content or size;
	  this.showData = function(url, title)
	 {	var divBg = this.divBg;
	 		OpenDiv(divBg);
			$(divBg).style.height = document.body.scrollHeight + 'px';
			var id = this.id;
			OpenDiv(id);
			getDataExec(url,'modalBody', function() {
					CenterObject(id);
					/*
					//add functionality to adjust the position when scrolled
					AttachEvent(window,"scroll", function() {
						if (modal.visible)
							{
								CenterObject(id);
							}
						});
					*/
					}
				);
			if (title) 
			  $('modalTitle').innerHTML = title;
			this.visible = true;
			hideObjects();
			return false;
	 }
	 
	 
	 this.Dialog = function(txt, title, confirmText, cancelText, fnClick, fnValue) {
        var divBg = this.divBg;
        OpenDiv(divBg);
        var id = this.id;
        OpenDiv(id);
				
				var html = '<div class="modalNew">' +
				'<div style="margin:5px 10px" id="modalDialogText">' + txt + '</div>' +
				'<div style="text-align:center; margin:10px">';
				
				if (confirmText)
					html +=' <input type="button" id="modalConfirmButton" class="aj-formsubmit" value=" '+ confirmText +' ">' ;
				
				if (cancelText)
					html += ' <input type="button" id="modalCancelButton" class="aj-formsubmit" value=" '+ cancelText +' ">';
				
				html += '</div>';
				
        $("modalBody").innerHTML = html;
		$(divBg).style.height = document.body.scrollHeight + 'px';
		CenterObject(id);
			//	$get(divBg).style.height = document.body.scrollHeight +  'px';
				//$j("#modalConfirmButton").click(function() { if(fnClick) {fnClick(true, fnValue)}; modal.hide(); });
				//$j("#modalCancelButton").click(function() {if(fnClick) {fnClick(false, fnValue)}; modal.hide(); });
				
        if (title)
            $('modalTitle').innerHTML = title;
        this.visible = true;
        hideObjects();
        return false;
    }
	 
	 
	 
	 this.showPlainBox = function(url)
	 {	var divBg = this.divBg;
	 		OpenDiv(divBg);
			$(divBg).style.height = document.body.scrollHeight + 'px';
			window.onscroll = function () {$(divBg).style.height = document.body.scrollHeight};
			$('modalPlainBody').innerHTML = "<div id=ajaxLoader></div>";
			$(this.divPlainBox).style.top = (document.body.scrollTop + 150) + 'px';
			
			getData(url,'modalPlainBody');
			OpenDiv(this.divPlainBox);
			this.visible = true;
			hideObjects();
			return false;
	 }
	 
	 	 this.showCenteredPlainBox = function(url)
	 {	var divBg = this.divBg;
	 		OpenDiv(divBg);
			$(divBg).style.height = document.body.scrollHeight + 'px';
			window.onscroll = function () {$(divBg).style.height = document.body.scrollHeight};
			$('modalPlainBody').innerHTML = "<div id=ajaxLoader></div>";
	
			getData(url,'modalPlainBody');
			OpenDiv(this.divPlainBox);
			CenterHObject(this.divPlainBox);
			this.visible = true;
			hideObjects();
			return false;
	 }
	
	 
	 this.hide = function()
	 { 
	  closeDiv(this.divBg);
	 	closeDiv(this.id);
		closeDiv(this.divPlainBox);
		showObjects();
	 }

	 this.init();
}

function hideObjects()
{	document.body.className = "hideObject";
}

function showObjects()
{
document.body.className = "";
}


var modal = new ajaxModal();


