var count = 0;

$('document').ready(function() {
	
	<!--- //catch all links and add mbox// --->
	$('a').live('click', function() {
		var linkOut = $(this).attr('href');
		if(linkOut.indexOf('search.iqrez') >0 || linkOut.indexOf('search.guestdesk') >0 || linkOut.indexOf('secure.guestdesk') >0) {
			if(linkOut.indexOf('?')) {
				linkOut = linkOut + '&mboxSession=' + mboxFactoryDefault.getSessionId().getId()
			} else {
				linkOut = linkOut + '?mboxSession=' + mboxFactoryDefault.getSessionId().getId()
			}
			//guestdesk analytics without referral implementation - yosi
			pageTracker._link(linkOut);
			return false;
		}
	})
							 
	$('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 ($('#retrieveItinerary2').length > 0) {
		$('#retrieveItinerary2').click(function() {
			window.open('https://secure.guestdesk.com/functions/loginMain.cfm?id=462','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');
			s_objectid = 'retrieveSavedItinerary';
			return false;
		});
	}
	
	//eDeals
	$('.trackEdeal').click(function() {
		s=s_gi(s_account);
		ratesName = $(this).attr('rel').toLowerCase();
		s.linkTrackVars="prop18";
		s.prop18=ratesName;
		s.tl(this, 'o', ratesName);
	});
	
	//golf deals
	if ($('.checkGolfLink').length > 0) {
		$('.checkGolfLink').click(function() {
			s=s_gi(s_account);
			ratesName = $(this).attr('rel').toLowerCase();
			s.linkTrackVars="prop19";
			s.prop19=ratesName;
			s.tl(this, 'o', ratesName);
		});
	}
	
	$('.trackSpecials').click(function(){
		s=s_gi(s_account);
			ratesName = $(this).attr('rel').toLowerCase();
			s.linkTrackVars="prop18";
			s.prop18=ratesName;
			s.tl(this, 'o', ratesName);
	});
	
	if ($('#memoryPhoto').length > 0) {
		$('#memoryPhoto').click(function(){
			var theHTML = '';
			theHTML += '<div>';
			theHTML += '<input id="filename" class="input" type="file" name="filename" />';
			theHTML += '</div>';
			$('#memoryUpload').html(theHTML);
			return false;
		});
	}
	
	//vip card page print link
	if ($('.omniPrint').length > 0) {
		$('.omniPrint').click(function() {
			s=s_gi(s_account);
			s.linkTrackVars="events";
			s.linkTrackEvents="event29";
			s.events = 'event29';
			s.tl(this, 'o', 'VMB print page');
		});
	}
	
});
