var count = 0;

$('document').ready(function() {
	
	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;
		}
	});
	
	$('#hotDealSpecials').cycle({
		fx: 'scrollHorz',
		timeout: 8000
	});
		
	count = 0;
	var golfDeals = $('#golfSpecialsCaro').children().length;
	$('#golfSpecialsCaro').cycle({ 
		delay: 2000,
		speedIn:  1000,
		speedOut: 800,
		timeout: 0, 
		pager:  '#golfSpecialsNav',
		next: '#golfSpecialsNext',
		prev: '#golfSpecialsPrev',
		pause: 1
	});
	
	$('#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;
	});
	
	// 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=161','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=161&siteID=captainsquarters','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=161&siteID=captainsquaters','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 = "prop16,eVar22";
		s.prop16 = ratesName;
		s.eVar22 = s.prop16;
		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 = "prop16,eVar22";
		s.prop16 = ratesName;
		s.eVar22 = s.prop16;
		s.tl(this, 'o', ratesName);
	});
	
	//home page video clicks
	$('#videoclick').click(function(){
		s=s_gi(s_account);
		s.linkTrackVars = "prop21";
		s.prop21 = "Home page video click";
		s.tl(this, 'o', "Video Click");
	});
	
	//Room Layout Views
	$('.accomodation').click(function(){
		s=s_gi(s_account);
		s.linkTrackVars = "prop18";
		s.prop18 = $(this).html();
		s.tl(this, 'o', "Room Layout");
	});
	
	//Amenities Views
	//$('.amenities').click(function(){
		//s=s_gi(s_account);
		//s.linkTrackVars = "prop22";
		//s.prop22 = $(this).html();
		//s.tl(this, 'o', "Amenities View");
	//});
	
	//home page above hot deals clicks
	$('.booknow-bttn').click(function(){
		s=s_gi(s_account);
		s.linkTrackVars = "prop16";
		s.prop16 = $('.orange').html();
		s.tl(this, 'o', "Hot deals Clicks");
	});
	
	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 = 1; //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());
		}
	);
}

// ----------------------------------------------------------------