﻿    
Browser = new function () {

this.isSupported = function(){
	return typeof document.getElementsByTagName != "undefined"
		&& typeof document.getElementById != "undefined";
};

	var ua = navigator.userAgent;
	var OMNI = /Omni/.test(ua);

	this.OP5 = /Opera [56]/.test(ua);
	this.OP7 = /Opera [7]/.test(ua);
	this.MAC = /Mac/.test(ua);
	
	if(!this.OP5 && !OMNI){
		this.IE5 = /MSIE 5/.test(ua);
		this.IE5_0 = /MSIE 5.0/.test(ua);
		this.MOZ =/Gecko/.test(ua);
		this.MAC_IE5 = this.MAC && this.IE5;
		this.IE6 = /MSIE 6/.test(ua);
		this.KONQUEROR = /Konqueror/.test(ua);
	}		
};


var TAB_CONTENT_INDEX = 1;
var TAB_HEADER_INDEX = 0;
var b = Browser;
var activeTabID = null;
var DEFAULT_PANE_HEIGHT = 189;
var INCREMENT_PANE_HEIGHT = 2;
var DEFAULT_COLLAPSED_HEIGHT = 20;
var numberOfTabs = 0;

if(b.MOZ)
{
    TAB_CONTENT_INDEX = 3;
    TAB_HEADER_INDEX = 1;
}
    var tabs = document.getElementById("SliderTabs");

function SetBorderOnBottomOfTabs()
{
    //set the class of the bottom tab header to sliderBarBottomTab
    tabs = document.getElementById("SliderTabs");
    
    if (tabs == null)
        return;
        
    if(!tabs.hasChildNodes)
      return;
      
   try
   {
        //spin the tabs and set the class to include the bottom border on all tabs not highlighted
        for(t = 1; t < tabs.childNodes.length - 1; t++)
        {
            if(tabs.childNodes[t].childNodes[TAB_HEADER_INDEX].className != "sliderBarOn")
                tabs.childNodes[t].childNodes[TAB_HEADER_INDEX].className = "sliderBar";
        }
          
        //get the last tab
         var last = tabs.childNodes[tabs.childNodes.length - 1];
        
        //check out the header div. if it is NOT highlighted, then it is at the bottom and we need to change the class
        if(last.childNodes[TAB_HEADER_INDEX].className == "sliderBar")
            last.childNodes[TAB_HEADER_INDEX].className = "sliderBarBottomTab";
    }
    catch(e)
    {
       ErrorLog.LogError(1,1,e.message);
    }
   
}



function ShowTab(eleID)
{
    try
    {
        //clear any residual infoboxes
        map.HideInfoBox();
        
        var ele = document.getElementById(eleID);
        var activeTab = document.getElementById(activeTabID);
        
        //this is the primary container for the sliders. if it is null, something is wrong and we need to punt

        SetBorderOnBottomOfTabs();
        
        //hide print menu
        TogglePrintMenu(false);


        
        if((eleID == "SearchResultsSldr") || (eleID == "SearchEventsResultsSldr")) 
        {
             //if < 25 results, there is only 1 child of ele.childNodes[TAB_CONTENT_INDEX].childNodes[0]
             //if > 25 results, then we have the controls div, so ele.childNodes[TAB_CONTENT_INDEX].childNodes[0] has 2 children
             //if no results, there is only one child
             
            if(!ele.childNodes[TAB_CONTENT_INDEX].hasChildNodes)
               return;
               
            if(ele.childNodes[TAB_CONTENT_INDEX].childNodes[0].hasChildNodes)
            {
                if( ele.childNodes[TAB_CONTENT_INDEX].childNodes[0].childNodes.length == 1)
                    ele.childNodes[TAB_CONTENT_INDEX].childNodes[0].childNodes[0].style.height =  (300 - ((numberOfTabs - 2) * 11)-10 - 4);
                    
                if( ele.childNodes[TAB_CONTENT_INDEX].childNodes[0].childNodes.length > 1)
                    ele.childNodes[TAB_CONTENT_INDEX].childNodes[0].childNodes[1].style.height =  (300 - ((numberOfTabs - 2) * 11)-10 - 4);
            }    
            
        }
        
       //no need to process any further
        if(activeTabID == eleID)
            return;
        
        if(activeTab != null)
        {
            activeTab.style.height = DEFAULT_COLLAPSED_HEIGHT + "px";
            activeTab.childNodes[TAB_CONTENT_INDEX].style.height = DEFAULT_COLLAPSED_HEIGHT + "px";
            activeTab.childNodes[TAB_CONTENT_INDEX].style.display = "none";
            activeTab.childNodes[TAB_HEADER_INDEX].className = "sliderBar";
        }
        
        //set content to show
        ele.childNodes[TAB_CONTENT_INDEX].style.display = "block";
        
        //set header sytle
        ele.childNodes[TAB_HEADER_INDEX].className = "sliderBarOn";
           
        //incrementally increase size of pane
        var totalHeight = ((DEFAULT_PANE_HEIGHT - (numberOfTabs * 11))  - 1);
        var incHeight = 0;
        
        //if pane is SearchResultsSldr, adjust the div containing the results, too (totalHeight - height of the controls div (next prev)


        
        for(i = INCREMENT_PANE_HEIGHT; i < totalHeight; i++)
        {
            incHeight = (i * INCREMENT_PANE_HEIGHT);
            ele.style.height = incHeight + "px";
            ele.childNodes[TAB_CONTENT_INDEX].style.height = incHeight + "px";
          
        }
      
        //set the visible tab element. this is the container for the head and content of the tab
        activeTabID = eleID;
        
        //now, set the top slider graphic to white if one of the search controls are open
        if((eleID == "SearchEventsAgainSldr") || (eleID == "SearchRouteAgainSldr") || (eleID == "SearchStoreAgainSldr"))
            document.getElementById("SliderTopImg").src = "Images/sliderbox/sliderbox_top_on.gif";//set on graphic
        else
            document.getElementById("SliderTopImg").src = "Images/sliderbox/sliderbox_top.gif";//set off graphic
    }
    catch(e)
    {
       ErrorLog.LogError(1,1,"ShowTab tab ele id = " + eleID + ". error: " + e.message);
    }
     
}

   
    function AddTab(tabid)
    {
        var hdntabs = document.getElementById("HiddenSliderTabs");
        var tabs = document.getElementById("SliderTabs");
        var tab = document.getElementById(tabid)
        
        try
        {
            //the tab might still be visible since we're checking to see if we should keep the search controls
            if(tab.parentElement.id == "SliderTabs")
                return; //this is already added
            
            //set visibility
            tab.style.display = "block";
            
            //remove tab from hidden
            hdntabs.removeChild(tab);
            
            //add tab to SliderTabs
            tabs.appendChild(tab);
            
            numberOfTabs++;
            
            //if we just added the store control, we need to reset the checkboxes. For some reason they lose 
            //their checked status when the control is moved to a new parent.
            if(tabid == "SearchStoreAgainSldr")
                CheckStoreFilters();    //this is on the findstore control
            if(tabid == "SearchRouteAgainSldr")
                CheckRouteFilters();
        }
        catch(e)
        {
           ErrorLog.LogError(1,1,"AddTab error = " +  e.message);
        }
        
    }
   
   function RemoveTab(tabid)
   {
        var hdntabs = document.getElementById("HiddenSliderTabs");
        var tabs = document.getElementById("SliderTabs");
        var tab = document.getElementById(tabid)
        try
        {
            //set visibility
            tab.style.display = "none";
            
            //remove tab from hidden
            tabs.removeChild(tab);
            
            //add tab to SliderTabs
            hdntabs.appendChild(tab);
            
            numberOfTabs--;
        }
        catch(e)
        {
           ErrorLog.LogError(1,1,"RemoveTab error = " + e.message);
        }
   }
   
   function ShouldIRemoveTab(tabID)
   {
        //only remove the tab if you are performing a DIFFERENT kind of search
        var remove = true;
        
        if(forceTabRemoval)
            return true;
        
        var ele = document.getElementById(tabID);

        if (ele.parentElement.id == "SliderTabs")
            remove = false;
            
        return remove;
        

   }
   
   function RemoveAllTabs()
   {
        var tabs = document.getElementById("SliderTabs");
    
        var children = tabs.childNodes;
        
        for(i = children.length - 1 ; i > -1; i--)
        {
            try
            {
                if (children[i].id != null)
                {
                    if(ShouldIRemoveTab(children[i].id))
                        RemoveTab(children[i].id);
                }
            }
            catch(e)
            {
               ErrorLog.LogError(1,1,"RemoveAllTabs error = " + e.message);
            }
        }
   }
   var forceTabRemoval = false;
   
   function SwapTab(eleID, oldEleID)
   {
   
        var hdntabs = document.getElementById("HiddenSliderTabs");
        var tabs = document.getElementById("SliderTabs");
        
        //loop through children of tabs and store ids in array
        //remove all tabs
        //loop through array of ids, adding tabs
        //if array.tabid == oldEleID, add eleID
        var tabids = new EntityDict();
        
        if(tabs == null)
            return;
            
        if(!tabs.hasChildNodes())
            return;
            
        for(i = 0; i < tabs.childNodes.length; i++)
        {
            tabids.Add(tabs.childNodes[i].id,tabs.childNodes[i].id);
        }
        
        forceTabRemoval = true;
        RemoveAllTabs();
        forceTabRemoval = false;
        
        try
        {
            for(c = 0; c < tabids.Count(); c++)
            {
                if(tabids.GetKey(c))
                {
                    if(tabids.GetKey(c) == oldEleID)
                    { 
                        AddTab(eleID);
                    }
                    else
                    {
                        AddTab(tabids.GetKey(c).toString());
                    }
                }
            }
        }
        catch(e)
        {
           ErrorLog.LogError(1,1,"SwapTab error = " + e.message);
        }
        
   }
   
   //pass in a slider id and get the next lower tab id returned. Empty string returned if the eleID is the last childNode
   function GetSiblingID(eleID)
   {
        var tabs = document.getElementById("SliderTabs");
        var tab = document.getElementById(eleID)
        var siblingID = "";
        try
        {        
            if(tabs.lastChild.id != eleID)
                siblingID = tab.nextSibling.id;
        }
        catch(e)
        {
           ErrorLog.LogError(1,1,"GetSiblingID error = " + e.message);
        }
            
        return siblingID;
        
   }

   function InsertTabAt(eleID,nextSibID)
   {
        var hdntabs = document.getElementById("HiddenSliderTabs");
        var tabs = document.getElementById("SliderTabs");
        var tab = document.getElementById(eleID)
        var sibTab = document.getElementById(nextSibID)
        
        try
        {
            //set visibility
            tab.style.display = "block";
            
            //remove tab from hidden
            hdntabs.removeChild(tab);
            
            //add tab to SliderTabs
            tabs.insertBefore(tab,sibTab);
        }
        catch(e)
        {
           ErrorLog.LogError(1,1,"InsertTabAt error = " + e.message);
        }
        
   }
   
   function ToggleShowEventsFunctionality(showCheckbox)
   {
   
        var chk = document.getElementById("showEvents");
        var lbl = document.getElementById("ShowEventsChkLbl");
        
        if(chk != null)
        {
            if(showCheckbox)
            {
                chk.style.visibility = "visible";
                lbl.style.visibility = "visible";
            }
            else
            {
                chk.style.visibility = "hidden";
                lbl.style.visibility = "hidden";
                
                //if we're hiding this, we should uncheck it, too
                chk.checked = false;
                lbl.innerHTML = "Show Events";
                tmpTypeOfSearch = "";
            }
        }
   }
   
   function IsTabVisible(eleID)
   {
        var tab = document.getElementById(eleID)
        if(tab.parentElement.id == "HiddenSliderTabs")
            return false;
        else
            return true;
  }
