
//Your JQuery Code Here
$(document).ready(function(){

	//Home Page
	
		//Drop Down
		$('nav li:has(ul)').hover(function(){
		    $(this).find('ul').fadeIn(400);
		  },function(){
		    $(this).find('ul').stop(true, true).fadeOut(200);
		  }
		);
		
		//Rotate Box
		
			//Shows First Item
			$('#rotate-list li:nth-child(1)').show();
						
			//Cycle Through Images
			var timeout = 10000;
			$('#rotate-list').cycle({fx:'fade',speed:1000,timeout:timeout,pager:'#rotate-thumbs',
				//Timeline
				before:function(){$('.timeline').animate({width:'0px'}, 0).stop().animate({width:'755px'}, timeout);}
			});
			
			//Adds images to thumbs
			$('#rotate-thumbs a:nth-child(1)').addClass('thumb1');
			$('#rotate-thumbs a:nth-child(2)').addClass('thumb2');
			$('#rotate-thumbs a:nth-child(3)').addClass('thumb3');
			$('#rotate-thumbs a:nth-child(4)').addClass('thumb4');
			
		//Suites Highlighting
		$('.suites-info li:nth-child(2)').hover(function(){
				$('.suites-info li:nth-child(1)').removeClass('active');
				$(this).addClass('active');
			},function(){
				$('.suites-info li:nth-child(1)').addClass('active');
				$(this).removeClass('active');		
			}
		);
		
		//advanced search toggle
		$(".adv-search").click(function(){
			//$("#advanced").slideToggle(400);return false;
		});
		
		//Faqs Box
		
			//Question Hover Effect
			$('.faq li a').hover(function(){
					$(this).addClass('active');
				},function(){
					$(this).removeClass('active');
				}
			);
			
			//Answer Button Toggles Answers
			$('.faq .rabutton').click(function(){
				$('.faq li a').toggleClass('active');
			});
			
		//Right Angle Button Highlight
		$('a.rabutton').hover(function(){
				$(this).addClass('rabutton-hover');
			},function(){
				$(this).removeClass('rabutton-hover');
			}
		);
		
		//Footer Contact Form
		$('#qcname').focus(function(){$(this).val('');});		
		$('#qcemail').focus(function(){$(this).val('');});		
		$('#qccomments').focus(function(){$(this).val('');});		
		$('#qcform').submit(function(){			
			var name = $('#qcname').val();
			var email = $('#qcemail').val();
			var email2 = $('#qcemail2').val();			
			if(email2 != ''){alert('You are spam go away!');return false;}			
			if(name == '' || email == '' || name == 'Name' || email == 'Email'){
				alert('Name and Email are required fields.');
				$('#qcform #qcname').css('color','red');
				$('#qcform #qcemail').css('color','red');				
				return false;
			}else{
				
				$.post('/submit/quickcontact', $('#qcform').serialize(),function(){
			  		$('#qcform').hide();
				    $('#qcmessage').html('Thanks! We have received your information.');
			  	});
			  			
				return false;
			}			
		});
	
	//Contact Page	
			
		//Contact Form
		$('#contactform').submit(function(){		
			var firstname = $('#contactfirstname').val();
			var lastname = $('#contactlastname').val();
			var email = $('#contactemail').val();				
			if(firstname == '' || lastname == '' || email == ''){
				alert('Name and Email are required fields.');
				$('#contactemail').css('border','1px solid red');
				$('#contactfirstname').css('border','1px solid red');
				$('#contactlastname').css('border','1px solid red');
				return false;
			}else{
				return true;
			}
		});	
		
	//Services and Amenities
	
		//Finds anchor names and matches them per tab
    $('.options a').click(function(){
        tab = $(this).parents('.column').attr('id');
        $('.document').addClass('hidden');
        $('#documents li').removeClass('active');
        $("#documents a[href=\'#!/"+tab+"\']").parents('li').addClass('active');
        $("#doc-"+tab).removeClass('hidden');
        return true;
    });
    //Controls the viewable tabbed categories
    $('#documents a').click(function() {
        $('#documents li').removeClass('active');
        $(this).parents('li').addClass('active');
        $('.document').addClass('hidden');
        $("#doc-"+$(this).attr('href').substr(3)).removeClass('hidden');
    });
    //Matches hash links
    if(window.location.href.indexOf("#") != -1) {
        if($("#documents a[href=\'"+window.location.hash+"\']").length > 0) {
					 $("#documents a[href=\'"+window.location.hash+"\']").click();
        }else{
           $(".options a[href=\'"+window.location.hash+"\']").click();
        }
    }	
		
	//Explore St Thomas
	
		//Image Slider
		$('.slider ul').cycle({ 
			fx:'scrollHorz', 
			prev:'.arrow-left', 
			next:'.arrow-right',
			timeout:6000,
			delay:-1000
		});
		
		//Fancy Box - Lightbox for Slider Images
		$(".slider li a[rel=group]").fancybox({
			'transitionIn':'none',
			'transitionOut':'none'
		});
	
	//All Pages
		
		//All H1's - add span
		$("h1").html(function(i,text){
				return text.replace(/\w+\s/, function(match){
	    	return '<span>' + match + '</span>'; 
			});
		});
		
		
	//Clears the email on the Sign up for Exclusive Deals form	
	$('#signup').focus(function(){
		$(this).val('');
	});
	
	//Sign up for Exclusive Deals form on homepage	
	$('#signupform').submit(function(){
		
		var email1 = $('input[name=signupemail1]').val();
		var email2 = $('input[name=signupemail2]').val();
				
		if(email2 == ''){
			
			if(email1 == '' || email1 == 'Enter Email Address'){
				alert('Email is a required field.');
				return false;
			}else{
						
			  	$.post('/submit/index', $('#signupform').serialize(),function(){
			  		$('#signupform').hide();
				    $('#signupmessage').html('Thanks! We have received your information.');
			  	});
			  			
				return false;
				
			}
		
		}
	});		
		
});


$(document).ready(function(){
	
	//Used to submit the Send Property to a Friend Form
	$(".send-property-to-friend-form").submit(function(){
	
	var str = $(this).serialize();
	
	   $.ajax({
	   	type: "POST",
	   	url: "/rentals/sptafprocessor",
	   	data: str,
	   	success: function(msg){
    
			$("#message").ajaxComplete(function(event, request, settings){
	
				result = msg;				
		
				$(this).html(result);
		
				}); 
	
			} //end ajaxComplete

 			}); //end success

			return false;

		}); //end serialize


	//Used to submit the Reviews form
	$("#reviewForm").submit(function(){
	
	var str = $(this).serialize();
	
	   $.ajax({
	   	type: "POST",
	   	url: "/rentals/reviewprocessor",
	   	data: str,
	   	success: function(msg){
    
			$("#note_review").ajaxComplete(function(event, request, settings){
	
				result = msg;				
		
				$(this).html(result);
		
				}); 
	
			} //end ajaxComplete

 			}); //end success

			return false;

		}); //end serialize
		
		
		
	//Used to submit the Question about this Property form	
	$("#commentForm").submit(function(){
		
	var str = $(this).serialize();
	
	   $.ajax({
	   	type: "POST",
	   	url: "/rentals/contactprocessor",
	   	data: str,
	   	success: function(msg){
    
			$("#note_contact").ajaxComplete(function(event, request, settings){
	
				result = msg;				
		
				$(this).html(result);
		
				}); 
	
			} //end ajaxComplete

 			}); //end success

			return false;

		}); //end serialize

});


/*

All of these functions are used on /booking/property and control anything to do with Favorites except toggleFavIcon() which is used
on the /booking/results page

*/

$(document).ready(function() {
	
		
	/*---------------------------------------------------------View Favorites at top of page; data is loaded and then window slides down---*/
	
	$('a.b-vf-head').click(function() 
	{
		
		var yurl = "/rentals/detailqueryfavs";
	 	
		$.get(yurl,'',function(data) {							
			
			$('#b-booking .b-sptaf .b-vflist').html(data);
			
		});//$.get
	 
		//slide the 'View Favorites' window down
		$('.b-vf-header').slideToggle(400);		
					 
	});
	
			
});



/*---------------------------------------------------------Add to Favorites---------------------------

	- Adds unitid to session
	- Toggles the icon at top in _header.cfm
	- Toggles the icon on the right hand side in _footer.cfm
	- Changes the H5 in the drop down

*/


function detailAddToFavorites( propertyid ) {
		
		var yurl = "/rentals/detailaddfavorite?propertyid="+propertyid;

		$.get(yurl,'',function(data) {
			
			var response = jQuery.trim(data);			
							
			if (response !='') {
				
				if (response.search("~")) {
						
										
					//Toggle the right side icon
					$('.b-actions .b-toggle').toggle();
					
					//Toggle the top icon
					$('.b-quick-nav span li').toggle();	
					
					$('.b-atf-header').fadeIn(400).fadeTo(2000, 1).fadeOut(400);	
					
					//Change h5 in the top icon drop down
					$('.b-atf-header .b-sptaf h5').html('Added to Favorites');		

				} 
			}
		});//$.get
	 return false;

}

/*---------------------------------------------------------Delete from Favorites---------------------------

	- Deletes unitid from session
	- Toggles the icon at top in _header.cfm
	- Toggles the icon on the right hand side in _footer.cfm
	- Changes the H5 in the drop down

*/

function detailDeleteFromFavorites( propertyid ) {

	//remove it from favorites
	var yurl = "/rentals/detaildeletefavorite?propertyid="+propertyid;
	
	$.get(yurl,'',function(data) {							
			
		$('#b-booking .b-sptaf .b-vflist li#'+propertyid).remove();
		
		//Toggle the right side icon
		$('.b-actions .b-toggle').toggle();
		
		//Toggle the top icon
		$('.b-quick-nav span li').toggle();		
		
		$('.b-atf-header').fadeIn(400).fadeTo(2000, 1).fadeOut(400);
		
		//Change h5 in the top icon drop down
		$('.b-atf-header .b-sptaf h5').html('Removed from Favorites');
			
	});
		
	return false;

}


//Toggles the heart icon for favorites on /booking/results
function toggleFavIcon( unitid ) {
		
	//Toggle the 'Add to Favorite' icon
	$('.b-results .b-actions .b-toggle'+unitid).toggle();			

}

// IE5.5+ PNG Alpha Fix v2.0 Alpha: Background Tiling Support
// (c) 2008-2009 Angus Turnbull http://www.twinhelix.com

// This is licensed under the GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

var IEPNGFix = window.IEPNGFix || {};

IEPNGFix.tileBG = function(elm, pngSrc, ready) {
	// Params: A reference to a DOM element, the PNG src file pathname, and a
	// hidden "ready-to-run" passed when called back after image preloading.

	var data = this.data[elm.uniqueID],
		elmW = Math.max(elm.clientWidth, elm.scrollWidth),
		elmH = Math.max(elm.clientHeight, elm.scrollHeight),
		bgX = elm.currentStyle.backgroundPositionX,
		bgY = elm.currentStyle.backgroundPositionY,
		bgR = elm.currentStyle.backgroundRepeat;

	// Cache of DIVs created per element, and image preloader/data.
	if (!data.tiles) {
		data.tiles = {
			elm: elm,
			src: '',
			cache: [],
			img: new Image(),
			old: {}
		};
	}
	var tiles = data.tiles,
		pngW = tiles.img.width,
		pngH = tiles.img.height;

	if (pngSrc) {
		if (!ready && pngSrc != tiles.src) {
			// New image? Preload it with a callback to detect dimensions.
			tiles.img.onload = function() {
				this.onload = null;
				IEPNGFix.tileBG(elm, pngSrc, 1);
			};
			return tiles.img.src = pngSrc;
		}
	} else {
		// No image?
		if (tiles.src) ready = 1;
		pngW = pngH = 0;
	}
	tiles.src = pngSrc;

	if (!ready && elmW == tiles.old.w && elmH == tiles.old.h &&
		bgX == tiles.old.x && bgY == tiles.old.y && bgR == tiles.old.r) {
		return;
	}

	// Convert English and percentage positions to pixels.
	var pos = {
			top: '0%',
			left: '0%',
			center: '50%',
			bottom: '100%',
			right: '100%'
		},
		x,
		y,
		pc;
	x = pos[bgX] || bgX;
	y = pos[bgY] || bgY;
	if (pc = x.match(/(\d+)%/)) {
		x = Math.round((elmW - pngW) * (parseInt(pc[1]) / 100));
	}
	if (pc = y.match(/(\d+)%/)) {
		y = Math.round((elmH - pngH) * (parseInt(pc[1]) / 100));
	}
	x = parseInt(x);
	y = parseInt(y);

	// Handle backgroundRepeat.
	var repeatX = { 'repeat': 1, 'repeat-x': 1 }[bgR],
		repeatY = { 'repeat': 1, 'repeat-y': 1 }[bgR];
	if (repeatX) {
		x %= pngW;
		if (x > 0) x -= pngW;
	}
	if (repeatY) {
		y %= pngH;
		if (y > 0) y -= pngH;
	}

	// Go!
	this.hook.enabled = 0;
	if (!({ relative: 1, absolute: 1 }[elm.currentStyle.position])) {
		elm.style.position = 'relative';
	}
	var count = 0,
		xPos,
		maxX = repeatX ? elmW : x + 0.1,
		yPos,
		maxY = repeatY ? elmH : y + 0.1,
		d,
		s,
		isNew;
	if (pngW && pngH) {
		for (xPos = x; xPos < maxX; xPos += pngW) {
			for (yPos = y; yPos < maxY; yPos += pngH) {
				isNew = 0;
				if (!tiles.cache[count]) {
					tiles.cache[count] = document.createElement('div');
					isNew = 1;
				}
				var clipR = Math.max(0, xPos + pngW > elmW ? elmW - xPos : pngW),
					clipB = Math.max(0, yPos + pngH > elmH ? elmH - yPos : pngH);
				d = tiles.cache[count];
				s = d.style;
				s.behavior = 'none';
				s.left = (xPos - parseInt(elm.currentStyle.paddingLeft)) + 'px';
				s.top = yPos + 'px';
				s.width = clipR + 'px';
				s.height = clipB + 'px';
				s.clip = 'rect(' +
					(yPos < 0 ? 0 - yPos : 0) + 'px,' +
					clipR + 'px,' +
					clipB + 'px,' +
					(xPos < 0 ? 0 - xPos : 0) + 'px)';
				s.display = 'block';
				if (isNew) {
					s.position = 'absolute';
					s.zIndex = -999;
					if (elm.firstChild) {
						elm.insertBefore(d, elm.firstChild);
					} else {
						elm.appendChild(d);
					}
				}
				this.fix(d, pngSrc, 0);
				count++;
			}
		}
	}
	while (count < tiles.cache.length) {
		this.fix(tiles.cache[count], '', 0);
		tiles.cache[count++].style.display = 'none';
	}

	this.hook.enabled = 1;

	// Cache so updates are infrequent.
	tiles.old = {
		w: elmW,
		h: elmH,
		x: bgX,
		y: bgY,
		r: bgR
	};
};


IEPNGFix.update = function() {
	// Update all PNG backgrounds.
	for (var i in IEPNGFix.data) {
		var t = IEPNGFix.data[i].tiles;
		if (t && t.elm && t.src) {
			IEPNGFix.tileBG(t.elm, t.src);
		}
	}
};
IEPNGFix.update.timer = 0;

if (window.attachEvent && !window.opera) {
	window.attachEvent('onresize', function() {
		clearTimeout(IEPNGFix.update.timer);
		IEPNGFix.update.timer = setTimeout(IEPNGFix.update, 100);
	});
}


function saturdayFilter(date) {

   	return (date.getDay() != 6) ? [false, ''] : [true, '']; 

   }

   function noFilter(date) {

      return [true, ''];

   }


   function checkinFilter(date) {
		
	   var roomtype = $("input[name='roomtype']:checked").val();
	   
		
		if (roomtype=='residence') {
			
			return (date.getDay() != 6) ? [false, ''] : [true, '']; 
			
		} else {
			
			return (date.getDay() != 5) ? [false, ''] : [true, '']; 
			
			
		}
      

   }

   $(document).ready(function() {

		$("#travelinsurance").click(function(){
			
			//console.log($(this).attr('checked'));
			//return true;
			
			if ($(this).attr('checked')==true) {
				
				$("#applytravel").val("Yes");
				
			} else {

				$("#applytravel").val("No");
				
				
			}
			
			$("#travelform").submit();
			
		}); 
		
	});

$(document).ready(function() {
	
	//$("input[name='roomtype']").click(function(){
		
		//$("input[name='checkin']").val("");
		//$("input[name='checkout']").val("");
		
		
	//});
	
	//jquery calendars used on Advanced Search, Search Results
	$("input[name='checkin']").datepicker({
     
      maxDate: "+1Y",
      minDate: '+1D',
	  beforeShowDay: saturdayFilter,
	  beforeShow:function(){
		  
		
	  	
	  	var checkin = $(this).val();
	  	    checkin = jQuery.trim(checkin);
	  	
	  	
	  	var checkout = $("input[name='checkout']").val();
	  	    checkout = jQuery.trim(checkout);
	  	
	  	
	  	var date_entered=true;
	  	
	  	if (checkin=='') {
	  	 	var t = new Date();
	  	 	date_entered=false;
	  	} else {	
	  		var t = $.datepicker.parseDate('mm/dd/yy', checkin);
	  	}
	  	
	  	
	  			var change_date = true; // Here we are assuming the date is gonna need changed.
				
				var one_day=1000*60*60*24;
				
				
				compare_checkin = $.datepicker.parseDate('mm/dd/yy', checkin);
				compare_checkout =  $.datepicker.parseDate('mm/dd/yy', checkout);
				
				days = Math.ceil(compare_checkout-compare_checkin)/one_day;
	  	
	  	
	  	
	  	
	  	
		//var t = new Date(2011, 6, 1); // summer
		var tm = t.getMonth()+1;
		var d = new Date();
		
		
				var saturday = new Date(t.getTime());

				//var roomtype = $("input[name='roomtype']:checked").val();
				
				//console.log(roomtype);
				
				//if (roomtype=='residence') {
					saturday.setDate(saturday.getDate() + 6 - saturday.getDay()); 
				//} else {
				//	saturday.setDate(saturday.getDate() + 5 - saturday.getDay()); 
				//}
				
				d.setDate(saturday); // Next Saturday
				
				
			
				//$("input[name='checkin']").datepicker( "option", "minDate", saturday );
				
				if (date_entered==false){
				$("input[name='checkin']").datepicker( "setDate" , saturday );
				}
	  },
	  
	  
	  
	  onSelect: function() {
				//below is default when we go live.  For testing I create a date 
	  		var checkin = $(this).val();
	  	    	checkin = jQuery.trim(checkin);
	  	
	  	
	  		var checkout = $("input[name='checkout']").val();
	  	    	checkout = jQuery.trim(checkout);
	  					
	  					
	  					
				  	if (checkin==='') {
				  	 	var t = new Date();
				  	} else {	
				  		var t = $.datepicker.parseDate('mm/dd/yy', checkin);
				  	}


  				var change_date = true; // Here we are assuming the date is gonna need changed.
				
				var one_day=1000*60*60*24;
				
				
				compare_checkin = $.datepicker.parseDate('mm/dd/yy', checkin);
				compare_checkout =  $.datepicker.parseDate('mm/dd/yy', checkout);
				
				days = Math.ceil(compare_checkout-compare_checkin)/one_day;
	  	
				//console.log(days);
				
				//var t = new Date(2011, 6, 1); // summer
				var d = $.datepicker.parseDate('mm/dd/yy', $(this).val());
				var dd = $.datepicker.parseDate('mm/dd/yy', $(this).val());
			
	
					dd.setDate(dd.getDate() + 7); // Add 5 days
					
					if (days >=7) {change_date=false;}
					
				
				
				$("input[name='checkout']").datepicker( "option", "minDate", d );
				
				if (change_date==true){
				$("input[name='checkout']").datepicker( "setDate" , dd );
				}
           }
           
   });
   
   	$("input[name='checkout']").datepicker({
        maxDate: "+1Y",
        minDate: '+1D',
        beforeShowDay: saturdayFilter,
        beforeShow: function() {
				//below is default when we go live.  For testing I create a date 
				  	var checkout = $(this).val();
				  	var checkin = $("input[name='checkin']").val();
				  	checkin = jQuery.trim(checkin);
				  	
	  				//console.log(" BeforeShow: checkin:",checkin);	  					
	  					
				  	if (checkin==='') {
				  	 	var d = new Date();
				  	} else {	
				  		var d = $.datepicker.parseDate('mm/dd/yy', checkin);
				  	}

				
				var change_date = true; // Here we are assuming the date is gonna need changed.
				
				var one_day=1000*60*60*24;
				
				
				compare_checkin = $.datepicker.parseDate('mm/dd/yy', checkin);
				compare_checkout =  $.datepicker.parseDate('mm/dd/yy', checkout);
				
				days = Math.ceil(compare_checkout-compare_checkin)/one_day;
				
				
				
				var dd = $.datepicker.parseDate('mm/dd/yy', checkin);
			
				
			
				
					if (days >=7 ) {change_date=false;}
					
					dd.setDate(dd.getDate()+ 7); // Add 5 days
					
					
					
				
				
				$("input[name='checkout']").datepicker( "option", "minDate", d );
				
				
				if (change_date ==true){
				
				$("input[name='checkout']").datepicker( "setDate" , dd );
				
				} else {
					
					// not needed yet
					
				}
				
           }
        
   });
   
	
	//Used on /booking/results
   	$("a[rel='openSPTAF']").colorbox({width:"550", height:"550", iframe:true, rel:"nofollow"}); //opens 'Send Property to a Friend'
	$("a[rel='openATF']").colorbox({width:"550", height:"160", iframe:true, rel:"nofollow"});   //opens 'Add to Favorites'
	$("a[rel='openVF']").colorbox({width:"550", height:"550", iframe:true, rel:"nofollow"});    //opens 'View Favorites'
	
	
	//Send property to a friend at top of detail page
	$('a.b-sptaf-head').click(function() {
		$('.b-sptaf-header').slideToggle(400);
		 return true;
	});
	
	
	//Used on /booking/booknow
	$("#finalize").click(function(){
		
		if( $('#termsAgree').is(':checked')==false){
			
			$('#agreediv').html('<-------------- You must Agree to Continue');
			$.scrollTo($('.b-rental-agreement'));
			
			return false;
		}
	
	});
	
	$('#booknowform').validate();
	
	//Booknow, credit card info same as contact info
	$('#ccsame').click(function(){
		
		$('input[name=cc_firstname]').val($('input[name=firstname]').val());
		$('input[name=cc_lastname]').val($('input[name=lastname]').val());
		$('input[name=cc_address1]').val($('input[name=address1]').val());
		$('input[name=cc_address2]').val($('input[name=address2]').val());
		$('input[name=cc_city]').val($('input[name=city]').val());
		$('select[name=cc_state]').val($('select[name=state]').val());
		$('input[name=cc_zip]').val($('input[name=zip]').val());
		$('input[name=cc_country]').val($('input[name=country]').val());
		$('input[name=cc_province]').val($('input[name=province]').val());		
		
	});
	
});


/* http://jamesvec.com
   simple thumbs v1
   Written by james vecchio (jamesvec[at]gmail.com) June 2010.
   Feel free to use this on any project. 
   I would love to see what people do with it, so drop me a line
   if you use it.
   */
$(document).ready (function(){		
	$("#large_images li").hide();
  $("#large_images li").each(function(index, element){$(element).attr("id", 'img'+index);});
  $("#thumb_holder li a").each(function(index, element){$(element).attr("rel", 'img'+index);});
	
	var mainImg ='img0';
	var current = 'img0';
	
	$('#img0').css('display', 'inline');
	$('#img0').addClass('current');
	
	$('#thumb_holder li a').click (function(){								   
		mainImg = $(this).attr('rel');
		if(mainImg != current){
		$('.current').fadeOut(0);
		$('#'+mainImg).fadeIn(0, function(){
		$(this).addClass('current');
		current = mainImg;
		
		});
		}
	});
	
	$('#thumb_holder li:nth-child(5n+5)').css('margin-right','0');
	
});



$(document).ready(function(){						   

  $(".view-event").colorbox({width:"960px", height:"400px", iframe:true});  

  $(".smcalday").colorbox({width:"960px", height:"400px", iframe:true});   

  $(".smcal").colorbox({width:"960px", height:"400px", iframe:true});   
  
});


//advance search
	$(document).ready(function(){	 
		$(".adv-search").click(function(){
			$("#advanced").slideToggle("slow");
			$(this).toggleClass("active"); return false;
		});			 
	});






