$(function(){
 //embed flash
	$('.flash_content_area').flash({
		src: '/extension/cdev_base/design/cdev_base/images/flash/RotatingBanner.swf',
//		src: '/var/cdev_base/storage/original/application/722ff390ae59bfa3722926f51bcc9147.swf',
		width: 1198,
		height: 459,
		wmode: 'transparent',
		flashvars: { xmlPath: '/layout/set/xml/flash-xml-generator'}
	});

// cufon
	Cufon.replace('.top_nav a, .photo_gallery', {
		fontFamily: 'DIN RgBd',
		hover: true
	});
	Cufon.replace('ul.types a', {
		fontFamily: 'DIN RgBd',
		hover: true
	});
	Cufon.replace('.widgets_control a', {
		fontFamily: 'DIN RgBd',
		hover: true
	});
	Cufon.replace('#nav a', {
		fontFamily: 'DIN RgBd',
		hover: true
	});
	Cufon.replace('.hp_sidebar h2, .mid_promos .rates h2. .email_exclusives h2, .title, #header .address, .price, .dol, .inf, .prom_date', {
		fontFamily: 'DIN RgBd'
	});
	Cufon.replace('.photo_num', {
		fontFamily: 'DIN RgBd'
	});
	Cufon.replace('h1, .promo h2, h3, .meetings_groups_tools h3', {
		fontFamily: 'DIN Lg'
	});
	$('#nav li li').mouseleave(function(){
		setTimeout(function() {
				Cufon.refresh('#nav a')
			}, 5);
	})

	$('#nav li li')
		.mouseenter(function(){
			$(this).parents('li').addClass('sfhover')
		})
		.mouseleave(function(){
			$(this).parents('li').removeClass('sfhover')
		})
		
// thumbnail rollover for "B" pages
		
		$('span.rollover').bind("mouseenter", function(){
			alert('what');
			$("img", this).attr("rel", $("img", this).attr("src")).attr("src", $(this).attr("rel"));
		}).bind("mouseleave", function() {
			$("img", this).attr("src", $("img", this).attr("rel"));
		});
	
	$("form.res_frm").submit(function(){
		var arrival = new Date($("input[name='check-in-date']").val());
		
		$("input[name='arrivalMonth']").val((arrival.getMonth())+1);
		$("input[name='arrivalDay']").val(arrival.getDate());
		$("input[name='arrivalYear']").val(arrival.getFullYear());
		
		var departure = new Date($("input[name='check-out-date']").val());
		
		$("input[name='departureMonth']").val((departure.getMonth())+1);
		$("input[name='departureDay']").val(departure.getDate());
		$("input[name='departureYear']").val(departure.getFullYear());
		
		if($("select[name='numRooms'] option:selected").val()>4)
		{
			$("form.res_frm").attr("action","http://www.hilton.com/en/hi/hotels/search/groups/gm/index3.jhtml;?ctyhocn=ORDILHF&WT.mc_id=1HH2ST3HiltonIndianLakes5EeventsQuickRes6ORDILHF");
			$("input[name=needGstRooms]").val(1);
			$("input[name=numGstRooms]").val($("select[name='numRooms'] option:selected").val());
			$("input[name=gstArrivalDay]").val(arrival.getDate());
			$("input[name=gstArrivalMonth]").val((arrival.getMonth())+1);
			$("input[name=gstArrivalYear]").val(arrival.getFullYear());		
			$("input[name=gstDepartureDay]").val(departure.getDate());
			$("input[name=gstDepartureMonth]").val((departure.getMonth())+1);
			$("input[name=gstDepartureYear]").val(departure.getFullYear());
		}else{
			$("form.res_frm").attr("action","https://secure.hilton.com/en/hi/res/choose_dates.jhtml?ctyhocn=ORDILHF&wt.mc_id=1HH2ST3HiltonIndianLakes5QuickRes6ORDILHF");
		}
		
	});
	
	
	$("form label:contains('\*')").each(function(){
		var replaceStar = $(this).text().replace("*","<span class='star'>*</span>");
		$(this).html(replaceStar);
		$("span.star").css("color","red");
	});



// special offers scrollable
	$("div.promo")
		.scrollable({
			size: 1,
			loop: true
/*			onSeek: function() {
				if(refreshed) {
					return
				}
				$('.promo .cloned cvml').css({display: 'none'})
				Cufon.refresh();
				refreshed = true;

			}
*/		})
//		.circular()
		.autoscroll(5000)
		.navigator({
			navi: 'ul.promo_controls_ul'
		})

// submit buttons hover states
	$('.res_frm .check_b')
		.mouseenter(function(){$(this).attr('src', '/extension/cdev_base/design/cdev_base/images/check_rates_over.gif')})
		.mouseleave(function(){$(this).attr('src', '/extension/cdev_base/design/cdev_base/images/check_rates.gif')})
	$('.res_frm_golf .check_b')
		.mouseenter(function(){$(this).attr('src', '/extension/cdev_base/design/cdev_base/images/book_time_over.gif')})
		.mouseleave(function(){$(this).attr('src', '/extension/cdev_base/design/cdev_base/images/book_time.gif')})
	$('.email_offers_f .submit_btn, .contact_f .submit_btn')
		.mouseenter(function(){$(this).attr('src', '/extension/cdev_base/design/cdev_base/images/submit_over.gif')})
		.mouseleave(function(){$(this).attr('src', '/extension/cdev_base/design/cdev_base/images/submit.gif')})
// preload images
	jQuery.preLoadImages('/extension/cdev_base/design/cdev_base/images/check_rates_over.gif', '/extension/cdev_base/design/cdev_base/images/submit_over.gif', '/extension/cdev_base/design/cdev_base/images/subnav_over.gif', '/extension/cdev_base/design/cdev_base/images/subnav.png', '/extension/cdev_base/design/cdev_base/images/book_time_over.gif');

// calendar popup
	// left rail
	$(".check-in-date, .check-out-date")
		.datepicker({
			showOn: 'button',
			buttonImage: '/extension/cdev_base/design/cdev_base/images/cal.gif',
			buttonImageOnly: true,
			showOn: 'both',
			constrainInput: true,
			showAnim: 'slideDown',
			minDate: '+0',
			maxDate: '+1Y',
			dateFormat: 'MM d, yy'
		})
	$('.check-in-date').datepicker('setDate', '+0');
	$('.check-out-date').datepicker('setDate', '+1');

	$(".check-in-date").change(function(){
		var ci = $(this).datepicker("getDate");
		ci.setDate(ci.getDate()+1);
		
		$(".check-out-date").datepicker("setDate",ci);
	});
	
	//golf res date
	$(".golf-date")
		.datepicker({
			showOn: 'button',
			buttonImage: '/extension/cdev_base/design/cdev_base/images/cal.gif',
			buttonImageOnly: true,
			showOn: 'both',
			constrainInput: true,
			showAnim: 'slideDown',
			minDate: '+0',
			maxDate: '+1Y',
			dateFormat: 'dd-MM-yy'
		})
	$('.golf-date').datepicker('setDate', '+0');

	// contact us form
	$('.contact_date')
		.datepicker({
			showOn: 'button',
			buttonImage: '/extension/cdev_base/design/cdev_base/images/cal.gif',
			buttonImageOnly: true,
			showOn: 'both',
			constrainInput: true,
			showAnim: 'slideDown',
			minDate: '+0',
			maxDate: '+2Y',
			dateFormat: 'MM d, yy'
		})
	$('.contact_date').datepicker('setDate', '+0');
	
	// meeting rfp
	$(".event_start_date, .event_end_date")
		.datepicker({
			showOn: 'button',
			buttonImage: '/extension/cdev_base/design/cdev_base/images/cal.gif',
			buttonImageOnly: true,
			showOn: 'both',
			constrainInput: true,
			showAnim: 'slideDown',
			minDate: '+0',
			maxDate: '+2Y',
			dateFormat: 'MM d, yy'
		})
	$('.event_start_date').datepicker('setDate', '+0');
	$('.event_end_date').datepicker('setDate', '+1');
	
	// wedding rfp
	$(".prop_send_date, .event-start-date")
		.datepicker({
			showOn: 'button',
			buttonImage: '/extension/cdev_base/design/cdev_base/images/cal.gif',
			buttonImageOnly: true,
			showOn: 'both',
			constrainInput: true,
			showAnim: 'slideDown',
			minDate: '+0',
			maxDate: '+2Y',
			dateFormat: 'MM d, yy'
		})
	$('.event-start-date').datepicker('setDate', '+0');
	$('.prop_send_date').datepicker('setDate', '+0');
	
// res form switch
	$('.show_room_res').click(function(){
		$(this).addClass('active')
		$('.show_golf_res').removeClass('active');
		Cufon.refresh('.widgets_control a')
		$('.res_frm').css({display: 'block'});
		$('.res_frm_golf').css({display: 'none'});
		return false;
	})
	$('.show_golf_res').click(function(){
		$(this).addClass('active')
		$('.show_room_res').removeClass('active');
		Cufon.refresh('.widgets_control a')
		$('.res_frm').css({display: 'none'});
		$('.res_frm_golf').css({display: 'block'});
		return false;
	})

// the stand-alone gallery
	if($('body').hasClass('photo-gallery')) {
		numImages = images.length;
		currentImg = 0;
		nextImg = 1;
		animating = false;
		$('.gallery_total').html(numImages);
		Cufon.refresh('.photo_num');
		$('.title').html(captions[0]);
		Cufon.refresh('.title');
		
		imgLoaded = new Array()
		for (i=0; i<numImages; i++){
			imgLoaded[i] = false;
		}
		waitingForNextImg = true;

		$('.cat0').click(function(){
			if(!animating) {
				nextImg = categories[0];
				showImage(nextImg);
			}
			return false;
		})
		$('.cat1').click(function(){
			if(!animating) {
				nextImg = categories[1];
				showImage(nextImg);
			}
			return false;
		})
		$('.cat2').click(function(){
			if(!animating) {
				nextImg = categories[2];
				showImage(nextImg);
			}
			return false;
		})
		
		$('.cat3').click(function(){
			if(!animating) {
				nextImg = categories[3];
				showImage(nextImg);
			}
			return false;
		})
		
		$('.cat4').click(function(){
			if(!animating) {
				nextImg = categories[4];
				showImage(nextImg);
			}
			return false;
		})
		
		$('.cat5').click(function(){
			if(!animating) {
				nextImg = categories[5];
				showImage(nextImg);
			}
			return false;
		})

		$('.cat6').click(function(){
			if(!animating) {
				nextImg = categories[6];
				showImage(nextImg);
			}
			return false;
		})
		$('.prev a').click(function() {
			if(!animating) {
				nextImg = currentImg - 1;
				showImage(nextImg)
			}
			return false;
		})
		$('.next a').click(function() {
			if(!animating) {
				nextImg = currentImg + 1;
				showImage(nextImg)
			}
			return false;
		})
		$('.play a').click(function(){
			if($(this).parent().hasClass('pause')) {
				$(this).parent().removeClass('pause');
				clearTimeout(rotation);
			} else {
				$(this).parent().addClass('pause');
				rotation = setTimeout(showImage, delay);
			}
			return false;
		})
		loadImage(0);
	}
	
	// multiple masthead images rotation
	mhr_total = $('.img_content_area img').length;
	if(mhr_total > 1) {
		mhr_delay = 4000;
		mhr_current = 0;
		mhr_rotation = setTimeout(mhr_next, mhr_delay);
	}

});

function mhr_next() {
	next = (mhr_current + 1) % mhr_total;
	$('.img_content_area img:eq('+ next +')').css({zIndex: '2'}).fadeIn(function(){
		$('.img_content_area img:eq('+ mhr_current +')').css({display: 'none'});
		$(this).css({zIndex: '1'});
		mhr_current = next;
		mhr_rotation = setTimeout(mhr_next, mhr_delay);
	})
}

//preload images
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)

function loadImage(i) {
	if(i < numImages) {
		$('.gallery_items').append('<img class="image'+i+'" alt="" src="'+images[i]+'">');
		$('.image'+i)
			.load(function(){
				imgLoaded[i] = true;
				if(!$('.controls').hasClass('loaded')) {
					$('.controls').addClass('loaded')
					$('.controls li').fadeIn('slow');
				}
				if(i == currentImg) {
					$(this).fadeIn('slow');
				}
				if(i == numImages-1) {
//					rotation = setTimeout(showImage, delay);
				} else {
					i++;
					loadImage(i);
				}
				if(waitingForNextImg) {
					waitingForNextImg = false;
					rotation = setTimeout(showImage, delay);
				}
			});
	}
}

function showImage() {
	nextImg %= numImages;
	if(nextImg < 0) {
		nextImg = numImages - 1;
	}
	waitingForNextImg = false;
	if(!imgLoaded[nextImg]) {
		waitingForNextImg = true;
		return;
	}
	animating = true;
	if(typeof(rotation) != "undefined") {
		clearTimeout(rotation);
	}
	cat = 0;
	if(nextImg>=categories[1]) cat = 1;
	if(nextImg>=categories[2]) cat = 2;
	if(nextImg>=categories[3]) cat = 3;
	if(nextImg>=categories[4]) cat = 4;
	if(nextImg>=categories[5]) cat = 5;
	if(nextImg>=categories[6]) cat = 6;

	$('.types li').removeClass('active');
	$('.types li:eq('+cat+')').addClass('active');
	Cufon.refresh('ul.types a')

	$('.title').html(captions[nextImg]);
	Cufon.refresh('.title');
	$('.gallery_current').html(nextImg+1);
	Cufon.refresh('.photo_num');
	$('.gallery_items img:eq('+currentImg+')').stop(true,true).css({zIndex: '2'});
	$('.gallery_items img:eq('+nextImg+')').stop(true,true).css({display: 'none', zIndex: '3'}).fadeIn('slow', function(){
		$('.gallery_items img:eq('+currentImg+')').css({display: 'none'})
		currentImg = nextImg;
		nextImg++;
		animating = false;
		rotation = setTimeout(showImage, delay);
	});
}

