var strTld;
var strAffiliateId;

function linkAmazon(b, a) {
    this.objLink = b;
    this.strAsin = a;
    this.getAffiliateUrl = function () {
        return "http://www.amazon." + strTld + "/exec/obidos/ASIN/" + this.strAsin + "/" + strAffiliateId
    };
}

function checkAmazonLinks() {
    var h = new RegExp("/([A-Z0-9]{10})");
    if (typeof google != "undefined" & google.loader.ClientLocation != null) {
 		var a = google.loader.ClientLocation.address.country_code
	} else {
		var a = "US";
 	}
	switch (a) {
	 	case "GB":
    	case "IE":
        	strTld = "co.uk";
        	break;
    	case "AT":
        	strTld = "de";
    	default:
        	strTld = (arrAffiliates[a.toLowerCase()] ? a.toLowerCase() : "com");
        	break
    }
    strAffiliateId = getAffiliateId(strTld);

    var f = document.getElementsByTagName("a");
    for (i = 0, ii = f.length; i < ii; i++) {
        var c = f[i].href.toLowerCase().indexOf("amazon.");
        if (c > 0 && c < f[i].href.substring(8).indexOf("/") + 8) {
            var d = h.exec(f[i].href);
            if (d) {
                myLinkAmazon = new linkAmazon(f[i], d[1]);
				var find = f[i].href.toLowerCase();
				var replace = myLinkAmazon.getAffiliateUrl();
				modifyHref(find, replace);
            }
        }
    }
}

function getAffiliateId(a) {
    return (arrAffiliates[a]);
}

if (window.addEventListener) {
    window.addEventListener("load", checkAmazonLinks, false)
} else {
    window.attachEvent("onload", checkAmazonLinks)
}

