//default DOM set of calculator elements
$(document).ready(function (){
	$('td.arrow0').mousedown(function()
	{
		var listID=this.id.substr(4);
		if($("div#list" + listID).css("display")=="none") $("div#list" + listID).slideDown(200);
		else $("div#list" + listID).slideUp(200);
	});	
	
	$('td.items0').mousedown(function()
	{
		var listID=this.id.substr(1);
		if($("div#list" + listID).css("display")=="none") $("div#list" + listID).slideDown(200);
		else $("div#list" + listID).slideUp(200);
	});
	
	$('td.arrow').mousedown(function()
	{
		var listID=this.id.substr(4);
		if($("div#list" + listID).css("display")=="none") $("div#list" + listID).slideDown(200);
		else $("div#list" + listID).slideUp(200);
	});	
	
	$('td.items').mousedown(function()
	{
		var listID=this.id.substr(1);
		if($("div#list" + listID).css("display")=="none") $("div#list" + listID).slideDown(200);
		else $("div#list" + listID).slideUp(200);
	});

	$('div.subtest').mouseleave(function(){
      $(this).slideUp(200);
    });
	
	// hover for Fing IE
	$('div.subtest > span').mouseover(function(){
	  this.className="over";
	});
	
	$('div.subtest > span').mouseout(function(){
	  this.className="";
	});

	$('div.subtest > span').click(function(){
			var optionsDivInVisible = $(this).parent("div.subtest");
			optionsDivInVisible.css("display","none"); // hide the list

			var newname = $(this).text(); // name of new added service from span click
			var newprice = $(this).attr('title'); // amount of the selected service
			var totalamount = $('span#price').attr('title'); // total amount (default)
			var whathasbeen = optionsDivInVisible.parent("div.select").parent("td.tdstyle").children("div.whathasbeenselected");
			var exists = false;

			if(whathasbeen.find("span.newname").eq(0).text()!=''){
				whathasbeen.find("span.newname").each(function(){
			   		if($(this).text() == newname && $(this).attr('title') == newprice || newprice=='0') {
		        		exists = true;
		        	}
		    	});				
		    }
		    
		    if(!exists && newprice!='0'){
		    	whathasbeen.append('<span class="newname" title="'+newprice+'">'+newname+'</span>');
				whathasbeen.append('<span class="newprice">$'+newprice+'</span>');
				whathasbeen.append('<span class="newclose" onclick="closeit(this);"><img border="0" vspace="0" hspace="0" alt="" src="images/cross2.gif" /></span>');				
				whathasbeen.show();
				
				var calculatedprice=new Number(parseFloat(totalamount)+parseFloat(newprice));
				recalculate(calculatedprice);
		    }
	});
	
	$('div.subtest2').mouseleave(function(){
      $(this).slideUp(200);
    });
	
	// hover for Fing IE
	$('div.subtest2 > span').mouseover(function(){
	  this.className="over";
	});
	
	$('div.subtest2 > span').mouseout(function(){
	  this.className="";
	});

	// if we change type of template we must rebuild DOM for calculator so that rebuildDOM() is calling
	$('div.subtest2 > span').click(function(){
		var optionsDivInVisible = $(this).parent("div.subtest2");
		var textOption = $(this).text(); // name of the selected service
		var inputInSelect = optionsDivInVisible.parent("div.select2").find("td.items0").eq(0);
		
		inputInSelect.text(textOption);
		optionsDivInVisible.css("display","none");
		
		var dataString = 'type='+$(this).attr('title');
			
		$.ajax({
	      type: "POST",
	      url: "select.php",
	      data: dataString,
	      error:function(){
		    $('table#selectlist').hide();
	    	$('table#selectlist').html('');
	      },
	      success: function(str) {
		    $('table#selectlist').show();
		    $('table#selectlist').html(str);
			rebuildDOM(); //initialize jQuery stuff
				$('tr#offer').hide();
				$('td#order').css('backgroundImage','url(/images/calc_bottom1.gif)');
				$('span#price').attr('title','0');
				$('span#price').text('$0');
				$('span#discount_amount').text('$0');
				$('div#discount_div').hide();					
				$('span#totalprice').attr('title','0');
				$('span#totalprice').text('$0');			
	      }
	    });
	});	
});

// function to clean up already added services
function closeit(id){
	var outprice = $(id).prev('span.newprice').prev('span.newname').attr('title');
	var outname = $(id).prev('span.newprice').prev('span.newname').text();
	
	// get the parent div of the span
	var whathasbeen = $(id).parent("div.whathasbeenselected");
	whathasbeen.find("span.newname").each(function(){
		if($(this).text() == outname && $(this).attr('title') == outprice) {
			 $(this).next('span.newprice').next('span.newclose').remove();
			 $(this).next('span.newprice').remove();
			 $(this).remove();
	   	}
	});
	
		
	//rest the part is for recalculation
	var totalamount = $('span#price').attr('title'); // total amount (default)
	var calculatedprice=new Number(parseFloat(totalamount)-parseFloat(outprice));
	recalculate(calculatedprice);
}

function recalculate(calculatedprice) {
	//here the real job for the special offer thing
	if(calculatedprice >= 300){ // ATTENTION! TURN BACK to 300! HERE 
		$('tr#offer').show();
		$('td#order').css('backgroundImage','url(/images/calc_bottom2.gif)');
	}else{
		$('tr#offer').hide();
		$('td#order').css('backgroundImage','url(/images/calc_bottom1.gif)');
	}

	$('span#price').attr('title',calculatedprice.toFixed(2));
	$('span#price').text('$'+calculatedprice.toFixed(2));
								
	//here the real job for discount thing
	if(calculatedprice >= 800){
		var discount = $('input#discount').val();
		var discount_amount = new Number(calculatedprice*parseFloat(discount)/100);
		calculatedprice = new Number(calculatedprice-discount_amount);
		$('span#discount_amount').text('$'+discount_amount);
		$('div#discount_div').show();
	}else{
		$('div#discount_div').hide();					
	}
				
	$('span#totalprice').attr('title',calculatedprice.toFixed(2));
	$('span#totalprice').text('$'+calculatedprice.toFixed(2));
}

function rebuildDOM(){
	$('td.arrow').mousedown(function()
	{
		var listID=this.id.substr(4);
		if($("div#list" + listID).css("display")=="none") $("div#list" + listID).slideDown(200);
		else $("div#list" + listID).slideUp(200);
	});	
	
	$('td.items').mousedown(function()
	{
		var listID=this.id.substr(1);
		if($("div#list" + listID).css("display")=="none") $("div#list" + listID).slideDown(200);
		else $("div#list" + listID).slideUp(200);
	});

	$('div.subtest').mouseleave(function(){
      $(this).slideUp(200);
    });
	
	// hover for Fing IE
	$('div.subtest > span').mouseover(function(){
	  this.className="over";
	});
	
	$('div.subtest > span').mouseout(function(){
	  this.className="";
	});

	$('div.subtest > span').click(function(){
			var optionsDivInVisible = $(this).parent("div.subtest");
			optionsDivInVisible.css("display","none"); // hide the list
			
			var newname = $(this).text(); // name of new added service from span click
			var newprice = $(this).attr('title'); // amount of the selected service
			var totalamount = $('span#price').attr('title'); // total amount (default)
			var whathasbeen = optionsDivInVisible.parent("div.select").parent("td.tdstyle").children("div.whathasbeenselected");
			var exists = false;

			if(whathasbeen.find("span.newname").eq(0).text()!=''){
				whathasbeen.find("span.newname").each(function(){
			   		if($(this).text() == newname && $(this).attr('title') == newprice || newprice=='0') {
		        		exists = true;
		        	}
		    	});				
		    }
		    
		    if(!exists && newprice!='0'){
		    	whathasbeen.append('<span class="newname" title="'+newprice+'">'+newname+'</span>');
				whathasbeen.append('<span class="newprice">$'+newprice+'</span>');
				whathasbeen.append('<span class="newclose" onclick="closeit(this);"><img border="0" vspace="0" hspace="0" alt="" src="images/cross2.gif" /></span>');				
				whathasbeen.show();
				
				var calculatedprice=new Number(parseFloat(totalamount)+parseFloat(newprice));
				recalculate(calculatedprice);
		    }		
	});
}

function doOrder(){
  $('input.inputs').css({backgroundColor:"#FFFFFF"});
  $('input.inputs').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.inputs').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

	var type = $('td.items0').text();

	var services = '';  
    $('div.whathasbeenselected').each(function(idx, item) {  
    	services +='serv'+ idx + '=';
    	$(this).find("span.newname").each(function(){
			   		services += escape($(this).text()) + '<br>';
		});	
		services += '&';
    });
    
	
	var discount = $('span#discount_amount').text();
	var price = $('span#price').attr('title');
	var totalprice = $('span#totalprice').attr('title');
	
	var order_name = $('input#order_name').val();
	if (order_name == "") { $("input#order_name").focus(); return false; }
	var order_email = $('input#order_email').val();
	var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if (!pattern.test(order_email)){ $("input#order_email").focus(); return false; }
	var item = $('input#item').val();
	
	
	var dataString = "type="+ type + "&"+ services + "discount=" + discount + "&totalprice=" + totalprice + "&price=" + price + "&item=" + item + "&order_name=" + order_name + "&order_email=" + order_email;

	$.ajax({
      type: "POST",
      url: "order.php",
      data: dataString,
      success: function(r) {
	    $('div.paypal').html("")  
	    .append(r)
	    .hide()  
	    .fadeIn(1500, function(){}); 
	    
      }
     });
    return false;
}


