// add load events
addLoadEvent(setPrefs);
addLoadEvent(prefsForm);
addLoadEvent(savePrefs);


// this function rewrites amazon links

function rewriteLinks(newDomain, newId){

  // if browser doesn't support this method, return false
  if(!document.getElementsByTagName) return false;

  // get all the links
  var links = document.getElementsByTagName("a");
  
  // loop through all the links
  for (var i=0; i < links.length ; i++){
  	// grab the href
  	var myHref = links[i].href;
  	
  	// make the href lowercase
  	myHref = myHref.toLowerCase();
  	
  	// check if it's an amazon link
  	if (myHref.indexOf("amazon") == -1) continue;
  	// check to see it's a product link
  	if (myHref.indexOf("asin") == -1 && myHref.indexOf("tg/detail/-") == -1 && myHref.indexOf("gp/product") == -1) continue;


	// get the old domain
	//var hrefArray = myHref.split("/");
	//var oldDomain = hrefArray[2];
	// update href with new domain
	//myHref = myHref.replace(oldDomain, "www." + newDomain);
			
	
	// get the item id
	if(myHref.indexOf("asin") != -1){
		var mySubstring = myHref.substr(myHref.indexOf("asin") + 4);
	} else if (myHref.indexOf("tg/detail/-") != -1) {
		var mySubstring = myHref.substr(myHref.indexOf("tg/detail/-") + 11);
	} else {
		var mySubstring = myHref.substr(myHref.indexOf("gp/product") + 10);
	}
	
	// extract the item id
	itemID = mySubstring.split("/")[1];
	itemID = itemID.split("&")[0];
	
	// create the new href
	myHref = "http://www." + newDomain + "/exec/obidos/redirect?link_code=as2&path=ASIN/" + itemID + "&tag=" + newId;
	
	// grab the href up to the end of the item id
	//myHref = myHref.replace(mySubstring, "/" + itemID + "/");
	
	//myHref += newId + "/";
	
	
	// write out link
	links[i].href = myHref;
  }


}

// this function sets the preferences
function setPrefs(){

	var domain = getCookie("vendor");
	//alert(domain);

	if(domain == "amazon.co.uk"){
		rewriteLinks("amazon.co.uk", "blogography-21");
	} else if (domain == "amazon.com"){
		rewriteLinks("amazon.com", "blogography00-20");
	} else if (domain == "amazon.ca"){
		rewriteLinks("amazon.ca", "blogography0e-20");
	} else if (domain == "amazon.de"){
		rewriteLinks("amazon.de", "andybudd-21");
	}

}

// this function saves the amazon prefs in a cookie

function savePrefs(){
	// if browser doesn't support this method, return false
	if(!document.getElementById) return false;
	
	// get form reference
	form = document.getElementById("amazonPrefs");
	
	// if the form isn't there, return false
	if(!form) return false;
	
	// set callback function
	form.onsubmit = function(){
		// get value of stores select box
		var domain = document.getElementById("stores")[document.getElementById("stores").selectedIndex].value;
		
		// get current date
		var now = new Date();
		now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
		
		// set prefs cookie
		setCookie("vendor", domain, now, "/");
		
		// update any link on page
		setPrefs();
				
		// stop form submitting to server
		//return(false);
	}


}

// this function creates the prefs form
function prefsForm(){

	// if there isn't an amazonPrefs element, return false
	if(!document.getElementById("amazonPrefs")) return false;

	// create a form elelemt
	var myForm = document.createElement('form');
	myForm.setAttribute('id','amazonPrefs');
	
	// create lable for select box
	var myLabel = document.createElement('label');
	myLabel.setAttribute('for','stores');
	myLabel.appendChild(document.createTextNode('Open Amazon links at: '));
	
	myForm.appendChild(myLabel);
	
	// create select box
	var mySelect = document.createElement('select');
	mySelect.setAttribute('name','stores');
	mySelect.setAttribute('id','stores');
	
	// create options
	var stores = new Array ("amazon.co.uk", "amazon.com", "amazon.ca");
	
	for (var i=0; i < stores.length ; i++){
		var myOption = document.createElement('option');
		myOption.setAttribute('value',stores[i]);
		myOption.appendChild(document.createTextNode(stores[i]));
		
		//check if set in cookie
		var domain = getCookie("vendor");
		if(stores[i] == domain){
			myOption.setAttribute('selected','selected');
		}
	
		mySelect.appendChild(myOption);
	}
	
	
	
	myForm.appendChild(mySelect);
	
	
	// create a submit button
	var mySubmit = document.createElement('input');
	mySubmit.setAttribute('name','prefsSubmit');
	mySubmit.setAttribute('id','prefsSubmit');
	mySubmit.setAttribute('type','submit');
	mySubmit.setAttribute('value','Submit');
	
	myForm.appendChild(mySubmit);
	
	
	
	// add form
	//document.getElementById("amazonPrefs").appendChild(myForm);
	document.getElementById("amazonPrefs").parentNode.replaceChild(myForm, document.getElementById("amazonPrefs"))

}