/*
************************
BASIC  SCRIPTS
************************
*/

function $(id) {
    return document.getElementById(id);
}


function $get(id) {
    return document.getElementById(id);
}


function OpenDiv(id) {
    $get(id).style.visibility = "visible";
    $get(id).style.display = "block"
}

function closeDiv(id) {
    $get(id).style.visibility = "hidden";
    $get(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;
}



/*
************************
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 obj = $j("#" + divID);
        // **** some text to put while waiting for contents to load
        //obj.innerHTML = "Loading...";
		if (loadImg)
		{ if (typeof loadImg == "string")
		 	obj.html("<img src='"+ loadImg +"'>");
		  else if (loadImg==1)
		 	obj.html("<img src='http://img.aujourdhui.com/blogs/progressimgred.gif'>");
		  else if (loadImg==2)
		  	obj.html("<img src='http://img.aujourdhui.com/ui/ajax-loader.gif'>");
		}
        window.status = url;
		//obj.load(url);
		$j.get(url, function(data) { obj.html(data)});
		

    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;
}



/*
************************
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:1000\">");
    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\">close</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);
    if (url == "/data/profilepreview.asp?rno=7")
        return false;

    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($get(Bubble.id), "mouseover", function() { Bubble.mouseover(1) });
        AttachEvent($get(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);
    $get(Bubble.msgid).innerHTML = "<img src='http://img.aujourdhui.com/profile/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);
    $get(Bubble.msgid).innerHTML = msg;
    //Bubble.ismouseover==0;
    Bubble.mouseover(0);
    if (delay)
        Bubble.preClose(delay);
    return false;
}


Bubble.Show = function(obj, options, url) {
    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
        x = objpos.x + 30;

    if (y == "center")
        y = objpos.y + objHeight / 2 - boxY;
    else if (y == "top")
        y = objpos.y - boxY;
    else
        y = objpos.y - boxY;

    $get("popArrow").style.top = arrowY + 'px';
    $get(Bubble.id).style.left = x + 'px';
    $get(Bubble.id).style.top = y + 'px';
    OpenDiv(Bubble.id);
    Bubble.mouseover(1);
    Bubble.hidden = false;
    //document.body.className = "hideObject";
    hideObjects();

    if (url) {
        $get(Bubble.msgid).innerHTML = "<img src='http://img.aujourdhui.com/profile/loader.gif' style='margin:50px' align='center' />"
        getData(url, Bubble.msgid);
    }



}

Bubble.mouseover = function(yn) {
    Bubble.ismouseover = 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 = $get(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.documentElement.scrollTop;

    divObj.style.left = x + 'px';
    divObj.style.top = y + 'px';
}

/*
************************
FUNCTION TO center the div object Horizontally
************************
*/

function CenterHObject(divID) {
    var divObj = $get(divID);
    var x = parseInt(document.body.scrollWidth / 2 - divObj.offsetWidth / 2);
    var y = 100 + document.documentElement.scrollTop;

    divObj.style.position = 'absolute';
    divObj.style.left = x + 'px';
    divObj.style.top = y + 'px';
}

/*
************************
MODAL POPUP SCRIPT
************************
*/

function ajaxModal() { // this.modalID = "modalPage";
    this.divBg = "modalOverlay";
    this.id = "modalbox";
    this.divPlainBox = "modalContent";
    this.visible = false;

    this.init = function() {
		document.write("<div id=\"" + this.divBg + "\"></div>");
        document.write("<div id=\"" + this.id + "\">");
        document.write("<a href=\"javascr" + "ipt: modal.hide()\" class=\"closeModal\">close</a>");
        document.write("<h3 class=\"modalTitle\" id=\"modalTitle\"></h3>");
        document.write("<div id=\"modalBody\"></div></div>");
        document.write("<div id=\"" + this.divPlainBox + "\">");
        document.write("<div id=\"modalPlainBody\"></div>");
        document.write("</div>");
		
    }
	
	//need to retain this. hmp!
	this.showData_test = function(url, title) {
		return this.showData(url, title);
	}

    this.showData = function(url, title) {
        var divBg = this.divBg;
        OpenDiv(divBg);
        //$get(divBg).style.height = document.body.scrollHeight + 'px';
        AttachEvent(window, "load", function() { $get(divBg).style.height = document.body.scrollHeight + 'px'; });
        var id = this.id;
        OpenDiv(id);
        $j("#modalBody").load(url, function() {
            CenterHObject(id);
            $get(divBg).style.height = document.body.scrollHeight + 'px';

        });

        if (title)
            $get('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>';
				
        $j("#modalBody").html(html);
				CenterHObject(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(); });
				//$j("#modalConfirmButton").click(function() {fnClick(true, fnValue); modal.hide(); });
				//$j("#modalCancelButton").click(function() {fnClick(false, fnValue); modal.hide(); });
				
        if (title)
            $get('modalTitle').innerHTML = title;
        this.visible = true;
        hideObjects();
        return false;
    }
		
	
	

    this.showPlainBox = function(url) {
        var divBg = this.divBg;
        OpenDiv(divBg);
        //$get(divBg).style.height = document.body.scrollHeight + 'px';
        AttachEvent(window, "load", function() { $get(divBg).style.height = document.body.scrollHeight + 'px'; });
        //AttachEvent(window, "scroll", function(){$get(divBg).style.height = document.body.scrollHeight + 'px';});
        //window.onscroll = function () {$get(divBg).style.height = document.body.scrollHeight};
        $get('modalPlainBody').innerHTML = "<div id=ajaxLoader></div>";

        OpenDiv(this.divPlainBox);
        $j("#modalPlainBody").load(url, function() {
            CenterHObject('modalPlainBody');
            $get(divBg).style.height = document.body.scrollHeight + 'px';
        });


        this.visible = true;
        hideObjects();
        return false;
    }

    this.showCenteredPlainBox = function(url) {
        var divBg = this.divBg;
        OpenDiv(divBg);
		
        AttachEvent(window, "load", function() { $get(divBg).style.height = document.body.scrollHeight + 'px'; });
        AttachEvent(window, "scroll", function() { $get(divBg).style.height = document.body.scrollHeight });
        //window.onscroll = function () {$get(divBg).style.height = document.body.scrollHeight};
        $get('modalPlainBody').innerHTML = "<div id=ajaxLoader></div>";

        OpenDiv(this.divPlainBox);
		var divPlainBox = this.divPlainBox;
        $j("#modalPlainBody").load(url, function() {
            CenterHObject(divPlainBox);
            $get(divBg).style.height = document.body.scrollHeight + 'px';
        });

        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();


