// JQuery
var $jq = jQuery.noConflict();

$jq(document).ready(function(){
	 /*
	  * RoudCorner generatoren:
	  * 	http://www.roundedcornr.com/rc5.php
	  * 	http://www.generateit.net/rounded-corner/index.htm?r=8&fg=DFE0E2&f=jpg&w=300&bg=4E5559&h=200&bc=000000&aa=1&bw=1&submit=Generate+It
	  * 	http://wigflip.com/cornershop/
	  * 	http://spiffybox.com/
	  *
	  *		Button Generator:
	  *			http://www.iwebtoolsonline.com/html-and-css-rounded-corner-button-generator
	  *
	  *		JQ RoundCornders Demo:
	  *			http://www.malsup.com/jquery/corner/
	  * 
	  * Techniken:
	  * 	http://www.cssjuice.com/25-rounded-corners-techniques-with-css/
	  *
	  *	Sonstiges: 
	  *		Async laden von JQPlugins: http://hublog.hubmed.org/archives/001816.html (funkt nur mit timer...)
	  */
	  
	// $jq("a").click(function(event) { alert("Thanks for visiting!"); });
	   
	// Sample f. JQ Round Corners (Backgroundcolor + Border muss gesetzt sein)
	// $jq('#navi-sub').corner();
  
  	
  	$jq(".price").css("color","#ff0000");
  	$jq(".priceselector").css("background-color","00ff00");
  	
  	$jq(".priceselector").click( function(event) 		{ calcSum(); });
  	$jq(".priceselectoredit").change( function(event) 	{ calcSum(); });
  	$jq(".priceselectorcombo").change( function(event) 	{ calcSum(); });
  	$jq("input.eventtypes").click( function(event) 		{ deselectOtherEventTypes(); hideOtherEventTypes(); });
  	$jq("#debuginfo").click(function() 					{ toggleDebugInfos(); })

	toggleDebugInfos();
  	calcSum();
  	deselectOtherEventTypes();
  	hideOtherEventTypes();
  	
});

function toggleDebugInfos() {
	$jq(".debugoutput").toggle();
	}
	
function calcSum() {
	var sum = 0.0;
	$jq('.priceselector').each(function(index, obj) {
			if(obj.checked) {
				var price_text = $jq(obj).nextAll(".price").first().text();
				var price = parseFloat(price_text);
				//alert(price_text);
				//alert(price);
				sum += price;
				}
		});
	
	$jq('.priceselectoredit').each(function(index, obj) {
		var price_text = $jq(obj).nextAll(".price").first().text();
		var price = parseFloat(price_text);
		sum += price * $jq(obj).val();
	});

	$jq('.priceselectorcombo').each(function(index, obj) {
		var price_text = $jq(obj).nextAll(".price").first().text();
		var price = parseFloat(price_text);
		var value = trim($jq(obj).val()).length > 0 ? 1 : 0;
		sum += price * value;
	});
	
	//sum = (Math.round(sum * 100.0) );
	
	$jq("#calcresult").text(sum);
	$jq("#complete").val(sum);
	$jq("input[name$='tx_mmsportevent_pi1[complete]']").val(sum);
}

function deselectOtherEventTypes() {
	$jq('input.eventtypes').each(function(index, obj) {
			if(!obj.checked) {
				$jq(obj).siblings().find('.priceselector').each(function(index2,child) {
					child.checked = false;
					//$jq(child).hide();
					});
					
				$jq(obj).siblings().find('.priceselectoredit').each(function(index, obj) {
					$jq(obj).val('');
				});

				$jq(obj).siblings().find('.priceselectorcombo').each(function(index, obj) {
					$jq(obj).val('');
				});
				
				calcSum();
				}
			else $jq(obj).siblings().find('.priceselector').show(); 
		});
	}	
	
function hideOtherEventTypes() {
	//return;
	$jq('input.eventtypes').each(function(index, obj) {
			//if(!obj.checked) $jq(obj).nextAll('.mmffcheckblock').hide();
			//else $jq(obj).nextAll('.mmffcheckblock').show();
			showOrHideBlock(obj,'.mmffcheckblock');
			showOrHideBlock(obj,'.mmffradioblock');
			showOrHideBlock(obj,'.mmffeditblock');
			showOrHideBlock(obj,'.mmffcomboblock');
			
		});
}

function showOrHideBlock(obj,blockstyle) {
	if(!obj.checked) $jq(obj).nextAll(blockstyle).hide();
	else $jq(obj).nextAll(blockstyle).show(); 
}

function trim(value) {
	    value = value.replace(/^\s+/, '');
	    return value.replace(/\s+$/, '');
}

function openDescription(obj) {
	$jq(obj).closest('.options').attr('border','solid 1px #ff0000');
	var description = $jq(obj).closest('.options').children('.description').html();
	$jq(obj).jDialog({
	        content : description,
	        width : 400
	      });	
}




 
