$(function(){
  
  // Blank links do nothing [to remove]
  $("a[href='#']").click(function(){return false;});
  
  // Map dots :hover tooltip
  $(".map-dot").tooltip({effect: 'slide'});
  
  // Sidebar navigation active style extra classes
  
  if($("li.active ul.level-2 li.active").length){
    $(".level-2").parents("li.active").addClass("no-chevron");
    // $(".level-2").parents("li.active:not(:first-child)").removeClass("level-sep");
  } else {
    $(".level-2").parents("li.active").removeClass("no-chevron");
    $(".level-2 li").css('margin-left','20px');
  }
  

  
  // Offices/Investments map
  if($("#the-map").length){
    
    function showPopup(targetPopup,coords){
      $(".map-popup").fadeOut("fast");
      $(targetPopup).css({"top":coords[0]+"px","left":coords[1]+"px"}).fadeIn("fast");
      return false;
    }
    
  
    
    
    
    // Clicking a map dot opens the city popup
    $(".map-dot").hover(
      function(){
        var coords = $(this).attr('rel').split('-');
        var targetPopup = $(this).attr("href");
        showPopup(targetPopup,coords);
        $('.tooltip').slide("fast");
        return false;
      },
      function(){
         $(".map-popup").fadeOut("fast");
         $('.tooltip').slide("fast");
         return false;
      }
    )
    
    // Close the popup
    $("a.close").click(function(){
      $(this).parents(".map-popup").fadeOut("fast");
      return false;
    })
    
  }
  
  // IE6/7/8 specifics
  if($.browser.msie){
    $("#breadcrumb li:last-child").css("background","none");
    $(".box.with-separators li:last-child").css("border-bottom","none");
  }
  
  // IE6 specifics
  if($.browser.msie && $.browser.version.substr(0,1)<7){
    $(".sidebar li.active > a").css("color","#77216f");
  }
  
  
  // Facebox
	if($('a[rel*=facebox]').length >0){  
  	$('a[rel*=facebox]').facebox({
  		loading_image : '/media/js/facebox/loading.gif',
  		close_image   : '/media/js/facebox/closelabel.gif'
  	});                
  }
  
  // Facebox Close
  
  if($(".action a.close").length >0){ 
      
      $(".action a.close").live('click', function(e) {
        $("#facebox").trigger('close.facebox');
        return false;
      })

  }
  
  
  if($(".action a.btn_ok").length >0){ 
      
      $(".action a.btn_ok").live('click', function(e) {
        $("#facebox").trigger('close.facebox');
        //return false;
      })

  }
  
  
  
  //Chevron H3
  $(".accordion_chevron h3").hover(
    function () {
      $(this).find('img').attr('src','/media/img/chevron_hover.png');
    }, 
    function () {
      $(this).find('img').attr('src','/media/img/chevron_normal.png');
    }
  );
  
  

  $("#accordion").accordion({ active:0 });
  $("#accordion_core").accordion({ active:99});
  
  
  function faqsAccordion() {    
      var locationHash = window.location.hash;
      var newLocation = (locationHash.replace('#',''))-1;
      $("#accordion").accordion({ active:newLocation });
  }
  
  faqsAccordion();
  
  $('.expandable .more').hide();
  $('.expandable .more-link').click(function(){
    $(this).hide().parents('.expandable').find('.more').show();
  });
  
  // Random image and link of video-container on what we look for page
  function get_random()
  {
      var ranNum= Math.floor(Math.random()*4);
      return ranNum;
  }
  
  if ($('#careers-videos').length > 0){
    var whichVideo = get_random();
    var video = new Array();
    video[0] = {'video_url':'/media/img/careers_video_1.jpg','video_alt':'Collab_V film','video_link':'#video_collaboration_player'};
    video[1] = {'video_url':'/media/img/careers_video_2.jpg','video_alt':'Global_rep film','video_link':'#video_global_player'};
    video[2] = {'video_url':'/media/img/careers_video_3.jpg','video_alt':'People_rep film','video_link':'#video_people_player'};
    video[3] = {'video_url':'/media/img/careers_video_4.jpg','video_alt':'Train_V film','video_link':'#video_trainning_player'};
    $('#careers-videos').empty();
    $('#careers-videos').append("<a href='"+video[whichVideo]['video_link']+"' rel='facebox'><img src='"+video[whichVideo]['video_url']+"' alt='"+video[whichVideo]['video_alt']+"'/></a>");
    
    $('#careers-videos a').facebox({
  		loading_image : '/media/js/facebox/loading.gif',
  		close_image   : '/media/js/facebox/closelabel.gif'
  	});
   
  }
  
});




SubmittingForm=function() {
  var name = $("#contact_form #sender_name").val();
  var email = $("#contact_form #sender_address").val();
  var contact = $("#contact_form #sender_contact").val();
  var subject = $("#contact_form #sender_subject").val();
  var comment = $("#contact_form #sender_comment").val();

  $.post("/contact/", { "name":name, "email": email, "contact":contact, "subject":subject,"comment":comment},
     function(data){
       $('#email_result').empty();
       $('#email_result').append('<p>Your email has been sent</p>');
       $('#contact_form #sender_name').val('');
       $('#contact_form #sender_address').val('');
       $('#contact_form #sender_contact').val('');
	   $('#contact_form #sender_subject').val('');
       $('#contact_form #sender_comment').val('');
       return false;
     });
  
}

$(document).ready(function() {
	$("#contact_form").validate({
		submitHandler:function(form) {
			SubmittingForm();
		},
		rules: {
			name: "required",		// simple rule, converted to {required:true}
			email: {				// compound rule
				required: true,
				email: true
			},
			number: {
				required: false
			},
			subject: {
				required: false
			},
			comment: {
				required: true
			}
			
		},
		messages: {
			comment: "Please enter a comment."
		}
	});
  
});

jQuery.validator.addMethod(
	"selectNone",
	function(value, element) {
		if (element.value == "none")
		{
			return false;
		}
		else return true;
	},
	"Please select an option."
);



