jQuery(function(){
	// Stufe errechnen
	function varigas_stufe()
	{
		$verbrauch = jQuery('#verbrauch').val();

		if ($verbrauch != '')
		{
			var data = {
				action: 'varigas_stufe',
				verbrauch: $verbrauch,
				leistung: jQuery('#leistung').val()
			};
			
			jQuery.post(ajaxurl, data, function(response) {
				//alert('Got this from the server: ' + response);
				response = response.substring(0,response.length - 2);
				if (jQuery('#verbrauch').val() <= 200000)
					jQuery('#stufe').replaceWith(response);
				else
					jQuery('#stufe').html('<p><strong>Bitte geben Sie einen Wert zwischen 1 und 200.000 kWh an.</strong></p>')
				tooltip();
			});
		
			// Varigas Stufe aktualisieren
			box = jQuery('.varigas');
			id = box.attr('id');
			starif = id.replace('erdgas_', '');
			sstufe = box.find('input[name=varigas_stufe]:checked').val();
			soekogas = box.find('.checkbox').attr('checked');
			sverbrauch = jQuery('#verbrauch').val();

			var data = {
				action: 'shortcode_gas',
				tarif: starif,
				oekogas: soekogas,
				verbrauch: sverbrauch
			};

			jQuery.post(ajaxurl, data, function(response) {
				response = response.substring(0,response.length - 2);
				jQuery('#' + id).replaceWith(response);
				tooltip();
			});
		}
	}


	// Tarif vergleichen 
	function varigas_rechner()
	{
		starif = jQuery('#tarif').val();
		sverbrauch = jQuery('#verbrauch').val();
		sstufe = jQuery('#varistufe').val();
		sleistung = jQuery('#leistung').val();
		soekogas = (jQuery('#oekogas').attr('checked') == true) ? 1 : 0;	
	
		if (sleistung < 10)
		{
			sleistung = 10;
			jQuery('#leistung').val('10');
		}
	
		var data = {
			action: 'varigas_rechner',
			tarif: starif,
			leistung: sleistung,
			verbrauch: sverbrauch,
			oekogas: soekogas,
			stufe: sstufe
		};
	
		jQuery.post(ajaxurl, data, function(response) {
			//alert('Got this from the server: ' + response);
			response = response.substring(0,response.length - 2);
			jQuery('.response').html(response);
			jQuery('.response').show('fast');
			tooltip();
		});
	
	};



	// Tarif vergleichen
	function vari_tarifcheck()
	{
		$tarifVal = jQuery('#tarif').val();
		if ($tarifVal)
		{
			if ($tarifVal == 'kleinverbrauchstarif' || $tarifVal == 'gewerbetarif')
			{
				// jQuery('#leistung').val('');
				jQuery('#leistungwrap').hide('fast');
				varigas_rechner();
			}
			else if(($tarifVal == 'standardtarif' || $tarifVal == 'heizgastarif') && $tarifVal == '')
			{
				jQuery('.response').hide('fast');
				jQuery('#leistungwrap').show('fast');
				varigas_rechner();
			}
			else if (($tarifVal == 'standardtarif' || $tarifVal == 'heizgastarif') && $tarifVal != '')
			{
				jQuery('#leistungwrap').show('fast');
				varigas_rechner();
				tooltip();
			}
		}
	}	



	/*=VARI GAS FORM
	======================================*/
	// Get Stufe
	jQuery('#kochgas').click(function(){
		jQuery('#verbrauch').val('1500');
	});
	jQuery('#gas-etagen').click(function(){
		jQuery('#verbrauch').val('15000');
	});
	jQuery('#fam1').click(function(){
		jQuery('#verbrauch').val('30000');
	});
	jQuery('#fam2').click(function(){
		jQuery('#verbrauch').val('49000');
	});
	jQuery('#kochgas, #gas-etagen,#fam1,#fam2,#vari-submit').click(function(){
		varigas_stufe();
		return false;
	});



	// Verbrauch übergeben
	jQuery('a.online').livequery('click', function(){
		$verbrauch = jQuery('#verbrauch').val();
	
		if ($verbrauch == '' || $verbrauch == false)
		{
			$stufe = jQuery('.varigas_stufe:checked').val();
		
			if ($stufe == 'S')
				$verbrauch = '3500';
			else if($stufe == 'M')
				$verbrauch = '14000';
			else if($stufe == 'L')
				$verbrauch = '28000';
			else if($stufe == 'XL')
				$verbrauch = '67500';
			else if($stufe == 'XXL')
				$verbrauch = '150000';
		}
		jQuery(this).attr('href', jQuery(this).attr('href') + '&verbrauch=' + $verbrauch);
	});



	// Toggle Vergleich
	jQuery('#varitoggle').livequery('click',function(){
		jQuery('#gastarif').toggle('slow');
		img = jQuery('#varitoggle img');
		if (img.hasClass('closed'))
		{
			img.attr('src', img.attr('src').replace('closed', 'open'));
			img.addClass('open').removeClass('closed');
		}
		else
		{
			img.attr('src', img.attr('src').replace('open', 'closed'));
			img.addClass('closed').removeClass('open');
		}
	});



	// Tarif ändert sich
	jQuery('#gastarif #tarif, #gastarif #oekogas').livequery('click',function(){
		vari_tarifcheck();
	});



	// Leistung ändert sich
	jQuery('#leistung').livequery('blur',function(){
		varigas_rechner();
	}).livequery('keypress', function(e){
		if (e.keyCode == '13')
			varigas_rechner();
	}).livequery('change',function(){
		varigas_rechner();
	});

	// Leistungserklärung
	jQuery('.leistungs-info').live('click', function(){
		jQuery.prettyPhoto.open(jQuery(this).attr('href'),jQuery(this).attr('title'),jQuery(this).attr('alt'));
		return false;
	});



	jQuery('.erdgastarif input').livequery('click', function(){
		box = jQuery(this).parent().parent().parent().parent().parent().parent();
		id = box.attr('id');
		starif = id.replace('erdgas_', '');
		sstufe = box.find('input[name=varigas_stufe]:checked').val();
		soekogas = box.find('.checkbox').attr('checked');
	
		var data = {
			action: 'shortcode_gas',
			tarif: starif,
			oekogas: soekogas,
			stufe: sstufe
		};
	
		jQuery.post(ajaxurl, data, function(response) {
			//alert('Got this from the server: ' + response);
			response = response.substring(0,response.length - 2);
			jQuery('#' + id).replaceWith(response);
			tooltip();
		});
	});



	jQuery('.erdgastarif2 input').livequery('click', function(){
		box = jQuery(this).parent().parent().parent().parent().parent().parent();
		id = box.attr('id');
		sstufe = box.find('input[name=varigas_stufe]:checked').val();
		if (sstufe)
		{
			jQuery('#stufe').val(sstufe);
	
			var data = {
				action: 'shortcode_gas2',
				tarif: 'vari',
				stufe: sstufe
			};
	
			jQuery.post(ajaxurl, data, function(response) {
				//alert('Got this from the server: ' + response);
				response = response.substring(0,response.length - 2);
				jQuery('#' + id).replaceWith(response);
				tooltip();
			});
		}
	});



	// Shortcode2 Bug Fix
	if (jQuery('.erdgastarif2')) {
		if (jQuery('#step').val() < 2) { 
			sstufe = jQuery('#stufe').val().toLowerCase();
			jQuery('#varigas_stufe'+sstufe).attr('checked', true);			
		}
	}
});
