//Modified by A. Riverin, on 2008-05-28
var TabsManager = {
  current: 'web',
  tabMap: {"lcn" : {"tabid" : "lcn_tab", "ctnid" : "lcn_container", "show_sponsor" : true},
          "quebec" : {"tabid" : "quebec_tab", "ctnid" : "quebec_container", "show_sponsor" : true},
          "montreal" : {"tabid" : "montreal_tab", "ctnid" : "montreal_container", "show_sponsor" : true},          
          "site" : {"tabid" : "site_tab", "ctnid" : "site_container", "show_sponsor" : true},
          "web" : {"tabid" : "web_tab", "ctnid" : "web_container", "show_sponsor" : true},
          "news" : {"tabid" : "news_tab", "ctnid" : "news_container", "show_sponsor" : true},
          "img" : {"tabid" : "images_tab", "ctnid" : "img_container", "show_sponsor" : false},
          "ent" : {"tabid" : "enterprises_tab", "ctnid" : "ent_container", "show_sponsor" : false},
          "shopping" : {"tabid" : "shopping_tab", "ctnid" : "shopping_container", "show_sponsor" : false}
          },
  lang : 'fr',
  changed : false,
	
	setCurrentTab : function(atab)
	{
	  if ( !this.changed && this.tabMap[atab] )
	    this.current = atab;
	},
	
  changeTab : function(atab)
	{
          if ( !this.tabMap[atab] ) return;
	 
	  if ( document.getElementById('searchbar_en') )
	    this.lang = 'en';
	 
	  var tabobj = this.tabMap[atab];
	  var curtabobj = this.tabMap[this.current];
	 
	 
	  var tab = document.getElementById(tabobj.tabid);
	  var tab_ctn = document.getElementById(tabobj.ctnid);
	  var current_tab = document.getElementById(curtabobj.tabid);
	  var current_tab_ctn = document.getElementById(curtabobj.ctnid);
	 
	  if ( current_tab_ctn && current_tab_ctn.style.display == 'none' )
	    return;
	 
                current_tab_ctn.style.display = 'none';
                current_tab.className = '';
		tab_ctn.style.display = '';
		tab.className = 'current';
		
		var current_query = this.findCtnQueryField(current_tab_ctn);
		var query = this.findCtnQueryField(tab_ctn);
		
		//set query courant et focus
		if ( query && current_query ) {
		  var val = current_query.value;
		  /*
                  if ( val == '' && atab == 'ent' ) {
			  val = this.lang == 'fr'? 'Nom' : 'Name';
		  }
		  else if ( this.current == 'ent' && (val == 'Nom' || val == 'Name' ) )
			  val = '';
                  */
                  
                  if ( this.current == 'ent' && (val == 'Nom, prénom' || val == 'Nom, prÃ©nom' || val == 'Last, First' || val == 'Entreprise' || val == 'Business') )
                    val = '';
                  
                  if ( atab == 'ent'){
                    if( val != ''){
                      query.value = val;
                      query.style.color = "black";
                      var city = this.findCtnLocationField(tab_ctn);
                      city.focus();
                    }
                    else
                      query.focus();
                    
                  }
                  else{
                    query.value = val;
                    query.focus();
                  }
		}
	 
	  // google sponsor
	  var goo_el = document.getElementById('yahoo_sponsor');
	  if ( goo_el ) {
 	    if ( tabobj.show_sponsor )
	      goo_el.style.display = '';
	    else
	      goo_el.style.display = 'none';
	  }
		
	  this.current = atab;
    this.changed = true;
	},
	
	findCtnQueryField: function(container)
	{
	  var inputs = container.getElementsByTagName('input');
	  for (var i=0; i < inputs.length; i++)
		  if (inputs[i].type == 'text' && (inputs[i].name == 'q' || inputs[i].name == 'what' || inputs[i].name == 'term' || inputs[i].name == 'name'))
		 	  return inputs[i];
			
		return null;
	},
        
        findCtnLocationField: function(container)
	{
	  var inputs = container.getElementsByTagName('input');
	  for (var i=0; i < inputs.length; i++)
		  if (inputs[i].type == 'text' && (inputs[i].name == 'location'))
		 	  return inputs[i];
			
		return null;
	}
}
