/* 	
	----------------------------------------------------------------------
	NEW SHOPPING CART
	Since my object creation is going spooobie, here is some new code 
	for dealing with the nice UI stuff in the cart.
	---------------------------------------------------------------------- 
*/
window.onload = init;

function init() {
	// Get all of the cart elements
	if (document.getElementById('cartLink') && document.getElementById('cartPanel')) {
		basket.init('cartLink', 'cartPanel');
	}
	// Assign toggle panels:
	anchorInit();
	buttonInit();
}

function anchorInit() {
	var anchors = document.getElementsByTagName('a');
		for (var i in anchors) {
			if (anchors[i].className == 'toggleControl') {
				anchors[i].onclick = function() {toggleFieldset(this);return false;};
			} else if (anchors[i].className == 'imageBrowserLink') {
				anchors[i].onclick = function() {swapImage(this);return false;};
			}
		}
}


function buttonInit() {
	if (document.getElementById('printLink')) {
		document.getElementById('printLink').onclick = function() {window.print();}
	}
}

//This function switches the image source of clicked thumbnails, in the product profile:
function swapImage(swapAnchor) {
	var imageSource = swapAnchor.getElementsByTagName('IMG');
	var imageDestination = document.getElementById('mainImage');
	
	var regexp = /:[0-9]+x[0-9]+:[a-z]/;
	var strNewSrc = imageSource[0].src.replace(regexp, ':180x240:w');
	
	//Switch the source:
		imageDestination.src = strNewSrc;
	//Switch the link location:
		imageDestination.parentNode.href = imageSource[0].src.replace(regexp, ':640x480:w');
	//nolight the other thumbs
		var linkContainer = swapAnchor.parentNode;
		var allThumbs = linkContainer.getElementsByTagName('IMG');
			for (var i in allThumbs) {
				allThumbs[i].className = '';
			}
	//highlight the newly selected thumbnail	
		imageSource[0].className = 'selected';
}

function toggleFieldset(toggleAnchor) {
	//Toggle controls open and shut a sibling fieldset classed with 'toggle'
	
	//Find the parent:
	var parent = toggleAnchor;
	do {
		parent = parent.parentNode;
	} while (parent.nodeName != 'FORM');
	
	//Find the sibling:
	var fieldsets = parent.getElementsByTagName('FIELDSET');
	for (var i in fieldsets) {
		if (fieldsets[i].className == 'toggle') {
			if (fieldsets[i].style.display == 'block') {
				fieldsets[i].style.display = 'none';
			} else {
				fieldsets[i].style.display = 'block';
			}
		}
	}
	return false;
}

/* HIGLIGHT THE UPDATE BUTTON */
var attActive = false; // this is a check so we don't have this function fired off more than once
function attention() {
	attActive = true;
	// update the notice
	elCartNotice.innerHTML = '<p>You need to update your cart</p>';
	elCartNotice.style.opacity = 1;
	elCartNotice.style.display = 'block';
	elCartNotice.className = 'noticeUpdate';
	elCartUpdate.className = 'attention';
}

/* COOKIE MANAGEMENT */
function cookieCutter() {
	/* Create the expiry date */
	var expiry = new Date();
	expiry.setTime(expiry.getTime()+(10*24*60*60*1000));
	/* Make the browser eat the COOOOOKIE! */
	document.cookie = 'murdock_wines=' + openState + '; expires=' +  expiry.toGMTString() + '; path=/;'
}
function cookieMuncher() {
	var cookieJar = document.cookie.split(';');
	for (var i in cookieJar) {
		var cookie = cookieJar[i];
		cookie = cookie.split('=');
		// for the sake of IE, remove whitespace and so on
		var cookieName = wsStripper(cookie[0]);
		if (cookieName == 'murdock_wines') {
			// fudge it a bit since we only have one paramater.
			if (cookie[1] == 'true') {
				openState = true;
				elCartContent.style.display = 'block';
			}
			else if (cookie[1] == 'false') {
				openState = false;
				elCartContent.style.display = 'none';
			}
		}
	}
}

/* UTILITY FUNCTIONS */
function wsStripper(text) {
	text = (text.replace(/^\W+/,'')).replace(/\W+$/,'')
	return text;
}


var basket = {
	panelNode: null,
	linkNode: null,
	openState: false,
	init: function(linkID, panelID) {
		this.linkNode = document.getElementById(linkID);
		this.panelNode = document.getElementById(panelID);
		this.panelNode.style.display = 'none';
		var basketObj = this;
		this.linkNode.onclick = function () {
			basketObj.toggle();
			return false;
		}
	},
	toggle: function() {
		if (this.openState == false) {
			this.panelNode.style.display = 'block';
			this.openState = true;
		}
		else {
			this.panelNode.style.display = 'none';
			this.openState = false;
		}
	}
}	