var pids = new Array;
var cids = new Array;
var currency;
var currencyData;
var prices;

function updatePrices() {
	
	$(".price, .pr_pop").each(function(){
		id = $(this).attr("pid");
		
		cycle = "monthly";
		if ($(this).attr("cycle") != undefined)
			cycle = $(this).attr("cycle");
		
		try {
			//monthPrice = prices.product[id].monthly;
			periodPrice = prices.product[id][cycle];
			html  = "<span>"+currencyData.prefix + periodPrice.int + "</span>";
			html += "\n<sup>" + periodPrice.float +" <b> " + currencyData.suffix + "</b></sup>";
			$(this).html(html);
		} catch (e) { }
	});
	
	
	$(".conf_price").each(function(){
		pid = $(this).attr("pid");
		if (pid && prices.configoptions[pid]) {
//			$(this).html(prices.configoptions[pid].monthly.raw);
            $(this).html(currencyData.prefix + prices.configoptions[pid].monthly.raw + currencyData.suffix);
		}
	});
	
	$(".detail_annually_price").each(function(){
		pid = $(this).attr("pid");
		sep = '<br />';
		if (currencyData.code == 'USD') sep = '&nbsp;';
		if (prices.product[pid]) {
			price = prices.product[pid].annually;
			mprice = Math.round(price.raw/12, 2);
			$(this).html(currencyData.prefix+price.raw+currencyData.suffix+sep+"("+currencyData.prefix+mprice+currencyData.suffix+"/месяц)");
		}
	});
	$(".detail_semiannually_price").each(function(){
		pid = $(this).attr("pid");
		sep = '<br />';
		if (currencyData.code == 'USD') sep = '&nbsp;';
		if (prices.product[pid]) {
			price = prices.product[pid].semiannually;
			mprice = Math.round(price.raw/6, 2);
			$(this).html(currencyData.prefix+price.raw+currencyData.suffix+sep+"("+currencyData.prefix+mprice+currencyData.suffix+"/месяц)");
		}
	});
	$(".detail_monthly_price").each(function(){
		pid = $(this).attr("pid");
		if (prices.product[pid]) {
			price = prices.product[pid].monthly;
			$(this).html(currencyData.prefix+price.raw+currencyData.suffix+"/месяц");
		}
	});
}

$(document).ready(function(){
	$(".price, .pr_pop").each(function(){
		pids.push($(this).attr("pid"));
	});
	$(".conf_price").each(function(){
		cids.push($(this).attr("pid"));
	});
	
	$("#langSelect").change(function(){
		currency = $(this).val().toUpperCase();
		
		$.get("/", {a: 'ch_currency', currency: currency, type: "json"},
		function(data) {
			prices = data.prices;
			currencyData = data.currency;
			updatePrices();
		}, 'json');
		
	});
});

