/*
 * Compare Yachts Tools
 *
 * Author: waldemarmatlosz
 */
 
$(function() {

/** Compare List Section  ----------------------------------*/

	/** set compare yachts a href to 'compare_yachts' **/
	$('#aCompareYachts').attr('href','/directory/compare_yachts.htm');


	$('.chbCompare').click(function(){								
		if($(this).attr('checked')){
			var yacht_id = $(this).parents('li,tr,div').attr('id').split('_')[2];		
			var property_yacht_list_element = null;
			/** clone template item **/
			var template_item = $('#sidebarCompareListItem_template > ul > li').clone();
			/** assign unique id **/
			template_item.attr('id','sidebar_item_id_'+yacht_id);
			$.ajax({
			   type: "GET",
			   url: "/property/yacht/compare_list",
			   data: "id="+yacht_id,
			   dataType: 'xml',
			   success: function(msg){
				 
				 property_yacht_list_element = '<div class="yachtDescription"><span class="model">'+$('property_yacht_list', msg).text()+'</span> | '+$('property_yacht_list', msg).attr('length')+' | '+$('property_yacht_list', msg).attr('type')+'</div><div class="yachtControl"><a href="#" class="deleteFromCompareList">Remove</a></div>';
				 template_item.html(property_yacht_list_element);
				//template_item.html();
				
				/** if empty info is displayed - then hide it **/
				if($('#compareListIsEmpty')){
					$('#compareListIsEmpty').remove();
				}
				
				/** append new data row **/
				// original code which does not work after design refactore 
				//$('#sidebarCompareList > ul > li[@class = "compareYachts"]').before(template_item);	
				$('#sidebarCompareList > ul ').append(template_item);	
				
				deleteFromComparionListHandler();
				/** call function which hide/display compare button **/
				displayOrHideCompareButton();	
			  }
			});
		}
		else{
			var yacht_id = $(this).parents('li,tr, div').attr('id').split('_')[2];						
			deleteItemFromComparsionList(yacht_id);
			
		}
		
	}); //eof chckb compare click
			
	if($('.addToCompareList')){
		if($('.addToCompareList').attr('id')){
			var yacht_id_tmp = $('.addToCompareList').attr('id').split('_')[2];
			/** check if current model exists on the compare list **/
                        if(checkIfSelectedModelExistsOnTheList(yacht_id_tmp)){
                            /** if exists then hide 'add to compare' button **/
                            $('.addToCompareList').parents('li').hide('fast');
                        }
		}
	}


	/** add to compare list on the yacht range **/		
	$('.addToCompareList').click(function(e){
			

			
			e.preventDefault(); 
			e.stopPropagation(); 
			var yacht_id = $(this).attr('id').split('_')[2];		
			
			/** check if selected item exists on the comparsion list **/			
			if(!checkIfSelectedModelExistsOnTheList(yacht_id)){
			
				var property_yacht_list_element = null;
				/** clone template item **/
				var template_item = $('#sidebarCompareListItem_template > ul > li').clone();
				/** assign unique id **/
				template_item.attr('id','sidebar_item_id_'+yacht_id);
				
				$.ajax({
				   type: "GET",
				   url: "/property/yacht/compare_list",
				   data: "id="+yacht_id,
				   dataType: 'xml',
				   success: function(msg){
					 
					 property_yacht_list_element = '<div class="yachtDescription"><span class="model">'+$('property_yacht_list', msg).text()+'</span> | '+$('property_yacht_list', msg).attr('length')+' | '+$('property_yacht_list', msg).attr('type')+'</div><div class="yachtControl"><a href="#" class="deleteFromCompareList">Remove</a></div>';
					 template_item.html(property_yacht_list_element);
					//template_item.html();
					
					/** if empty info is displayed - then hide it **/
					if($('#compareListIsEmpty')){
						$('#compareListIsEmpty').remove();
					}
					
					/** append new data row **/
					$('#sidebarCompareList > ul > li[@class = "compareYachts"]').before(template_item);	
					deleteFromComparionListHandler();
					/** call function which hide/display compare button **/
					displayOrHideCompareButton();	
				  }
				});		
				
				/** hide add to compare list button **/
				$(this).parents('li').hide('fast');
				
			}
		
	});
	
	
	
	/** eof add to compare list on the yacht range **/
			
	deleteFromComparionListHandler();
	
	/** call function which hide/display compare button **/
	displayOrHideCompareButton();	
	
	
	/** bind action when compare button clicked **/
	/**
	 * COMPARE YACHTS BUTTON
	 *
	 * Author: waldemarm
	 **/
	$('#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');};
	});	
	

/** eof Compare List Section  ------------------------------*/


}); //eof DOM ready



	/** 
	 * check if selected model exists on the comparsion list 
	 *
	 * Author: waldemarm
	 **/
	function checkIfSelectedModelExistsOnTheList(property_yacht_id){
		var selected_model_exists_on_the_list = false;
		$('#sidebarCompareList > ul > li').each(function(){
			var cmp_property_yacht_id = $(this).attr('id').split('_')[3];
			if(cmp_property_yacht_id==property_yacht_id){
				selected_model_exists_on_the_list = true;
			}
			
		});
		
		return selected_model_exists_on_the_list;	
	}


	function deleteFromComparionListHandler(){
		/*
		 * Delete from comparsion list
		 *
		 * Author: waldemarm
		 */
		 $('.deleteFromCompareList').click(function(e){	 	
									
			e.preventDefault(); 
			e.stopPropagation();
			var yacht_id = $(this).parents('li').attr('id').split('_')[3];
					
					
			deleteItemFromComparsionList(yacht_id);
		 });		 
	}
	
	
	/*
	 * Function which remove item from yacht comparsion list and call service to remove entry  
	 *
	 * Author: waldemarm
	 */
	function deleteItemFromComparsionList(property_yacht_id){
		/** delete item from list **/
		$('#sidebarCompareList > ul').find('#sidebar_item_id_'+property_yacht_id).remove();
		
		/** call service to remove from system **/
		$.ajax({
		   type: "DELETE",
		   url: "/property/yacht/compare_list?id="+property_yacht_id,
		   success: function(msg){
		   }
		});	
		
		/** uncheck checkbox **/
		$('#property_yacht_'+property_yacht_id).find('.chbCompare').attr('checked','');
		
		/** if there is not any item in the comparsion list then display appropriate information **/
		
		if($('#sidebarCompareList > ul > li').size()==1){
				/** clone template item **/
				var template_item = $('#sidebarCompareListItem_template > li').clone();
				/** assign unique id **/
				template_item.attr('id','compareListIsEmpty');
				template_item.html('You do not have any yachts to compare.');
				//template_item.html('');
				//$('#sidebarCompareList > ul').append(template_item);	
				
		}
				if($('.addToCompareList').length!=0){
					var yacht_id_tmp = $('.addToCompareList').attr('id').split('_')[2];
					if(!checkIfSelectedModelExistsOnTheList(yacht_id_tmp)){
						/** if exists then hide 'add to compare' button **/
						$('.addToCompareList').parents('li').show('fast');
					}
				}
		
		/** call function which hide/display compare button **/
		displayOrHideCompareButton();	
	}
	
	/*
	 * Function which display/hide compare button  
	 *
	 * Author: waldemarm
	 */	
	function displayOrHideCompareButton(){	
		if($('#compareListIsEmpty').length==1){
			$('#aCompareYachts').fadeOut();
		}
		else{
			$('#aCompareYachts').fadeIn();
		}
	}

