window.onload = function()
{
  enableToolTips();
  prepareImageSwap("nav");
  getMovLinks();
}

function getElementsByClassName(oElm, strTagName, oClassNames){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements);
}
function enableToolTips(id){
    var links,i,h;
    if(!document.getElementById || !document.getElementsByTagName) return;
    h=document.createElement("span");
    h.id="btc";
    h.setAttribute("id","btc");
    h.style.position="absolute";
    document.getElementsByTagName("body")[0].appendChild(h);
    links=getElementsByClassName(document, "a", "glossary");
    for(i=0;i<links.length;i++){
        prepareLinks(links[i]);
    }
}

function prepareLinks(el){
    rel=el.getAttribute("rel");
    if(rel==null || rel.length==0) return;
    defEl = document.getElementById("tt"+rel);
    if (!defEl) return; 
    defText = defEl.innerHTML;
    tooltip=createEl("span","tooltip");
    topSpan=createEl("span","top");
    topSpan.innerHTML = defText;
    tooltip.appendChild(topSpan);
    setOpacity(tooltip);
    el.tooltip=tooltip;
    el.onmouseover= showTooltip;
    el.onmouseout=hideTooltip;
    //el.onmousemove=Locate;
}

function showTooltip(e){
  document.getElementById("btc").appendChild(this.tooltip);
  locate(e);
}

function hideTooltip(e){
  var d=document.getElementById("btc");
  if(d.childNodes.length>0) d.removeChild(d.firstChild);
}

function setOpacity(el){
  el.style.filter="alpha(opacity:95)";
  el.style.KHTMLOpacity="0.95";
  el.style.MozOpacity="0.95";
  el.style.opacity="0.95";
}
function createEl(t,c){
  var x=document.createElement(t);
  x.className=c;
  x.style.display="block";
  return(x);
}


function locate(e){
  var posx=0,posy=0;
  if(e==null) e=window.event;
  if(e.pageX || e.pageY){
    posx=e.pageX; posy=e.pageY;
}

  else if(e.clientX || e.clientY){
    if(document.documentElement.scrollTop){
        posx=e.clientX+document.documentElement.scrollLeft;
        posy=e.clientY+document.documentElement.scrollTop;
    }
    else{
        posx=e.clientX+document.body.scrollLeft;
        posy=e.clientY+document.body.scrollTop;
    }
  }
  document.getElementById("btc").style.top=(posy+10)+"px";
  document.getElementById("btc").style.left=(posx-20)+"px";
}
function getMovLinks(){
    var objBody = document.getElementsByTagName("body").item(0);
    var objMovHolder = document.createElement("div");
    objMovHolder.setAttribute('id', 'movholder');
    objMovHolder.style.display = 'none';
    objMovHolder.style.position = 'absolute';
    objMovHolder.style.zIndex = '100'; 
    objBody.insertBefore(objMovHolder, objBody.firstChild); 


    var div = document.getElementById('movlinks');
    if (!div) return;
    var links = div.getElementsByTagName("a");
    for (var i=0; i < links.length; i++){
        links[i].onclick = function(){
            movid = this.getAttribute('rel');
            if(!movid){ return; }
            ajaxManager('getmovie');
            showFlashBox('movholder');
            return false;
        }
    }
}

function hideMovHolder(){
    obj = document.getElementById('movholder');
    obj.style.display = 'none';
}
function showFlashBox(obj)
{

	obj = document.getElementById(obj);
        arrayPageSize = getPageSize();
        arrayPageScroll = getPageScroll();
        obj.style.visibility = 'hidden';
	obj.style.display = (obj.style.display == 'block') ? 'none' : 'block';

        var objTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - obj.offsetHeight) / 2);
	var objLeft = ((arrayPageSize[0] - obj.offsetWidth) / 2);
       
	obj.style.top = (objTop < 0) ? "0px" : objTop + "px";
	obj.style.left = (objLeft < 0) ? "0px" : objLeft + "px";
	obj.style.visibility = (obj.style.visibility == 'visible') ? 'hidden' : 'visible'; 

}


function placeIt(obj)
{
	obj = document.getElementById(obj);
	if (document.documentElement)
	{
		theLeft = document.documentElement.scrollLeft;
		theTop = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		theLeft = document.body.scrollLeft;
		theTop = document.body.scrollTop;
	}
	theLeft += x;
	theTop += y;
	obj.style.left = theLeft + 'px' ;
	obj.style.top = theTop + 'px' ;
	setTimeout("placeIt('layer1')",500);
}

function ajaxManager(){
    var ran_unrounded= Math.random()*100;
    var ran_number=Math.floor(ran_unrounded);
    var args = ajaxManager.arguments;
    switch(args[0]){
        case "load_page":
            if (document.getElementById){
                var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
            }
            if (x){
                x.onreadystatechange = function(){
                    if (x.readyState == 4 && x.status == 200){
                        el = document.getElementById(args[2]);
                        el.innerHTML = x.responseText;
                    }
                }
                x.open("GET", args[1], true);
                x.send(null);
            }
        break;
        case "getmovie": 
            ajaxManager('load_page', 'tutorialmov.php?id='+movid+'&num='+ran_number, 'movholder');
        break;
    } 
}

// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
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;
}



// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
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;
}

