	var tabLinks = new Array();
	 var contentDivs = new Array();
	 
	 String.prototype.capitalize = function(){
	   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
	 };
	
     function bing_init() {

      // Grab the tab links and content divs from the page
      var tabListItems = document.getElementById('bing_tabs').childNodes;
      for ( var i = 0; i < tabListItems.length; i++ ) {
        if ( tabListItems[i].nodeName == "LI" ) {
          var tabLink = getFirstChildWithTagName( tabListItems[i], 'A' );
          
          var id = getHash( tabLink.getAttribute('href') );
          tabLinks[id] = tabLink;
          contentDivs[id] = document.getElementById( id );
        }
      }

	  
      // Assign onclick events to the tab links, and
      // highlight the first tab
      var i = 0;

      for ( var id in tabLinks ) {
        tabLinks[id].onclick = showTab;
        tabLinks[id].onfocus = function() { this.blur() };
        if ( i == 0 ) tabLinks[id].className = 'selected';
        i++;
      }

	  document.getElementById('bing').style.display = "block";
    }
    
   
    function showTab() {
      
      var selectedId = getHash( this.getAttribute('href') );
	  // Highlight the selected tab, and dim all others.
      // Also show the selected content div, and hide all others.
      for ( var id in contentDivs ) {
        if ( id == selectedId ) {
          tabLinks[id].className = 'selected';
          jQuery('#'+id).removeClass('hide');
        } else {
          tabLinks[id].className = '';
          jQuery('#'+id).addClass('hide');
        }
      }
      
      if ( jQuery("#"+selectedId+"_empty").length == 1 ) {
	  	document.getElementById("the_link").style.display = "none";
	  } else {
	  	var bing_link_tab = document.getElementById('the_link');
	  	var q = document.getElementById('q').value;
	  	var rand = document.getElementById('rand').value;
	  	var media_type; 
	  	if(selectedId=="images"){ media_type = "Image"; }
	  	else { media_type = selectedId.capitalize(); }
	  	
		bing_link_tab.innerHTML = "More "+media_type+" Results &raquo;";
		if (selectedId=="news") { 
			bing_link_tab.href = "http://clk.atdmt.com/MRT/go/249924077/direct;wi.1;hi.1/01/"+rand+"/search?q="+q;
		} else if (selectedId=="images") { 
			bing_link_tab.href = "http://clk.atdmt.com/MRT/go/249924076/direct;wi.1;hi.1/01/"+rand+"/search?q="+q;
		} else {
			bing_link_tab.href = "http://clk.atdmt.com/MRT/go/249924078/direct;wi.1;hi.1/01/"+rand+"/search?q="+q;
		}
		document.getElementById("the_link").style.display = "block";
	  }
      // Stop the browser following the link
      return false;
    }

	
	function getHash( url ) {
      var hashPos = url.lastIndexOf ( '#' );
      return url.substring( hashPos + 1 );
    }

	function getFirstChildWithTagName( element, tagName ) {
      for ( var i = 0; i < element.childNodes.length; i++ ) {
        if ( element.childNodes[i].nodeName == tagName ) return element.childNodes[i];
      }
    }


