// Easy Events client side AJAX
// I know this could be reduced a lot...

var EasyEvents = {
    listDivId: 'popup_box',
    calDivId: '',
    linkId: '',
    xhr0: window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0"),
    updateList:function(n,y,m,d,lp,gtdp){
        var xhro = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
        xhro.open('post',this.ajaxPageUrl,true);
        xhro.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhro.onreadystatechange = function()
        {
            if (xhro.readyState == 4 && xhro.status == 200)
            {
                document.getElementById(EasyEvents.listDivId).innerHTML = xhro.responseText;
			}
        }
        if ( xhro )
        {
            xhro.send('a=ul&y='+y+'&m='+m+'&d='+d+'&lp='+lp+'&gtdp='+gtdp+'&n='+n);

        }
        return false;
    },
    updateCalendar:function(n,y,m,d,pid,gtdp){
        EasyEvents.hideDetails();
		var xhro = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
        xhro.open('post',this.ajaxPageUrl,true);
        xhro.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhro.onreadystatechange = function()
        {
            if (xhro.readyState == 4 && xhro.status == 200)
            {
                document.getElementById(EasyEvents.calDivId).innerHTML = xhro.responseText;
            }
        }
        if ( xhro )
        {
            xhro.send('a=uc&y='+y+'&m='+m+'&d=0&pid='+pid+'&n='+n+'&gtdp='+gtdp);

        }
        return false;
    },
/*    hideDetails:function(){
		var div  = document.getElementById(EasyEvents.listDivId);
		var speA = document.getElementById('speech_img');
		var speB = document.getElementById('speech_below_img');

		div.style.visibility = "hidden";
		speA.style.visibility = "hidden";
		speB.style.visibility = "hidden";
    },*/
    getDetails:function(n,eeid,linkId,lp){
        var xhro = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
        xhro.open('post',this.ajaxPageUrl,true);
        xhro.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhro.onreadystatechange = function()
        {
            if (xhro.readyState == 4 && xhro.status == 200)
            {
				var iDiv = document.getElementById('popup_box_inner');
				iDiv.innerHTML = xhro.responseText;

				EasyEvents.linkId = linkId;

				var img  = document.getElementById('popup_box_image');
				if (img == null) {
					EasyEvents.positionPopup();
				}			// else positionPopup will be called by onLoad function of image
			}
		}
        if ( xhro )
        {
            xhro.send('n='+n+'&a=gd&i='+eeid+'&lp='+lp);

        }
        return false;
    },
    positionPopup:function(){
		var w  = 280;		// width (excl. image)
		var mR = 12;		// margin to remove if no image
		var oH = 15;		// max overHang
		var lO = -60;		// left offset
		var tO = -60;		// top offset
		var bO = 10;		// top offset (below)
		var sO = 60;		// speech offset
		
		var div  = document.getElementById(EasyEvents.listDivId);
		var speA = document.getElementById('speech_img');
		var speB = document.getElementById('speech_below_img');

		div.style.visibility = "visible";

		var lnk  = document.getElementById(EasyEvents.linkId);
		var cont = document.getElementById('page');
		var img  = document.getElementById('popup_box_image');

		if (img == null) {
			div.style.width = (w - mR) + 'px';
		} else {
			div.style.width = (w + img.width) + 'px';
		}

		var contpos = EasyEvents.getPos(cont);
		var lnkpos  = EasyEvents.getPos(lnk);
		
		var left = lnkpos[0] - contpos[0] + lO;
		var top  = lnkpos[1] - contpos[1] + tO - div.offsetHeight;
		
		var speL = left + sO;

		if (left + div.offsetWidth > cont.offsetWidth - oH) {
			left = cont.offsetWidth - div.offsetWidth + oH;
		}

		if (left < - oH) {
			left = - oH;
		}

		if (top >= 0) {
			spe  = speA;
			speN = speB;
		} else {
			top = lnkpos[1] - contpos[1] + bO;
			spe  = speB;
			speN = speA;
		}

		div.style.left = left + 'px';
		div.style.top = top + 'px';
		
		spe.style.left = (speL - left) + 'px';
		spe.style.visibility = "visible";
		speN.style.visibility = "hidden";
    },
    getPos:function(obj){
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	}
}

