// ================================================================================================
var tabs = {
  
  // =========================================================================
  init: function()
  {
    // init vars
    var boxes = $('.tabs ul li > a');
    var boxes_length = boxes.length;
    
    // loop through the tabs
    for (var i = 0; i < boxes_length; i ++)
    {
      $(boxes[i]).click(function() {tabs.change(this); return false;});
    }
  },
  
  // =========================================================================
  change: function(element)
  {
    // remove the class
    $('.tabs ul li.proper_tab').addClass('off');
    $(element).parent().removeClass('off');
    
    // loop through the tabs and hide them all
    $('.latest_news_home div.dynamic_tab').hide();
    
    // show the element
    var pos = element.href.match(/#(\w+)$/);
    $('#' + pos[1]).show();
  }
};

// ================================================================================================
function set_main_menu() {
  // set up those buttons that need dropdowns to have them
  $(" #nav div:first").css({display: "none"});
  $(" #nav li").hover(function() {
  		$(this).find('div:first').css({visibility: "visible",display: "none"}).show(400);
  	},
    function() {
  		$(this).find('div:first').css({visibility: "hidden"});
    });
  
  // set up the menu buttons to not respond
  $('#menu_products, #menu_about').click(function() {return false;});
}

// ================================================================================================
function set_dropdown() {
  // hide the basic dropdown and replace with the js version
  $('.country span').hide();
  $('.country_wrapper, .country_wrapper_bottom').addClass('js_on');
  $('.country_wrapper_bottom').removeClass('country_wrapper_bottom');
  
  // init the actual dropdowns
  MSDropDown.init('#country, #country_bottom', 171, 50);
  MSDropDown.showIconWithTitle(false);
  
  // store the serach text for later recall
  $('#search_text').attr('storage', $('#search_text').attr('value'));
  
  // remove the default text if showing
  $('#search_text').focus(function() {
    if ($(this).attr('value') == $(this).attr('storage'))
    {
      $(this).attr('value', '');
    }
  });
  
  // show the default text if none showing
  $('#search_text').blur(function() {
    if ($(this).attr('value') == '')
    {
      $(this).attr('value', $(this).attr('storage'));
    }
  });
  
  // set up the dropdowns to submit on change
  $('#country, #country_bottom').change(function() {
    var val = $(this).children('option:selected').attr('value');
    if (val)
    {
      // check for an http as the option
      if (val.match(/^http\:\/\//))
      {
        document.location.href = val;
      }
      else
      {
        $(this).closest('form').submit();
      }
    }
  });
  
  // set up the dropdown for the country on contact page
  MSDropDown.init('#contact_country', 171, 50);
}

// ================================================================================================
$(document).ready(function() {
  // simple tabs switcher
  tabs.init();
    
  // set up adverts changing
  $('.ctas_home .left_col').cycle({speed: 500, pause:1});
  $('.ctas_home .right_col').cycle({speed: 500, pause:1});
   
  // set up the menu
	set_main_menu();
  
  // set up the dropdown
  set_dropdown();
  
  // open links to another domain in a new window
	$("a[href^=http]").each(
		function(){
			if(this.href.indexOf('http://'+location.hostname) == -1) {
				$(this).attr('target', '_blank');
			}
		}
	)	  
});