// Detect Browser Capabilities
var capable = (document.getElementById && document.getElementsByTagName);
// New Window
var newwin = '';

// Preload banner images
function preloadBanners(pgName)
{
    var imageObj = Array(); 
    imageObj[0] = new Image(); 
    imageObj[0].src = 'images/themes/' + pgName + '_dk_ov.gif';
    imageObj[1] = new Image(); 
    imageObj[1].src = 'images/themes/' + pgName + '_lt_ov.gif';
}


// Print the contents of a DOM element
function printSection(ele,m1,m2) {
    var HTML = document.getElementById(ele).innerHTML; 
    var popWidth = 600;
    var popHeight = 400;
    var popLeft = self.screen.availWidth/2 - popWidth/2;
    var popTop = self.screen.availHeight/2 - popHeight/2;
    var preHTML = "<html><head><style>div.panelSelected{font-weight:bold; font-size:1.5em;} div.panelContent{height:auto;} .noprint{display:none;} .print{display:block;}</style>\n" +
        "<meta name=\"WT.cg_n\" content=\"" + m1 + "\" />\n" +
        "<meta name=\"WT.cg_s\" content=\"PRINT " + m2 + " (Trilogy)\" />\n";

    // Webtrends Javascript
    preHTML+='</head><body onload="document.getElementById(\'js1\').src=\'js/sdc_tag.js\'; window.print();">\n' +
             '<script type="text/javascript" id="js1"></script>\n' +
             '<noscript><IMG ALT="" BORDER="0" NAME="DCSIMG" WIDTH="1" HEIGHT="1" SRC="http://webtrendssdc.jfshea.com/dcscus21400000cdquvwq2j55_8p2x/njs.gif?dcsuri=/nojavascript&amp;WT.js=No&amp;WT.tv=8.0.0">\n' +
             '</noscript>\n';
    
    // DoubleClick Spotlight Tag
    // Creation Date:9/10/2007
    preHTML+='<SCRIPT language="JavaScript">\n' +
             'var axel = Math.random()+"";\n' +
             'var a = axel * 10000000000000;\n' +
             'document.write(\'<IMG SRC="http://ad.doubleclick.net/activity;src=1446938;type=trilo166;cat=super777;ord=1;num=\'+ a + \'?" WIDTH=1 HEIGHT=1 BORDER=0>\');\n' +
             '</SCRIPT>\n' +
             '<NOSCRIPT>\n' +
             '<IMG SRC="http://ad.doubleclick.net/activity;src=1446938;type=trilo166;cat=super777;ord=1;num=1?" WIDTH=1 HEIGHT=1 BORDER=0>\n' +
             '</NOSCRIPT>\n';

    // Section title
    preHTML+='<h1>' + m2 + '</h1>';

    var postHTML = "</div></body></html>\n";
    
    if(HTML)
    {
        preview = window.open('','preview','left='+popLeft+',top='+popTop+',width='+popWidth+',height='+popHeight+
                  ',scrollbars=yes,toolbar=yes,menubar=yes,resizable=yes,status=no');
        preview.document.open();
        preview.document.write(preHTML);
        preview.document.write(HTML);
        preview.document.write(postHTML);
        preview.document.close();
    }

    return; 
}

// MenuImage constructor
function MenuImage(img_id, img_out, img_in, img_act, swapped) 
{
    this.img_id = img_id;           // the image id
    this.img_out = new Image();     // the image to use when mouseout
    this.img_out.src = imageDir + img_out;
    this.img_in = new Image();      // the image to use when mouseover
    this.img_in.src = imageDir + img_in;
    this.img_act = new Image();     // the image when page active
    if(img_act) {
        this.img_act.src = imageDir + img_act;
    } else {
        this.img_act.src = '';
    }    
    this.swapped = swapped;         // register if the image is swapped
    this.over = 0;                  // register if the image is 'over'
}

// take a full or partial path, and return it's last part
function basename(path) 
{
    var parts = path.split('/');
    return parts[parts.length -1];
}

// get an image from an image_id
function get_image(id)
{
    for(var a=0; a<=MImages.length; a++)
    {
        if(MImages[a].img_id == id)
            return a;
    }
}

// swap in the image
function imgIn(image_id)
{
    var imgId = get_image(image_id);

    if(capable && MImages[imgId]) 
    {
        var theObj = MImages[imgId];
        var theImg = document.getElementById(theObj.img_id);
        var filename = basename(theImg.src);

        // don't swap if image is 'down' or currently active 
        if( filename.indexOf('_over') == -1 )
        {
            theImg.src = MImages[imgId].img_in.src;
            MImages[imgId].swapped = 1;
        }
    }
}

// swap out the image
function imgOut(image_id) 
{
    var imgId = get_image(image_id);

    if(capable && MImages[imgId]) 
    {
        theImg = document.getElementById(MImages[imgId].img_id);
        // only revert if "swapped" is true
        if(MImages[imgId].swapped) 
        {
            theImg.src = MImages[imgId].img_out.src;
            MImages[imgId].swapped = 0;
        }
    }
}


/*
 * Functions to swap the background color of a div 
 */

var savedBG = new Array();

function changeBG(id, color)
{
    if(capable && id && color)
    {
        var objStyle = document.getElementById(id).style;
        savedBG[id] = objStyle.backgroundColor;
        objStyle.backgroundColor = "#"+color;
    }
}

function changeBackBG(id)
{
    if(capable && id && savedBG[id])
    {
        var objStyle = document.getElementById(id).style;
        objStyle.backgroundColor = savedBG[id];
        savedBG[id] = ""; 
    }
}


/*
 * Functions to open links in a new window
 */

// open a popup window
function popWindow(theUrl,width,height,full) {
    // use defaults if width and height were not supplied
    var ismoz = navigator.userAgent.indexOf("Gecko");
    var isie6 = navigator.userAgent.indexOf("MSIE 6.0");
    var isie7 = navigator.userAgent.indexOf("MSIE 7.0");
    var default_width = 600;
    var default_height = 450;
    var offset_width = (isie6 != -1) ? 12 : 0;
    var offset_height = (isie6 != -1) ? 155 : 0;
    var offset_height = (isie6 == -1 && isie7 != -1) ? 45 : offset_height;
    var popType = (full) ? ",scrollbars=yes,resizable=yes,status=yes,toolbar=yes,menubar=yes,location=yes,directories=yes" : ",scrollbars=no,resizable=no,status=no";
    
    var popWidth = (width) ? width + offset_width : default_width + offset_width;
    var popHeight = (height) ? height + offset_height : default_height + offset_height;
    var popLeft = self.screen.availWidth/2 - popWidth/2;
    var popTop = self.screen.availHeight/2 - popHeight/2;
    
    if(theUrl) {
        if(newwin)
            newwin.close();
        newwin = window.open(theUrl,'newwin','left='+popLeft+',top='+popTop+',width='+popWidth+',height='+popHeight+popType);
    }

    return;
}

// This will convert the querystring to an associative array
function querystring_to_array(q)
{
    var arr1 = q.split('&'); 
    var arr2 = new Array();
    var arr3 = new Array();

    for(var i=0; i<arr1.length; i++) {
        arr2 = arr1[i].split('=');
        arr3[arr2[0]] = arr2[1]; 
    }

    return arr3;
}


// Capture the event
function mouseLeaves(element, evt) {
    if (typeof evt.toElement != 'undefined' && typeof element.contains != 'undefined') {
        // Internet Explorer
        return !element.contains(evt.toElement);
    } else if (typeof evt.relatedTarget != 'undefined' && evt.relatedTarget) {
        // Mozilla
        return !contains(element, evt.relatedTarget);
    }
} 

// Make sure we're dealing with the top level container
function contains(container, item) {
    while (item) {
        if (container == item) {
            return true;
        }
        item = item.parentNode;
    }
    return false;
}

function PopUp(url, name, h, w, s)
{
    leftPos = 0; 
    if (name.document) {
        name.document.location.href = url;
    } else {
        popup = window.open(url,name,"height="+h+",width="+w+",scrollbars="+s+",screenX=0,screenY=0,left="+leftPos+",top=0,resizable=1");
        popup.focus();
    } 
}

