var W3CDOM = (document.createElement && document.getElementsByTagName);

function addEvent(obj, eventType, func) {
	if(obj.addEventListener) {
		obj.addEventListener(eventType, func, false);
		return true;
	} else if(obj.attachEvent) {
		var r = obj.attachEvent("on" + eventType, func);
		return r;
	} else {
		return false;
	}
}

addEvent(window, "load", initPurchasePage);
addEvent(window, "load", initPopupLinks);
addEvent(window, "load", initLinkConfirmations);
addEvent(window, "load", pinballEffect);
addEvent(window, "load", externalLinks);

var popupLinkConfig = new Array;
popupLinkConfig["exhibitions-popup"] = new Array("", "width=970, height=600, scrollbars=yes, resizable=yes, menubar=no");
popupLinkConfig["purchase-print-order-popup"] = new Array("", "width=700, height=500, scrollbars=yes, resizable=yes, menubar=yes");
popupLinkConfig["terms-popup"] = new Array("", "width=500, height=500, scrollbars=yes, resizable=yes, menubar=yes");
popupLinkConfig["z-code-popup"] = new Array("", "width=500, height=500, scrollbars=yes, resizable=no, menubar=no");

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "top")
			anchor.target = "_top";
		else if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
}


function initPopupLinks() {
	if (!W3CDOM) return;
	var pageLinks = document.getElementsByTagName("a");
	for(var i = 0; i < pageLinks.length; i++) {
		if(((pageLinks[i].className != null) && 
		(pageLinks[i].className != "")) ||
		((pageLinks[i].parentNode.className != null) && 
		(pageLinks[i].parentNode.className != ""))) {
			var linkClass = " " + pageLinks[i].className + " ";
			if((linkClass == "  ") && (pageLinks[i].parentNode.className != "")) {
				linkClass = " " + pageLinks[i].parentNode.className + " ";
			}
			for(var theKey in popupLinkConfig) {
				if(linkClass.indexOf(" " + theKey + " ") > -1) {
					if((pageLinks[i].target == "") || (pageLinks[i].target == null)) {
						pageLinks[i].target = (popupLinkConfig[theKey][0] != "") ? popupLinkConfig[theKey][0] : theKey;
					}
					pageLinks[i].settings = popupLinkConfig[theKey][1];
					pageLinks[i].onclick = popUp;
				}
			}
		}
	}
	return true;
}

function popUp() {
	newWin = window.open(this.href, this.target, this.settings);
	newWin.focus();
	return false;
}

function initLinkConfirmations() {
	if(!W3CDOM) return;
	
	var lists = document.getElementsByTagName("ul");
	
	for(var i = 0; i < lists.length; i++) {
		var links = lists[i].getElementsByTagName("a");
		for(var i2 = 0; i2 < links.length; i2++) {
			if(links[i2].parentNode.className == "basket-empty") {
				links[i2].onclick = function() {
					if(!confirm("Are you sure that you would like to empty your basket?")) return false;
				}
			}
			if(links[i2].parentNode.className == "wish-list-delete-list") {
				links[i2].onclick = function() {
					if(!confirm("Are you sure that you would like to permanently delete your wish list?")) return false;
				}
			}
		}
	}
}

function pinballEffect() {
	if(!W3CDOM) return;
	var allElements = document.getElementsByTagName('*');
	var originalBackgrounds = new Array();
	for(var i=0; i<allElements.length; i++) {
		if(allElements[i].className.indexOf('pinball-scoop') !=-1) {
			allElements[i].onmouseover = mouseGoesOver;
			allElements[i].onmouseout = mouseGoesOut;
			allElements[i].onclick = mouseGoesClick;
		}
	}
}
	function mouseGoesOver() {
		if(!W3CDOM) return;
		originalClassNameString = this.className;
		this.className += " pinball-on";
		this.style.cursor = "pointer";
	}
	function mouseGoesOut() {
		if(!W3CDOM) return;
		this.className = originalClassNameString;
	}
	function mouseGoesClick() {
		if(!W3CDOM) return;
		var allThisAreasElements = this.getElementsByTagName('*');
		for(var j=0; j<allThisAreasElements.length; j++) {
			if(allThisAreasElements[j].className.indexOf('pinball-sinkhole') != -1) {
				var url = allThisAreasElements[j].href;
				window.location = url;
			}
		}
	}
	
	
function initPurchasePage() {
	if(!W3CDOM) return;
	
	var addressBlock = document.getElementById('delivery-address-block');
	if(addressBlock != null) { 
		var collectionBlock = document.getElementById('collection-block');
		var totalWith = document.getElementById('total-with-delivery');
		var totalWithout = document.getElementById('total-without-delivery');
		var totalWithInternational = document.getElementById('total-with-international');
		var nonUKNote = document.getElementById('non-uk-delivery-note');
		var delivery_yes = document.getElementById('delivery_yes');	
		var delivery_yes_free = document.getElementById('delivery_yes_free');		
		var delivery_no = document.getElementById('delivery_no');
		var delivery_international = document.getElementById('delivery_international');
		
		var delivery_address_country = document.getElementById('delivery_address_country');
		delivery_address_country.onchange = function() {
			if(this.value != 'United Kingdom') {
				delivery_international.checked = true;
				delivery_international.onclick();
			}
		}
		
		document.getElementById('copy_cardholder').onclick = function() {
			document.getElementById('delivery_address').value = document.getElementById('cardholders_name').value + '\n' + document.getElementById('cardholders_address').value; 
			document.getElementById('delivery_postcode').value = document.getElementById('cardholders_postcode').value; 
			document.getElementById('delivery_address_country').selectedIndex = document.getElementById('cardholders_address_country').selectedIndex;
			delivery_address_country.onchange();
		}
		
		// page re-load
		
		if(delivery_yes.checked == true) {
			collectionBlock.style.display = 'none';
			addressBlock.style.display = 'block';
			totalWith.style.display = 'block';
			totalWithout.style.display = 'none';
			nonUKNote.style.display = 'none';
			totalWithInternational.style.display = 'none';
		}		
		if(delivery_no.checked == true) {
			collectionBlock.style.display = 'block';
			addressBlock.style.display = 'none';
			totalWith.style.display = 'none';
			totalWithout.style.display = 'block';
			nonUKNote.style.display = 'none';
			totalWithInternational.style.display = 'none';
		}
		if(delivery_yes_free != null) {
			if(delivery_yes_free.checked == true) {
				collectionBlock.style.display = 'none';
				addressBlock.style.display = 'block';
				totalWith.style.display = 'none';
				totalWithout.style.display = 'block';
				nonUKNote.style.display = 'none';
				totalWithInternational.style.display = 'none';
			}
		}
		if(delivery_international.checked == true) {
			collectionBlock.style.display = 'none';
			addressBlock.style.display = 'block';
			totalWith.style.display = 'none';
			totalWithout.style.display = 'none';
			nonUKNote.style.display = 'block';
			totalWithInternational.style.display = 'block';
		}
		
		// radio clicks
		
		delivery_yes.onclick = function() {
			if(this.checked == true) { 
				collectionBlock.style.display = 'none';
				addressBlock.style.display = 'block';
				totalWith.style.display = 'block';
				totalWithout.style.display = 'none';
				nonUKNote.style.display = 'none';
				totalWithInternational.style.display = 'none';
			}
		}
		if(delivery_yes_free != null) {
			delivery_yes_free.onclick = function() {
				if(this.checked == true) { 
					collectionBlock.style.display = 'none';
					addressBlock.style.display = 'block';
					totalWith.style.display = 'none';
					totalWithout.style.display = 'block';
					nonUKNote.style.display = 'none';
					totalWithInternational.style.display = 'none';
				}
			}
		}
		delivery_no.onclick = function() {
			if(this.checked == true) {
				collectionBlock.style.display = 'block';
				addressBlock.style.display = 'none';
				totalWith.style.display = 'none';
				totalWithout.style.display = 'block';
				nonUKNote.style.display = 'none';
				totalWithInternational.style.display = 'none';
			}
		}
		delivery_international.onclick = function() {
			if(this.checked == true) {
				collectionBlock.style.display = 'none';
				addressBlock.style.display = 'block';
				totalWith.style.display = 'none';
				totalWithout.style.display = 'none';
				nonUKNote.style.display = 'block';
				totalWithInternational.style.display = 'block';
			}
		}
	}
}
