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

function updatePrices() {
	
	$(".price").each(function(){
		id = $(this).attr("pid");
		
		try {
			monthPrice = prices.product[id].monthly;
			html  = "<span>"+currencyData.prefix + monthPrice.int + "</span>";
			html += "\n<sup>" + monthPrice.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").each(function(){
		pids.push($(this).attr("pid"));
	});
	$(".conf_price").each(function(){
		cids.push($(this).attr("pid"));
	});
	
	/*
	$.get("/get_price.php", {a: 'getprices', pids: pids, cids: cids},
	function(data) {
		currency = data.currency;
		$("#langSelect").val(currency);
		
		//refresh dropdown
		oHandler = $(".mydds").msDropDown().data("dd");
		oHandler.visible(true);
		
		prices = data;
		updatePrices();
	},"json");
	*/
	
	$("#langSelect").change(function(){
		currency = $(this).val().toUpperCase();
		//alert("currency:" + currency);
		/*$.get("/get_price.php", {a: 'ch_currency', currency: currency},
		function(data) {
			updatePrices();
		});*/
		
		$.get("/", {a: 'ch_currency', currency: currency, type: "json"},
		function(data) {
			prices = data.prices;
			currencyData = data.currency;
			updatePrices();
		}, 'json');
		
	});
});

