/**

Portal Javascript.

This system is designed to be invisible to non JS browsers. All behaviours are attached to the relevent divs at runtime

*/

var yMousePosition=0;
var oldPortal='';

//window.captureEvents(Event);
//window.onmouseover= storeCoords;

function storeCoords(e) {
	yMousePosition=e.pageY;
}




/**  this is the loader that calls all the other onload functions */
function portal_init(){

	createPortalButtons();

}

/**  create the buttons */
function createPortalButtons(){

    var divs = document.getElementsByTagName("div");

    var templ;
	for (i=0;i<divs.length ;i++ ){

        if (divs[i].className.substring(0,10)=="portalLink"){
            templ = divs[i].className.substring(10,11);
            divs[i].className='portalButton'+templ;				// this displays the button

			/*divs[i].onmouseover=function() {
				openPortal(this);
			}*/

			divs[i].onclick=function() {
				openPortal(this);
			}

			divs[i].ondblclick=function() {
				openPortal(this);
			}

		}
	}
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1)
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }



function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function include_js(id){

    eval ($(id+'_js').innerHTML);
    eval(id+'_js()');

}


/**  open a single portal - close all the others */
function openPortal(obj){

    var portal = document.getElementById("portal");

	//determine the template and switch around:
	/*var templ = obj.className.substring(12,13);
	obj.className='portalPointer'+templ;	// do the button
	var objid = obj.id;
    var destm = 'prtmain_'+templ;
    var destl = 'prtleft_'+templ;*/
    obj.className='portalPointer';
	// reset the button
	if (window.oldPortal!=''){

		var oldfound = 1;
	}else{
	    var oldfound = 0;
	}


	window.oldPortal=obj.id;		// we'll need this later

	// close the portal if it's open
	if (oldfound && parseInt($('prtleft').style.left) <= -100){
		/*new Effect.MoveBy( 'prtleft', 0, -parseInt($('prtleft').style.left) , {duration: 1,  transition: Effect.Transitions.sinoidal, queue: {position: "end", scope: "myQueue"}});
		new Effect.QueueThis('prtleft', function (elem) { elem.setStyle({display: "none"});},{ queue: {position: "end", scope: "myQueue"}});
		new Effect.BlindUp('prtmain',{ queue: {position: "end", scope: "myQueue"}});*/
		closePortal();
	}
	//position the portal in the queue, not working
	
		new Effect.QueueThis('portal', function (elem) {
	
			$('prtmain').className = 'prtmain_'+parseInt($('ps_'+obj.id).innerHTML);
			$('prtmain').style.left = findPosX($('portal_positioner'))-72+'px';
			$('prtleft').style.left = findPosX($('portal_positioner'))-72-127+'px';
		
			$('prtleft').className = 'prtleft_'+parseInt($('ps_'+obj.id).innerHTML);
		},{ queue: {position: "end", scope: "myQueue"}});
	
		new Effect.QueueThis('portal', function (elem) {
			var toppos = findPosY(obj) + "px";
			portal.style.top=toppos;
			$('prtmain').style.top=0;
		},{ queue: {position: "end", scope: "myQueue"}});
	
		new Effect.QueueThis('portal', function (elem) { ajax('prtmain','prtleft',"portal/content/getPortal.php?client=" + obj.id);},{ queue: {position: "end", scope: "myQueue"}});
	
		// show the portal
	
		new Effect.BlindDown('prtmain',{ queue: {position: "end", scope: "myQueue"},duration: 0.5});
		new Effect.QueueThis('prtleft', function (elem) {
			elem.setStyle({display: "block"});
	
		},{ queue: {position: "end", scope: "myQueue"}});
	
	
		new Effect.MoveBy( 'prtleft', 0, -127 , {duration: 0.5,  transition: Effect.Transitions.sinoidal, queue: {position: "end", scope: "myQueue"}});
	
		new Effect.QueueThis('prtleft', function (elem) { include_js(obj.id);},{ queue: {position: "end", scope: "myQueue"}});
	
		new Effect.QueueThis('prtleft', function (elem) {_rsEvent("portal/content/getPortal.php?client=" + obj.id, 'interest');
		pageTracker._trackPageview("portal/content/getPortal.php?client=" + obj.id);},{ queue: {position: "end", scope: "myQueue"}});



}

/** close a single portal */
function closePortal(){

    if (window.oldPortal!=''){
        // reset the button
        if (window.oldPortal!=''){
            var op = document.getElementById(window.oldPortal);
            op.className='portalButton';

            var oldfound = 1;
        }else{
            var oldfound = 0;
        }


        /*new Effect.MoveBy( 'prtleft', 0, 127 , {duration: 0.5,  transition: Effect.Transitions.sinoidal, queue: 'end', scope: "myQueue"});
        new Effect.QueueThis('prtleft', function (elem) { elem.setStyle({display: "none"});},{ queue: {position: "end", scope: "myQueue"}});
        new Effect.BlindUp('prtmain',{queue: 'end', scope: "myQueue"});*/

        new Effect.MoveBy( 'prtleft', 0, 127 , {duration: 0.2,  transition: Effect.Transitions.sinoidal, queue: {position: "end", scope: "myQueue"}});
		new Effect.QueueThis('prtleft', function (elem) { elem.setStyle({display: "none"});},{ queue: {position: "end", scope: "myQueue"}});
		new Effect.BlindUp('prtmain',{ duration:0.2, queue: {position: "end", scope: "myQueue"}});
    }
}



/**  get the data from a PHP script and output it to an innerHTML */
function ajax(destinationDiv1,destinationDiv2,ajaxURL){

	document.getElementById(destinationDiv1).innerHTML='<div id="waiting"><img src="portalclient/waiting.gif" /></div>';
	document.getElementById(destinationDiv2).innerHTML='';


    // Mozilla/Safari
	if (window.XMLHttpRequest) {
		xmlHttpReq = new XMLHttpRequest();

	// IE<6
	}else if (window.ActiveXObject) {
		xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlHttpReq.open('GET', ajaxURL, true);
    if (xmlHttpReq.overrideMimeType) {
            //xmlHttpReq.overrideMimeType('text/xml');
	    }
	xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlHttpReq.onreadystatechange = function() {
		if (xmlHttpReq.readyState == 4) {

			var str=xmlHttpReq.responseText;

		    var s=str.split(":::");

		    if (s[0] && s[1]){
		        var bodyStr=s[0].replace("<content>","");		// the string needs to be wrapped in <content></content> for XML compliance
		        var navStr=s[1].replace("</content>","");

		        document.getElementById(destinationDiv1).innerHTML=bodyStr;
		        document.getElementById(destinationDiv2).innerHTML=navStr;
		    }
		}
	}
	xmlHttpReq.send('');

}



//-----------------------------------------------------------------------------------------------------------------------------------------------------------
function hideDiv(divName){

	var obj=document.getElementById(divName);
	if (typeof(obj)!='undefined'){
		new Effect.SlideUp(divName);
	}
}


/*
 * non portal stuff, due here for legacy reasons, and should be
 * removed once every page includes dropdown.js
 */

function init() {
	if (TransMenu.isSupported()) {
		TransMenu.initialize();
	}
}

function getScrollY(){

    var x,y;
    if (self.pageYOffset) // all except Explorer
    {
        //x = self.pageXOffset;
        y = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    {
       //x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    else if (document.body) // all other Explorers
    {
        //x = document.body.scrollLeft;
        y = document.body.scrollTop;
    }

    return y;
}

function flashPlay() {var vodcast = window.document.hanover;vodcast.TCallFrame("/",3)}
