

/*
----------------- Global object containing commonoly used funcitons -----------------*/

	var g = {
		
	
	// Prenvent default action
		stopDefault: function (e) {
			if (window.event) {
				window.event.returnValue = false;
			} 
			if (e && e.preventDefault) {
				e.preventDefault();
			}
		},
	
	
	// Cross browser add event
		addEvent: function (elm, evType, fn, useCapture) {
			if (elm.addEventListener) {
				elm.addEventListener(evType, fn, useCapture);
				return true;
			} else if (elm.attachEvent) {
				var r = elm.attachEvent('on' + evType, fn);
				return r;
			} else {
				elm['on' + evType] = fn;
			}
		}
	};	


/*
-----------------   Fire Functions as soon as DOM is ready -----------------------------------*/	
	
	// Assign $j to jQuery to avoid conflicts with other Frameworks.
	var $j = jQuery;
	$j(document).ready(function() {
			setLastTab();	
			navigationAnimate.getBoxValues();
	});
		
/*
-----------------  4. Set up Tabindex -------------------------------------------------------*/

	var setTabIndex = {
	
		/** Give top area a tabindex in increments of 1 so this is always the area tabbed through first **/
		topArea: function () {
			if (!$j("#stv-header")) {
				return false;
			}       
		   var topSectionLinks = $j('#stv-header a');
			var localTabindex = 0;
			for (var i = 0; i < topSectionLinks.length; i++) {
				topSectionLinks[i].setAttribute("tabIndex", localTabindex);
				localTabindex ++;
			}
		},
	
		/** Give primary naigation links a tabindex value in increments of 100 **/
		primNav: function () {
			if (!$j("#primary-nav")) {
				return false;
			}
			var topTabLinks = $j("#primary-nav a");
			var tabTabindex = 100;
			for (var i = 0; i < topTabLinks.length; i++) {
				topTabLinks[i].setAttribute("tabIndex", tabTabindex);
				tabTabindex = tabTabindex + 100;
			}
		},
		
		/* Give the category links a tabindex, starting with the associated link from the primary nav */
		boxTabIndex: function () {
			if (!$j(".togglenav")) {
				return false;
			}       			
			$j('.togglenav').each (function () {
				var category = this.id;
				var currentTabIndex = $j(this).attr('tabIndex');
				var startIndex = parseInt(currentTabIndex) + 1;
				$j('.' + category + ' a').each (function () {
					$j(this).attr('tabIndex', startIndex);
					startIndex++;
				});
			});
		}
	};

	
	function ieBgFlickerFix() {
		// Fix background image caching problem
		    if (jQuery.browser.msie) {
		        try { document.execCommand("BackgroundImageCache", false, true); 
				} catch(err) {}
		    }
	}

/*
------------------- Navigation -------------------------------------------*/	

	

	var navigationAnimate = {
	 
		currentNav: null,
		baseHeight: 0,
		origNav: null,
	
		/* Find link selected and match it with a category box.  Also get the height a open category box and pass values through
		to the nav_morph function */
		getBoxValues: function() {
				$j(".togglenav").click(function (e) {
					//e.preventDefault();
					//alert('hi');
					this.blur();
					var category = this.id;
					var categoryBox = $j('#secondary-nav div.' + category)[0];
					var categoryHeight = $j('.' + category).outerHeight();
					var getCurrentId = categoryBox.id;
					var startPos = getCurrentId.indexOf(getCurrentId);
					var restOfString = getCurrentId.substr(startPos + 14);			
					navigationAnimate.nav_morph(restOfString, categoryHeight);
					
					return false;
									//alert("respond")
					
					/*
					this.blur();
					var category = this.id;
					var categoryBox = $j('#secondary-nav div.' + category)[0];
					alert($j(categoryBox).outerHeight())
					var categoryBoxId = categoryBox.id;
					var categoryHeight = $j('.' + categoryBoxId).outerHeight();
					var getCurrentId = categoryBox[0].id;
					var startPos = getCurrentId.indexOf(getCurrentId);
					var restOfString = getCurrentId.substr(startPos + 14);			
					navigationAnimate.nav_morph(restOfString, categoryHeight);
					return false;
				*/
					
				});
		},	
		
		
		/* Animate the display of the category area by hiding a section and changing the height of the containing box  */
		nav_morph: function (obj,newH) {
			if(navigationAnimate.currentNav != null) {
				$j('#secondary-nav-'+navigationAnimate.currentNav).hide();
				$j('#primary-nav-'+navigationAnimate.currentNav).css({color: ""});
			}
			
			
			if(navigationAnimate.currentNav != obj) {
				if(navigationAnimate.origNav != null) {
					$j('#primary-nav-' + navigationAnimate.origNav).css({color: ""});
				}
				$j("#secondary-nav").animate({height: newH+navigationAnimate.baseHeight}, 500 );
				$j('#secondary-nav-' + obj).show();
				$j('#primary-nav-' + obj).css({color: "#eee"});
				navigationAnimate.currentNav = obj;
			} else {
				if(navigationAnimate.origNav != null) {
					$j('#primary-nav-' + navigationAnimate.origNav).css({color: "#eee"});
				}
				navigationAnimate.currentNav = null;
				$j("#secondary-nav").animate({  height: navigationAnimate.baseHeight}, 500 );
			}
		}
	
	};
	

/*
----------------- Tab size calculations -----------------------------------*/	

	
	function setLastTab() {
			
			if (!$j('#primary-nav') || !$j('#videosearch')) {
				return false;
			}
			
			var liwidth = 0;
			var totalWidth = $j("#primary-nav ul").outerWidth();
			$j("#primary-nav ul li").each( function() {
				liwidth = liwidth +  $j(this).outerWidth();
			});
			
			var spaceLeft = parseInt(totalWidth) - parseInt(liwidth);
			var addToInput = parseInt(spaceLeft) - 2; 
			var inputBox = $j("#videosearch")
			if (spaceLeft > 0) {
				var inputBoxWidth = $j(inputBox).outerWidth();
				var newWidth = addToInput + inputBoxWidth;
				$j("#videosearch").css({width: newWidth})
			}
			/*$j("#nav-search").css({paddingRight:"0px"})*/
			$j("#searchsubmit").css({right: "2px"})
			
	};
		

				  function runSearch()
                  {
				      fixSearch();
					  //document.forms["Search"].submit();
					  var domain = "http://video.stv.tv";//window.location.host;
					  //domain = domain.substring(7); //get the part after http://
					  //domain = domain.substring(0, domain.indexOf('/'));
					  window.location = domain + "/bc/search/" +
					                    document.forms["search"]["videosearch"].value;
                  }
				  function fixSearch()
				  {
				      var inputString=document.forms["search"]["videosearch"].value;  
                      var encodedInputString=escape(inputString);
                      encodedInputString=encodedInputString.replace("+", "%2B");
                      encodedInputString=encodedInputString.replace("/", "%2F"); 
                      document.forms["search"]["videosearch"].value=encodedInputString;
			      }

  
/*
----------------- 11. Load events on page load -----------------------------*/




g.addEvent(window, 'load', setTabIndex.topArea, false);
g.addEvent(window, 'load', setTabIndex.primNav, false);
g.addEvent(window, 'load', setTabIndex.boxTabIndex, false);
g.addEvent(window, 'load', ieBgFlickerFix, false);




	
