function initialLoad(tabType)
{
    // set default tab
    var channels = document.getElementById("channels_" + tabType);
    
    if (channels) 
    {
        tabs = channels.getElementsByTagName("li");
        if (tabs[0]) tabs[0].className = "current"; // first tab selected by default
        
        var viewAllButtons = document.getElementById("viewAllButtons");
        buttons = viewAllButtons.getElementsByTagName("a"); 
        if (buttons[0].toString().indexOf(".aspx") != -1) buttons[0].parentNode.style.display = "block"; buttons[0].parentNode.style.visibility = "visible";        
//        if (tabType.toLowerCase() == "home") 
//        {
//            for (var index = 0; index < buttons.length; index++)
//                buttons[index].parentNode.style.top = "0px";
//        }
    }
    initCarousel(tabType);
}

function settab(tabLink, tabType)
{
    container = document.getElementById("channels_" + tabType);
    if (container)
    {
        // deselect all of the tabs in the container
        tabs = container.getElementsByTagName("LI");
        for (var tabIndex = 0; tabIndex < tabs.length; tabIndex++)
            deselectTab(tabs[tabIndex]);
        
        // select the one they just clicked on
        selectTab(tabLink.parentNode);
    }
}

function deselectTab(tab)
{
    tab.className = '';
    
    // hide the content associated with this tab
    tabCarousel = document.getElementById(tab.id + "_carousel");
    if (tabCarousel) { tabCarousel.style.display = "none"; tabCarousel.style.visibility = "hidden"; }
    
    // hide the view all button associated with this tab
    shortID = tab.id.substring(0, tab.id.length - 3);
    tabButton = document.getElementById(shortID + "_ViewAll");
    if (tabButton.parentNode) { tabButton.parentNode.style.display = "none"; tabButton.parentNode.style.visibility = "hidden"; }
}

function selectTab(tab)
{
    tab.className = 'current';
    // show the content associated with this tab
    tabCarousel = document.getElementById(tab.id + "_carousel");
    if (tabCarousel) {  tabCarousel.style.display = "block"; tabCarousel.style.visibility = "visible"; }
    
    // show the view all button for this tab
    shortID = tab.id.substring(0, tab.id.length - 3);
    tabButton = document.getElementById(shortID + "_ViewAll");
    if (tabButton) { 
        // make the link goes to something, if so display it
        if (tabButton.toString().indexOf(".aspx") != -1) tabButton.parentNode.style.display = "block"; tabButton.parentNode.style.visibility = "visible"; 
    }    
    
    LoadSelectedTabImages(tabCarousel, 0);
    refreshAdCalls();
}

function LoadImages(imageArr,listItemID,startIndex)
{
    var channelItemCount = 7;
    var endIndex = startIndex + channelItemCount;

    // get the list item
    var liObj = document.getElementById(listItemID);
    // get a collection of the images in that list item
    images = liObj.getElementsByTagName('img');
            
    // loop over the array, starting at the startIndex and assign image sources
    for (imageLoadIndex = startIndex; imageLoadIndex < endIndex; imageLoadIndex++)
    {
        images[imageLoadIndex].src = imageArr[imageLoadIndex];
    }
}
function LoadSelectedTabImages(tabcarousel, startIndex)
{
    if (tabcarousel)
    {
       if (tabcarousel.style.visibility == "visible")
       {
            var tabType = tabcarousel.id.replace("_carousel", "");
            LoadImages(eval (tabType + "Arr"), tabType + "_content", startIndex);
       }
    }
}  
function getShowTimes(pageToGo, zipCodeElement) {
    var zipCode = zipCodeElement.value;
    if (validateZipCode(zipCode))
        document.location = pageToGo + "?address=" + zipCode;    
    else
        zipCodeElement.value = "Invalid Zip";        
}

function clearZipInput(element) {
    if (element.value.toLowerCase() == "zip" || element.value.toLowerCase() == "invalid zip") element.value = "";
}
function showTimesKeyUp(e, pageToGo, zipCodeElement) {
    if (e.keyCode == 13)
    {
        getShowTimes(pageToGo, zipCodeElement);
    }
}

function validateZipCode(elementValue){
    var zipCodePattern = /^\d{5}$/;
     return zipCodePattern.test(elementValue);
}

// update ads in iframes
function refreshAdCalls() {
    ord = Math.random() * 10000000000000000;
    setCookie("__TEMP_ADORD", ord);
    $('iframe').each(function(i) {
        if (this.src != '' && this.src.indexOf('adsource.aspx') >= 0) {
            var index = this.src.indexOf('&adord=');
            if (index >= 0) {
                var iframeSrc = this.src.substring(this.src.indexOf('&adord='));
                iframeSrc = iframeSrc.substring(0, iframeSrc.indexOf(';'));
                this.src = this.src.replace(iframeSrc, '&adord=' + (Math.random() * 10000000000000000) + ';');
            }
            else {
                this.src = this.src + '&adord=' + (Math.random() * 10000000000000000) + ';';
            }
        }
    });
}