// JavaScript Document
/**
 * Global variables 
 *
 * Author: waldemarm
 */
var m_to_ft = 3.2808399;	// value which is required to correct recalculate meters to feets


jQuery(document).ready(function() {
				

				
	/**
	 * Yacht search section (for sale)
	 *
	 * Author: waldemarm
	 */	
	 
	 	/**
		 * If cookie with 'length_id_for_sale' is not available then set the cookie 
		 *
		 * Author: waldemarm
		 */		
		 if(_getCookie('property_yacht_length_id_preferred')==null){
		 	_setCookie('property_yacht_length_id_preferred',$('#length_id_for_sale').val());
		 }
		 
		/**
		 * If cookie with 'property_yacht_length_id_preferred' is available then set selected value in selectbox 
		 *
		 * Author: waldemarm
		 */			 
		  if(_getCookie('property_yacht_length_id_preferred')!=null){
			  	/** @todo - refactore this code because it does not work with account refine search logic **/
	  			//$("#length_id_for_sale").selectOptions(_getCookie('property_yacht_length_id_preferred'));
		  }
	 
		/** set default min/max values for length slider **/
		var slider_length_for_sale_min = 24;
		var slider_length_for_sale_max = 100;
		
		var slider_length_for_sale_min_converted = slider_length_for_sale_min;
		var slider_length_for_sale_max_converted = slider_length_for_sale_max;		
		
		var slider_length_for_sale_min_current = null;
		var slider_length_for_sale_max_current = null;		
		
		
		/** assign length_id letter from the selectbox **/
		var length_id_for_sale = $('#length_id_for_sale :selected').text();	 
			 
	 	
		/** if we have a cookie with length_id_for_sale set for feets then we have to recalculate default values **/
		if(length_id_for_sale=='ft'){
				slider_length_for_sale_min_converted = Math.round(slider_length_for_sale_min*m_to_ft);			
				slider_length_for_sale_max_converted = Math.round(slider_length_for_sale_max*m_to_ft);	
				//$("#length_for_sale_from").val(slider_length_for_sale_min_converted);
				//$("#length_for_sale_to").val(slider_length_for_sale_max_converted); 
				
				length_for_sale_from = Math.round(slider_length_for_sale_min*m_to_ft);
				length_for_sale_to = Math.round(slider_length_for_sale_max*m_to_ft);				
				
				if($("#length_for_sale_from").val()==''){
					slider_length_for_sale_min_current = slider_length_for_sale_min_converted;
					slider_length_for_sale_max_current = slider_length_for_sale_max_converted;					
				}
				else{
					slider_length_for_sale_min_current = $("#length_for_sale_from").val();
					slider_length_for_sale_max_current = $("#length_for_sale_to").val();					
				}
				
			updateLengthLabelForSale(slider_length_for_sale_min_current,slider_length_for_sale_max_current,length_id_for_sale);
		}
		else{
				if($("#length_for_sale_from").val()==''){
					slider_length_for_sale_min_current = slider_length_for_sale_min_converted;
					slider_length_for_sale_max_current = slider_length_for_sale_max_converted;					
				}
				else{
					slider_length_for_sale_min_current = $("#length_for_sale_from").val();
					slider_length_for_sale_max_current = $("#length_for_sale_to").val();					
				}
			
			updateLengthLabelForSale(slider_length_for_sale_min_current,slider_length_for_sale_max_current,length_id_for_sale);
		}
		
	 
	 
		/** bind actions on length unit change **/
		$('#length_id_for_sale').change(function(){
			length_id_for_sale = $('#length_id_for_sale :selected').text();
			
			/**
			 * Store selected length id in the cookie.
			 * This will be used to set default unit when user visit the website next time.
			 *
			 * Author: waldemarm
			 */
			_setCookie('property_yacht_length_id_preferred',$('#length_id_for_sale').val());
			
			
			/** on change we also need to recalculate values from metres to feets or feets to metres **/
			if(length_id_for_sale=='ft'){
				length_for_sale_from = Math.round(slider_length_for_sale_min*m_to_ft);
				length_for_sale_to = Math.round(slider_length_for_sale_max*m_to_ft);
				
				/** assign recalculated values to the slider **/
				$("#slider-length-for-sale").slider('option','min',length_for_sale_from);
				$("#slider-length-for-sale").slider('option','max',length_for_sale_to);					
				$("#slider-length-for-sale").slider('values',0,length_for_sale_from);
				$("#slider-length-for-sale").slider('values',1,length_for_sale_to);	
				
				$('#length_for_sale_from').val(length_for_sale_from);
				$('#length_for_sale_to').val(length_for_sale_to);				

				/** update label values **/
				updateLengthLabelForSale(length_for_sale_from,length_for_sale_to,length_id_for_sale);				
			}
			else{
				$("#slider-length-for-sale").slider('option','min',slider_length_for_sale_min);
				$("#slider-length-for-sale").slider('option','max',slider_length_for_sale_max);					
				$("#slider-length-for-sale").slider('values',0, slider_length_for_sale_min);
				$("#slider-length-for-sale").slider('values',1,slider_length_for_sale_max);	
				
				$('#length_for_sale_from').val(slider_length_for_sale_min);
				$('#length_for_sale_to').val(slider_length_for_sale_max);					
				
				/** update label values **/
				updateLengthLabelForSale(slider_length_for_sale_min,slider_length_for_sale_max,length_id_for_sale);						
				
			}
		});	 
	 
		/** initialize slider length for sale **/
		$("#slider-length-for-sale").slider({
			range: true,
			min: slider_length_for_sale_min_converted,
			max: slider_length_for_sale_max_converted,
			values: [slider_length_for_sale_min_current, slider_length_for_sale_max_current],
			slide: function(event, ui) {
				
				var add_plus = '';
				if($('#length_id_for_sale').val()==2 && ui.values[1]==328){
					add_plus = '+';
				}
				if($('#length_id_for_sale').val()==1 && ui.values[1]==100){
					add_plus = '+';
				}					
				
				$("#length_for_sale").val('' + Math.round(ui.values[0]) + length_id_for_sale+' - ' + Math.round(ui.values[1])+length_id_for_sale+add_plus);
				$("#length_for_sale_from").val(Math.round(ui.values[0]));
				$("#length_for_sale_to").val(Math.round(ui.values[1])); 
			},
			stop: function(event, ui){
				ajaxGetForSaleYachts();
			}			
		});	 	
		
		
		
		
		/**
		 * If cookie with 'currency_code_id_for_sale' is not available then set the cookie 
		 *
		 * Author: waldemarm
		 */		
		 if(_getCookie('currency_code_id_preferred')==null){
		 	_setCookie('currency_code_id_preferred',$('#currency_code_id_for_sale').val());
		 }
		 
		/**
		 * If cookie with 'currency_code_id_preferred' is available then set selected value in selectbox 
		 *
		 * Author: waldemarm
		 */			 
		  if(_getCookie('currency_code_id_preferred')!=null){
	  			$("#currency_code_id_for_sale").selectOptions(_getCookie('currency_code_id_preferred'));
		  }			
		
		
		/** set default min/max values for length slider **/
		var slider_price_for_sale_min = 0;
		var slider_price_for_sale_max = 120000000;
		
		var slider_price_for_sale_min_current = slider_price_for_sale_min;
		var slider_price_for_sale_max_current = slider_price_for_sale_max;	
		
		if($("#price_for_sale_from").val()!=''){
			slider_price_for_sale_min_current = $("#price_for_sale_from").val();
			slider_price_for_sale_max_current =	$("#price_for_sale_to").val()	
		}
		
		
		
		/** assign length_id letter from the selectbox **/
		var currency_code_id_for_sale = $('#currency_code_id_for_sale :selected').text();	 	
			 
		
	 	updatePriceLabelForSale(slider_price_for_sale_min_current,slider_price_for_sale_max_current,currency_code_id_for_sale);		
		
		/** initialize slider price for sale **/
		$("#slider-price-for-sale").slider({
			range: true,
			min: slider_price_for_sale_min,
			max: slider_price_for_sale_max,
			step: 5000000,
			values: [slider_price_for_sale_min_current, slider_price_for_sale_max_current],
			slide: function(event, ui) {
				
				var add_plus = '';
				if(ui.values[1]==slider_price_for_sale_max){
					add_plus = '+';
				}
				
				$("#price_for_sale").val('' +currency_code_id_for_sale + Math.round(ui.values[0]/1000000) +'m - ' + currency_code_id_for_sale + Math.round(ui.values[1]/1000000)+'m'+add_plus);
				$("#price_for_sale_from").val(ui.values[0]);
				$("#price_for_sale_to").val(ui.values[1] == slider_price_for_sale_max ? 9999999999 : ui.values[1]); 
				
				slider_price_for_sale_min_current = ui.values[0];
				slider_price_for_sale_max_current = ui.values[1];
				
			},
			stop: function(event, ui){
				ajaxGetForSaleYachts();
			}			
		});	 		
		
		/** bind actions on length unit change **/
		$('#currency_code_id_for_sale').change(function(){
														
			/**
			 * Store selected currency_code_id_for_sale id in the cookie.
			 * This will be used to set default unit when user visit the website next time.
			 *
			 * Author: waldemarm
			 */
			$.cookie('currency_code_id_preferred', $('#currency_code_id_for_sale').val(), { path: '/', expires: 365 });
			
			
			currency_code_id_for_sale = $('#currency_code_id_for_sale :selected').text();
		 	updatePriceLabelForSale(slider_price_for_sale_min_current,slider_price_for_sale_max_current,currency_code_id_for_sale);				
			
		});
		
		
		/** bind inchange action to all selectboxes with class name 'forSaleSelectbox' **/
		$('.forSaleSelectbox').each(function(){
			$(this).change(function(){			
				ajaxGetForSaleYachts();		
			});	
		});
		
		/** call search service with default values to get number of available for sale yachts **/
		ajaxGetForSaleYachts();
		
		 /** bind submit button action **/
		 $('#searchYachtsForSale').click(function(){
			$('#yacht_for_sale_form').submit();
		 });		
		
		/** onchange check selected builder id and call service which return all models for selected builder
		 *  Then create selectbox list with all returned models 
		 **/
		$('#property_yacht_builder_id_for_sale').change(function(){
			if($("#property_yacht_builder_id_for_sale").val()!=''){
				$.ajax({
				  type: "GET",
				  url: "/property/yacht/model/name?model=custom&property_yacht_builder_id="+$("#property_yacht_builder_id_for_sale").val(),
					 success: function(responseXML){			
						createModelSelectList(responseXML);						
					}
				});	
			 }	
		 });
		
		
				
		/**
		 * If we have some value selected by default, then we must trigger builder select event to get list of models 
		 *
		 * Author: waldemarm
		 */				
		if($('#property_yacht_builder_id_for_sale :selected').val()!=''){
			$('#property_yacht_builder_id_for_sale').trigger('change');
		}		



		$('#btnSearchSubmit').click(function(){
			$('#yacht_for_sale_form').submit();
		});
		
		$(".searchFieldOption").change(function() {
			setTimeout("ajaxGetForSaleYachts();",250);
		});
		
		
		/** initialize stylish dropdown stuff **/	   
//		MSDropDown.init("#currency_code_id_for_sale, #length_id_for_sale");
//		MSDropDown.showIconWithTitle(false);			
		
		$("#advancedSearchFilter").click(function() {
			$('#advancedOptions').toggle('fast',function(){
				
				if($('#advancedOptions:visible').length){
					$("#advancedSearchFilter").addClass('collapse').text('Hide Advanced Search - Toys &amp; Amenities');
				} else{
					$("#advancedSearchFilter").removeClass('collapse').text('Advanced Search - Toys &amp; Amenities');
				}
			});
			
		});
		
	 /** EOF Yacht search section (for sale) **/
		
	/**
	 * Call service which return list of all builders assigned to yachts for sale 
	 *
	 * Author: waldemarm
	 */	
		
		$.ajax({
		  type: "GET",
		  url: "/index/get_sales_builders",
			 success: function(responseXML){	
				$("#property_yacht_builder_id_for_sale").removeOption(/./);		
				//add default first value 
				$("#property_yacht_builder_id_for_sale").addOption('', 'All');				
					$('property_yacht_builder_list > builder',responseXML).each(function(){
						$("#property_yacht_builder_id_for_sale").addOption($(this).attr('id'), $(this).text());		
					});
				//select first option
				$("#property_yacht_builder_id_for_sale").selectOptions("");
				
			}
		});		
});
//eof DOM Ready


/**
 * Yacht search section (for sale) - functions 
 *
 * Author: waldemarm
 */
 
 
 	function forSaleOnChange(){
		//ajaxGetForSaleYachts();	
	}
	/**
	 * Function which update length label 
	 *
	 * Author: waldemarm
	 */
	function updateLengthLabelForSale(slider_length_min, slider_length_max, length_id_label){
		 $('#length_for_sale').val('' + slider_length_min + length_id_label+' - ' + slider_length_max+length_id_label+'+');
	} 
 
	/**
	 * Function which update price label
	 *
	 * Author: waldemarm
	 */
	function updatePriceLabelForSale(slider_length_min, slider_length_max, length_id_label){
		 $('#price_for_sale').val('' +length_id_label+ Math.round(slider_length_min/1000000) +'m - ' +length_id_label+ Math.round(slider_length_max/1000000)+'m+');
	}  
	
	/**
	 * Function which call service which return number of yachts for sale found (based on passed search criterias)
	 *
	 * Author: waldemarm
	 */
	function ajaxGetForSaleYachts(){
		
		$('#results_for_sale').html('<span>Updating...</span>');
		
		var max_price_tmp = $('#price_for_sale_to').val();
		if($('#price_for_sale_to').val()==120000000){
			max_price_tmp = 999999999;
		}
		
		var max_length_tmp = $('#length_for_sale_to').val();
		if($('#length_id_for_sale').val()==2 && $('#length_for_sale_to').val()==328){
			max_length_tmp = 500;
		}
		if($('#length_id_for_sale').val()==1 && $('#length_for_sale_to').val()==100){
			max_length_tmp = 500;
		}				
		if( $("#yacht_for_sale_form").length > 0 ) {
			$("#yacht_for_sale_form").ajaxSubmit({
			   type: "POST",
			   url: "/property/search",
			   success: function(returnedData){
					$('#results_for_sale').fadeOut(100, function(){
						profileCount = $("property_profile_id_count", returnedData).text();															 	
						$('#results_for_sale').html(profileCount+'&nbsp;<span>yachts</span>').fadeIn(300);
					});
			   }
			 });
		}
		
	}	



	/**
	 * Function which get service XML response and create list of selectbox options with models available for selected builder
	 *
	 * Author: waldemarm
	 */
	function createModelSelectList(XMLResponse){
		// clear select list
		$("#property_yacht_model_id").removeOption(/./);		
		//add default first value 
		$("#property_yacht_model_id").addOption('', 'All');			
		// if there are some returned models then show model options + fill in with model data **/		
		if($(XMLResponse).find('rs').length>0){
			$(XMLResponse).find('rs').each(function(){
				/** avoind empty strings **/
				if($(this).text()!=''){
					$("#property_yacht_model_id").addOption($(this).attr('id'), $(this).text());		
				}
			});
		}
		
//		if(typeof($('#property_yacht_model_id_selected').val())!='undefined'){
//			//select previously selected option
//			console.log($('#property_yacht_model_id_selected').val());
//			$("#property_yacht_model_id").selectOptions($('#property_yacht_model_id_selected').val());
//		}
//		else{
			//select first option
			$("#property_yacht_model_id").selectOptions("");
//		}
		
	}
 
 
/** EOF Yacht search section (for sale) - functions **/


/**
 * Function which set cookie with name, value and lifetime passed as the function parameter 
 * @param 'cookieLifetime' in default is set to 365 days
 *
 * Author: waldemarm
 */
function _setCookie(cookieName, cookieValue, cookieLifetime){
		if(isNaN(cookieLifetime)){
			cookieLifetime = 365;
		}
		$.cookie(cookieName, cookieValue, { expires: cookieLifetime });
}

/**
 * Function which set cookie value of the selected cookie
 * @param 'cookieName' 
 *
 * Author: waldemarm
 */
function _getCookie(cookieName){
		return $.cookie(cookieName);
}









