jQuery().ready(function(){
	
	//set the format for the dates with the datepicker
	jQuery.datePicker.setDateFormat('mdy', '/');

	//allow for rel="external" to open a new winder
	jQuery('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
    
	
	//1 day after arrival (yosi)
	$('#arrivalDate').change(function () {
		var dateA = new Date($(this).val());
		dateA.setDate(dateA.getDate() + 1);
		dateA = dateA.toLocaleFormat('%m/%d/%Y');
		$('#departureDate').val(dateA)
	});
	
    // toggle specials details
    jQuery(".more-details").click(function(){
    	jQuery(this).siblings(".special-details").toggle('slide', {direction: 'up'}, 500);
    	if(jQuery(this).html() == "More Details"){
    		jQuery(this).html("Hide Details");
			s = s_gi(s_account);
			s.linkTrackVars = 'prop34';
			s.prop34 = $(this).attr('rel');
			s.tl(this, 'o', 'Specials More Details Clicks');
    	} else {
    		jQuery(this).html("More Details");
    	}
    	return false;
    });
    
    
    //handle the huge carousel on the home page
    if (jQuery('#featuredResorts')[0]) {
    	jQuery('#featuredResorts').children(':not(.display)').hide();
    	jQuery('#featurednav').children().children().click(function(){
    		var el = jQuery(this);
    		var resortId = el.children().attr('rel');
    		jQuery('#featurednav ul li a.selected').removeClass('selected');
    		el.children().addClass('selected');
    		jQuery('#featuredResorts').children().hide();
    		jQuery('#resort' + resortId).show('slide', {direction: 'right'}, 500);
    		//start the slideshow and stop the other one.
    		toggleSlideShow(resortId);
    		omniture_trackHomePageViews(el.children().html());
    		return false;
    	});
    	
    	//make sure the photogallery is photogallering
    	var firstResort = jQuery('#featurednav ul li a.selected').attr('rel');
    	toggleSlideShow(firstResort);
    }
    
    //on the individual resorts pages, we need to make the slideshow work as well
    if (jQuery('.slideshow-single')[0]) {
    	slideshowid = jQuery('.slideshow-single').attr('id').replace('slideshow', '');
    	toggleSlideShow(slideshowid);
    }
    
    //make the date pickers actually do something
    jQuery('.date-picker').datePicker();
    
    //if we're on a resort photo gallery page, make some magic WOW happen
    if (jQuery('#galleryThumbs')[0]) {
    	jQuery('#resortsPagesleft p').hide();
    	jQuery('#resortsPagesleft p:first').show();
    	jQuery('#galleryThumbs a').hover(function(){
    		var el = jQuery(this);
    		var id = el.attr('id').replace('thumb', '');
    		jQuery('#prevImage').attr('rel', id);
    		jQuery('#nextImage').attr('rel', id);
    		jQuery('#resortsPagesleft p').hide();
    		jQuery('p#id' + id).show();
    		return false;
    	});
    	jQuery('#prevImage').click(function(){
    		var id = jQuery(this).attr('rel');
    		id = parseInt(id - 1);
    		jQuery('#prevImage').attr('rel', id);
    		jQuery('#nextImage').attr('rel', id);
    		jQuery('#resortsPagesleft p').hide();
    		jQuery('p#id' + id).show();
    		return false;
    	});
    	jQuery('#nextImage').click(function(){
    		var id = jQuery(this).attr('rel');
    		id = parseInt(parseInt(id) + 1);
    		jQuery('#prevImage').attr('rel', id);
    		jQuery('#nextImage').attr('rel', id);
    		jQuery('#resortsPagesleft p').hide();
    		jQuery('p#id' + id).show();
    		return false;
    	});
    }
    
    //and here's a photo gallery on the accommodations pages
    if (jQuery('ul.thumbnails')[0]) {
    	jQuery('.photos').children().hide();
    	jQuery('.photos').children(':first').show();
    	jQuery('ul.thumbnails li').hover(function(){
    		var el = jQuery(this);
    		var theId = el.attr('rel').replace('thumb', '');
    		jQuery('.photos').children().hide();
    		jQuery('#photo' + theId).show();
    	});
    }
	
	//shootout for the low rate guarantee
	if (jQuery('#lowrates')[0]) {
		jQuery('#lowrates').hoverIntent({
			over: function(){
				jQuery('#lowratesexpand').show('scale', { origin: ['top', 'left'] });
			},
			out: function() {}
		});
	}
	
	//hide the low rate guarantee when they click on it.
	if (jQuery('#resexpand')[0]) {
		jQuery('#resexpand a.button').click(function(){
			jQuery('#resexpand').hide('slow');
			return false;
		});
	}
	
	//shootout for the call today for reservations
	if (jQuery('#reserv')[0]) {
		jQuery('#reserv').hoverIntent({
			over: function(){
				jQuery('#resexpand').show('scale', { origin: ['top', 'left'] });
			},
			out: function() {}
		});
	}
	
	//hide the call today for reservations when they click on it.
	if (jQuery('#lowratesexpand')[0]) {
		jQuery('#lowratesexpand a.button').click(function(){
			jQuery('#lowratesexpand').hide('slow');
			return false;
		});
	}
    
    //if we're on the homepage, make the date picker actually show up and do something -- bottom, right --->
    if (jQuery('#datepicker')[0]) {
		jQuery("#datepicker").datepicker({
			onSelect: function(dateText, inst) {
				var theDate = dateText.split('/');
				var d = theDate[0] + '/' + theDate[1] + '/' + theDate[2];
				window.location = '/events/?d=' + d;
			}
		});
	}
	
	//the homepage has a gallery all to itself at the top of the page
	if (jQuery('#homeimage')[0]) {
		jQuery('#homeimage').cycle({
			fx:			'fade',
			speed:		1000,
			timeout:	5000
		});
	}
	
	//run the equal columns script
	equalHeight(jQuery(".column"));
	
	//specials clicks omniture
	if (jQuery('#specialoffers .offer')[0]) {
		jQuery('#specialoffers .offer a').click(function() {
			el = jQuery(this);
			var special = el.parent().children(':first').html();
			if (jQuery('#featurednav')[0]) {
				var resort = jQuery.trim(jQuery('#featurednav ul li a.selected').html());
			}
			else {
				var resort = jQuery.trim(jQuery('h1').html());
			}
			omniture_trackSpecialBookClicks(resort + ': ' + special);
		});
	}
	
	//get the check rates/book now buttons on search panel to auto-search
	if (jQuery('.checkRatesResort')[0]) {
		jQuery('.checkRatesResort a').click(function(){
			var arv = jQuery('#arrivalDate').val();
			var dep = jQuery('#departureDate').val();
			jQuery(this).attr('href', jQuery(this).attr('href') + '&checkin=' + arv + '&checkout=' + dep);
			return true;
		});
	}

});

var toggleSlideShow = function(resortId) {
	if (jQuery('#slideshow' + resortId).children().length > 1) {
		jQuery('#slideshow' + resortId).cycle({
										fx:			'fade',
										next:		'#next' + resortId,
										prev:		'#previous' + resortId
		});
		jQuery('#playpause' + resortId).click(function(){
			if (jQuery(this).attr('rel') == 'play') {
				jQuery('#slideshow' + resortId).cycle('resume');
				jQuery(this).attr('rel', 'play');
				jQuery(this).children().attr('src', 'media/layout/control-play.gif'); //image to play button
			}
			else {
				jQuery('#slideshow' + resortId).cycle('pause');
				jQuery(this).attr('rel', 'pause');
				jQuery(this).children().attr('src', '/media/layout/control-pause.gif'); //image to pause button
			}
			return false;
		});
	}
}

//reservations search form
var checkfields = function() {
		URLVars='siteID='+jQuery('#siteID').val()+'&checkin='+jQuery('#arrivalDate').val()+'&checkout='+jQuery('#departureDate').val()+'&adults='+jQuery('#adults').val()+'&children='+jQuery('#children').val()+'&rooms='+jQuery('#rooms').val()+'&bookedOnrefLoc='+jQuery('#bookedOnrefLoc').val();
		document.formHO.action='https://secure.guestdesk.com/globalSearch/processing.cfm?'+URLVars; document.formHO.submit(); //}
}

//equal column height
var equalHeight = function(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

// omniture code //

//track views to home page resorts
var omniture_trackHomePageViews = function(resort){
	s = s_gi(s_account);
	s.linkTrackVars = 'prop25';
	s.prop25 = resort.toLowerCase();
	s.tl(this, 'o', resort.toLowerCase() + ' view');
}

var omniture_trackSpecialBookClicks = function(special) {
	s = s_gi(s_account);
	s.linkTrackVars = 'prop21';
	s.prop21 = special.toLowerCase();
	console.log(s.prop21);
	s.tl(this, 'o', special.toLowerCase() + ' click');
}
