var count = 0;

$('document').ready(function() {
	//$('h2.fancySlogan').clone().removeClass('fancySlogan').appendTo('h2.fancySlogan');
	
	var images = $('#slideshow').children().size();
	var spanInfo = $('span#spanInfo').attr('rel');
	$('#slideshow').after('<ul id="slideshow-nav">').cycle({ 
		fx:     'fade', 
		speed:  1000, 
		timeout: 5000, 
		pager:  '#slideshow-nav',
		// pause: 1, // killed b/c of conflict with play/pause button
		
		pagerAnchorBuilder: function(idx, slide) {
			count++;
			var markup = '<li><a href="#">' + count + '</a></li>'
			if (count == images && spanInfo != 'noMore') {
				markup += '<li>&nbsp; <a id="slideshowPlayPause" href="">Play/Pause</a></li>';
				// markup += '<li>&nbsp; <a id="photoGalleryLink" href="/photo-gallery.cfm">View All</a></li>'; // this adds view all link
			}
			return markup;
		} 
	});
	
	var pause = 0;
	$('#slideshowPlayPause').click(function() {
		if (pause == 0) {
			$('#slideshow').cycle('pause');
			pause = 1;
			return false;
		} else if (pause == 1) {
			$('#slideshow').cycle('resume');
			pause = 0;
			return false;
		}
	});
	
	count = 0;
	var deals = $('#hotDealsContainer').children().size();
	$('#hotDealsContainer').after('<ul id="hotDealsNav">').cycle({ 
		fx:     'scrollHorz', 
		speedIn:  1000,
		speedOut: 800,
		timeout: 8000, 
		pager:  '#hotDealsNav',
		pause: 1,
		next: '#hotDealsNavFwd',
		prev: '#hotDealsNavBack',
		
		pagerAnchorBuilder: function(idx, slide) {
			count++;
			var markup = '<li><a href="#">' + count + '</a></li>'
			if (count == deals) {
				markup += '<li> &nbsp; <a id="hotDealsLink" href="">See All</a></li>';
			}
			return markup;
		} 
	});
	
	count = 0;
	var golfDeals = $('#golfSpecialsContainer').children().size();
	$('#golfSpecialsContainer').after('<ul id="golfSpecialsNav">').cycle({ 
		fx:     'scrollHorz',
		delay: 2000,
		speedIn:  1000,
		speedOut: 800,
		timeout: 0, 
		pager:  '#golfSpecialsNav',
		pause: 1,
		next: '#golfSpecialsNavFwd',
		prev: '#golfSpecialsNavBack',
		
		pagerAnchorBuilder: function(idx, slide) {
			count++;
			var markup = '<li><a href="#">' + count + '</a></li>'
			if (count == golfDeals) {
				markup += '<li> &nbsp; <a id="golfDealsLink" href="">See All</a></li>';
			}
			
			return markup;
		} 
	});
	
	$('#photoGalleryLink').click(function() {
		var goHere = $(this).attr('href');
		window.location = goHere;
	});
	
	
	var hotDealsViewAll = $('#hotDealsViewAll').attr('rel');
	$('#hotDealsLink').click(function() {
		eval(hotDealsViewAll);
		return false;
	});
	
	var viewAll = $('#golfSpecialsViewAll').attr('rel');
	$('#golfDealsLink').click(function() {
		eval(viewAll);
		return false;
	});

	var message = 'Unauthorized use of content or photography from this website is strictly prohibited without the prior written consent of the website owner. To inquire about authorized use, please email your request to images@endlessfunresorts.com';

	/*
$('img').noContext().rightClick(function() {
		alert(message);
		return false;
	});
*/
	
	// Photo gallery with thumbs
	
	var currentImg = 1;
	
	var containerHeight = '400px' //$('#photoGallery img:visible').height(); <-- this wasn't working b/c the image wasn't loaded on document ready ... might have to rewrite this part
	$('#photoGallery').css('height',containerHeight);
	
	function switchImg(thisOne) {
		var thumbName = 'pic' + thisOne;
		var thumbNameImg = thumbName + 'Img';
		$('#photoGalleryThumbs img').css('border-color','#3b2b2b');
		$('#' + thumbName).css('border-color','#ffffff');
		$('#photoGallery img:visible').fadeOut(200);
		var containerHeight = $('#photoGallery #' + thumbName + 'Img').height();
		$('#photoGallery').css('height',containerHeight);
		$('#photoGallery #' + thumbName + 'Img').fadeIn(200);
		$('#photoGalleryDescriptions div:visible').hide();
		$('#' + thumbName + 'Desc').show();
	}
	
	var numImgs = $('#photoGallery img').length;
	
	$('#galleryPrev').click(function() {
		currentImg--;
		if (currentImg == 0) {
			currentImg = numImgs;
		}
		switchImg(currentImg);
		return false;
	});
	
	$('#galleryNext').click(function() {
		currentImg++;
		if (currentImg > numImgs) {
			currentImg = 1;
		}
		switchImg(currentImg);
		return false;
	});
	
	$('#photoGalleryThumbs img').hover(
		function() {
			var thumbName = $(this).attr('id');
			currentImg = thumbName.substr(3);
			parseInt(currentImg);
			var thumbNameImg = thumbName + 'Img';
			var currentName = $('#photoGallery img:visible').attr('id');
			if (thumbNameImg != currentName) {
				$('#photoGalleryThumbs img').css('border-color','#3b2b2b');
				$('#' + thumbName).css('border-color','#ffffff');
				$('#photoGallery img:visible').fadeOut(200);
				var containerHeight = $('#photoGallery #' + thumbName + 'Img').height();
				$('#photoGallery').css('height',containerHeight);				
				$('#photoGallery #' + thumbName + 'Img').fadeIn(200);
				$('#photoGalleryDescriptions div:visible').hide();
				$('#' + thumbName + 'Desc').show();
			}
		},
		function() {
			var nothing = 0;
		}
	);
	
	// retrieve saved itinerary window.open
	if ($('#retrieveItinerary').length > 0) {
		$('#retrieveItinerary').click(function() {
			window.open('https://secure.guestdesk.com/functions/loginMain.cfm?id=53','Itinerary','toolbar=yes,scrollbars=yes,location=no,resizable=yes,directories=no,status=yes,menubar=no,width=750,height=400,screenX=1,screenY=1,top=1,left=1');
		});
	}
	
	//show golf package popup
	$('#golfExitLink').click(function() {
		window.open('https://secure.guestdesk.com/golf/?id=53&siteID=IslandVista','golfPackages','toolbar=no,scrollbars=auto,location=no,resizable=no,directories=no,status=yes,menubar=no,width=790,height=450,screenX=1,screenY=1,top=1,left=1');
		stop();
	});
	$('.golfExitLink').click(function() {
		window.open('https://secure.guestdesk.com/golf/?id=53&siteID=IslandVista','golfPackages','toolbar=no,scrollbars=auto,location=no,resizable=no,directories=no,status=yes,menubar=no,width=790,height=450,screenX=1,screenY=1,top=1,left=1');
		stop();
	});
	
	//eDeals
	$('.hotDeal a').click(function() {
		s=s_gi(s_account);
		ratesName = $(this).attr('rel').toLowerCase();
		s.linkTrackVars = "prop19";
		s.prop19 = ratesName;
		s.tl(this, 'o', ratesName);
	});
	
	//main eDeal
	$('#currentEdeal div a').click(function(){
		s=s_gi(s_account);
		ratesName = $(this).parent().children(':first').html().toLowerCase();
		s.linkTrackVars = "prop18";
		s.prop18 = ratesName;
		s.tl(this, 'o', ratesName);
	});
	
	$('#pdf').click(function() {
			s=s_gi(s_account);
			s.linkTrackVars="events,prop25";
			s.linkTrackEvents = "event14";
			s.events = "event14";
			s.prop25 = $(this).attr('rel');
			s.prop25 = s.prop25.toLowerCase();
			s.tl(this,'o','Download Amenities PDF');		 
		});
	
	initDateRanges();
});

// ----------------------------------------------------------------
// initExpansionBlocks
// - initializes the date ranges and binds a callback
// ----------------------------------------------------------------
function initDateRanges(){
		
	// set the date format for the calendar plugin
	jQuery.datePicker.setDateFormat('mdy', '/');
	
	// get the package date ranges
	var datesBetween = jQuery("input#calNumberOfNights").val();
	var startingDate = jQuery("input#calStartDate").val();
	var endingDate = jQuery("input#calCutOffDate").val();

	jQuery('input#arrivalDate').datePicker({startDate:startingDate, endDate:endingDate});
	jQuery('input#departureDate').datePicker({startDate:startingDate, endDate:endingDate});
	
	// will move the departure date depending on a few rules:
	// the minimum nights stay, the departure date is before the arrival date
	jQuery('input#arrivalDate').bind('change',function(){
		var checkInArray = jQuery(this).val().split('/');
		var checkOutArray = jQuery("input#departureDate").val().split('/');
		var numberOfNights = jQuery("input#calNumberOfNights").val();
		var tempCheckInDate = new Date(Number(checkInArray[2]),Number(checkInArray[0]-1),Number(checkInArray[1])+Number(numberOfNights));
		var tempCheckOutDate = new Date(checkOutArray[2],checkOutArray[0]-1,checkOutArray[1]);
		if(tempCheckOutDate < tempCheckInDate){
			tempCheckOutDate = tempCheckInDate;
			var newCheckOutString = "";
			if(tempCheckOutDate.getMonth() < 9){
				newCheckOutString = "0"+(tempCheckOutDate.getMonth()+1);
			} else {
				newCheckOutString = tempCheckOutDate.getMonth()+1;
			}
			if(tempCheckOutDate.getDate() < 10){
				newCheckOutString += "/0"+tempCheckOutDate.getDate();
			} else {
				newCheckOutString += "/"+tempCheckOutDate.getDate();
			}
			newCheckOutString += "/"+tempCheckOutDate.getFullYear();
			jQuery("input#departureDate").val(newCheckOutString);
			//alert(tempCheckOutDate.getMonth()+"/"+tempCheckOutDate.getDate()+"/"+tempCheckOutDate.getFullYear());
		}
	});
	
	// for testing/debugging
	jQuery('input#departureDate').bind('change',function(){
			//alert(jQuery(this).val());
		}
	);
}

// ----------------------------------------------------------------