// RWilliams - 05/27/2009
// All UI related functions should now be placed in here and
// I'll slowly move everything else into this file.
//----------------------------------------------------------
var FootSmartUI = Class.create({
    // Initialization class if you need to init anything, 
    // think of it as a constructor
    initialize: function(){},    
    
    // Sets width of Search box based on the banner width    
    setSearchTextBoxWidth: function(){
       if ( $('banner') ) {       
           var bannerWidth = $('banner').getWidth();
           if (bannerWidth != 512)
           {
                $$('#searchArea .tbNavSearch')[0].setStyle({width: '132px'});
           }
        }
    },
    
    // Generic Function to toggle the defualt value for a text box
    toggleTextField: function(e){
        var defaultText = this.readAttribute('toggletext');
	    if (defaultText == null){defaultText = this.defaultValue;}	    
	    if (e.type == "blur" && this.value == ''){this.value = defaultText;}
        if (e.type == "focus" && this.value == defaultText){this.value = '';}
	    if (e.type == "keypress" && this.value == defaultText){this.value = '';}
	    if (e.type == "click" && this.value == defaultText){this.value = '';}
    },
    
    // Sets up the search textbox area and starts observing some events 
    setSearchTextBoxEvents: function() {       
	   if($$('#searchArea input.tbNavSearch')[0]) 
	   {
			$$('#searchArea input.tbNavSearch')[0].focus();
       }
       if (Prototype.Browser.IE)
       {
            $$('#searchArea .tbNavSearch')[0].observe('focusin', this.toggleTextField);
       }
       else
       {
            $$('#searchArea .tbNavSearch')[0].observe('focus', this.toggleTextField);
       }       
       $$('#searchArea .tbNavSearch')[0].observe('blur', this.toggleTextField);
       $$('#searchArea .tbNavSearch')[0].observe('keypress', this.toggleTextField);
       $$('#searchArea .tbNavSearch')[0].observe('click', this.toggleTextField);          
    },
    
    // Ensures that all submenus are visible by using the pmenuid query string param
    ensureSubmenuVisibility: function(){
        var menuId = GetCookie("pmnuid");
        if(menuId != null && menuId != 'undefined' && (typeof SwitchMenu == 'function')){
            SwitchMenu(menuId);
        }
    },
    
    // add target property to every anchor tag on page
    //  "_blank" "_parent" "_self" "_top" 
    insertParentTarget: function () {
        $$('a').each(function(link) { link.writeAttribute("target", "_parent"); } );
        if ($('searchLinkButton'))
        {
             $('searchLinkButton').writeAttribute("target", "");
        }
    }
});


// All initial load events should be loaded here
// dom:loaded - loads after HTML doc but defore images are fully loaded
//----------------------------------------------------------
document.observe("dom:loaded", function(){
    var fsuiw = new FootSmartUI();
    fsuiw.ensureSubmenuVisibility();

	// from dotomiTag.ascx
	if (typeof loadDotomi == 'function')
	{
        loadDotomi();
    }
    
    //need to block Gomez Users, via HTTP_USER_AGENT identification, not done by CoreMetrics
    //We also do not want to load a page view tag for the blog page since page.aspx is used by the 
    //blog in an iframe.
	var GomezUser = GetCookie("GomezUser");
    if(GomezUser==null && document.URL.toLowerCase().toQueryParams().src != "blog")
    {
        if (typeof(coremetrics) != 'undefined') 
        {
            coremetrics.PageViewTag();
            coremetrics.DisplayCoremetricsFormErrorTag();
        }
    }
    else
    {
        //Only execute for a custom blank page that is meant to be used in an iframe)
        fsuiw.insertParentTarget();
    }
    
    //Only execute for a custom blank page that is meant to be used in an iframe)
//    if(document.URL.toLowerCase().toQueryParams().src == "blog")
//    {
//        
//    }
    
    
});

// window.load - fires after HTML doc and images are loaded
//----------------------------------------------------------
Event.observe(window, 'load', function(){
	var fsuiw = new FootSmartUI();	
	if ($('searchBar')) 
    {
		fsuiw.setSearchTextBoxWidth();
		fsuiw.setSearchTextBoxEvents();
    }
    
});