// Begin Selector Drop Down Script
(function(jQuery){
    jQuery.fn.extend({

    //pass the options variable to the function
    dropDownPanels: function(options) {

		var defaults = {
			speed: 250,
			resetTimer: 1000
		};

		// Extend our default options with those provided.
		var opts = jQuery.extend(defaults, options);
		//Assign current element to variable, in this case is UL element
 		var jQuerythis = jQuery(this);

 		var closetimer;
 		
 		function resetMenu(){
 			jQuerythis.find(".hover").removeClass("hover");
 			jQuerythis.find(".submenu:visible").slideUp(opts.speed);
 		}

 		  function activateTimer(){  
 		      closetimer = window.setTimeout(resetMenu, opts.resetTimer);
 		  }

 		  function cancelTimer(){  
 			    if(closetimer){  
 			 	    window.clearTimeout(closetimer);
 					closetimer = null;
 				}
 			}
 			
 		 jQuerythis.find(">li").hoverIntent(function() {
 			cancelTimer();
 			if(!jQuery(this).find(".submenu").is(":visible")){
 				jQuery(this).parent().find(".hover").removeClass("hover");
 				jQuery(this).parent().find(".submenu:visible").hide();
 				if(jQuery(this).has(".submenu")){
 					jQuery(this).find(".submenu").slideDown(opts.speed); // Added delay in display of submenu
 					jQuery(this).find("a:first").addClass("hover");
 				}
 			}
 		}, function(){
 			activateTimer();
		});
    }
});
})(jQuery);
// End Selector Drop Down Script

jQuery(function(jQuery) {
jQuery(document).ready(function() {				   

  // If cookies are not set by IP, then set both as US
  // if trxLang is null
  if (jQuery.cookie('trxLang')==null){
    intlSetCookie("trxLang","US");
  }
  // if trxShip is null
  if (jQuery.cookie('trxShip')==null){
    intlSetCookie("trxShip","US");
  }

  if ((jQuery.cookie('trxLang')!=null) && (jQuery.cookie('trxShip')!=null)){
    // set images for ship & lang
    intlSetImgLang(jQuery.cookie('trxLang'));// change lang img based on cookie
    intlSetImgShip(jQuery.cookie('trxShip'));// change ship img based on cookie
    // disable language panel when trxLang=GB & trxShip=GB
    if((jQuery.cookie('trxLang')=="GB") && (jQuery.cookie('trxShip')=="GB")){
        jQuery('.selectors #lang-container').css('visibility','hidden');
        jQuery('#toggle-lang a.language').removeClass('language');
    }
  }

  function intlRedirect(intlRedir){
    document.location.href="http://" + intlRedir + ".trxtraining.com" + thisServerRequestURI;
  }

  // Intl. Select Shipping Country
  jQuery(".shipcountry").click(function(e){
    e.preventDefault();
    intlSelShip(jQuery(this).attr("id"));
  });
  
   // Intl. Select Language
  jQuery(".langcountry").click(function(e){
    e.preventDefault();
    intlSelLang(jQuery(this).attr("id"));
  });

    // Intl. Set Cookie function
  function intlSetCookie(intlCookieName,intlCookieVal){
    jQuery.cookie(intlCookieName, intlCookieVal, {path: '/', expires: 365, domain: '.trxtraining.com'});
  }

  // Intl. Select Language function
  function intlSelLang(langid) {
    intlSetImgLang(langid);
    if(langid=="lang-uk"){
      intlSetCookie("trxLang","GB");
      intlRedirect("uk");
    }
    if(langid=="lang-us"){
      intlSetCookie("trxLang","US");
      // if trxShip == GB, change it to US
      if((jQuery.cookie('trxShip')) =="GB"){
        intlSetCookie("trxShip","US");
      }
      intlRedirect("www");
    }
	if(langid=="lang-zs"){
      intlSetCookie("trxLang","ZS");
      intlRedirect("zs");
    }
	if(langid=="lang-fr"){
      intlSetCookie("trxLang","FR");
      intlRedirect("fr");
    }
	if(langid=="lang-de"){
      intlSetCookie("trxLang","DE");
      intlRedirect("de");
    }
	if(langid=="lang-it"){
      intlSetCookie("trxLang","IT");
      intlRedirect("it");
    }
	if(langid=="lang-ja"){
      intlSetCookie("trxLang","JA");
      intlRedirect("ja");
    }
	if(langid=="lang-ko"){
      intlSetCookie("trxLang","KO");
      intlRedirect("ko");
    }
	if(langid=="lang-pt"){
      intlSetCookie("trxLang","PT");
      intlRedirect("pt");
    }
	if(langid=="lang-es"){
      intlSetCookie("trxLang","ES");
      intlRedirect("es");
    }
  }

  // TODO: certain cases aren't making the shopping cart change correctly
  function intlSelShip(shipid) {
    if(shipid=="ship-usa"){
      intlSetImgShip("US"); // set US defaults
      intlSetCookie("trxShip","US");
      intlSetCookie("trxLang","US");
      intlRedirect("www");
    }
    if(shipid=="ship-can"){
      intlSetImgShip("CA"); // set CA defaults
      intlSetCookie("trxShip","CA");
      intlSetCookie("trxLang","US");
      intlRedirect("www");
    }
    if(shipid=="ship-gb"){
      intlSetImgShip("GB"); // set GB defaults
      intlSetCookie("trxShip","GB");
      intlSetCookie("trxLang","GB");
      intlRedirect("uk");
    }
	if(shipid=="ship-aus"){
      intlSetImgShip("AT"); // set AT defaults
      intlSetCookie("trxShip","AT");
      intlSetCookie("trxLang","AT");
      intlRedirect("www");
    }
	if(shipid=="ship-fra"){
      intlSetImgShip("FR"); // set FR defaults
      intlSetCookie("trxShip","FR");
      intlSetCookie("trxLang","FR");
      intlRedirect("www");
    }
	if(shipid=="ship-ger"){
      intlSetImgShip("DE"); // set DE defaults
      intlSetCookie("trxShip","DE");
      intlSetCookie("trxLang","DE");
      intlRedirect("www");
    }
	if(shipid=="ship-nor"){
      intlSetImgShip("NO"); // set NO defaults
      intlSetCookie("trxShip","NO");
      intlSetCookie("trxLang","NO");
      intlRedirect("www");
    }
	if(shipid=="ship-swe"){
      intlSetImgShip("SE"); // set SE defaults
      intlSetCookie("trxShip","SE");
      intlSetCookie("trxLang","SE");
      intlRedirect("www");
    }
	if(shipid=="ship-br"){
      intlSetImgShip("BR"); // set SE defaults
      document.location.href="http://www.trxtraining.com.br"; // Redirect for Brazil
    }
  }
  
  function intlSetImgLang(lang){
      
    if(lang=="GB" || lang=="lang-uk"){
      jQuery("#toggle-lang a.language").css("cursor","default");
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -1px");
      jQuery("#lang-uk").css("background-position","-255px -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
    if(lang=="US" || lang=="lang-us"){
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -14px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","-255px 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="ZS" || lang=="lang-zs"){
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -27px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","-255px -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="FR" || lang=="lang-fr"){
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -40px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","-255px -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="DE" || lang=="lang-de"){
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -53px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","-255px -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="IT" || lang=="lang-it"){
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -66px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","-255px -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="JA" || lang=="lang-ja"){
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -79px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","-255px -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="KO" || lang=="lang-ko"){
          jQuery("#toggle-lang").children("#selected").css("background-position","-230px -92px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","-255px -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="PT" || lang=="lang-pt"){
        jQuery("#toggle-lang").children("#selected").css("background-position","-230px -105px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","-255px -585px");
	  jQuery("#lang-es").css("background-position","0 -630px")
    }
	if(lang=="ES" || lang=="lang-es"){
      jQuery("#toggle-lang").children("#selected").css("background-position","-230px -118px");
      jQuery("#lang-uk").css("background-position","0 -45px");
      jQuery("#lang-us").css("background-position","0 0");
	  jQuery("#lang-zs").css("background-position","0 -315px");
	  jQuery("#lang-fr").css("background-position","0 -360px");
	  jQuery("#lang-de").css("background-position","0 -405px");
	  jQuery("#lang-it").css("background-position","0 -450px");
	  jQuery("#lang-ja").css("background-position","0 -495px");
	  jQuery("#lang-ko").css("background-position","0 -540px");
	  jQuery("#lang-pt").css("background-position","0 -585px");
	  jQuery("#lang-es").css("background-position","-255px -630px")
    }
  }

  function intlSetImgShip(ship){
    if(ship=="US"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -19px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","-255px -90px");
      jQuery("#ship-can").css("background-position","0 -135px");	  
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
    if(ship=="CA"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -37px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","-255px -135px");  
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
    if(ship=="GB"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -1px");
      jQuery("#ship-gb").css("background-position","-255px -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","0 -135px");
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
	if(ship=="AT"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -91px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","0 -135px");
	  jQuery("#ship-aus").css("background-position","-255px -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
	if(ship=="FR"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -109px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","0 -135px");
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","-255px -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
	if(ship=="DE"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -127px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","0 -135px");
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","-255px -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
	if(ship=="NO"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -145px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","0 -135px");
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","-255px -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
	if(ship=="SE"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -163px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","0 -135px");
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","-255px -855px");
	  jQuery("#ship-br").css("background-position","0 -900px");
    }
	if(ship=="BR"){
      jQuery("#toggle-ship").children("#selected").css("background-position","-171px -181px");
      jQuery("#ship-gb").css("background-position","0 -225px");
      jQuery("#ship-usa").css("background-position","0 -90px");
      jQuery("#ship-can").css("background-position","0 -135px");
	  jQuery("#ship-aus").css("background-position","0 -675px");
	  jQuery("#ship-fra").css("background-position","0 -720px");
	  jQuery("#ship-ger").css("background-position","0 -765px");
	  jQuery("#ship-nor").css("background-position","0 -810px");
	  jQuery("#ship-swe").css("background-position","0 -855px");
	  jQuery("#ship-br").css("background-position","-255px -900px");
    }
/*    if(ship=="ship-ire"){
        jQuery("#toggle-ship #selected").css("background-position","-171px -73px");
		jQuery("#ship-ire").css("background-position","-255px -270px");
      }
      if(ship=="ship-mex"){
		jQuery("#toggle-ship #selected").css("background-position","-171px -55px");
		jQuery("#ship-mex").css("background-position","-255px -180px");
      }
*/
  }

  // change shopping link based on cookie
  if (jQuery.cookie('trxShip')!=null){
    if((jQuery.cookie('trxShip')=="GB")){
      jQuery("#shopping-cart").attr("href", function() {
        return "http://uk.shop.trxtraining.com/content/shopping/7726.0.4.3";
      });
    }
  }

});
})
