// set your AFFILIATE IDs here. Leave blank any you do not have an account for.
var arrAffiliates = {
	'co.uk' : 'gizunk-21',
	'com'	: 'gizunk-20',
	'de'	: '',
	'fr'	: '',
	'ca'	: '',
	'jp'	: ''
}

/**
 * search for any AMAZON product links and replace with a referral link to the user's local Amazon site
 * @author Pete Williams
 * @url http://petewilliams.info/blog/2009/07/javascript-amazon-associate-link-localiser/
 */
function checkAmazonLinks() {
	// set required VARIABLES
	var strTld;
	var objRegexAsin	= new RegExp( '\/([A-Z0-9]{10})' );
	
	//	Incase google isn't working for IP.
	if ( google.loader.ClientLocation ) {
		var strCountry		= google.loader.ClientLocation.address.country_code;
	} else {
	//If Google JSapi fails try 
		strCountry = geoip_country_code();
	}
	// get domain TLD from country code
	switch ( strCountry ) {
		case 'GB':
		case 'IE':
			strTld = 'co.uk';
			break;
		default:
			// by default, TLD is lowercase of country code. if no associate, then use .com
			strTld = 'com';
			break;
	}

	// get all LINKS
	var arrLinks = document.getElementsByTagName( 'a' );

	// get appropriate aFFILIATE ID
	strAffiliateId = ( arrAffiliates[strTld] ? arrAffiliates[strTld] : arrAffiliatesSpares[strTld] );

	// LOOP through
	for ( i=0; i<arrLinks.length; i++ ) {

		// is it an AMAZON link
		var intIndex = arrLinks[i].href.toLowerCase().indexOf( 'amazon.' );

		if ( intIndex > 0) {

			// find ASIN
			var arrResults = objRegexAsin.exec( arrLinks[i].href );

			if ( arrResults ) {
				// REPLACE URI
				var strAsin = arrResults[1];
				arrLinks[i].href  = 'http://www.amazon.' + strTld + '/exec/obidos/ASIN/' + strAsin + '/' + strAffiliateId;
			}


		}
	}
}

// initialise on page LOAD
if (window.addEventListener){
    window.addEventListener('load',checkAmazonLinks,false); //W3C
} else{
    window.attachEvent('onload',checkAmazonLinks); //IE
}

// these are the author's affiliate IDs. They will only be used for localities you do not provide an ID for.
var arrAffiliatesSpares = {
	'co.uk' : 'pcrev05',
	'com'	: 'petewill-20',
	'de'	: 'petewill05-21',
	'fr'	: 'petewill-21',
	'ca'	: 'petewill00-20',
	'jp'	: 'petewill-22'
}
