/*
 * Compare Yachts Tools
 *
 * Author: waldemarmatlosz
 */
 
$(function() {

/** Compare List Section  ----------------------------------*/

	$('.chbCompare').click(function(){								
		if($(this).attr('checked')){
			var yacht_model_id = null;
			if(typeof($(this).parents('li').attr('id'))!='undefined'){
				yacht_model_id = $(this).parents('li').attr('id').split('_')[2];	
			}
			else{
				yacht_model_id = $(this).parent().attr('id').split('_')[2];
			}
			
			
			
			$(this).parent('div, .compare').find('span').html('<a href="#" class=\'aCompareYachts\'>Compare Yachts</a>');
			$(this).parent('div, .compare').find('span.js_compareLabel').html('Compare Yachts');
			 $('.aCompareYachts').click(function(){
				$('#aCompareYachts').trigger('click'); 	
			 });			
			
			$.ajax({
			   type: "GET",
			   url: "/search/compare_list",
			   data: "id="+yacht_model_id,
			   dataType: 'xml',
			   success: function(msg){
				 
				displayOrHideCompareButton();	
			  }
			});
		}
		else{
			var yacht_model_id = null;
			if(typeof($(this).parents('li').attr('id'))!='undefined'){
				yacht_model_id = $(this).parents('li').attr('id').split('_')[2];	
			}
			else{
				yacht_model_id = $(this).parent().attr('id').split('_')[2];
			}
			
			deleteItemFromComparsionList(yacht_model_id);
			$(this).parent('div, .compare').find('span').html('Compare');
			
		}
		
	}); //eof chckb compare click
			
	


	/** bind action when compare button clicked **/
	/**
	 * COMPARE YACHTS BUTTON
	 *
	 * Author: waldemarm
	 **/
	 $('.aCompareYachts').click(function(e){
		e.stopPropagation();
		e.preventDefault();
		//console.log('click');
		$('#aCompareYachts').trigger('click'); 	
	 });
	 
	$('#aCompareYachts').click(function(e){
		e.preventDefault(); 
		e.stopPropagation(); 
		
		/** create highslide object **/
		hs.htmlExpand(this, {
					  		 preserveContent: false, //disable iframe content caching
					  		 objectType: 'iframe',
					  		 width: 996,
							 objectHeight: 650,
							 objectLoadTime: 'after', 
							 allowWidthReduction: 1,
							 showCredits: false,
							 dimmingOpacity: 0.70,
							 align: 'center'
					  		}
					  );
		//hs.Expander.prototype.onAfterClose = function(sender){alert('close');};
	});	


			
	//deleteFromComparionListHandler();
	
	/** call function which hide/display compare button **/
	displayOrHideCompareButton();	
	
	
	

/** eof Compare List Section  ------------------------------*/

}); //eof DOM ready


	
	/*
	 * Function which remove item from yacht comparsion list and call service to remove entry  
	 *
	 * Author: waldemarm
	 */
	function deleteItemFromComparsionList(property_yacht_model_id){
		
		/** call service to remove from system **/
		$.ajax({
		   type: "DELETE",
		   url: "/search/compare_list?id="+property_yacht_model_id,
		   success: function(msg){
		   }
		});	
		
		/** uncheck checkbox **/
		$('#property_yacht_'+property_yacht_model_id).find('.chbCompare').attr('checked','');		
				
		/** call function which hide/display compare button **/
		displayOrHideCompareButton();	
	}
	
	/*
	 * Function which display/hide compare button  
	 *
	 * Author: waldemarm
	 */	
	function displayOrHideCompareButton(){	
		$checked_checkboxes = false;
		$('.chbCompare').each(function(){
			if($(this).attr('checked')==true){
				$checked_checkboxes = true;
			}
		});
	
	
	
		
		/** compare yacht buttons show/hide (option is disabled) **/
		if($checked_checkboxes==true){
			//$('#aCompareYachts').fadeIn();
		}
		else{
			//$('#aCompareYachts').fadeOut();
		}
		
	}
