var swapswap = {
	suffix : "2", // default image file name suffix (before file type suffix, like .jpg)

	/**
	 * init()
	 * Initializes swapswap functionality, finding all img elements with a class of swap 
	 * and attaching the swap() function to them.
	 * @param suff	Optional suffix for mouseover state images. The default is 2, but can be anything you want.
	 */
	init : function(suff) {
		this.suffix = (suff == "") ? this.suffix : suff;			// check for supplied suffix. use default if not found
		var imgs = document.getElementsByTagName("img");			// get all the images on the page
		for (var i=0; i<imgs.length; i++) {
			if(imgs[i].className.indexOf("swap") != -1) {			// find the img.swap elements
				
				var img_path_array = imgs[i].src.split("/");
				var img_file_name = img_path_array[img_path_array.length - 1];	// the image file name
				var img_type = img_file_name.split(".")[1];		// the image type (jpg, gif, png)
	
				var img_alternate = new Image();					// preload the OVER state
				img_alternate.src = imgs[i].src.substring(0, imgs[i].src.length - 4) + this.suffix + "." + img_type;
				
				// attach the swap function to the img.swap elements
				imgs[i].onmouseover = function() { swapswap.swap(this); }
				imgs[i].onmouseout = function() { swapswap.swap(this); }
			}
		}
	},
	
	/**
	 * swap()
	 * Interchanges the src of the supplied image with an alternate image
	 * @param img	An img element
	 */
	swap : function(img) {
		var src = img.src;
		var img_path_array = img.src.split("/");
		var img_file_name = img_path_array[img_path_array.length - 1];
		var img_type = img_file_name.split(".")[1];
		var newSrc = "";																			// Variable to hold the new image to swap in
		if (src.indexOf(this.suffix + "." + img_type) == -1) {										// Check if current image NOT is the MouseOver state image
			newSrc = src.substring(0, src.length - 4) + this.suffix + "." + img_type;				// Swap image src attribute to the MouseOver state image
		} else {																					// current image IS the mouseOver state image
			newSrc = src.substring(0, src.length - (4 + this.suffix.length)) + "." + img_type;		// swap src attribute to the NORMAL state image
		}
		img.src = newSrc;																			// Set the image src attribute
	}
}

var formScraper = {
	scrape_area : Object,
	optionText : "",
	paypalOptions : Object,
	itemAmount : Object,
	itemAmountBase : Object,
	inputs : Object,
	selects : Object,
	total : Number,

	init : function() {
		this.paypal = document.getElementById("paypal");
		if (this.paypal) {
			this.scrape_area = document.getElementById("scrape_area");
			this.paypalOptions = document.getElementById("paypal_options");
			this.itemAmount = document.getElementById("amount");
			this.itemAmountBase = document.getElementById("base");

			this.paypal.style.display = "block";
			this.paypal.onsubmit = function() { // can this be changed to onsubmit = formScaper.scrape ?
				formScraper.scrape();
				return false;
			}
		
			// attach SUM functionality to SELECTS
			this.selects = this.scrape_area.getElementsByTagName("select");
			for (var i=0; i<this.selects.length; i++) {
				this.selects[i].onblur = function() { formScraper.sum(); }
			}
			// attach SUM functionality to TEXT INPUTS
			this.inputs = this.scrape_area.getElementsByTagName("input");
			for (var i=0; i<this.inputs.length; i++) {
				if (this.inputs[i].getAttribute("type") == "text") {
					this.inputs[i].onblur = function() { formScraper.sum(); }
				}
			}
		}
	},

	scrape : function() {
		this.optionText = "";
		var labels = this.scrape_area.getElementsByTagName("label");
		for (var i=0; i<labels.length; i++) {
			var control = document.getElementById(labels[i].htmlFor);
			var label = labels[i].firstChild.nodeValue;
			
			if (control.value != "") {
				this.optionText += label + control.value;
				if (i < labels.length - 1) {
					this.optionText += ", ";
				}
			}
		}
		this.paypalOptions.value = this.optionText;
		this.paypal.submit();
	},
	
	sum : function() {
		this.total = (this.itemAmountBase.value * 1);
		for (var i=0; i<this.inputs.length; i++) {
			if ((this.inputs[i].type == "text") && (this.inputs[i].value.length > 0)) {
				this.total += (this.inputs[i].getAttribute("rel") * 1);
			}
		}
		for (var i=0; i<this.selects.length; i++) {
			var selected = this.selects[i].options.selectedIndex;
			this.total += (this.selects[i].options[selected].getAttribute("rel") * 1);
		}
		this.itemAmount.value = this.total;
	}
}

function debug(aMsg) {
   setTimeout(function() { throw new Error("[debug] " + aMsg); }, 0);
}

window.onload = function() {
	swapswap.init("2");
	formScraper.init();
}

