jQuery(document).ready(function() {
	
	
	/**
	 * Handle the default video/images display 
	 *
	 * Author: waldemarm
	 */	 
	 
	 /** check if we have cookie - if not then default set to images **/
	 if(_getCookie('profile_default_display')==null){
	 	_setCookie('profile_default_display','images');
	 }
	 
	$('.js_turnOnVideo').click(function(e){
		e.preventDefault();
		e.stopPropagation();										
		_setCookie('profile_default_display','video');
		document.location.href = document.location.href;
	});
	
	$('.js_turnOnImages').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		_setCookie('profile_default_display','images');
		document.location.href = document.location.href;		
	});	
	
	$('.js_turnOnSlideshow').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		_setCookie('profile_default_display','slideshow');
		document.location.href = document.location.href;		
	});	
	
	
	
	
	/** handle stars reset **/
	$('.stars-reset > a').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		$(".stars-wrapper").stars("selectID", -1); // remove selection	
		var property_yacht_id_tmp = $('.stars-wrapper').attr('id').split('_')[3];
		rateYacht(property_yacht_id_tmp, 0);
	});
	
	/**
	 * Dispplay organisation branch details 
	 *
	 * Author: waldemar.matlosz
	 */	
	$('.js_getBranchDetails').click(function(e){
		e.stopPropagation();
		e.preventDefault();
		//property_profile_type_id_{$type}_organisation_group_id_{@id}
		var ajax_property_profile_type_id = $(this).attr('id').split('_')[4];
		var ajax_organisation_group_id = $(this).attr('id').split('_')[8];
		
		saveStatistics(23,$('#property_description_id').val(), ajax_organisation_group_id);		
		
		//console.log(ajax_property_profile_type_id);
		//console.log(ajax_organisation_group_id);
		var current_node = this;
		
		/** expand all nodes**/
		$('.js_getBranchDetails').each(function(){
			$(this).removeClass('collapse');
		});
		/** add classs collapse **/
		$(this).addClass('collapse');
		
		//$('.js_ajax_branch_data').slideUp(function(){
			if($('.js_ajax_branch_data').length>0){
					$('.js_ajax_branch_data').each(function(){
						$(this).remove();
					});							
					/*
					$('.js_ajax_branch_data').slideUp(function(){
						$('.js_ajax_branch_data').each(function(){
							$(this).remove();
						});		
							
					});
					*/
					getbranchDetails(current_node, ajax_property_profile_type_id, ajax_organisation_group_id);	
					
			}
			else{
				getbranchDetails(current_node, ajax_property_profile_type_id, ajax_organisation_group_id);
			}

		//});
		
		
		
	});

	/**
	 * Show contact details 
	 *
	 * Author: waldemar.matlosz
	 */	
	 $('.js_showContactDetailsContainer').click(function(e){
	 	e.stopPropagation();
		e.preventDefault();
				
		$('.companyDetails').each(function(){
			$(this).slideDown('fast');
		});
		
		if($('.companyDetails').length==1){
			$('.js_showContactDetailsLvl').trigger('click');
		}
		
		
		$(this).hide();
	 });
	 
	//
	$('.js_showContactDetailsLvl').click(function(e){
		//e.stopPropagation();
		//e.preventDefault();							
		
		click_item_id = $(this).attr('id').split('_')[4];
		$('#js_tab_'+click_item_id).trigger('click');
		$('.js_contactDetailsContainer').slideDown('fast');
		
		$('.companyDetails').each(function(){$(this).remove()});
	});
	 
	 
	 
	 $('.js_showContactDetails').click(function(e){
		e.stopPropagation();
		e.preventDefault();
		
		/** clear branch data wrapper **/
		$('.js_BranchDetailsWrapper').html('');
		
		/** unset active status for other tabs **/
		$('.js_showContactDetails').each(function(){
			$(this).attr('class','js_showContactDetails');
		});
		
		/** set tab active class **/
		$(this).attr('class','js_showContactDetails active');
		
		
		$item_id = $(this).attr('id').split('_')[2];
				
		/** hide other contact divs **/
		$('.contactDetails').each(function(){
			$(this).hide();
		});
		
		$('.agentHeader').each(function(){
			$(this).hide();
		});
		
		$('#js_contact_details_'+$item_id).show();
		$('#js_contact_details_additional_'+$item_id).show();
		
		//console.log($('.js_getBranchDetails','#js_contact_details_'+$item_id).length);
		/** count number of branches - if there is only one then display details **/
		if($('.js_getBranchDetails','#js_contact_details_'+$item_id).length==1){
			$('.js_getBranchDetails','#js_contact_details_'+$item_id).hide();
			$('.js_selectLabel').hide();
			$('.js_contactAdditional').hide();
	 		$('.js_getBranchDetails','#js_contact_details_'+$item_id).trigger('click');
		}
		else{
			$('.js_selectLabel').show();
			$('.js_contactAdditional').hide();
		}
		
		//$(this).parent('div').css('display','none');

		callStatsService('10',$item_id);
		
		
	 });
	 


/**
 * Display organisation select option when multiple organisations are assigned to one profile
 *
 * Author: waldemarm
 */
	$('.js_SelectPrintOrganisation').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		
		/** create highslide object **/
		hs.htmlExpand(this, {
			preserveContent: false, //disable iframe content caching
			objectType: 'iframe',
			width: 645,
			objectHeight: 540,
			objectLoadTime: 'after',
			allowWidthReduction: 1,
			showCredits: false,
			dimmingOpacity: 0.70,
			align: 'center'
		}
		);
	});



	/**
	 * SHOW VIDEO
	 *
	 * Author: waldemar.matlosz
	 **/
	$('#yacht_video').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'
		}
		);
	});


	/**
	 * Star rating stuff
	 *
	 * Author: waldemar.matlosz
	 */	
			/**
			 * LOGIN AND ASSING THE PONTS
			 *
			 * Author: waldemar.matlosz
			 **/
			$('#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(){
				$(".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');
			});
		 }								
			

/** EOF STARS RATING SECTION **/
		
		
		$('.js_ShowDeckPlans').click(function(e){
			//$("a[rel='showDeckPlans']:first").trigger('click');	
			
			e.preventDefault();
			e.stopPropagation();
			
			/** create highslide object **/
			hs.htmlExpand(this, {
				preserveContent: false, //disable iframe content caching					  
				objectType: 'iframe',
				width: 1000,
				objectHeight: 640,
				objectLoadTime: 'after',
				allowWidthReduction: 1,
				showCredits: false,
				dimmingOpacity: 0.70,
				align: 'center'
			}
			);
			
			
			saveStatistics(34,$('#property_description_id').val(),null);	
			
			
		});
		
		$('.js_companyProfile').click(function(){
			saveStatistics(23,$('#property_description_id').val(),null);	
		});


		$("a[rel='showDeckPlans']").lightBox({
			imageBlank: '/resources/images/lightbox-blank.gif',
			imageLoading: '/resources/images/lightbox-ico-loading.gif',
			imageBtnClose: '/resources/images/lightbox-btn-close.gif',
			imageBtnPrev: '/resources/images/lightbox-btn-prev.gif',
			imageBtnNext: '/resources/images/lightbox-btn-next.gif',
			fixedNavigation:true								   
		});



	$('.floorTitle').each(function(){
								   	
	   $(this).find('a').lightBox({
		imageBlank: '/resources/images/lightbox-blank.gif',
		imageLoading: '/resources/images/lightbox-ico-loading.gif',
		imageBtnClose: '/resources/images/lightbox-btn-close.gif',
		imageBtnPrev: '/resources/images/lightbox-btn-prev.gif',
		imageBtnNext: '/resources/images/lightbox-btn-next.gif',
		fixedNavigation:true
	   });		
	});
	
	
	
	$('.roomPhoto').each(function(){
		$(this).find('.roomPhotosLightbox').lightBox({
		imageBlank: '/resources/images/lightbox-blank.gif',
		imageLoading: '/resources/images/lightbox-ico-loading.gif',
		imageBtnClose: '/resources/images/lightbox-btn-close.gif',
		imageBtnPrev: '/resources/images/lightbox-btn-prev.gif',
		imageBtnNext: '/resources/images/lightbox-btn-next.gif',
		fixedNavigation:true								   
		});
	});
	
	$('.roomPhotosLightboxHref').click(function(e){
		e.stopPropagation();
		e.preventDefault();
		$(this).parent().parent().find('.firstLightboxImage').trigger('click'); 
	});
	
	 	
		
		$('.lightboxAdditional').lightBox({
			imageBlank: '/resources/images/lightbox-blank.gif',
			imageLoading: '/resources/images/lightbox-ico-loading.gif',
			imageBtnClose: '/resources/images/lightbox-btn-close.gif',
			imageBtnPrev: '/resources/images/lightbox-btn-prev.gif',
			imageBtnNext: '/resources/images/lightbox-btn-next.gif',
			fixedNavigation:true								   
		});	  
	
	

	/** load lightbox **/
	   /*
	   $('a[@rel*=lightbox]').lightBox({
		imageBlank: '/resources/images/lightbox-blank.gif',
		imageLoading: '/resources/images/lightbox-ico-loading.gif',
		imageBtnClose: '/resources/images/lightbox-btn-close.gif',
		imageBtnPrev: '/resources/images/lightbox-btn-prev.gif',
		imageBtnNext: '/resources/images/lightbox-btn-next.gif',
		fixedNavigation:true
	   });
	   */
	   
	   
	/**
	 * Change currency handler
	 *
	 * Author: waldemar.matlosz
	 */	   
	$('select.js_CurrencyCode').change(function(){
			_setCookie('currency_code_id_preferred',$(this).val());	
			window.location.reload();		
	});
	
	
	
	$("#mycarousel").find("img").css("cursor","pointer");
	$("#mycarousel").find("img").click(function () {
		$("#overview_image").attr("src", $(this).attr("large_src") );
	});

	jQuery('#mycarousel').jcarousel({
		itemVisibleInCallback: function(carousel, item, i, state, evt) {
			if($('img',item).attr('image_source')){
				var source_image = $('img',item).attr('image_source');
				$('img',item).attr('src',source_image);
				$('img',item).removeAttr('image_source');
			}
		}								
		// Configuration goes here
	});	


});



saveStatistics = function(log_type_id, ref_id_1, ref_id_2){

	$.ajax({
	   type: "GET",
	   url: "/stats/utility",
	   data: 'log_type_id='+log_type_id+'&ref_id_1='+ref_id_1+'&ref_id_2='+ref_id_2,
	   dataType: "html",	
	   processData: true,
	   success: function(returnedData){
	   }
	 });		
	
}


getbranchDetails = function(current_node, property_profile_type_id, organisation_group_id){
	$.ajax({
	   type: "GET",
	   url: "/particulars/service/branch_details.htm",
	   data: 'organisation_group_id='+organisation_group_id+'&property_profile_type_id='+property_profile_type_id,
	   dataType: "html",	
	   processData: true,
	   success: function(returnedData){
			$('.js_ajax_branch_data').each(function(){
				$(this).remove();
			});
			$(current_node).after('<div class="js_ajax_branch_data" style="display: none;"><br />'+returnedData+'</div>');
			$('.js_ajax_branch_data').slideDown();
			initializeOnClickHighslide();
			//$('.js_BranchDetailsWrapper').html(returnedData);
			//$('.js_ajax_branch_data').slideDown();
	   }
	 });		
}

initializeOnClickHighslide = function(){
	/**
	 * Submit branch email handler 
	 *
	 * Author: waldemar.matlosz
	 */
	 
	$('.branchEmail > a').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		
		/** create highslide object **/
		hs.htmlExpand(this, {
			preserveContent: false, //disable iframe content caching
			objectType: 'iframe',
			width: 645,
			objectHeight: 570,
			objectLoadTime: 'after',
			allowWidthReduction: 1,
			showCredits: false,
			dimmingOpacity: 0.70,
			align: 'center'
		}
		);
	});
}



/**
 * Function which set cookie with name, value and lifetime passed as the function parameter 
 * @param 'cookieLifetime' in default is set to 365 days
 *
 * Author: waldemar.matlosz
 */
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: waldemar.matlosz
 */
function _getCookie(cookieName){
		return $.cookie(cookieName);
}	
	


/**
 * Function which call service and allow to rate the yacht 
 *
 * Author: waldemar.matlosz
 */
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);
		   }
		 });		
}

