/**
 * @author pprice
 */
var totals = {
	workstations: '',
	servers: ''
};

var PRICE_PER_WORKSTATION = 8.40;
var PRICE_PER_SERVER = 40;

jQuery(document).ready(function(){
	$('#header .tag').toggle(function(){ $('#whats_included').slideDown(); }, function(){ $('#whats_included').slideUp(); });
	$("label, .information_icon").click(function(){
		$(this).nextAll('.information').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=getSupport]").click(function(){
		recalculateTotals();
		alert("Please call 0845 226 1914 and quote " + $('#ref').text());
//		window.close();
	});

	$("input[name=contactUs]").click(function(){
		window.opener.document.location = '/contact-us';
		window.close();
	});
});

function setAmmountValue(element, value){
	if ((value + '').slice(-6, -1) == '99999')
        value = Math.ceil(value);
		
	$('.ammount', element).text(value);
	recalculateTotals();
}

function recalculateTotals(){
	totals.workstations = $('#workstations .slider .bar').slider('option', 'value');
	totals.servers = $('#servers .slider .bar').slider('option', 'value');

	if(totals.workstations >= 15) { PRICE_PER_WORKSTATION = 8; $('#workstations #discount').html('+ <strong>5%</strong> discount').fadeIn(); }
	if(totals.workstations >= 25) { PRICE_PER_WORKSTATION = 7.60; $('#workstations #discount').html('+ <strong>10%</strong> discount').fadeIn(); }
	if(totals.workstations >= 50) { PRICE_PER_WORKSTATION = 6.40; $('#workstations #discount').html('+ <strong>25%</strong> discount').fadeIn(); }
	if(totals.workstations < 15) { PRICE_PER_WORKSTATION = 8.40; $('#workstations #discount').fadeOut(); }

	var total = Math.ceil((totals.workstations * PRICE_PER_WORKSTATION) + (totals.servers * PRICE_PER_SERVER));

	$("#total .value").text(total);
	calculateRef();
}

function calculateRef(){
	var qRef = totals.workstations + 'W' + totals.servers + 'S-';

	if($('input[name=online_backup]:checked').val() == 'on') qRef += 'O';
	if($('input[name=network_monitoring]:checked').val() == 'on') qRef += 'N';
	if($('input[name=business_continuity_plan]:checked').val() == 'on') qRef += 'B';
	if($('input[name=backup_mail_servers]:checked').val() == 'on') qRef += 'M';

	$('#ref').text(qRef);
}

