/*
* scripts.js
*
* Javascripts functions required for the page to function.
*
* @author Peter Törnstrand
* @version 1.0 (2007-03-13)
*/

var ie;
if (navigator.appVersion.indexOf("MSIE")!=-1) { ie = true; } else { ie = false; }

var ie6;
if(ie && (navigator.appVersion.indexOf("6.0")!=-1 || navigator.appVersion.indexOf("6.1")!=-1)){ ie6 = true} else { ie6 = false};


fixIE6SelectBoxPosition = function() {
    if(ie6){
        travelEl = document.getElementById("travel")
        if(travelEl != null && travelEl.clientWidth > 250){
            travelEl.style.left = -300;
        }
    }
}

/* Hide select boxes on menu mouseover */
hideSelect = function() {
    var selects = document.getElementsByTagName("select");
    for ( var i=0; i<selects.length; i++ ) {
        selects[i].style.visibility = 'hidden';
    }
}

/* Show select boxes on menu mouseout */
showSelect = function() {
    var selects = document.getElementsByTagName("select");
    for ( var i=0; i<selects.length; i++ ) {
        selects[i].style.visibility = 'visible';
    }
}

/* Remove click box from links */
linkBoxRemover = function() {
    var el = document.getElementsByTagName('a');
    for(i=0; i<el.length; i++) {
        el[i].onfocus = function() {
            this.blur();
        }
    }
}



/* Hover effects for the Submit/Site/Section Selector buttons */
btnSelectorHover = function() {
    var idArray = ["ctl00_QSearch_QuickSearchButton","ctl00_LinkDdl1_SiteSelectorButton","ctl00_LinkDdl_SiteSelectorButton"];
    var objEl;    
    for(i=0; i < idArray.length;i++){        
        objEl = document.getElementById(idArray[i]);
        
        if(objEl != null && objEl != "undefined"){
            objEl.onmouseover = function() { this.src = '/images/btn_siteselector_hover.gif'; }                
            objEl.onmouseout = function() { this.src = '/images/btn_siteselector.gif'; }       
        }  
    }       
}


/* The following is a fix for the submenu on IE6 and the visibility problem in Safari */
activateMenu = function() {
    if (document.all && document.getElementById('navigationContainer')!=null && document.getElementById('navigationContainer').currentStyle) {
        var navroot = document.getElementById('navigationContainer').firstChild;
        if(navroot == null)
		return;
        /* Get all the list items within the menu */
        var lis=navroot.getElementsByTagName("LI");  
        for (i=0; i<lis.length; i++) {
        
            /* If the LI has another menu level */
            if(lis[i].lastChild.tagName=="UL"){
                
                /* assign the function to the LI */
                lis[i].onmouseover=function() {	
                    
                    /* display the inner menu */
                    this.lastChild.style.display="block";
                    hideSelect();
                }
                lis[i].onmouseout=function() {                       
                    this.lastChild.style.display="none";
                    showSelect();
                }
            }
        }
    }
}

/* Fix for the non clickable logo in IE6, check if user clicked within the image coordinates */
function clickedLogo(evt) {

if (navigator.appVersion.indexOf("MSIE 6") != -1){
    var link = document.getElementById('logotext');
    var cords = findPos(link);
    var matrix = [cords[0], cords[1], (cords[0]+link.offsetWidth), (cords[1]+link.offsetHeight)];
    var xypos = getXY(evt); // x, y
    if(xypos[0]>=matrix[0] && xypos[0]<=matrix[2] && xypos[1]>=matrix[1] && xypos[1]<=matrix[3])
       window.top.location = link.href;
   }
}

function logoClicked(evt) {

    var link = document.getElementById('logotext');
    var cords = findPos(link);
    var matrix = [cords[0], cords[1], (cords[0]+link.offsetWidth), (cords[1]+link.offsetHeight)];
    var xypos = getXY(evt); // x, y
    if(xypos[0]>=matrix[0] && xypos[0]<=matrix[2] && xypos[1]>=matrix[1] && xypos[1]<=matrix[3])
    window.top.location = link.href;
}

/* Get the coordinates of an element */
function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

/* Get the coordinates of a mouse click */
function getXY(evt) {
    var posx = 0;
    var posy = 0;
    if (!evt) var evt = window.event;
    if (evt.pageX || evt.pageY) {
        posx = evt.pageX;
        posy = evt.pageY;
    }
    else if (evt.clientX || evt.clientY) {
        posx = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
        posy = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    return [posx, posy];
}

/* Create the tree menu */
function createTreeMenu() {
    if(document.getElementById('treemenu2'))
        ddtreemenu.createTree("treemenu2", true, 5);
}

function disableButtonsOnJavaScriptDisbled()
{   
    if (document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkAreas') != null)
    {
       document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkAreas').style.visibility = 'hidden';
    }
    if (document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkTowns') != null)
    {
        document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkTowns').style.visibility = 'hidden';
    }
    if (document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkLines') != null)
    {
        document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkLines').style.visibility = 'hidden';
    }
    if (document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkPoints') != null)
    {
        document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkPoints').style.visibility = 'hidden';
    }
    if (document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkDaytype') != null)
    {
	    document.getElementById('ctl00_IframeRegion_mainRegion_newsRegion_ctl00_btnOkDaytype').style.visibility = 'hidden';
    }	
    if (document.getElementById('ctl00_btnOkDaytype') != null)
    {
	    document.getElementById('ctl00_btnOkDaytype').style.visibility = 'hidden';
    }
   

	
    return true;
}


// Add event listeners
if (window.attachEvent) window.attachEvent("onload", btnSelectorHover);
    else window.addEventListener("load", btnSelectorHover, false);
    
if (window.attachEvent) window.attachEvent("onload", fixIE6SelectBoxPosition);
    else window.addEventListener("load", fixIE6SelectBoxPosition, false);    
    
if (window.attachEvent) window.attachEvent("onload", linkBoxRemover);
    else window.addEventListener("load", linkBoxRemover, false);

if (ie) window.attachEvent("onload", activateMenu);

if (window.attachEvent) window.attachEvent("onload", createTreeMenu);
    else window.addEventListener("load", createTreeMenu, false);
    
if (window.attachEvent) window.attachEvent("onload", disableButtonsOnJavaScriptDisbled);
    else window.addEventListener("load", disableButtonsOnJavaScriptDisbled, false);
  
if (ie) document.attachEvent("onclick", clickedLogo);
