// Default JS Content - Do Not Delete

$(document).ready(function() {
  
 	$('#rotatorContainer').load('/i/SITE_110310_16040166_W1SU4/specials_rotator.cfm', function(e) {
			$("#slider").easySlider({
				auto: true,
				continuous: true
			});
		$(document).pngFix();
		$("#slider").slideToggle('slow');




			$('#controls').each(function(){
				var $t = $(this);
				var $li = $t.children();
				$li.each(function(){
					$(this).css({
						//width: $t.width()/$li.length-1
                                                width:67
					});
					$(this).children().css({
						//width: $t.width()/$li.length-1
                                                width:67
					})
				});
				/* Man, I really don't have time to fix this plugin to work in IE8 or IE7 ... if we have to make it work, we should probably just rewrite it */
				if($.browser.msie && $.browser.version == 8)
					$t.css({
						top: '0'
					})
                                if($.browser.msie && $.browser.version == 7)
					$t.css({
						top: '0'
					})
					$("#slider").css({
						'display':'block',
						'height':'317px'
					})
			});
	});
        
       $(document).pngFix();

       $('#weatherContainer').load('/weather.cfm');

   	/* For the Navigation being stretched to full width */
	var $win = $(window);
	$win.resize(function(){
		$('.subnav ul').width(960-218+($win.width()-950)/2);
	}).resize();
	
	/* For the Navigation rollovers */
	var $original = $('.main-nav li.active');
	var timer = 0;
	$('.main-nav li').mouseenter(function(){
		clearInterval(timer);
		var $t = $(this);
		
		/* Show it's children */
		$('.subnav ul').removeClass('active');
		$('.subnav ul[pageID='+$t.attr('pageID')+']').addClass('active');
		
		/* Highlight this main item as active */
		$('.main-nav li').removeClass('active');
		$t.addClass('active');
		
		if(!$('.main-nav').hasClass('navbar-top-open')){
			$('.subnav ul[pageID='+$t.attr('pageID')+']').css({top:66,height:0}).stop(true,true).animate({height:72,top:30},750);
			$('.main-nav').addClass('navbar-top-open').css({marginTop:38,lineHeight:'20px'}).stop(true,true).animate({marginTop:-37,lineHeight:'95px'},750);
		}
	})
	$('#top-bar').mouseleave(function(){
		timer = setTimeout(function(){
			if($original.length)
				$original.mouseenter();
			else{
				$('.subnav ul.active').stop(true,true).animate({height:0,top:66},750,function(){$(this).removeClass('active').css({top:30,height:72})});
				$('.main-nav').removeClass('navbar-top-open').stop(true,true).animate({marginTop:38,lineHeight:'20px'},750);
			}
		},750);
	});
/* Search Loading */



	$('form.searchForm').live('submit', function(){									 
		
		var keywords = this.search.value;
        $('#searchResults').remove();
        $(document.body).append('<div id="searchResults" />');
        $('#searchResults').attr('title','Search Results for "'+keywords+'"').html('<div id="scrolling" style="height:500px;overflow:auto;" >Searching ...</div>');
        $('#searchResults').dialog({modal:true,width:800});
		$('.ui-widget-overlay').css({'z-index':'999'});
		$.ajax({
			url: '/i/SITE_110310_16040166_W1SU4/cfcs/search.cfc',
			cache: false,
			dataType: 'json',
			data:{
				returnFormat: 'json',
				method: 'search',
				keywords: keywords
			},
			success:function(r){
				var r = convertCFQuery(r);
                $('#scrolling').html('');
				for(var i in r){
					$('#scrolling').append(
						'<div class="subcopy" style="overflow:hidden;background:#FFF;padding:10px;margin:10px 10px 10px 0;cursor:pointer;" onClick="document.location.href=\''+r[i].URL+'\';">'
							+'<img src="/i/SITE_110310_16040166_W1SU4/webshots/ws_'+r[i].PAGEID+'.jpg" style="float:left;margin:0 10px 0 0;">'
							+'<h2>'+(i- -1)+'. '+r[i].TITLE+'</h2>'
							+'<p>'+r[i].URL+'</p>'
							+'<p>'+r[i].SUMMARY+'</p>'
						+'</div>'
					);
				}
                if(r.length == 0){
                	$('#scrolling').html('No results found, please try another search term.');
                }
				_gaq.push(['_trackPageview', '/search/?q='+escape(keywords)]); 
			}
		});
		return false;
	});
	var convertCFQuery = function(original){
		var data = [];
		for(var i=0;i<original.DATA.length;i++){
			var item = {};
			for(var j=0;j<original.COLUMNS.length;j++){
				item[original.COLUMNS[j]]=original.DATA[i][j];
			}
			data.push(item);
		}
		return data;
	};
	$.fn.scaleMe = function() {
		return this.each(function() {
			var $this = $(this);
			if($this.data('scaleMe')) return true;
			$this.data('scaleMe',true)
			var originalWidth = $this.width();
			var originalHeight = $this.height();
			var divHeight = $this.closest('div').height();

			$(window).resize(function() {
				var divWidth = $this.closest('div').width();
				var newWidth = divWidth;
				var newHeight = (divWidth/originalWidth)*originalHeight;

				if( newHeight < divHeight ) {
					newHeight = divHeight;
					newWidth = originalWidth*(newHeight/originalHeight);
				}

				var topMargin = (divHeight-newHeight)/2;
				var leftMargin = (divWidth-newWidth)/2;

				$this.height(newHeight)
				.width(newWidth)
				.css({
					'margin-top' : topMargin,
					'margin-left' : leftMargin
				});

			}).resize();
		});
	};
	$(window).load(function(){
		$('.subImg img,#bgimage img,.subImgSmall img').scaleMe();
		if ($('.subImg img').length==1)
			$('.subImg img').fadeIn();
	});
});


/*
*
* MAP Javascript
*
*/
	$(function(){
		$('#map>ul>li>a').click(function(){
			var $t = $(this).parent();
			$('ol').slideUp();
			$t.find('ol').slideDown();
			$('.map-overlay').fadeOut();
			if($t.attr('class')=='overview'){
				$('#map').removeClass('map-bw');
			}else{
				$('.map-overlay.'+$t.attr('class')).fadeIn();
				$('#map').addClass('map-bw');
			}
		});
		$('#map ol>li>a,.map-overlay area').click(function(){
			var $t = $(this);
			var c = $t.attr('class');
			if(c&&c.length)
				$('.popup.'+c).fadeIn();
			return false;
		});
		$('.popup').click(function(){$(this).fadeOut();})
	});

//photogallery apples style

function loadNewGallery() {

$(document).ready(function(){
    $.ajax({
        url: path+'myimages.xml',
        dataType: 'xml',
        success: function(data){
        	var container = $('#NewGalleryDiv').css({
        		height: totalH,
        		width: totalW,
        		margin: '0 auto',
        		textAlign: 'left'
        	});
        	var thumbs = $('<div />').css({
        		width: totalW,
        		margin: '15px auto 30px auto'
        	}).insertAfter(container);
            $(data).find('data').each(function(i){
				var $t = $(this);
				var item = $(
					'<img class="item" src="'+path+$t.attr('img')+'" title="'+$t.attr('imgcaption')+'">'
				).css({
					position: 'absolute',
					opacity: 0,
					border: '5px solid #FFF',
					'-moz-box-shadow': '0px 0px 4px #000',
					'-webkit-box-shadow': '0px 0px 4px #000',
					'box-shadow': '0px 0px 4px #000'
				}).click(function(){
					thumbs.find('.thumb:eq('+$(this).prevAll().length+')').click();
				});
				container.append(item);
				var thumb = $(
					'<img class="thumb" src="'+path+$t.attr('thumb')+'" title="'+$t.attr('imgcaption')+'">'
				).css({
					border: '1px solid #bcb5a5',
					margin: '10px 6px',
					height: 25,
					width: 25
				}).click(function(){
					var $t = $(this);
					var $a = container.find('.item:eq('+$t.prevAll().length+')');
					if(!$a.length)
						$a = container.find('.item:first');
					var $n = $a.next();
					if(!$n.length)
						$n = container.find('.item:first');
					var $p = $a.prev();
					if(!$p.length)
						$p = container.find('.item:last');
					$n.css({zIndex:99}).show();
					$p.css({zIndex:99}).show();
					$a.css({zIndex:100}).show();
					$n.stop(true,false).animate({
						marginLeft:totalW-30,
						marginTop:totalH/4,
						opacity:.5,
						height: totalH/2,
						width: totalW/2
					},1000);
					$p.stop(true,false).animate({
						marginLeft:30-totalW/2,
						marginTop:totalH/4,
						opacity:.5,
						height: totalH/2,
						width: totalW/2
					},1000);
					$a.stop(true,false).animate({
						marginLeft:0,
						marginTop:0,
						opacity:1,
						height: totalH,
						width: totalW
					},1000);
					$a.prevAll().not($n).not($p)
					.stop(true,false).animate({
						marginLeft:30-totalW/2,
						marginTop:totalH/4,
						opacity:0,
						height: totalH/2,
						width: totalW/2
					},0);
					$a.nextAll().not($n).not($p)
					.stop(true,false).animate({
						marginLeft:totalW-30,
						marginTop:totalH/4,
						opacity:0,
						height: totalH/2,
						width: totalW/2
					},0).hide();
				});
				thumbs.append(thumb);
			});
			thumbs.find('.thumb:first').click();
        }
    });
});
	
}
function addDays(myDate,days) {
//myDate = starting date, days = no. days to add.
    var temp_date = new Date();
    var i = 0;
    var days_to_add = 0;
    while (i < (days)){
        temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));
            i+=1;
        days_to_add += 1;
    }
    return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
}
$(document).ready(function(){
/*		$("#arrivalDate").datepicker({
			minDate:0,
			showAnim: "fadeIn",
			showOn: 'both',
			buttonText: 'Select Check In Date',
			buttonImage: '/i/SITE_110310_16040166_W1SU4/templates/res-calendar-icon.png',
			buttonImageOnly: true
		});*/
		
		/*new*/
		
		$("#arrivalDate").datepicker({
			minDate:0,
			showAnim: "drop",
			showOn: 'both',
			buttonText: 'Select Check In Date',
			buttonImage: '/i/SITE_110310_16040166_W1SU4/templates/res-calendar-icon.png',
			buttonImageOnly: true,
			onSelect:function(theDate) {
				$("#departDate").datepicker('option','minDate',addDays(new Date(theDate),1))
				if(($("#departDate").datepicker( 'getDate' )===null)||(trim($("#departDate").datepicker( 'getDate' ))=="")||(comparedate($("#arrivalDate").datepicker( 'getDate' ),$("#departDate").datepicker( 'getDate' )))){
					$("#departDate").datepicker('setDate' ,addDays(new Date(theDate),1))
				}
			}
		});

		$("#departDate").datepicker({
			minDate:1,
			showAnim: "drop",
			showOn: 'both',
			buttonText: 'Select Check Out Date',
			buttonImage: '/i/SITE_110310_16040166_W1SU4/templates/res-calendar-icon.png',
			buttonImageOnly: true,
			defaultDate: +1,
			gotoCurrent: true
		});	
		
		
		
		
		
		
		
		/*end new*/
		
       	$("#arrivalDate").datepicker('setDate',addDays(new Date(),1));
	$("#departDate").datepicker('setDate',addDays(new Date(),2));		   
		$("#teeDate").datepicker({
			minDate:0,
			showAnim: "fadeIn",
			showOn: 'both',
			buttonText: 'Select Check In Date',
			buttonImage: '/i/SITE_110310_16040166_W1SU4/templates/res-calendar-icon-golf.png',
			buttonImageOnly: true
		});	
		
		var overlays = null;
		overlays = $("a[rel]");
		 for (var n = 0; n < overlays.length; n++) {
			$(overlays[n]).overlay({
				oneInstance: false, 
				onBeforeLoad: function() {
					overlay_before_load(this);
				}
			});
		  }

	       $("#dialog").dialog({
                       bgiframe: true,
                       modal: true,
                       autoOpen: false,
                       height:160,
                       width:350,
                       show: "fold",
                       position:"center",
                       close: function(){$('#'+$('#putFocus').val()).focus();},
                       buttons: {"Thank You": function() {$(this).dialog("close");}}
            });	
	       $("#pressDialog").dialog({
                       bgiframe: true,
                       modal: true,
                       autoOpen: false,
                       height:600,
                       width:600,
                       show: "fold",
                       position:"center",
                       buttons: {"Thank You": function() {$(this).dialog("close");}}
            });	
            $("#terms").dialog({
                       bgiframe: true,
                       modal: true,
                       autoOpen: false,
                       height:500,
                       width:550,
                       show: "fold",
                       position:"center",
                       buttons: {"Thank You": function() {$(this).dialog("close");}}
            });
			   
/*
	$('#reservations').hoverIntent(function() {
			$('#reservations').stop(true,true).animate({'width':'780px'},1000,function() {
				$('#ui-datepicker-div').css({'z-index':'999'});
				//console.log('callback');																	   
			});
		},function(){
			$('#reservations').stop(true,true).animate({'width':'400px'},1000)
	});
*/
	/*$('#golfRes').hover(function() {
			$('#golfRes').stop(true,true).animate({'width':'680px'},1000,function() {
				$('#ui-datepicker-div').css({'z-index':'999'});
				//console.log('callback');																	   
			});
		},function(){
			//$('#golfRes').stop(true,true).animate({'width':'400px'},1000)
	});*/

	$('a.pressDetails').click(function(e) {
		$('#pressDialog').load('/i/SITE_110310_16040166_W1SU4/pressDetails.cfm?prID='+$(this).attr("id")+'',function() {
			$("#pressDialog").dialog('open');
			$('.ui-widget-overlay').css({'z-index':'999'});
		});
		return false;
	});

	$('a.disclaimerID').click(function(e) {
		var ourContent = $('#' + $(this).attr("title")+ '').html();
		$('#terms').html('<div class="popDis">'+ ourContent + '</div>')
			$("#terms").dialog('open');
			$('.ui-widget-overlay').css({'z-index':'999'});
		return false;
	});
$('a[href^="/enter.cfm?task=template"]').css({'border':'1px solid #ccc','z-index':'999999','display':'block'});
});


//booking mask
	function validateForm(oForm) {
		if(trim(oForm.arrivalDate.value)==''){
			$('#dialog').html("<input type='hidden' id='putFocus' value='arrivalDate' /><div style='padding:10px 3px; color:#3E703B;'>Please select a check in date before proceeding</div>");
			$('#dialog').dialog("open");
			return false;
		}

	_gaq.push(['_trackEvent', 'Booking_Action','Booking_Mask',getPath()]);
	
	return true;
	}

$(document).ready(function(){
	$('a[href^="https://laquinta.ezlinks.com/"]').click(function (event) {
		_gaq.push(['_trackEvent', 'Golf_Booking_Action','Golf_Mask',getPath()]);
		
	});
	$('a[href^="http://www.opentable.com/"]').click(function (event) {
		_gaq.push(['_trackEvent', 'openTable_Link','Dining',getPath()]);
		
	});

});

function getPath() {
	var a = location.pathname;
	var myPagePath = '';
	if(a=='/'){
		myPagePath = 'Home_Page';
	}else{
		myPagePath = a;
	}
 return myPagePath; 
}

//contact us form
	function formCheck(oForm){
		if(trim(oForm.First_Name.value)==''){
			$('#dialog').html("<input type='hidden' id='putFocus' value='First_Name' /><div style='padding:10px 3px; color:#3E703B;'>Please enter a first name before proceeding</div>");
			$('#dialog').dialog("open");
			return false;
		}
		
		if(trim(oForm.Last_Name.value)==''){
			$('#dialog').html("<input type='hidden' id='putFocus' value='Last_Name' /><div style='padding:10px 3px; color:#3E703B;'>Please enter a last name before proceeding</div>");
			$('#dialog').dialog("open");
			return false;
		}
		
		if(isEmailAddr(trim(oForm.Email.value))==''){
			$('#dialog').html("<input type='hidden' id='putFocus' value='Email' /><div style='padding:10px 3px; color:#3E703B;'>Please enter a valid email address before proceeding</div>");
			$('#dialog').dialog("open");
			return false;
		}
	_gaq.push(['_trackEvent', 'Form_Submission','Contact_US',getPath()]);
	return true;	
	}

	function trim(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	
	function isEmailAddr(str) {
		var re  = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9-])+(\.[a-zA-Z0-9_-]+)+$/;
		if (!str.match(re)) {
			return false;
		} else {
			return true;
		}
	}


//photo gallery for header images
var buffer = false;
var loaded = false;
var thumbnails = false;
var aG = [];
function loadGallery(){
aG.push({
	totalW: totalW,
	totalH: totalH,
	path: path
});
$(function(){
	if(loaded) return false;
	loaded = true;
	for(var p in aG){
		var gSlideshow = $('div.slideshow:eq('+p+')')
		gSlideshow.data('mP',p);
		gSlideshow.height(aG[p].totalH);
		$.ajax({
			url: aG[p].path+'myimages.xml',
			dataType: 'xml',
			context: gSlideshow,
			success: function(data){
				var slideshow = $(this);
				var mP = slideshow.data('mP');
				var dataArray = [];
				$(data).find('data').each(function(){
					dataArray.push({img:$(this).attr('img'),imgcaption:$(this).attr('imgcaption'),thumb:$(this).attr('thumb')});
				});
				slideshow.append(
					'<img src="'+aG[mP].path
					+dataArray[0].img
					+'" imagelabel="'
					+dataArray[0].imgcaption
					+'" style="position: absolute;display: none; ">'
				);
				if(thumbnails){
					var thumbs = $('<div class="thumbnails"></div>');
					slideshow.after(thumbs);
					thumbs.append(
						'<img src="'+aG[mP].path
						+dataArray[0].thumb.replace(/_c/,'_bw')
						+'">'
					);
					var nt = $('.thumbnails img');
					nt.addClass('active').attr('src',nt.attr('src').replace(/(_bw|_b|_c)\./,'_c.'));;
					thumbs.before('<div class="descriptionText"></div>');
				}
				var loaded = false;
				dataArray.shift();
				/* As soon as the first image is loaded, we may begin */
				slideshow.find('img:first').load(function(){
					if(loaded) return false;
					loaded = true;
$(window).resize();
					for(var i in dataArray){
						slideshow.append(
							'<img sr'+'c="'+aG[mP].path
							+dataArray[i].img
							+'" imagelabel="'
							+dataArray[i].imgcaption
							+'" style="position: absolute;display: none; ">'
						);
						if(thumbnails){
							thumbs.append(
								'<img src="'+aG[mP].path
								+dataArray[i].thumb.replace(/_c/,'_bw')
								+'">'
							);
						}
					}
					
					if(thumbs){
						thumbs.append('<br style="clear:both;" />');
						$('.thumbnails img').hover(function(){
							var $t = $(this);
							$t.attr('src',$t.attr('src').replace(/(_bw|_b|_c)\./,'_b.'));
						},function(){
							var $t = $(this);
							if($t.hasClass('active'))
								$t.attr('src',$t.attr('src').replace(/(_bw|_b|_c)\./,'_c.'));
							else
								$t.attr('src',$t.attr('src').replace(/(_bw|_b|_c)\./,'_bw.'));
						}).click(function(){
							var $t = $(this);
							var i = $t.prevAll('img').length;
							pause();
							nextSlide('.slideshow img:eq('+i+')');
						});
					}
					
					/* Variables */
					var transitionTime = 4200;
					
					/* Show First Image / Gallery / Hide Play Button */
					var n = slideshow.find('img:first');
					$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
					n.show();
					if(buffer){
						$('.buffer').css({
							'height':n.height(),
							'width':n.width(),
							'margin-left' : (aG[mP].totalW-n.width())/2,
							'margin-top' : (aG[mP].totalH-n.height())/2
						}).fadeOut(1000,function(){
							n.css('z-index','5');
							$('.buffer').show();
						});
						n.css({
							position : 'absolute',
							'margin-left' : (aG[mP].totalW-$(this).width())/2,
							'margin-top' : (aG[mP].totalH-$(this).height())/2
						});
					}
					
					$('.play').hide();
					var animating = false;
					var nextSlide = function(next){
						/* Make sure we're not already in transition */
						if(animating) return false;
						animating = true;
						var f = slideshow.find('img:visible');
						if(next){
							var n = $(next);
						}else{
							var n = slideshow.find('img:visible').next('img');
							if(n.length==0) n = slideshow.find('img:first');
						}
						/* Make sure the next image is loaded before proceeding (don't worry, the loop event will re-fire) 
						if(!n[0].complete) return false;*/
						
						if(thumbs){
							var i = n.prevAll('img').length;
							var nt = $('.thumbnails img:eq('+i+')');
							var pt = $('.thumbnails img.active');
							pt.removeClass('active').attr('src',pt.attr('src').replace(/(_bw|_b|_c)\./,'_bw.'));
							nt.addClass('active').attr('src',nt.attr('src').replace(/(_bw|_b|_c)\./,'_c.'));;
						}
						
						/* Buffered Animation */
						if(buffer){
							$('.buffer').css({
								'z-index':'4',
								'height':f.height(),
								'width':f.width(),
								'margin-left' : (aG[mP].totalW-f.width())/2,
								'margin-top' : (aG[mP].totalH-f.height())/2
							});
							f.css('z-index','5').fadeOut(600,function(){
								$('.buffer').animate({
									'height':n.height(),
									'width':n.width(),
									'margin-left' : (aG[mP].totalW-n.width())/2,
									'margin-top' : (aG[mP].totalH-n.height())/2
								},300,'swing',function(){
									n.css({
										position : 'absolute',
										'margin-left' : (aG[mP].totalW-$(this).width())/2,
										'margin-top' : (aG[mP].totalH-$(this).height())/2,
										'z-index' : 3
									}).show();
									animating=false;
									/* Alter Text */
									$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
									
									$('.buffer').fadeOut(1000,function(){
										n.css('z-index','5');
										$('.buffer').show();
									});
								});
							});
						}else{
							/* Non Buffered Animation */
							f.css('z-index',5);
							n.css('z-index',4).show();
								f.fadeOut(800,function(){
									animating=false;
									$('.descriptionText').html(n.attr('imageLabel').replace(/ /g,'')==''?' ':n.attr('imageLabel'));
								});
						}
					};
					var looping=true;
					var t = setTimeout(function(){beginLoop();},transitionTime);
					var beginLoop = function(){
						nextSlide();
						t = setTimeout(function(){beginLoop();},transitionTime- -800);
					};
					var play = function(){
						clearTimeout(t);
						$('.play').hide();
						$('.pause').show();
						beginLoop();
					};
					var pause = function(){
						clearTimeout(t);
						$('.play').show();
						$('.pause').hide();
					};
					$('.play').click(play);
					$('.pause').click(pause);
					$('.next').click(function(){nextSlide();play();});
					slideshow.find('img:first').scaleMe();
					slideshow.find('img:not(:first)').load(function(){
						$(this).scaleMe();
					}).each(function(){
						if(this.complete) $(this).trigger("load");
					});
					
				/* If the image is loaded from cache, the load event doesn't fire, this checks if it's already loaded and the load event missed */
				}).each(function(){
					if(this.complete) $(this).trigger("load");
				});
			}
		});
	}
});
}

		
	function addDays(myDate,days) {
	//myDate = starting date, days = no. days to add.
		var temp_date = new Date();
		var i = 0;
		var days_to_add = 0;
		while (i < (days)){
			temp_date = new Date(myDate.getTime() + (days_to_add*24*60*60*1000));
				i+=1;
			days_to_add += 1;
		}
		return new Date(myDate.getTime() + days_to_add*24*60*60*1000);
	}
	
	function comparedate(date1str,date2str){
		var date1 = new Date(date1str);
		var date2 = new Date(date2str);
		if (date1 > date2){
			return true;
		}else if (date1 < date2){
			return false;
		}else{
			return true;
		}
	}
	
	function trim(stringToTrim) {
		return stringToTrim.toString().replace(/^\s+|\s+$/g,"");
	}
