/**
 * @author pprice
 */
var totals = {
	data_gb: '',
	retention_days: '',
	exchange_server: '',
	exchange_mailboxes: '',
	exchange_mailboxes_num: '',
	continuous_protection: '',
	microsoft_sql: ''
};

jQuery(document).ready(function(){
	$("#wrapper").css('padding', '20px').corner('cc:#333');
	$(".information_icon").click(function(){
		$(this).next().toggle();
	});
	
	$(".slider").each(function(){
		var slider_min = parseInt($(this).attr('min'));
		var slider_max = parseInt($(this).attr('max'));
		var slider_default = parseInt($(this).attr('default'));
		var slider_step = parseInt($(this).attr('step'));
		var slider_id = $(this).parent().attr("id");
		
		$(this).find('.bar').slider({
			max: slider_max,
			min: slider_min,
			value: slider_default,
			step: slider_step,
			slide: function(event, ui){
				setAmmountValue($(this).parent().parent(), ui.value);
			},
			change: function(event, ui){
				setAmmountValue($(this).parent().parent(), ui.value);
			},
			start: function(event, ui){
				setAmmountValue($(this).parent().parent(), ui.value);
			},
			stop: function(event, ui){
				setAmmountValue($(this).parent().parent(), ui.value);
			}
		});
		
		setAmmountValue($(this).parent(), slider_default);
	});
	
	$("input[type=checkbox]").click(function(){ recalculateTotals(); });
	$("input[name=exchange_mailboxes_num]").keyup(function(){ recalculateTotals(); });
	
	$("input[name=getBackup]").click(function(){
		window.opener.focus();
		window.close();
		window.opener.getBackup(calculateRef());
	});
	
	$("input[name=getTrial]").click(function(){
		window.opener.focus();
		window.close();
		window.opener.getTrial();
	});
});

function setAmmountValue(element, value){
	if ((value + '').slice(-6, -1) == '99999')
        value = Math.ceil(value);
		
	$('.ammount', element).text(value);
	recalculateTotals();
}

function recalculateTotals(){
	totals.data_gb = $('#data_gb .slider .bar').slider('option', 'value');
	totals.retention_days = $('#retention_days .slider .bar').slider('option', 'value');
	totals.exchange_server = $("#addons #exchange_server input[name=exchange_server]:checked").val();
	totals.exchange_mailboxes = $("#addons #exchange_mailboxes input[name=exchange_mailboxes]:checked").val();
	totals.exchange_mailboxes_num = $("#addons #exchange_mailboxes input[name=exchange_mailboxes_num]").val();
	totals.continuous_protection = $("#addons #continuous_protection input[name=continuous_protection]:checked").val();
	totals.microsoft_sql = $("#addons #microsoft_sql input[name=microsoft_sql]:checked").val();
		
	var total = Math.ceil((totals.data_gb * 2.50) * (totals.retention_days / 40));
	if(totals.exchange_mailboxes == "on") total += Math.ceil(totals.exchange_mailboxes_num);
	if(totals.continuous_protection == "on") total += 20;
	
	$("#total .value").text(total);
}

function calculateRef(){
	var qRef = totals.data_gb + 'R' + totals.retention_days;

	if(totals.exchange_server == 'on') qRef = qRef + 'E';
	if(totals.exchange_mailboxes == 'on') qRef = qRef + 'M' + totals.exchange_mailboxes_num;
	if(totals.continuous_protection == 'on') qRef = qRef + 'C';
	if(totals.microsoft_sql == 'on') qRef = qRef + 'S';
	
	return qRef;
}

