// JavaScript Document
var timer, pictimer;
var menuid, pic;
//get menu ready
window.onload = function(){
    if (document.getElementById('menu1')!=null){
	    var menu1 = document.getElementById('menu1');
	    var menu2 = document.getElementById('menu2');
	    var menu3 = document.getElementById('menu3');
	    var menu4 = document.getElementById('menu4');
	    var menu5 = document.getElementById('menu5');
	
	    menu1.onmouseover = swapImg;
	    menu2.onmouseover = swapImg;
	    menu3.onmouseover = swapImg;
	    menu4.onmouseover = swapImg;
	    menu5.onmouseover = swapImg;
	
	    menu1.onmouseout = swapOut;
	    menu2.onmouseout = swapOut;
	    menu3.onmouseout = swapOut;
	    menu4.onmouseout = swapOut;
	    menu5.onmouseout = swapOut;
	}
};
//swap image on mouse over
function swapImg(){
    if (pic != null) switchOut();
    pic = this;
    clearTimeout(pictimer);
    
	var fname = this.src;
	var key = 'butt_';
	var selector = 'sel_';
	fname = fname.substring(0,fname.indexOf(key) + key.length) + selector + fname.substring(fname.indexOf(key)+key.length);
	this.src = fname;
	
	//check if dropdowns are for this page
	if (document.getElementById('usedropdown') != null){
	    if (menuid != null) closeDropDown();
	    menuid = null;
	    var xpos = getX(this);
	    var ypos = getY(this) + this.offsetHeight;	    
	    openDropDown(xpos, ypos, this.id);
	}
}
//swap image back
function swapOut(){    
	//check if dropdowns are for this page
	if (document.getElementById('usedropdown') != null){	    
	    menuOut();
	}else{
	    switchOut();
	}
}
function switchOut(){
	var fname = pic.src;
	var selector = 'sel_';
	fname = fname.replace(selector,'');
	pic.src = fname;
	pic = null;
}
//get the x position
function getX(obj){
    var currleft = 0;
    if (obj.offsetParent){
        do{
            currleft += obj.offsetLeft;
        }while (obj = obj.offsetParent);
    }
    return currleft;
}
//get the y position
function getY(obj){
    var currtop = 0;
    if (obj.offsetParent){
        do{
            currtop += obj.offsetTop;
        }while (obj = obj.offsetParent);
    }
    return currtop;
}
//get corresponding dropdown menu name
function getMenuId(id){    
    switch (id){
        case "menu1":
            menuid = "ddCorporate";
            break;
        case "menu2":
            menuid = "ddOperations";
            break;
        case "menu3":
            menuid = "ddInvestors";
            break;
        case "menu4":
            menuid = "ddNews";
            break;
        case "menu5":
            menuid = "ddContact";
            break;
    }
    return menuid;
}

//open dropdown menu
function openDropDown(xpos, ypos, id){       
    clearTimeout(timer);    
    var menu = document.getElementById(getMenuId(id));    
    menu.style.left = xpos + "px";
    menu.style.top = ypos + "px";
    menu.style.display = "block";
    menu.onmouseover = keepAlive;
    menu.onmouseout = menuOut;
}
//close dropdown menu
function closeDropDown(){
    var menu = document.getElementById(menuid);
    menu.style.display = "none";
}
//keep menu open
function keepAlive(){
    clearTimeout(timer);
    clearTimeout(pictimer);
}
//menu closing on mouseout
function menuOut(){
    //alert(menuid);
    pictimer = setTimeout("switchOut()",1000);
    timer = setTimeout("closeDropDown()",1000);
}
//makes a popup description appear
function showDescript(link){
    var dl = document.getElementById('dl' + link.id);
    //var xpos = getX(link);
    var xpos = link.offsetWidth + getX(link);
    var ypos = getY(link);
    dl.style.left = xpos + 'px';
    dl.style.top = ypos + 'px';
    dl.style.display = 'block';
    
}
//makes a popup description disappear
function hideDescript(link){
    var dl = document.getElementById('dl' + link.id);
    dl.style.display = 'none';
}

//basic Ajax function call, all functions with request call this one, after formatting the request string
//url: the aspx file to handle the server stuff
//targetDiv: Where to put the output of the aspx file
//requestVar: the request variables sent to the aspx file, it uses the get method
function getLink (url,type,id,tagtype){
	var xmlDoc;
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
	xmlDoc.async=false;
	xmlDoc.load(url);
	
	var c=0;
	var categories = xmlDoc.getElementsByTagName(tagtype);
	var xcat = categories[c].getAttribute('id');
	//alert(xcat);
	while ((xcat != type)&&(c<categories.length)){
		c++;
		xcat = categories[c].getAttribute('id');
		//alert(xcat);
	}
	var xlink = categories[c].getElementsByTagName('link')[id].childNodes[0].nodeValue;
	//alert(xlink);
	windoc = 
	window.open('dataroom/'+xlink+'#toolbar=0','VoyageurDocument','toolbar=no,menubar=no,location=no,directories=no,copyhistory=no,titlebar=no');
	windoc.document.title = 'Voyageur Document';
	/*
	var viewdiv = document.getElementById('viewdiv');	
	viewdiv.style.display = 'block';
	
	window.frames['viewarea'].location = xlink + '#toolbar=0';
	*/
	//view.src = xlink;
}
//opens a pdf document
function openlegaldoc(link){
    var catname = link.id.replace(/\d/g,'');
    var catnum = link.id.replace(/\D/g,'');
    //alert(catnum);
    getLink('dataroom/drlegal.xml',catname,catnum,'category');
	
    //window.open('<%# XPath("link") %>#toolbar=0','document','toolbar=0');
}
//opens a pdf document
function opentechdoc(link){
    var catname = link.id.replace(/\d/g,'');
    var catnum = link.id.replace(/\D/g,'');
    //alert(catnum);
    getLink('dataroom/drtechnical.xml',catname,catnum,'section');
	
    //window.open('<%# XPath("link") %>#toolbar=0','document','toolbar=0');
}
//shows the links after clicking OK
function showAbstractLinks(button){
    var linkpanel = document.getElementById(button.id.replace('button','list'));
    linkpanel.style.display = 'block';
}