$(function(){
	//we init an array we will use to calculate stuff with :)
	var selected_options = [];
	var vat = 0;	/*vat percentage*/
	var starting_price = parseInt($("#pre-desing-website_txt strong b").text(),10);	
	
	$('#modules li').hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	}).click(function(e){
		var $li = $(this);
		var txt = $(this).find("input");
		var arrayKey = $li.find('div').attr('className');		
			if($li.hasClass("selected")){
				$li.removeClass("selected");
				txt.attr("value",0);
				selected_options[arrayKey] = 0;
				$('#'+txt.attr('name')+'_txt').addClass('hide');
			}else{
				$li.addClass("selected");
				txt.attr("value",1);
				selected_options[arrayKey] = parseInt($li.find('strong').text(),10);
				$('#'+txt.attr('name')+'_txt').removeClass('hide');
			}
			updateDisplay();
			e.stopPropagation();
			e.preventDefault();
	});
	$("#options li a").click(function(e){
		var $a = $(this);
		//debugger;
		var txt2 = $(this).parent("li").find("input");
		var li2 = $(this).parent("li");
		var arrayKey = txt2.attr('name');
			if($a.hasClass("selected")){
				$a.removeClass("selected");
				txt2.attr("value",0);
				$('#'+txt2.attr('name')+'_txt').addClass('hide');
				selected_options[arrayKey] = 0;
			}else{
				$a.addClass("selected");
				txt2.attr("value",1);
				$('#'+txt2.attr('name')+'_txt').removeClass('hide');
				selected_options[arrayKey] = parseInt(li2.find('strong').text(),10);
			}		
		updateDisplay();
		e.stopPropagation();
		e.preventDefault();	
	});
	$("#options li.pages input").change(function(e){
		var price_per_page = $("#price_per_page").text()-0;
		var nr_pages = $(this).val();
		var aditional_pages_price = nr_pages * price_per_page;
		var arrayKey = $(this).attr("name");
		if(nr_pages>0){
			$('#custom-pages_txt').removeClass('hide');
			$('#custom-pages_txt b').text(aditional_pages_price);
			selected_options[arrayKey] = parseInt(aditional_pages_price,10);
		}else{
			$('#custom-pages_txt').addClass('hide');
			selected_options[arrayKey] = 0;
		}
		updateDisplay();
	});	
	
	function updateDisplay(){
		var total = starting_price;
		for(var key in selected_options){
			if(selected_options.hasOwnProperty(key)){
				total += selected_options[key];
			}
		}
		//add VAT
		$('#total').text(total);
		var current_vat = total*vat /100;
		$('#gstat b').text(
			(current_vat).toFixed(2)
		)
		$('#grand_total').text((total+current_vat).toFixed(2));
	};
});