/*
 * Jquery actions for non modals
 */
$(document).ready(function() {

	var autoScroll = false;
	var imageCount = jQuery('#slider>ul>li').size()
	if(imageCount > 1) {
		autoScroll = true;
	}
	$(".slidershow").easySlider({
		auto: autoScroll,
		continuous: true,
		numeric: true,
		speed: 1500,
		pause: 5000
	});
	
	updateShortlist();
	
	$('.expand').expander({
		'slicePoint':	100,
		expandText: 'more',
		expandPrefix: 'É '
	});
	
	$("#mainmenu li").hover(function() {
		$(this).find("ul").attr('style', 'width: 160px; visibility: visible;');
	}, function() {
		$(this).find("ul").attr('style', 'width: 160px; visibility: hidden;');
	});
	
	$("a.bookmark").click(function(e){
		e.preventDefault();
		var bookmarkUrl = $(location).attr('href');
		
		if (window.sidebar) { // For Mozilla Firefox Bookmark
			window.sidebar.addPanel(bookmarkUrl, bookmarkUrl,"");
		} else if( window.external || document.all) { // For IE and Chrome Favorite
			if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
				showErrorToast('Your browser does not support this bookmark action');
				return false;			
			} else {
				window.external.AddFavorite( bookmarkUrl, bookmarkUrl);
			}
		} else if(window.opera) { // For Opera Browsers
			$("a.bookmark").attr("href",bookmarkUrl);
			$("a.bookmark").attr("title",bookmarkUrl);
			$("a.bookmark").attr("rel","sidebar");
		} else { // for other browsers which does not support
			showErrorToast('Your browser does not support this bookmark action');
			return false;
		}
	});
	
	$("#destinationarea" ).tabs();
	$("#propertyarea" ).tabs({'selected': 1});
	$("#shortlistarea" ).tabs();
	
	$('#quicksSearchText').setDefaultValue();
	$("#quicksSearchText").autocomplete( {
		source : function(request, response) {
			$.ajax( {
				url : "\/en\/search\/quick-search",
				dataType : "json",
				data : {
					term : request.term
				},
				success : function(data) {
					response(data);
				}
			});
		},
		minLength : 3,
		delay : 0
	});
	
	$('.newsl').setDefaultValue();
	$("#footernewsletter_form").submit(function() {	
		var valid = jQuery("#footernewsletter_form").validationEngine('validate');
		if(valid) {
			var str = $("#footernewsletter_form").serialize();
			$.ajax( {
				type : "POST",
				url : "/en/newsletter/add",
				data : str,
				success : function(msg) {
					if (msg == true) {
						showStickySuccessToast('Thank you for subscribing to our newsletter.' + 
								'Now you can be the first to know of our deals and special offers.');
					} else {
						showStickyErrorToast('Sorry. We were unable process your request. Please try again.');
					}
				}
			});
		}
	    return false;
	});
	
	$("#newsletter_form").submit(function() {	
		var valid = jQuery("#newsletter_form").validationEngine('validate');
		if(valid) {
			var str = $("#newsletter_form").serialize();
			$.ajax( {
				type : "POST",
				url : "/en/newsletter/add",
				data : str,
				success : function(msg) {
					if (msg == true) {
						showStickySuccessToast('Thank you for your interest');
					} else {
						showStickyErrorToast('Sorry. We were unable process your request. Please try again.');
					}
				}
			});
		}
	    return false;
	});
	
	$("#brochure_form").submit(function() {	
		var valid = jQuery("#brochure_form").validationEngine('validate');
		if(valid) {
			var str = $("#brochure_form").serialize();
			$.ajax( {
				type : "POST",
				url : "/en/brochure/index",
				data : str,
				success : function(msg) {
					if (msg == true) {
						showStickySuccessToast('Thank you for your request. It will be processed shortly.');
					} else {
						showStickyErrorToast('Sorry. We were unable process your request. Please try again.');
					}
				}
			});
		}
	    return false;
	});
	
	$("#quote_form").submit(function() {	
		var valid = jQuery("#quote_form").validationEngine('validate');
		if(valid) {
			var str = $("#quote_form").serialize();
			$.ajax( {
				type : "POST",
				url : "/en/quote/index",
				data : str,
				success : function(msg) {
					msg = jQuery.parseJSON( msg );
					if(msg._id) {
						showStickySuccessToast('Thank you for your request. It will be processed shortly.');
						var tag = '<img src="https://147.xg4ken.com/media/redir.php?' + 
							'track=1&token=98f912ac-2c64-4f78-86f2-a55736dbbae5&' +
							'type=Lead_Confirm_VFB&val=0.0&orderId=' + msg._id +
							'&promoCode=' + msg._pax + '|' + msg._departureAirport + '|' + msg._city +
							'&valueCurrency=GBP"' +
							'width="1" height="1">';
							$("#quotediv").html('Thank you for your request. It will be processed shortly.' + tag);						
					} else {
						showStickyErrorToast('Sorry. We were unable process your request. Please try again.');
					}
				}
			});
		}
	    return false;
	});
	
	$(".addshortlist").click(function() {
		var id = $(this).attr('id');
		$.getJSON('/en/shortlist/add?property='+id, function(msg) {
			if (msg == true) {
				showStickySuccessToast('Property shortlisted');
				updateShortlist();
			} else {
				showStickyErrorToast('Sorry. The property could not be shortlisted. Please try again');
			}
		});
	});
	
	$(".removeshortlist").click(function() {
		var id = $(this).attr('id');
		$.getJSON('/en/shortlist/remove?property='+id, function(msg) {
			if (msg == true) {
				showStickySuccessToast('Shortlisted property removed');
				window.location.reload();
			} else {
				showStickyErrorToast('Sorry. The property shortlist could not be removed. Please try again');
			}
		});
	});		

});

function updateShortlist() {
	$.getJSON('/en/shortlist/count', function(count) {
		if (count) {
			$(".shortlistnumber").text(count);
		}
	});	
}
