Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;i++) { if (parseInt(this[i])>0) { sum += parseInt(this[i]); } }
	return sum;
}
Array.prototype.num = function(skipHi){
	for(var i=0,num=0;i<this.length;i++) { if (skipHi && (i >= 900))  continue; if ((parseInt(this[i])>0))  num++; }
	return num;
}

var max_licences   = 0;
var nums           = new Array();
var packs          = new Array();
var prices_packs   = new Array();
var prices_modules = new Array();
var prices         = new Array();
var prices1        = new Array();
var maintenances   = new Array();
var hw_keys        = new Array();
var test_total     = 0;

$(document).ready(function(){
	// rollete events
	$(".rollette").each(function(){
		var element = $(this).attr("rel");
		if ($("#"+element+':visible').length) {
			$(this).find('img:first').attr('src', '/shop/public/img/toggler-close.gif');
			$(this).find('em').hide();
		} else {
			$(this).find('img:first').attr('src', '/shop/public/img/toggler.gif');
			$(this).find('em').show();
		}
	});
	$(".rollette").click(function(){
		var element = $(this).attr("rel");
		if ($("#"+element+':visible').length) {
			$("#"+element).slideUp('middle');
			$(this).find('img:first').attr('src', '/shop/public/img/toggler.gif');
			$(this).find('em').show();
		} else {
			$("#"+element).slideDown('middle');
			$(this).find('img:first').attr('src', '/shop/public/img/toggler-close.gif');
			$(this).find('em').hide();
		}
	});
	// inline help init
	if ('#inline-help-desc'){
		$('#inline-help-desc').addClass('active');
	};
	// tables (calc)
	$(".table_calc_simple").each(function(){
		$(this).find("input[@type=text]").each(function(){
			calc_simple(this);
		});
		$(this).find("input[@type=text]").keyup(function(){
			calc_simple(this);
		});
	});
	$(".sumall_keyup").keyup(function(){
		sum_all();
	});
	$(".table_calc").each(function(){
		var category = $(this).attr("rel");
		if (category>0) {
			nums[category]           = new Array();
			packs[category]          = new Array();
			prices_packs[category]   = new Array();
			prices_modules[category] = new Array();
		}
	});
	$(".table_calc").each(function(){
		var table = this;
		var category = $(this).attr("rel");
		$(this).find("input[@type=text]").each(function(){
			calc(this);
		});
		calc_ext(table);
		$(this).find("input[@type=text]").keyup(function(){
			calc(this);
			calc_ext(table);
			if (test_total){
				$("#maintenance_"+category).attr('checked', 'checked');
			} else {
				$("#maintenance_"+category).removeAttr('checked');
			}
		});
	});
	
	// bubbles
  $('#language li').hover(
		function(){ $(this).find('p.bubble').css('display', 'block'); },
    function(){ $(this).find('p.bubble').css('display', 'none'); }
	).click(function(){
		window.location = $(this).find('a:first').attr('href');
	});
});

// calculate price w/o discount
function calc_simple(input)
{
	var num      = parseInt($(input).attr("value"));
	var price    = parseInt($(input).parent().prev().html().replace(/\s+/g, ''));
	var res      = $(input).parents("tr").find("td:last");
	// calculate result
	if (num>100) num = 100;
	var calc  = num*price;
	// result
	res.html( format_price(calc) );
	hw_keys[$(input).attr("rel")] = calc;
	sum_all();
	//
	if (!(num>0)) num = "";
	input.value = num;
}

// calculate price w/ discount
function calc(input)
{
	var num      = parseInt($(input).attr("value"));
	var category = $(input).parents("table").attr("rel");
	var type     = null;
	if ($(input).parents("tr").hasClass("module")) {
	    type     = "module";
	} else if ($(input).parents("tr").hasClass("pack")) {
	    type     = "pack";
	}
	var id       = parseInt($(input).attr("rel"));
	var price    = null;
	if ($("#" + type + "_" + category + "_" + id).children(".td-price").length) {
	    price    = parseInt($("#" + type + "_" + category + "_" + id).children(".td-price").html().replace(/\s+/g, ''));
	} else {
	    price    = parseInt($(input).parent().prev().html().replace(/\s+/g, ''));
	}
	var res;
        switch (id) {
        case 900:
            res = $(input).parents("tr").prev("tr").find("td:last");
            break;
        default:
            res = $(input).parents("tr").find("td:last");
        }
	var price0   = num*price;
	// calculate result
	if (num>100) num = 100;
	var calc  = num*price;
	
	if ((id < 900) || (id == 902) || (id == 903)) { // quantity discount
	    if (num==2) calc = price + 0.6*price;
	    if (num==3) calc = price + 0.6*price + 0.4*price;
	    if (num==4) calc = price + 0.6*price + 0.4*price + 0.4*price;
	    if (num==5) calc = price + 0.6*price + 0.4*price + 0.4*price + 0.4*price;
	    if (num>=6) calc = price * 3;
	}
	// educational
	if (($(input).parents("tr.educational").length) && (num>0)) {
		calc   = price;
		price0 = price;
	}
	// result
	res.html( format_price(calc) );
	// count
	switch (type) {
	    case "module":
		prices_modules[category][id] = price0;
		nums          [category][id] = calc;
		break;
	    case "pack":
		prices_packs[category][id] = price0;
		packs       [category][id] = calc;
		break;
	}
	//
	if (!(num>0)) num = "";
	input.value = num;
}

// calculate maintenance
function calc_maintenance(category)
{
	if (category>0) {
		var total       = prices1[category];
		var maintenance = Math.round(total*0.15);
		/*if (maintenance>0) {
			$("#maintenances_view_"+category).show();
		} else {
			$("#maintenances_view_"+category).hide();
		}*/
/*		if (maintenance>0){
			if (!$("#maintenance_"+category).attr("checked")){
				$("#maintenance_"+category).click()
			}
		} else {
			if ($("#maintenance_"+category).attr("checked")){
				$("#maintenance_"+category).click()
			}
		}*/
		$licence_no = $("input[name^=licence_no]").attr('value');
		$("#maintenance_"+category+"_price").html(format_price(maintenance));
		if (!$licence_no && $("#maintenance_"+category).attr("checked")) {
			maintenances[category] = maintenance;
		} else {
			maintenances[category] = 0;
		}
	}
}


// calculate extended price
function calc_ext(table)
{
	var total    = 0;
	var total0   = 0;
	var price    = 0;
	var num      = 0;
	var disc_ext = 0;
	var category = $(table).attr("rel");
	if (category>0) {
		total  = nums[category].sum();
		total0 = total + packs[category].sum();
		num    = nums[category].num(true);
	} else {
		$(table).find("input").each(function(){
			var price = $(this).parents("td").next().next().html();
			if (price!=null) {
				price = parseInt(price.replace(/\s+/g, ''));
				if (price>0) {
					num++;
					total += price;
				}
			}
		});
	}
	// maintenance
	var category = $(table).attr('rel');
	prices1[category] = total0;
	calc_maintenance(category);
	// continue calculations
	if (num>= 3) disc_ext =  5;
	if (num>= 5) disc_ext = 10;
	if (num>= 9) disc_ext = 15;
	if (num>=12) disc_ext = 20;
	if (num>=15) disc_ext = 25;
	if (num>=20) disc_ext = 30;
	total = Math.round(total * (100 - disc_ext) / 100);
	// include packs
	if (category>0) {
		 total += packs[category].sum();
	}
	// total
	$(table).find(".disc_ext").html(format_disc(disc_ext));
	$(table).find(".disc_price").html(format_price(total0-total));
	$(table).find(".total_price0").html(format_price(total0));
	$(table).find(".total_price").html(format_price(total));
	if (category>0) {
		prices[category] = total;
	}
	// sum
	sum_all();
}

function sum_all()
{
	var total  = 0;
	var total0 = 0;
	// categories
	for(var i=0; i<prices_packs.length; i++) if (i>0) {
		total0 += prices_packs[i].sum() + prices_modules[i].sum();
		total  += prices[i];
	}
	test_total = total0;
	// hw keys
	total  += hw_keys.sum();
	total0 += hw_keys.sum();
	// maintenances
	total  += maintenances.sum();
	total0 += maintenances.sum();
	// other payment
	if ($("#other_payment_price").val()) {
		total  += parseInt($("#other_payment_price").val());
		total0 += parseInt($("#other_payment_price").val());
	}
	// result
	$("#price_total").html(format_price(total));
	$("#price_total_vat").html(format_price(Math.round(total*1.19)));
	$("#saved_total").html(format_price(total0-total));
	$("#disc_total" ).html(100 - Math.round(total / total0 * 100));
}

function format_price(price)
{
	var pricen = price;
	price += '';
	if (price.length>6) price = price.substring(0, price.length-6) + ' ' + price.substring(price.length-6);
	if (price.length>3) price = price.substring(0, price.length-3) + ' ' + price.substring(price.length-3);
	if (pricen>0) {
		return price/* + ',-'*/;
	} else {
		return '&nbsp;';
	}
}

function format_disc(disc)
{
	if (disc>0) {
		return disc + '%';
	} else {
		return '&nbsp;';
	}
}

// inline help functions
function restoreHelp(a, img){
  document.getElementById(img).src = '/img/help.gif';
	var helpUrl = a.href.substring(a.href.indexOf('#')+1);
	$('#'+helpUrl).toggleClass('active');
}
function displayHelp(a, img){
	$('#'+img).attr('src','/img/help-off.gif');
	var helpUrl = a.href.substring(a.href.indexOf('#')+1);
	var pageWrapperOffset = $('#page-wrapper').offset();
	var imgOffset = $('#'+img).offset();
	$('#'+helpUrl).toggleClass('active');
	$('#'+helpUrl).css({
	  left: (imgOffset.left - 250 - pageWrapperOffset.left - 22),
		top: (imgOffset.top - $('#'+helpUrl).height() + 3 )
	});
}
