

jQuery(function() {
	/**
	 * Star rating stuff
	 *
	 * Author: waldemarm
	 */	
			/**
			 * LOGIN AND ASSING THE PONTS
			 *
			 * Author: waldemarm
			 **/
			$('#stars_rating_login').click(function(e){
				e.preventDefault();
				e.stopPropagation();
				
				/** create highslide object **/
				hs.htmlExpand(this, {
					preserveContent: false, //disable iframe content caching
					objectType: 'iframe',
					width: 645,
					objectHeight: 510,
					objectLoadTime: 'after',
					allowWidthReduction: 1,
					showCredits: false,
					dimmingOpacity: 0.70,
					align: 'center'
				}
				);
			});
		 
		 
			var global_value = null;
		 
//			 $(".stars-wrapper").stars({
//		
//						callback: function(element, type, value){
//							var element_id = element.element[0].id;
//							var property_yacht_id = element_id.split('_')[3];
//							var element_value = value;
//							//console.log(element_id+': '+property_yacht_id+' => '+element_value);
//							rateYacht(property_yacht_id, element_value);
//							global_value = value;
//						},
//						captionEl: $('stars_cap')
//						
//			 });	
		 
		var star_rating_customer_account_href = $('#stars_rating_login').attr('href');
		 if($('#lock_stars_rating').val()=='true'){
			 
			 
		 	$(".stars-wrapper").click(function(){
		 		/**
		 		 * on rating a yacht it is added to the compare list
		 		 * @author stanislav
		 		 */
		 		try {
		 			var tmp = $('input.chbCompare', $(this).parents('li:first') ).get(0).checked;
		 			if( !tmp ) {
			 			$('input.chbCompare', $(this).parents('li:first') ).get(0).checked = !tmp;
						$('input.chbCompare', $(this).parents('li:first') ).trigger('click');
						$('input.chbCompare', $(this).parents('li:first') ).get(0).checked = !tmp;
		 			}
				}catch(e) { }
				
				try {
					var tmp = $('input.chbCompare', $(this).parents('div.controlContainer').siblings('div.photoContainer') ).get(0).checked;
					if( !tmp ) {
						$('input.chbCompare', $(this).parents('div.controlContainer').siblings('div.photoContainer') ).get(0).checked = !tmp;
						$('input.chbCompare', $(this).parents('div.controlContainer').siblings('div.photoContainer') ).trigger('click');
						$('input.chbCompare', $(this).parents('div.controlContainer').siblings('div.photoContainer') ).get(0).checked = !tmp;
					}
				}catch(e) { }

				$(".stars-wrapper").stars('select',0);
				var element_id = '#'+$(this).attr('id');
				var property_yacht_id = $(this).attr('id').split('_')[3];
				$('#stars_rating_login').attr('href',star_rating_customer_account_href+'&property_yacht_id='+property_yacht_id+'&rating='+global_value)
				$('#stars_rating_login').trigger('click');
			});
		 }


		/**
		 * Accordion feature in the search list 
		 *
		 * Author: waldemarm
		 */		
		$('.accordion').click(function(e){	
			if($(this).attr('class')=='accordion collapse'){
				$(this).parent('li').find('.accordionWrapper').slideDown();		
				$(this).attr('class','accordion');
				$(this).parent('li').find('.accordion').find('a').attr('class','');	
			}
			else{
				$(this).parent('li').find('.accordionWrapper').slideUp();	
				$(this).attr('class',$(this).attr('class')+' collapse');
				$(this).parent('li').find('.accordion').find('a').attr('class','collapse');					
				
			}

		});
		
		
		$('.showMore').click(function(e){
			$(this).parent().parent().find('li').each(function(){
				if($(this).attr('style','display: none')){
					$(this).attr('style','display: block;');
				}
			});	
			
			$(this).parent().remove();
			
		});
		
		/**
		 * Currency switch section
		 *
		 * Author: waldemarm
		 */		
		 
 
		/**
		 * Part which assign (active) class on selected currency button
		 *
		 * Author: waldemarm
		 */			 
		  if(_getCookie('currency_code_id_preferred')!=null){
	  			var currency_code_id = _getCookie('currency_code_id_preferred');
				$('#currency_code_'+currency_code_id).attr('class','active');
		  }
		  else{
				/** just set default cookie value to 1 **/
				_setCookie('currency_code_id_preferred',3);			  
		  }
		  
		  
		/**
		 * Currency switch bind action 
		 *
		 * Author: waldemarm
		 */		  
		$('.currencySwitch').click(function(e){
			e.stopPropagation();
			e.preventDefault();
			_setCookie('currency_code_id_preferred',$(this).attr('id').split('_')[2]);
			
			// Remove the 'min_price' and 'max_price' values if we're changing currency - they will lose all meaning otherwise!
			var location = window.location.toString();
			
			var min_price_start = location.indexOf('&min_price=');
			if (min_price_start != -1)
			{	
				min_price_start = min_price_start+11;
				location = location.substring( 0, min_price_start ) + location.substring( location.indexOf('&', min_price_start), location.length);
			}
			
			var max_price_start = location.indexOf('&max_price=');
			if (max_price_start != -1)
			{	
				max_price_start = max_price_start+11;
				location = location.substring( 0, max_price_start ) + location.substring( location.indexOf('&', max_price_start), location.length);
			}
			
			if (window.location.toString() == location)
			{
				window.location.reload();
			}
			else
			{
				window.location = location;
			}
		});
		
		
		/**
		 * Setup selected value in 'items_per_page' selectbox based on the hidden form value 
		 *
		 * Author: waldemarm
		 */		

	if($('#per_page').val()!=''){
		$("#items_per_page").selectOptions($('#per_page').val());
	}
	
	
/**
 * Get all params from form and assign to rss href 
 *
 * Author: waldemarm
 */
var search_form_params_serialized = $('#urlstring').html();
//$('.js_yachtSearchRSS').attr('href','/'+$('.js_yachtSearchRSS').attr('id').split('_')[1]+'/search/newsfeed.htm?'+search_form_params_serialized);
	
	
		//console.log($('#per_page').val());
		/**
		 * Handle 'customise results' (items_per_page)
		 *
		 * Author: waldemarm
		 */		
	 $('#items_per_page, #order, #order_guests, #order_type, #order_length').change(function(){
		$('input[name="'+$(this).attr('name')+'"]', $('#pageForm')).val( $(this).val() );
//		 $("#total_count").html("<img src=\"/resources/images/loading.gif\">");	
//		$(this).parents('form:first').submit();
		return;
//		document.pageForm.submit();
	 });
	 
	 $('#sort_results').click(function(){
		 
//		 $("#total_count").html("<img src=\"/resources/images/loading.gif\">");	
		 $(this).parents('form:first').submit();
		 $(this).replaceWith("<img class=\"sort_loading\" src=\"/resources/images/loading_small.gif\">");
		 return false;
	 });
//	 $('#sort_results').replaceWith("<img style=\"padding-left: 9px; padding-top: 7px;\" src=\"/resources/images/loading_small.gif\">");
		
	$("#detail_view").click(function() {
		$(this).parents('form:first').get(0).action = '/search/detail.htm';
		$(this).parents('form:first').submit();
		return;
		document.pageForm.action = "/search/detail.htm";
		document.pageForm.submit();
		//return false;
	});

	$("#grid_view").click(function() {
		$(this).parents('form:first').get(0).action = '/search/grid.htm';
		$(this).parents('form:first').submit();
		return;
		document.pageForm.action = "/search/grid.htm";
		document.pageForm.submit();
		//return false;
	});

	$("#list_view").click(function() {
		$(this).parents('form:first').get(0).action = '/search/list.htm';
		$(this).parents('form:first').submit();
		return;
		document.pageForm.action = "/search/list.htm";
		document.pageForm.submit();
		//return false;
	});
	
	$('.detailContainer .logoAgent').each(function(i){
		if( $('a', $(this)).length > 1 ) {
			$(this).cycle({
				fx: 'fade',
				timeout: 5500,
				speed: 3500,
				requeueOnImageNotLoaded: true			
			});
		}
	})
	
});


/**
 * Function which call service and allow to rate the yacht 
 *
 * Author: waldemarm
 */
function rateYacht(property_yacht_id, rating){
		$.ajax({
		   type: "GET",
		   url: "/customer/yacht/stars/rating/vote",
		   data: 'property_yacht_id='+property_yacht_id+'&rating='+rating,
		   success: function(returnedData){
				//console.log(returnedData);
		   }
		 });		
}


/**
 * 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, {  path: '/', expires: cookieLifetime });
}

/**
 * Function which set cookie value of the selected cookie
 * @param 'cookieName' 
 *
 * Author: waldemarm
 */
function _getCookie(cookieName){
		return $.cookie(cookieName);
}
