/*

Written by Richard Zadorozny @ theory11
	(except getElementsByClassName)
	
Get your own sandwich.

*/

var dbw_deck_price = 4.95;
var dbw_product_ids = [];
var dbw_discounts = [3, 6, 12];

function getElementsByClassName(oElm, strTagName, strClassName){
	/*
		getElementsByClassName
		Written by Jonathan Snook, http://www.snook.ca/jonathan
		Add-ons by Robert Nyman, http://www.robertnyman.com
	*/
	
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function setupBuyDecks() {
	var ups = getElementsByClassName(document, 'div', 'dbd_up');
	for (var i = 0; i < ups.length; ++i) {
		var matches = ups[i].id.match(/^dbd_up_([0-9])+$/);
		if (matches) {
			ups[i].deckId = matches[1];
			var counter = document.getElementById('dbd_count_' + matches[1]);
			counter.value = 0;
			counter.onchange = function() {
				updateTotals();
			}
			
			ups[i].onclick = function() {
				var counter = document.getElementById('dbd_count_' + this.deckId);
				counter.value = parseInt(counter.value) + 1;
				updateTotals();
				return false;
			}
		}
	}

	var downs = getElementsByClassName(document, 'div', 'dbd_down');
	for (var i = 0; i < downs.length; ++i) {
		var matches = downs[i].id.match(/^dbd_down_([0-9])+$/);
		if (matches) {
			downs[i].deckId = matches[1];
			downs[i].onclick = function() {
				var counter = document.getElementById('dbd_count_' + this.deckId);
				if (parseInt(counter.value) > 0) {
					counter.value = parseInt(counter.value) - 1;
					updateTotals();
				}
				return false;
			}
		}
	}
}

function price(p) {
	if (p == 0) return '0.00';
	return (parseInt(p*100) + '').replace(/([0-9]{2})$/, '.$1');
}

function updateTotals() {
	var totalCount = 0;
	for (var i = 1; i <= dbw_product_ids.length; ++i) {
		var elem = document.getElementById('dbd_count_' + i);
		if (elem) {
			totalCount += parseInt(elem.value);
		}
	}

	var found = 0;
	for (var i = dbw_discounts.length - 1; i >= -1; --i) {
		var div = document.getElementById('dbuy_discount_' + (i + 1));
		
		if (div) {
			var img = div.firstChild;
			var on = false;
			
			if (!found && -1 == i) {
				on = totalCount >= 1;
			} else if (!found && totalCount >= dbw_discounts[i]) {
				found = i + 1;
				on = true;
			}
			
			if (on) {
				img.src = img.src.replace(/\_off.gif/, '_on.gif');
			} else {
				img.src = img.src.replace(/\_on.gif/, '_off.gif');
			}
		}
	}
	
	if (found) {
		var discount = totalCount * Math.round(dbw_deck_price * found * 5) / 100;
		
		document.getElementById('dbuy_price').childNodes[1].innerHTML = '<strike>$' + price(totalCount * dbw_deck_price) + '</strike> <strong>$' + price(totalCount * dbw_deck_price - discount) + '</strong>';
		document.getElementById('dbuy_savings').childNodes[1].innerHTML = '$' + price(discount);
	} else {
		document.getElementById('dbuy_price').childNodes[1].innerHTML = '<strong>$' + price(totalCount * dbw_deck_price) + '</strong>';
		document.getElementById('dbuy_savings').childNodes[1].innerHTML = '$0.00';
	}
}

function decks_buy() {
	// build the url
	var items = 0;
	var url = 'http://www.theory11.com/store/quick_location.php?';
	for (var i = 0; i < dbw_product_ids.length; ++i) {
		var elem = document.getElementById('dbd_count_' + (i+1));
		if (elem) {
			var count = elem.value;
			for (var j = dbw_product_ids[i].length-1; j >= 0; --j) {
				if (count >= j*12) {
					++items;
					this_count = j > 0 ? parseInt(count / j / 12) : count;
					url += 'id' + items + '=' + dbw_product_ids[i][j] + '&qty' + items + '=' + this_count + '&';
					count = j > 0 ? parseInt(count % (j*12)) : 0;
				}
			}
		}
	}
	if (!items) {
		alert('Use the up and down arrows above to tell us how many of each colour you would like.');
	} else {
		location.href = url + 'loc=www.theory11.com/added-to-cart_suggestions.php';
	}
}

window.onload = function() { setupBuyDecks() };
