﻿
/* 'FindControl' can be used to find element names in JavaScript, since .NET mangles the ID tag. */
function FindControl(ControlName, ReturnType)
{
    var theForm = document.forms[0];
    var iMax = theForm.elements.length;
    var ElementName = '';
    
    for (i=0; i<iMax; i++)
    {
        if (theForm.elements[i].name.indexOf(ControlName) > 0)
        {
            ElementName = theForm.elements[i].name;
            break;
        }
    }
    
    if (ReturnType == 'string')
    {
        return ElementName;
    } else {
        return eval('theForm.' + ElementName);
    }
}


/* The "nav" functions below cause the top-nav / sub-nav magic to happen */

var DefaultNav = '1';
var TimerIdArray = new Array();
var TimerIdCount = 0;

function ShowDefaultSubNav()
{   //show the default subnav, in one second (sets a timer)
    TimerIdArray[TimerIdCount++] = self.setTimeout("nav(DefaultNav)", 1000);
}

function ClearTimeOuts()
{   //because of the <a> tags in the subnav div's, there are multiple "onmouseout" events that fire, and 
    //therefore, multiple timers are set. This keeps track of the potentially multiple "showdefaultsubnav"
    //functions that are waiting to fire off.
    for (var i=0; i < TimerIdCount; i++)
    {
        clearTimeout(TimerIdArray[i]);
    }
}

function nav(item)
{   
    if (document.getElementById)
    {
        if (arguments.length > 1)
        {
            DefaultNav = item;
        }
        
        if (item == 'out')
        {
            ShowDefaultSubNav();
        } 
        else 
        {
            
            //first, get rid of any pending "showdefaultsubnav" functions that may be waiting to fire off.
            ClearTimeOuts();
            
            //now, grab all the <li> tags that are in the "topnav" td
            var NavLis = document.getElementById('topnav').getElementsByTagName('ul')[0].getElementsByTagName('li');
            
            //now, loop through them.
            for (var i=0; i<NavLis.length; i++)
            {   //if this is the item that was actually moused over... and it's not the "spacer" between main nav and classifieds...
                if (1 == 1) // (i != 13) // removed spacer because there are so many tabs now... want to keep the logic here in case it's needed
                {
                    if (i == (item - 1)) 
                    {   //then give it the highlighted style, and show its subnav div.
                        if (NavLis[i].dir != 'ltr')
                            NavLis[i].className = 'on';
                        eval("document.getElementById('subnav_" + item + "').style.display = 'inline';");
                        //alert("document.getElementById('subnav_" + item + "').style.display = 'inline';");
                    } else {
                        //check to see if the "off" style is "classifieds red" or not.
                        if (NavLis[i].dir != 'ltr')
                            NavLis[i].className = '';
                        //and hide the associated subnav
                        eval("document.getElementById('subnav_" + (i + 1) + "').style.display = 'none';");
                    }
                }
            }
            
        }
    }
}

// can pass in image name and image to swap
// ie changeImages('mc', '/images/template/mostcommented.jpg')
function changeImages()
{
    if (document.images)
    {
        for (var i=0; i<changeImages.arguments.length; i+=2) 
        {
            document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
        }
    }
}

function ChangeBGColor(item, itemColor) {
    //if the browser supports this...
    if (document.getElementById) {
        document.getElementById(item).style.backgroundColor = itemColor;
    }
}


function ShowHide(item)
{
    //if the browser supports this...
    if (document.getElementById)
    {
        //if the user specifically passed in a state to use for the item in question (inline, none)
        if (arguments.length > 1)
        {
            document.getElementById(item).style.display = arguments[1];
        } else {
            //otherwise, we want to take the current state of this item, and flip it.
            if (document.getElementById(item).style.display == 'none')
            {
                document.getElementById(item).style.display = 'inline';
            } else {
                document.getElementById(item).style.display = 'none';
            }
        }
    }
}

function OpenWindow(url)
{
    var params;
    
    if (arguments.length > 1)
        params = arguments[1];
    else
        params = 'scrollbars=yes,resizable=yes,width=640,height=480'
    
    var win = window.open(url,'NewWin',params);
}


function SetHomePage(obj) {
    //This script fails in anything but IE, so the "return false" never happens.
    // Therefore, homepage is set automagically in IE, and others get manual instructions.
    obj.style.behavior = 'url(#default#homepage)';
    obj.setHomePage('http://www.tulsaworld.com');
    return false;
}