//on page load call
$( function(){
	$("span.noJS").parent().click(function(){gotoLink(this);});
	$("span.noJS").hide();
	_uacct = "UA-532276-1";
	urchinTracker();
});

$(document).ready(linkAction);

function $1() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1) 
      return element;
      
    elements.push(element);
  }
  
  return elements;
}

function linkAction() {
	$("a.ext").click(function() { window.open(this.href);return false;})
	$("form.ext").submit(function() { window.open(this.href);return false;})
	
	$("a[@href*=.pdf]").addClass("pdf");
	$("a.PopNotice").click(function() { window.open(this.href,'PopNotice','scrollbars=yes,width=600,height=400,left=0,top=0');return false;} )	
	$("a.external").click(function() { this.target="_blank";} )	

	$("#SKey").click( function() { $(this).val("");})
	$("#mailAddr").click(function() { $(this).val("");})
	$("#search").submit(function() { gotoLink(this);return false;})
	
	$("#u1").click(function() { setCountry('USA');})
	$("#c1").click(function() { setCountry('CAN');})
	$("#u2").click(function() { shopByModel('USA');setCountry('USA');return false;})
	$("#c2").click(function() { shopByModel('CAN');setCountry('CAN');return false;})

//	$("#u3").click(function() { countrySelect('USA');processTmpURL();return false;})
//	$("#c3").click(function() { countrySelect('CAN');processTmpURL();return false;})
//  $("#m1").click(function() { chooseCountry(this);})
}

var tmpURL="";
var lastObj;

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function gotoLink(obj){
	lastObj=obj;
	
	var CountryInCookies2 = getCookie("customerCountry");
	
	 if(CountryInCookies2 != null) {
			processTmpURL(CountryInCookies2);
		}
		else{ 
			TB_show("Select your country","#TB_inline?height=125&amp;width=300&amp;inlineId=setCountry");
		}
}

function processTmpURL(CountryInCookies){
	if(lastObj.id == "search"){
		CountryInCookies = CountryInCookies.toLowerCase();
		$("#shop").val(CountryInCookies);
		lastObj.submit();
	}
	else{	document.location = $(lastObj).find("a."+CountryInCookies).attr("href"); }
}