function clone(obj) {
    if (null == obj || "object" != typeof obj) return obj;
    var copy = obj.constructor();
    for (var attr in obj) {
        if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
    }
    return copy;
}

var minicalc = clone(calc);
minicalc.loadDepositParams = function(deposit){

	this.current_deposit = deposit;
	this.currency_list = [];
	for(i in deposit.deposites){
		this.currency_list.push(i);
	}
	var days = this.days = Math.round((deposit.max_d-deposit.min_d)/3+deposit.min_d);
	var count = 0;

	this.cashing_type = false;
	this.min_days = deposit.min_d;
	this.max_days = deposit.max_d;
	this.begin_date= new Date();
	this.end_date= meDate.addDays2Date(this.begin_date,days);
	if(deposit.capitalization_deposit)
		this.cashing_type = 'capitalization_deposit';
	else
		this.cashing_type = 'mounth_cashing';


	$("#duration_amount").change(function(){
		
		var new_days = parseInt($(this).val());
		if (new_days < deposit.min_d)
			new_days = deposit.min_d;
		else if(new_days > deposit.max_d)
			new_days = deposit.max_d;
		$(this).val(new_days);
		var days = new_days;
		minicalc.writeNewValues(minicalc.sum,days);
	});
	$('#depo_currency_up').on('click',function(){
		if(minicalc.currency_list.length > 1){
			var cur = meIndexOf(minicalc.currency_list,minicalc.currency);
			minicalc.loadNewCurrency(cur?'RUR':'EUR_USD');
			$('#depo_currency').text(cur?'RUR':'USD');
		}
	});
	$('#depo_currency_down').on('click',function(){
		if(minicalc.currency_list.length > 1){
			var cur = meIndexOf(minicalc.currency_list,minicalc.currency);
			minicalc.loadNewCurrency(cur?'RUR':'EUR_USD');
			$('#depo_currency').text(cur?'RUR':'USD');
		}
	});
	for(i in deposit.deposites){
		//$('#depo_currency').append($('<option></option>').val(i).html(deposit.deposites[i].currency_name));

		if(!count){
			this.currency = i;

			//---amount slider initialization---//
			this.loadNewCurrency(i);
			//-------------end------------------//
			count++;

		}
	}
};
minicalc.loadDB = function(depo_db){
	this.depo_db = depo_db;
};
minicalc.writeNewValues = function(sum,days){
	this.days = days;
	this.end_date= meDate.addDays2Date(this.begin_date,this.days);
	this.sum = sum;
	var percent = this.percent = this.calcPercent(sum,days);

	var contributionWithPercent = false; 
	switch(this.cashing_type) {
		case 'mounth_cashing': 
		case 'end_cashing':
		case 'minimal_balance':
			contributionWithPercent = this.calcContributionMounthCashing(sum,this.percent,days);
			break;
		case 'capitalization_deposit':
			contributionWithPercent = this.calcCap(sum,this.percent,days);
			break;
	}

	var summaryIncome = this.calcProfit(contributionWithPercent,sum);
	var mounthProfit = this.calcMounthProfit(summaryIncome,days);                                                     
	$("#summary_income").text(Math.round(summaryIncome) + this.currency_short_names[this.currency]);

	$("#summ_amount" ).val(sum);

	$( "#duration_amount" ).val(days);

	$("#percents").val(minicalc.percent.toFixed(2)+ '%');
};
minicalc.loadNewCurrency = function(currency){
	deposit = this.current_deposit;
	this.currency = currency;
	this.loadTables();
	var sum = this.sum = Math.round((deposit.deposites[currency].max_summ - deposit.deposites[currency].min_summ)/3+deposit.deposites[currency].min_summ);

	$("#summ_amount").val(sum);
	$("#summ_amount").change(function(){
		var new_sum = parseInt($(this).val());
		if (new_sum < deposit.deposites[currency].min_summ)
			new_sum = deposit.deposites[currency].min_summ;
		else if(new_sum > deposit.deposites[currency].max_summ)
			new_sum = deposit.deposites[currency].max_summ;
		$(this).val(new_sum);
		var sum = minicalc.sum = new_sum;
		var days = minicalc.days;
		minicalc.writeNewValues(sum,days);
	});
	this.writeNewValues(sum,this.days);
};
$(document).ready(function(){
	var vkl_name = $(".minicalc_widget").data("name");
	$(".minicalc_widget").html("<div class='vklad_calc'><img src='/nn/css/images/vkl/"+vkl_name+".png' style='z-index:0;'/>"+
		"<div class='mini_calc'><img src='/css/images/minicalc.png' />"+
		"<input id='summ_amount' class='summ_amount' type='text' /><input id='duration_amount' class='duration_amount' type='text' />"+
		"<div id='summary_income' class='summary_income'>41265 руб.</div>"+
		"<input id='percents' class='percents' type='text' value='8.57%' disabled='disabled'><span class='depo_currency' id='depo_currency'>RUR</span>"+
		"<span id='depo_currency_up' class='depo_currency_up'></span><span id='depo_currency_down' class='depo_currency_down'></span><button id='calc_submit' class='calc_submit' style='z-index:200;'/></div></div>");
	
	minicalc.loadDB(calc_db);

	minicalc.loadDepositParams(calc_db[vkl_name]);

	$('#calc_submit').on('click',function(){
		var params={
					depo_name:vkl_name,
					currency:minicalc.currency, 
				  	days:minicalc.days,
				  	cashing_type:minicalc.cashing_type,
				    sum:minicalc.sum
				   };
		var hash = minicalc.hashEncode(params);
		window.location.href = "/online/calc.htm"+hash;
	});
}); 
