/*global js*/

//keep section highlighted when subnav is open
function topNavUI(){
//exclude ie6, we have special nav functions for it already
if($.browser.msie && $.browser.version < 7.0){
  return;
}

else{
$('#primary li ul, #secondary li ul').live('mouseover',function(){
  $(this).parent('li').addClass("active2");
});


$('#primary li ul, #secondary li ul').live('mouseout',function(){
  $(this).parent('li').removeClass("active2");
});


$('.mega').live('mouseover',function(){
  $(this).parent('li').addClass("active3");
});


$('.mega').live('mouseout',function(){
  $(this).parent('li').removeClass("active3");
});

}}

topNavUI();

//use this for toggle list elements
$('.toggleElement').live('click', function()
{
    $(this).parent('li').toggleClass('on');  
    $(this).siblings('ul').toggle();

});


/*this is for button animations */
$('.btn, a.btn').live('mousedown', function(){
  $(this).css({ color: '#2089BE'});
});

$('.btn, a.btn').live('mouseup', function(){
  $(this).css({ color: '#003C67'});

});


$('.toggler, a.toggler').live('click', function(){
        $('.hidden.toggleTarget').toggle();
});

//used for smaller tabbed panels on various pages

function remove_active_class()
{
    $('#tabbed-panel ul li, #tabbed-panel .navigation a').removeClass('active');  
};

function loadTabbedPanels(){
    $('#tabbed-panel div.tab').hide(); 
    $('#tabbed-panel div.tab:first').show();
    $('#tabbed-panel .navigation a:first, #tabbed-panel ul li:first').addClass('active');
    
    $('#tabbed-panel ul li a').click(function(){
        remove_active_class();
        var currentTab = $(this).attr('href'); 
        $(this).parent().addClass('active');
        $('#tabbed-panel div.tab').hide();
        $('#tabbed-panel .navigation a[href='+currentTab+']').addClass('active'); 
        $(currentTab).show(); 
        return false;
    });
    
    $('#tabbed-panel .navigation a').click(function(){  
        remove_active_class();
        var currentTab = $(this).attr('href'); 
        $(this).addClass('active');
        $('#tabbed-panel ul li a[href='+currentTab+']').parent().addClass('active');
        $('#tabbed-panel div.tab').hide(); 
        $(currentTab).show();            
        return false;           
    });
}

function setDefaultFocus(field){
  $(field).focus();
}

//a temp fix for the fact that country selectors are selecting the separator and not first value
function setDefaultCountryField(){
  var defaultSelected = $(".tabular.country").find("option:first");
  $(defaultSelected).attr("selected", "selected");
}
/* global UI related functions for highlighting active links in sections */
function loadSection(section){
    switch(section){
      
      case "industries":
        $('#tLink_industries').parent('li').addClass('active');
        break;

      case "products":
        $('#tLink_products').parent('li').addClass('active');
        break;

      case "resources":
        $('#tLink_resources').parent('li').addClass('active');
        break;

      case "support":
        $('#tLink_support').parent('li').addClass('active');
        break;

      case "company":
        $('#tLink_company').parent('li').addClass('active');
        break;
        
      case "partners":
        $('#tLink_partners').parent('li').addClass('active');
        break;

      case "news":
        $('#tLink_news').parent('li').addClass('active');
        break;

      default:
        break;
    }
};

function showSubnav(section){
  section = '#' + section;
 $(section).find('ul').toggle();
};



function validateSelectFields(field){
  
    if( ($(field).val() == 0) || ($(field).val == "- Select One -") ){
        
     if( ($(field).hasClass("valid")) || !($(field).hasClass("error")) ){
        $(field).removeClass("valid");
        $(field).addClass("error");
        if(!($(field).parent("div.tabular").find(".errorWrapper").is(":visible"))){
          $(field).after("<div class=\'errorWrapper\'><span class=\'error\'>This field is required.</span></div>");
        }
  
        }
      }
      
     else {
      $(field).removeClass("error");
      $(field).addClass("valid");
      $(field).parent("div.tabular").find(".errorWrapper").hide();
      }
}


function loadContactMap(){ 
    var activeMap = "sf";
     
   function removeAllClasses(activeMap){
     $("#map").not(activeMap).removeClass("paris sf london singapore china tokyo");
   }

     
   function highlightMap(activeMap){
        removeAllClasses(activeMap);
        $("#map").addClass(activeMap);
        $("#"+ activeMap).addClass("light-box");
   }
   
   function loadMap(){
   var location = document.location.toString();
		if(-1 !== location.indexOf("location=Beijing")){
		activeMap = "china";
       highlightMap(activeMap);
       }
   
   	else if(-1 !== location.indexOf("location=Singapore")){
		activeMap = "singapore";
       highlightMap(activeMap);
       }
   
   
       else if(-1 !== location.indexOf("location=London")){
		activeMap = "london";
       highlightMap(activeMap);
       }
   
       else if(-1 !== location.indexOf("location=Tokyo")){
		activeMap = "tokyo";
       highlightMap(activeMap);
       }
   
       else if(-1 !== location.indexOf("location=Paris")){
		activeMap = "paris";
       highlightMap(activeMap);
       }
   
   
       else{
           activeMap = "sf";
           highlightMap(activeMap);
       }
             
 };
 
 loadMap();
 
 $(".mapTarget").mouseover(function(){
     activeTarget = ($(this).attr("id")).split("-"); 
       var activeMap = activeTarget[1];
       $(".empty-box").not("#" + activeMap).removeClass("light-box");
       highlightMap(activeMap);
 });

   $(".empty-box").mouseover(function(){ 
       $(".empty-box").not(this).removeClass("light-box");
       activeMap = ($(this).attr("id"));
       highlightMap(activeMap);
      
   });

};


