// *********************************
// Devon Hotels JS Header Scripts
// Ed Rackham / Clockwork Marketing
// Monday 23rd Nov 2009
// *********************************

$.preloadImages("images/quick_search/restaurants_on.gif", "images/quick_search/hotels_off.gif");

var currentFeature 		= 'home';
var currentDetailTab 	= 'Main';
var currentSearchTab 	= 'Hotels';
var featureZIndex 		= 999;
var gMarkers 			= [];
var gPopupTexts			= [];
var gDetailTexts		= [];
var gLats				= [];
var gLons				= [];
var map;

// ---------------------------------
// Functions
// ---------------------------------

function toggleImage(imageName, elementId){
	$('#'+elementId).attr("src", '../../images/'+imageName);
}

function newsletterSubscribe(){
	$('#hiddenNext').click();
	$.post("../../inc/newsletter.php", $("#NewsletterSubscriberForm").serialize(),
	function(data){
		$('#newsletterSignupResult').html(data);
	});
}

function newsletterSubscribeFeature(){
	$('#hiddenNextFeature').click();
	$.post("../../inc/newsletter.php", $("#NewsletterSubscriberFormFeature").serialize(),
	function(data){
		$('#newsletterSignupResultFeature').html(data);
	});
}

function showPopup(iBusinessId){
	$('#featuremapItemDetails').hide();
	gMarkers[iBusinessId].openInfoWindow(gPopupTexts[iBusinessId]);
	$('#featuremapItemDetails').html(gDetailTexts[iBusinessId]);
	$('#featuremapItemDetails').fadeIn("slow");
	map.panTo(new GLatLng(gLats[iBusinessId], gLons[iBusinessId]));
}

function showRestaurantList(){
	$('#hotelBusinessList').hide();
	$('#restaurantBusinessList').show();
}

function showHotelList(){
	$('#restaurantBusinessList').hide();
	$('#hotelBusinessList').show();
}

$(document).ready(function(){
	
	jQuery(function($) {
		$("img").pngfix();
	});
	
	// ----------------------------------------------------------------------------
	//
	// Quick Search AJAX
	//
	// ----------------------------------------------------------------------------
	
	var hotelDetailPage 		= 'http://www.devonhotels.com/hotel';
	var restaurantDetailPage 	= 'http://www.devonhotels.com/restaurant';
	var searchPage 				= 'http://www.devonhotels.com/search';
	
	// HOTELS
	$("select#HotelArea").change(function(){
		$.getJSON("/inc/quick_search.php",{town: $(this).val(), 
										   ideal: $("select#HotelIdealFor").val(), 
										   type: 'hotel', 
										   ajax: 'true'}, function(j){
			
			var theOptions = '<option value="">-- All --</option>';
			
			if(j[0].id == 0){
				theOptions = '<option value="">-- NO RESULTS --</option>';
			}else{
				for (var i = 0; i < j.length; i++) {
					theOptions += '<option value="' + j[i].id + '">' + j[i].title + '</option>';
				}
			}
			$("#HotelQuickSearchSubmit").attr('src', 'images/quick_search/search.gif');
			$("select#HotelNames").html(theOptions);
			$("#hotelResultsUpdated").fadeIn("slow").animate({opacity: 1.0}, 1000).fadeOut("slow");
		})
	})
	
	$("select#HotelIdealFor").change(function(){
		$.getJSON("/inc/quick_search.php",{town: $("select#HotelArea").val(), 
										   ideal: $(this).val(), 
										   type: 'hotel', 
										   ajax: 'true'}, function(j){
			
			var theOptions = '<option value="">-- All --</option>';
			
			if(j[0].id == 0){
				theOptions = '<option value="">-- NO RESULTS --</option>';
			}else{
				for (var i = 0; i < j.length; i++) {
					theOptions += '<option value="' + j[i].id + '">' + j[i].title + '</option>';
				}
			}
			$("#HotelQuickSearchSubmit").attr('src', 'images/quick_search/search.gif');
			$("select#HotelNames").html(theOptions);
			$("#hotelResultsUpdated").fadeIn("slow").animate({opacity: 1.0}, 1000).fadeOut("slow");
		})
	})
	
	$("select#HotelNames").change(function(){
		$.get("/inc/quick_search.php",{ajax: 'true', hotelName: $(this).val()});
		
		if($(this).val() != 0 && $(this).val != ''){
			$("#HotelQuickSearchSubmit").attr('src', 'images/quick_search/view_details.gif');
			$("#HotelQuickSearchForm").attr('action', hotelDetailPage + '/' + $(this).val());
		}else{
			$("#HotelQuickSearchSubmit").attr('src', 'images/quick_search/search.gif');
			$("#HotelQuickSearchForm").attr('action', searchPage);
		}
	})
	
	// RESTAURANTS
	$("select#RestaurantArea").change(function(){
		$.getJSON("/inc/quick_search.php",{town: $(this).val(), 
										   ideal: $("select#RestaurantIdealFor").val(), 
										   type: 'restaurant', 
										   ajax: 'true'}, function(j){
			
			var theOptions = '<option value="">-- All --</option>';
			
			if(j[0].id == 0){
				theOptions = '<option value="">-- NO RESULTS --</option>';
			}else{
				for (var i = 0; i < j.length; i++) {
					theOptions += '<option value="' + j[i].id + '">' + j[i].title + '</option>';
				}
			}
			$("#RestaurantQuickSearchSubmit").attr('src', 'images/quick_search/search.gif');
			$("select#RestaurantNames").html(theOptions);
			$("#restaurantResultsUpdated").fadeIn("slow").animate({opacity: 1.0}, 1000).fadeOut("slow");
		})
	})
	
	$("select#RestaurantIdealFor").change(function(){
		$.getJSON("/inc/quick_search.php",{town: $("select#RestaurantArea").val(), 
										   ideal: $(this).val(), 
										   type: 'restaurant', 
										   ajax: 'true'}, function(j){
			
			var theOptions = '<option value="">-- All --</option>';
			
			if(j[0].id == 0){
				theOptions = '<option value="">-- NO RESULTS --</option>';
			}else{
				for (var i = 0; i < j.length; i++) {
					theOptions += '<option value="' + j[i].id + '">' + j[i].title + '</option>';
				}
			}
			$("#RestaurantQuickSearchSubmit").attr('src', 'images/quick_search/search.gif');
			$("select#RestaurantNames").html(theOptions);
			$("#restaurantResultsUpdated").fadeIn("slow").animate({opacity: 1.0}, 1000).fadeOut("slow");
		})
	})
	
	$("select#RestaurantNames").change(function(){
		$.get("/inc/quick_search.php",{ajax: 'true', hotelName: $(this).val()});
		
		if($(this).val() != 0 && $(this).val != ''){
			$("#RestaurantQuickSearchSubmit").attr('src', 'images/quick_search/view_details.gif');
			$("#RestaurantQuickSearchForm").attr('action', restaurantDetailPage + '/' + $(this).val());
		}else{
			$("#RestaurantQuickSearchSubmit").attr('src', 'images/quick_search/search.gif');
			$("#RestaurantQuickSearchForm").attr('action', searchPage);
		}
	})

	
	// ----------------------------------------------------------------------------
	//
	// Quick Search Buttons
	//
	// ----------------------------------------------------------------------------
	$('#quickSearchHotelBtn').click(function(){
		$('#quickSearchHotelBtn').attr('src', 'images/quick_search/hotels_on.gif');
		$('#quickSearchRestaurantBtn').attr('src', 'images/quick_search/restaurants_off.gif');
		$('#restaurantQuickSearch').hide();
		$('#hotelQuickSearch').show();
	});
	
	$('#quickSearchRestaurantBtn').click(function(){
		$('#quickSearchRestaurantBtn').attr('src', 'images/quick_search/restaurants_on.gif');
		$('#quickSearchHotelBtn').attr('src', 'images/quick_search/hotels_off.gif');
		$('#hotelQuickSearch').hide();
		$('#restaurantQuickSearch').show();
	});
	
	$('#scrollOffers').cycle({ 
		fx:     'scrollHorz', 
		speed:  'slow', 
		timeout: 5000, 
		next:   '#slideRight', 
		prev:   '#slideLeft' 
	});
	
	$('#newsletterScroll').cycle({ 
		fx:     'scrollHorz', 
		speed:  500, 
		timeout: 0, 
		next:   '#hiddenNext', 
		prev:   '#hiddenPrev' 
	});
	
	$('#newsletterScrollFeature').cycle({ 
		fx:     'scrollHorz', 
		speed:  500, 
		timeout: 0, 
		next:   '#hiddenNextFeature', 
		prev:   '#hiddenPrevFeature' 
	});
});