YAHOO.namespace("twojekwiaty.container");
var resetPrice;
var setOwnPrice;
document.observe("dom:loaded", function() {
	if ($('prop(_country)')) {
		if ( "PL" == $('prop(_country)').value) 
			$('postCodeSearch').show();			
		else 	
			$('postCodeSearch').hide();
	
		$('prop(_country)').observe('change',function(){
			if ( "PL" == this.value) 
				$('postCodeSearch').show();			
			else 	
				$('postCodeSearch').hide();
		});	
	}
	if (!YAHOO.twojekwiaty.container.wait) {
		// Initialize the temporary Panel to display while waiting for external content to load
		YAHOO.twojekwiaty.container.wait = 
		new YAHOO.widget.Panel("wait",  
			                { width: "240px", 
			                  fixedcenter: true, 
			                  close: false, 
			                  draggable: false, 
			                  zindex:4,
			                  modal: true,
			                  visible: false
			                } 
							);
		YAHOO.twojekwiaty.container.wait.setHeader("");
		YAHOO.twojekwiaty.container.wait.setBody("<img src=\"js/yui/build/rel_interstitial_loading.gif\"/>");
		YAHOO.twojekwiaty.container.wait.render(document.body);
	}
	
	
	
	var Event = YAHOO.util.Event,
	Dom   = YAHOO.util.Dom,
	lang  = YAHOO.lang;
	
	//flower card slider
	var floweCardSlider,
		floweCardSliderBg="flowerCard-bg",
		flowerCardThumb="flowerCard-thumb",
		flowerCardTextfield="slider-converted-value",
		flowerCardValuearea="slider-value";
	
	var floweCardTopConstraint = 0;

	var floweCardBottomConstraint = 314;

	// Custom scale factor for converting the pixel offset into a real value
	var floweCardScaleFactor = 1;

	// The amount the slider moves when the value is changed with the arrow
	// keys
	var floweCardKeyIncrement = 1;

	var floweCardTickSize = 1;
								
        floweCardSlider = YAHOO.widget.Slider.getHorizSlider(floweCardSliderBg, 
                         flowerCardThumb, floweCardTopConstraint, floweCardBottomConstraint);

        // Sliders with ticks can be animated without YAHOO.util.Anim
        floweCardSlider.animate = true;

        floweCardSlider.getRealValue = function() {
            return Math.round(rescale(this.getValue() * scaleFactor));
        }

        floweCardSlider.subscribe("change", function(offsetFromStart) {
            var fld = Dom.get(flowerCardTextfield);
            var valnode = Dom.get(flowerCardValuearea);
						

            // use the scale factor to convert the pixel offset into a real
            // value
            var actualValue = floweCardSlider.getRealValue();

            // update the text box with the actual value
            fld.value = actualValue;
            valnode.innerHTML = actualValue;
            // Update the title attribute on the background.  This helps assistive
            // technology to communicate the state change
            Dom.get(floweCardSliderBg).title = "Aktualna cena: " + actualValue;

        });
        var fld = Dom.get(flowerCardTextfield);
        var v = parseFloat(fld.value, 10);
	v = (lang.isNumber(v)) ? v : 0;
	if (v!=0) {
		floweCardSlider.setValue(rescaleDown(Math.round(v/scaleFactor)));
		var valnode = Dom.get(flowerCardValuearea);
		valnode.innerHTML = rescale(v);
	}
        // Listen for keystrokes on the form field that displays the
        // control's value.  While not provided by default, having a
        // form field with the slider is a good way to help keep your
        // application accessible.
        
        Event.on(flowerCardTextfield, "keydown", function(e) {
            // set the value when the 'return' key is detected
            if (Event.getCharCode(e) === 13) {
                var v = parseFloat(this.value, 10);
                v = (lang.isNumber(v)) ? v : 0;
                // convert the real value into a pixel offset
                floweCardSlider.setValue(Math.round(v/scaleFactor));
            }
        });
        resetPrice = function() {
		var fld = Dom.get(flowerCardTextfield);
		fld.value = "";

	        var valnode = Dom.get(flowerCardValuearea);
	        valnode.innerHTML = "0";
	        setDisplay("proposition-price", false);
	}
	setOwnPrice = function(minimum) {
		var v = parseFloat(minimum, 10);
		v = (lang.isNumber(v)) ? v : 0;
		var fld = Dom.get(flowerCardTextfield);
		fld.value = minimum;
		var valnode = Dom.get(flowerCardValuearea);
		valnode.innerHTML = minimum;
		setDisplay("proposition-price", true);
		floweCardSlider.setValue(rescaleDown(Math.round(v/scaleFactor)));
	}

	if (v == 0) {
		setDisplay("proposition-price", false);
	}else {
		$('price-last').checked = true;
		setDisplay("proposition-price", true);
	}
	
	// Can't decide slider
	var slider, 
	bg="slider-bg", thumb="slider-thumb", 
	valuearea="slider-value",textfield="slider-converted-value"
	// The slider can move 0 pixels up
	var topConstraint = 0;

	var bottomConstraint = 340;

	// Custom scale factor for converting the pixel offset into a real value
	var scaleFactor = 1;

	// The amount the slider moves when the value is changed with the arrow
	// keys
	var keyIncrement = 1;

	var tickSize = 1;
								
        slider = YAHOO.widget.Slider.getHorizSlider(bg, 
                         thumb, topConstraint, bottomConstraint);

        // Sliders with ticks can be animated without YAHOO.util.Anim
        slider.animate = true;

        slider.getRealValue = function() {
            return Math.round(rescale(this.getValue() * scaleFactor));
        }

        slider.subscribe("change", function(offsetFromStart) {
            var fld = Dom.get(textfield);
            var valnode = Dom.get(valuearea);
						

            // use the scale factor to convert the pixel offset into a real
            // value
            var actualValue = slider.getRealValue();

            // update the text box with the actual value
            fld.value = actualValue;
            valnode.innerHTML = actualValue;
            // Update the title attribute on the background.  This helps assistive
            // technology to communicate the state change
            Dom.get(bg).title = "Aktualna cena: " + actualValue;

        });
        var fld = Dom.get(textfield);
        var v = parseFloat(fld.value, 10);
	v = (lang.isNumber(v)) ? v : 0;
	if (v!=0) {
		slider.setValue(rescaleDown(Math.round(v/scaleFactor)));
		var valnode = Dom.get(valuearea);
		valnode.innerHTML = rescale(v);
	}
        // Listen for keystrokes on the form field that displays the
        // control's value.  While not provided by default, having a
        // form field with the slider is a good way to help keep your
        // application accessible.
        
        Event.on(textfield, "keydown", function(e) {
            // set the value when the 'return' key is detected
            if (Event.getCharCode(e) === 13) {
                var v = parseFloat(this.value, 10);
                v = (lang.isNumber(v)) ? v : 0;
                // convert the real value into a pixel offset
                slider.setValue(Math.round(v/scaleFactor));
            }
        });
        resetPrice = function() {
		var fld = Dom.get(textfield);
		fld.value = "";

	        var valnode = Dom.get(valuearea);
	        valnode.innerHTML = "0";
	        setDisplay("proposition-price", false);
	}
	setOwnPrice = function(minimum) {
		var v = parseFloat(minimum, 10);
		v = (lang.isNumber(v)) ? v : 0;
		var fld = Dom.get(textfield);
		fld.value = minimum;
		var valnode = Dom.get(valuearea);
		valnode.innerHTML = minimum;
		setDisplay("proposition-price", true);
		slider.setValue(rescaleDown(Math.round(v/scaleFactor)));
	}

	if (v == 0) {
		setDisplay("proposition-price", false);
	}else {
		$('price-last').checked = true;
		setDisplay("proposition-price", true);
	}
});
/**
 * Post code search engine
 */
function codeSearch(streetElementId, cityElementId, postcodeElementId) {
	/**
	 * 'prop(_delivery_street)'
	 * 'prop(_delivery_city)'
	 * 'prop(_delivery_postcode)'
	 */
	YAHOO.twojekwiaty.container.wait.show();
	var myAjax = new Ajax.Request(
		'/do/postCodeSearch/codes', 
			{
				method: 'post',
				parameters: 'streetName=' + $(streetElementId).value + "&cityName=" + $(cityElementId).value,
				onComplete: function(response) {
					var update = document.createElement('div');
					update.innerHTML = response.responseText;
					var ul = $(update).down();
					var liElements = $(ul).childElements();
					if (liElements.length == 1 && $(postcodeElementId).value.length == 0) {
						$(postcodeElementId).value = $(ul).down().innerHTML;
						YAHOO.twojekwiaty.container.wait.hide();
					} else {
						var codesForm = new Ajax.Request('/do/postCodeSearch/?&cityElementId=' + cityElementId + '&postcodeElementId=' + postcodeElementId,{
							method: 'post',
							onComplete: function(response) {
								YAHOO.twojekwiaty.container.wait.hide();
								var handleSubmit = function() {
									YAHOO.twojekwiaty.container.codes.hide();
									YAHOO.twojekwiaty.container.wait.show();
									var save = new Ajax.Request('/do/postCodeSearch/?&cityElementId=' + cityElementId + '&postcodeElementId=' + postcodeElementId,{
										parameters: $('postCodesForm').serialize(),
										onComplete: function(response) {
											YAHOO.twojekwiaty.container.codes.setBody(response.responseText);
											YAHOO.twojekwiaty.container.wait.hide();
											YAHOO.twojekwiaty.container.codes.show();
										}
									});
								}
								var handleCancel = function() {
									YAHOO.twojekwiaty.container.codes.hide();
								}
					            YAHOO.twojekwiaty.container.codes = 
			                    new YAHOO.widget.Dialog("codes",  
			                                                    { width: "660px", 
			                                                      fixedcenter: true, 
			                                                      draggable: true, 
			                                                      zindex:4,
			                                                      modal: true,
			                                                      visible: false,
			                                                      buttons : [ { text:"Szukaj", handler:handleSubmit, isDefault:true },
			                            								      { text:"Anuluj", handler:handleCancel } ]
			                            											                                                      
			                                                    } 
			                                                );
					    
					            YAHOO.twojekwiaty.container.codes.setHeader("Wyszukiwarka kodów");
					            YAHOO.twojekwiaty.container.codes.setBody(response.responseText);
					            YAHOO.twojekwiaty.container.codes.render(document.body);
					            YAHOO.twojekwiaty.container.codes.show();
							}
						});
					}
				}
			}
	);
}
function useCode(cityElementId, postcodeElementId, code, city) {
	$(postcodeElementId).value = code;
	if (city.length > 0) {
		$(cityElementId).value = city;
	}
	YAHOO.twojekwiaty.container.codes.hide();
}

/**
 * Can't decide
 */
function useInstruction(instruction) {
	$('cantDecideInstructionsText').value = instruction; 
	YAHOO.twojekwiaty.container.instructions.hide();
}

							
function showInstructions() {
	YAHOO.twojekwiaty.container.wait.show();
	var myAjax = new Ajax.Request(
		'/do/ef/cantDecideInstructions', 
			{
				method: 'post',
				onComplete: function(response) {
						var handleCancel = function() {
							 YAHOO.twojekwiaty.container.instructions.hide();
						}
						YAHOO.twojekwiaty.container.wait.hide();
			            YAHOO.twojekwiaty.container.instructions = 
	                    new YAHOO.widget.Dialog("instructions",  
	                                                    { width: "660px", 
	                                                      fixedcenter: true, 
	                                                      draggable: true, 
	                                                      zindex:4,
	                                                      modal: true,
	                                                      visible: false,
	                                                      buttons : [ { text:"Anuluj", handler:handleCancel,isDefault:true } ]
	                                                    } 
	                                                );
			    
			            YAHOO.twojekwiaty.container.instructions.setHeader("Specjalne instrukcje");
			            YAHOO.twojekwiaty.container.instructions.setBody(response.responseText);
			            YAHOO.twojekwiaty.container.instructions.render(document.body);
			            YAHOO.twojekwiaty.container.instructions.show();
				}
			}
	);
}

