// Amazon Link Localizer (Canada)
// version 1.0
// http://petewilliams.info/blog/2009/09/amazon-link-localizer-greasemonkey-script
// 2009-09-25
// Copyright (c) 2009, Pete Williams
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://www.greasespot.net/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Amazon Link Localizer (Canada)", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name			Amazon Link Localizer (Canada)
// @namespace		http://petewilliams.info/
// @description		Script to automatically convert links to foreign Amazon sites into Amazon.ca ones.
// @include			*
// @exclude			http://*.amazon.*
// @exclude			https://*.amazon.*
// ==/UserScript==

// VARIABLES
var strTld			= 'ca';
var strAffiliateId	= 'petewill00-20';
var objRegexAsin	= new RegExp( '\/([A-Z0-9]{10})' );

// avoid conflicting with the Amazon Affiliate Link Localizer script ( http://bit.ly/4wg5wa )
if ( typeof( arrAffiliates ) == "undefined" ) {
		
	// get all LINKS
	var arrLinks = document.getElementsByTagName( 'a' );
	// 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;
			}
		}
	}
}