function getTierPrice (id, qty){
	var newProductPrice = 0;
	$.each (OBBPrices.tiers[id], function(range, price) {
		if (parseInt(qty) >= parseInt(range)) {
			newProductPrice = price;
		}
	});
	return newProductPrice;
}

function setPrice (id, qty) {
	newPrice = getFinalPrice(id, qty);
	if (newPrice) {
		$('#article-price-' + id + ' .price .regular span[class^="js_price"]').empty().html(currency(newPrice[0]));
		$('#article-price-' + id + ' .price .regular span[class^="js_price"]').attr("title", newPrice[0]);
		$('#article-price-' + id + ' .price .alternative span[class^="js_price"]').empty().html(currency(newPrice[1]));
		$('#article-price-' + id + ' .price .alternative span[class^="js_price"]').attr("title", newPrice[1]);
	}
}

function getFinalPrice (id, qty) {
	var tier = getTierPrice (id, qty);
	var bundles = getBundlePrices();
	return [tier[0]*1+bundles[0]*1, tier[1]*1+bundles[1]*1];
}

function getBundlePrices (){
	var selected = [];
	var bundlesSumTax = 0;
	var bundlesSumNoTax = 0;
	var allBundles = jQuery('#bundles .bundle-choice-box:checked:enabled, #bundles .bundle-is-included, #bundles .bundle-choice-dropdown option:selected');
	jQuery.each (allBundles, function() {
		if (this.value > 0) {
			selected.push(this.value);
			bundlesSumTax += OBBPrices.bundles[this.value][0];
			bundlesSumNoTax += OBBPrices.bundles[this.value][1];
		}
	});
	return [parseFloat(bundlesSumTax), parseFloat(bundlesSumNoTax)];
}

function togglePrice(context, checkbox, value, taxFreeValue) {
	// This method must be in onchange (not onclick) see commits in #6627 for all the details
	var
		checkbox = jQuery(checkbox),
		value = Math.max(0, value),
		taxFreeValue = Math.max(0, taxFreeValue);

	jQuery("span.js_price, span.js_price_tax", context).each(function(){
		var
			priceSpan = jQuery(this),
			title = priceSpan.attr("title");

		if (title == null || title == '') return;

		var newValue = parseFloat(title) + (checkbox.is(":checked") ? 1 : -1) * (priceSpan.is(".js_price") ? taxFreeValue : value);
		priceSpan
			.attr("title", Math.max(0, newValue))
			.html(currency(newValue));
	});
}


function toInt(value) {
	value = parseInt(value);
	if (!value) value = 0;
	return value;
}

function toggleReadOnly(id) {
	obj = document.getElementById(id);
	if (!obj) return;
	obj.readOnly = !obj.readOnly;
	obj.style.background = obj.readOnly ? '#EEE' : '#FFF';
}

jQuery.fn.initSaveMoney = function () {
	var collection = jQuery(this);

	collection.each( function() {
		var self = jQuery(this),
			trigger = self.find('a'),
			info_box = trigger.next('.product-tier-prices-info'),
			ib_timeout = false,
			ib_timeout_period = 1500,
			fade_period = 300;

		trigger.click(function(e){
			var self = jQuery(this);

			collection.find('.product-tier-prices-info').hide();

			info_box.fadeIn(fade_period);

			e.preventDefault();
		})

		self.mouseout(function() {
			ib_timeout = setTimeout(
				function(){
					if (info_box.is('.product-tier-prices-info:visible')) info_box.fadeOut(fade_period);
				}, ib_timeout_period)
		})
		.mouseover(function() {
			if(ib_timeout) {
				clearTimeout(ib_timeout);
				ib_timeout = false;
			}
		});
	});
}

jQuery(document).ready(function(){
	jQuery('.product-tier-prices').initSaveMoney();

	// workaround for onchange="togglePrice(...)"  in IE8/IE9
	// Fixing doubleclick on a checkbox in IE9 (when onclick is set)
	// Doubleclick trigers 2 onclick, while the checkbox is changed once.
	// Changing onclick to onchange helps, but will make it NOT work in IE8
	// where onchange fired only after you move focus away from checkbox.
	// So doing it automatically (on checkboxes with onchange)
	jQuery("input[type=checkbox][onchange]").click(function(){
		jQuery(this).blur().focus();
	});

});

