// Dynamic changing of visible product prices
// Input: radio input / select options of product variations

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};

function getAttr(elem, attrName) {
	if (!elem || !elem.attributes) return false;
	var value = "";
	for (var i=0;i<elem.attributes.length;i++) {
		if (elem.attributes[i].nodeName == attrName) {
			value = elem.attributes[i].nodeValue;
			break;
		}
	}
	return value;
}

var inputs = document.getElementsByTagName('input');
var options = document.getElementsByTagName('option');
var product_prices = document.getElementsByClassName('product-price');
var forms = document.getElementsByTagName('form');
var selects = document.getElementsByTagName('forms');
var additional_prices = new Object();

function storeAdditionalPrice(elem) {
	var product_id = getAttr(elem, 'product');
	product_id = +product_id;
	var price = getAttr(elem, 'price');
	price = price.replace(',', '.');
	price = price.replace('+', '');
	price = +price;
	if (additional_prices[product_id]) {
		var tmp = additional_prices[product_id];
		tmp = +tmp;
		price = tmp + price;
		additional_prices[product_id] = additional_prices[product_id] + price;
	} else {
		additional_prices[product_id] = price;
	}
	return true;
}

function updateAdditionalPrice(elem) {
	var product_id = getAttr(elem, 'product');
	product_id = +product_id;
	var price = getAttr(elem, 'price');
	price = price.replace(',', '.');
	price = price.replace('+', '');
	price = +price;
	if (additional_prices[product_id] || additional_prices[product_id] == 0) {
		var tmp = additional_prices[product_id];
		tmp = +tmp;
		price = price + tmp;
		price = +price;
		price = price.toFixed(2);
		price = " " + price; 
		price = price.replace('.', ',');
		elem.innerHTML = price;
	}
	return true;
}
var inputs_found = false;
function refreshVariationPrices() {
	if (inputs_found === 0) {
		return null;
	}
	inputs_found = 0;
	for (var i=0;i<inputs.length;i++) {
		if (inputs[i].type == 'radio' && inputs[i].className.indexOf('variation') >= 0 )
 			{
			inputs_found++;
			if (inputs[i].checked == true) {
				storeAdditionalPrice(inputs[i]);
			}
		}
	}
	for (var i=0;i<options.length;i++) { 
		if (options[i].className.indexOf('variation') >= 0) {
			inputs_found++;
			if (options[i].selected == true ) {
				storeAdditionalPrice(options[i]);
			}
		}
	}
	for (var i=0;i<product_prices.length;i++) {
		updateAdditionalPrice(product_prices[i]);
	}
	additional_prices = new Object;
}

//written by Dean Edwards, 2005
//with input from Tino Zijdel, Matthias Miller, Diego Perini
//http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		handlers[handler.$$guid] = handler;
		element["on" + type] = handleEvent;
	}
};
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	var handlers = this.events[event.type];
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

// Bind events to form and document click
for (var i = 0; i < forms.length; i++) {
  addEvent(forms[i], "change", function (evt) { 
	    refreshVariationPrices(); 
  });
}
addEvent(document, "click", function (evt) { 
    refreshVariationPrices(); 
});
refreshVariationPrices();
