/******************************************************************************
Name:    SiMa Offercalculator JS
Version: 2.0 (Dezember 2009)
Author:  Michael Preis @ SiMa Design
Support: http://www.simadesign.de
******************************************************************************/

jQuery.noConflict();


/**********************
Main Function. AjaxCheck. Pricecalculations. Requirements
*********************/
			
function ajaxupdate(moduleid,areaid,checked,lastcheckdradio) {
	
	jQuery.ajax({
		url: "index.php",  // get the url
		type: "POST",   // transfer data per post
		data: {
			eID: "tx_simaoffercalculator_eId",	// eid
			moduleid: moduleid,	// moduleid
			areaid: areaid,   // areaid
			checked: checked,  // checked
			lastcheckdradio: lastcheckdradio // last checked radio button id
		},
		dataType: "json",   // response data per json
		success: function(response) {  

			// if success calculate the price	
			areaid = '#' + response.areaid;
			var price = jQuery(areaid).children('div.price').children('div.pricecontent').children('div.pricecount').children('span.singleprice').html() *1;
			
			// deside if the price goes up or down
			if (response.checked == 'false') {
				addprice = response.price *1;
				price =  price - addprice;								
			}
			else
			{
				// check if there is an old price from a radiobutton and remove it
				if (response.oldprice > 0)
				{ price = price - response.oldprice; }
				
				// add the new price to the price field
				addprice = response.price *1;
				price = price + addprice;
			}
			
			// update the pricefield
			jQuery(areaid).children('div.price').children('div.pricecontent').children('div.pricecount').children('span.singleprice').html(price);


			// check if there are any old requirement linkings
			if (response.oldrequirement)
			{
				jQuery.each(response.oldrequirement, function(i, oldrequirement) {

					// get the actual requirement parameter
					var oldparameter = jQuery("input.requirement[name='" + oldrequirement + "']").val();

					var oldparameter = oldparameter * 1;
					var oldrequirementuid = response.oldrequirementcount[oldrequirement] * 1;
					var oldrequirementprice = response.oldrequirementprice[oldrequirement];

					var oldparametercalculation = oldparameter + oldrequirementuid;
					
					// load function to remove checks
					enableordisable(oldrequirement,oldrequirementprice,oldparametercalculation);
				
				});
				
			}

			// check if there any requirement linkings	
			if (response.requirements)
			{
				jQuery.each(response.requirements, function(i, requirement) {
					
					// get the actual requirement parameter
					var parameter = jQuery("input.requirement[name='" + requirement + "']").val();
					
					var parameter = parameter * 1;
					var requirementuid = response.requirementcount[requirement] * 1;
					
					// check if the checkbox is checked *lol*
					if (response.checked == 'true') { 
						if( parameter > 0 )
						{ var parametercalculation = parameter - requirementuid; }
						else
						{ var parametercalculation = 0;	}
						
						var requirementprice = '';
					}
					else if(response.checked == 'rechecked')
					{
						var parametercalculation = parameter; 
						var requirementprice = response.requirementprice[requirement];
					}
					else
					{ 
						var parametercalculation = parameter + requirementuid; 
						var requirementprice = response.requirementprice[requirement];
					}
						
					enableordisable(requirement,requirementprice,parametercalculation);
					
				});
			}
			
			// set the totalprice
			var totalprice = '0' *1;
			
			jQuery("div.simaoffercalculator_title div.pricecount span.singleprice").each(function() {
				prices = jQuery(this).html() * 1;
				totalprice = prices + totalprice;
			});
			
			jQuery('div.tx-simaoffercalculator-pi1 div.totalprice span.totalprices').html(totalprice);

		},
		error: function(error) { // if data transfer false get a response
			alert("Es ist ein Fehler aufgetreten.");
		},
		complete: function(complete) {
			//jQuery("div#loadingicon").remove();
			jQuery('div.loadingicon').html('');
		}
	});
}


/**********************
Enable / Disable Checkboxes, Radiobuttons and text 
**********************/

function enableordisable(requirement,requirementprice,parametercalculation) {

	// change the value parameter of the hidden-key to enable or disable the checkbox
	jQuery("input.requirement[name='" + requirement + "']").val(parametercalculation);
					
	// enable or disable the checkbox
	if (parametercalculation <= 0)
	{
		jQuery("input#" + requirement).removeAttr("disabled");
		jQuery("input#" + requirement).parent("div").removeClass("disablefield");
	}
	else
	{
		jQuery("input#" + requirement).attr('disabled', true);

		jQuery("input#" + requirement).parent("div").addClass("disablefield");
		
			
		jQuery("input[@id=selector]:checked")
		
		if (jQuery("input[id="+requirement+"]:checked").length > 0)
		{ 
			
			jQuery("input#" + requirement).attr('checked', false);
			
			var requirementprice =  requirementprice *1;
			
			// get the actual price
			var price = jQuery("input#" + requirement).parents("div.simaoffercalculator_title").children('div.price').children('div.pricecontent').children('div.pricecount').children('span.singleprice').html() *1;
			
			var price = price - requirementprice;
			
			// update the pricefield
			jQuery("input#" + requirement).parents("div.simaoffercalculator_title").children('div.price').children('div.pricecontent').children('div.pricecount').children('span.singleprice').html(price);
			
		} 
		else
		{}

	}
	
}


/**********************
Ajax Recheck after Reload 
**********************/

function ajaxrecheck() {

	// create the vars
	var areaid;
	var moduleid;
	var checked;

	jQuery('input').each(function() {
	
		if (this.checked == true) { 
					
			// add a marker to know which radiobutton is checked
			jQuery(this+":radio:checked").addClass("checked");
			
			var moduleid = this.id;
			var areaid = jQuery(this).parents('div.simaoffercalculator_title').attr("id");

			// check if the field is allready checked
			if ( this.type == "radio" ){ 
				var checked = 'true';
			}
			else
			{ var checked = 'rechecked'; }
			
				// var checked = 'true';
			
			ajaxupdate(moduleid,areaid,checked,'');
		
		}
	
	});

}


/**********************  
Loadinganimation 
**********************/

function loadpage(loadingid) {
	// update the pricefield
	jQuery("input#" + loadingid).parents("div.simaoffercalculator_title").children('div.price').children('div.pricecontent').children('div.loadingicon').html('<img src="typo3conf/ext/sima_offercalculator/res/img/ajax-loader.gif">');
}


/**********************  
Action after Loadpage - Click function 
**********************/

jQuery(document).ready(function() {
	
	// create the vars
	var price;
	var areaid;
	var moduleid;
	var checked;
	
	// after Reload get the actual informations
	ajaxrecheck();
	
	// Information-popup
	jQuery('img.information_icon').mouseover(function(e){

	var height;

		jQuery(this).next('div.informations').css({display:'block'});
		// get the height of the information block
	//	height = parseFloat(jQuery(this).next('div.informations').css('height'),10) + 8;
		
		var height = jQuery(this).next('div.informations').height() +8;

		// set the position
		
		var x = this.offsetLeft + 8;
		var y = this.offsetTop - height;

		jQuery(this).next('div.informations').css({"left" : x , "top" : y });
		
	});
	jQuery('div.simaoffercalculator_item').mouseout(function(){
		jQuery(this).children('div.informations').css({display:'none'});
	});
	
	// change informations by click on a checkbox

	 jQuery("input").bind((jQuery.browser.msie ? "click" : "change"), function () {

		if(this.id != "submit_button") {
		
			loadpage(this.id);
	
			var lastcheckdradio = '';
			
			// get the id of the last checked radiobox
			jQuery("input[name=" +  this.name +"].checked").each(function(){				   
				jQuery(this).removeClass("checked");
				lastcheckdradio = this.id;
			});
			
			jQuery(this).addClass("checked");
							
			var moduleid = this.id;
			var areaid = jQuery(this).parents('div.simaoffercalculator_title').attr("id");
	
			// check if the field is allready checked
			if (this.checked == true) {	var checked = 'true'; }
			else {	var checked = 'false'; }
	
			ajaxupdate(moduleid,areaid,checked,lastcheckdradio);

		}

	});

});
