jQuery(document).ready(function() {

    if (jQuery('#vymera').length) {
	var vymera = jQuery('#vymera').html();
	jQuery('#vymera').html(' ');
	$('input[name=iQuantity]').next().after('<br /><button type="button" style="margin-top:3px;" onclick="vymeraCalculator(' + vymera +')" class="submit">Výpočet množství</button>');
    }

});

function vymeraCalculator(vymera) {

	var formstr = '<p><h1>Výpočet potřebného množství balíků podlahy</h1></p>'+
	'<p>POZOR!!! Výpočet je platný pro tento konkrétní produkt. Pro jiný se může lišit.</p>'+
	    '<div class="field"><label for="plocha">Zadejte Vaši plochu podlahy včetně rezervy</label><input type="text" name="plocha" id="plocha" value="" /> m2</div>';

		jqistates = {
		    state0: {
			html: formstr,
			focus: 1,
			buttons: { Zrušit: false, Výpočet: true },
			submit: function(v, m, f){
			    var e = "";
			    m.find('.errorBlock').hide('fast',function(){ jQuery(this).remove(); });
			    
			    if (v) {
				
				if(isNaN(f.plocha))
				    e += "Prosím vložte číslo zaokrouhlené na celé nahoru (bez tečky nebo čárky).<br />";
				    
				
				if (e == "") {
								    
				    var mnozstvi = Math.ceil(f.plocha/vymera);
				    jQuery.prompt.getStateContent('state1').find('#intmonthlypayment').text(mnozstvi);									
				    jQuery.prompt.goToState('state1');
				    
				}
				else{
				    jQuery('<div class="errorBlock" style="display: none;">'+ e +'</div>').prependTo(m).show('slow');
				}
				return false;
			    }
			    else return true;
			}
		    },
		    state1: {
			html: 'Potřebné množství balíků podlahy: <span id="intmonthlypayment"></span> ks',
			focus: 1,
			buttons: { Zpět: false, Hotovo: true },
			submit: function(v,m,f){
			    if(v)
				return true;
				
			    jQuery.prompt.goToState('state0');
			    return false;
			}
		    }
		};
		$.prompt(jqistates);


    
}



/*

function openLoanCalculatorPrompt(o){
		o = jQuery.extend({},{ amount:100000, down: 1500, years:15, rate:5 },o);
		
		    
		
	    }
}
**/
