$(function(){
// custom select elements
	$("select").selectbox();


// positioning copy show/hide
	$('.home #positioning_copy_link').click(function(){
		$('#positioning_copy').animate({height: '330px'});
		$('#positioning_copy_link .inner').animate({top: '0px', height: '0px'});
	});
	$('.home #positioning_copy .close').click(function(){
		$('#positioning_copy').animate({height: '0px'});
		$('#positioning_copy_link .inner').animate({top: '-75px', height: '75px'});
	});

// reservations show/hide
	$('#quick_reservations.colapsed').click(function(){
		$(this).animate({height: '330px'}).parents(':first').removeClass('colapsed');
		$('#quick_reservations .close').css({display: 'block'});
	});
	$('#quick_reservations .close').click(function(){
		$(this).css({display: 'none'});
		$('#quick_reservations').animate({height: '80px'}).parents(':first').addClass('colapsed');
		return false;
	});

// top drop show/hide
	$('#header .c_inner').click(function() {
		$(this).slideUp();
		$('#top_drop, #top_drop .inner').animate({height: '86px'});
		if(!setHeight)
			$('#content').animate({marginTop: '-58px'});
	});

	$('#header .close').click(function() {
		$('#top_drop, #top_drop .inner').animate({height: '0px'});
		$('#header .c_inner').slideDown(function(){$('#header .c_inner').css({position: 'relative', zIndex: '10'})});
		if(!setHeight)
			$('#content').animate({marginTop: '0px'});
	});

// highlights rotation
	$('.navi span').click(function(){
		which = $(this).attr('id').charAt(13);
		$('.scrollable .items .visible').removeClass('visible').fadeOut(function(){
			$('.i_special'+which).fadeIn().addClass('visible');
		});
		$('.navi span').removeClass();
		$(this).addClass('active');
		$('.scrollable').stopTime('controlled').oneTime('5s', function(){$('.scrollable').everyTime('5s', 'controlled', rotateSpecials);})
		whichAuto = which;
	});
	whichAuto = 0;
//	$('.scrollable').everyTime('5s', 'controlled', rotateSpecials);
	function rotateSpecials() {
		whichAuto++;
		whichAuto %= 1;
		$('.scrollable .items .visible').removeClass('visible').fadeOut(function(){
			$('.i_special'+whichAuto).addClass('visible').fadeIn();
		});
		$('.navi span').removeClass();
		$('#i_special_nav'+whichAuto).addClass('active');
	}

// external links
	$('a').each(function(){
		if($(this).attr('rel')=='external') this.target = '_blank';
	});

// reveal the photo
	var imgRev = false;
	pCopy = $('.seo_copy').text();
	if (pCopy.length > 0) {
		$('#positioning_copy_link .inner').html('<p>' + pCopy + '</p>');
	} else {
		$('#positioning_copy_link').not('.home #positioning_copy_link').html('');
	}
	$('.reveal, #positioning_copy_link').not('.home #positioning_copy_link').click(function() {
		if(imgRev) {
			hideImage();
		} else {
			showImage();
		}
		imgRev = !imgRev;
	});

// hp background image rotation

	bgr = $.cookie('hpBgr');
	if(bgr) {
		$('#hp_bgr'+bgr).show();
		bgr++; bgr %= 3;
		$.cookie('hpBgr', bgr);
	} else {
		bgr = 0;
		$.cookie('hpBgr', 1);
	}

	$('#bgr_wrap').everyTime(7000, 'controlled', function(){		
		$('#hp_bgr'+bgr).fadeIn(2500, function(){
			$('#bgr_wrap div').not('#hp_bgr'+bgr).fadeOut(2500);
			bgr++;
			bgr %= 3;
		});
		
	});


// datepicker
// display format oct 2009 2
// actual format  10 2009 02
	$('#qr_idate0')
	.datepicker({ showAnim: 'slideDown', minDate: '+0', maxDate: '+1Y', dateFormat: 'mm y-dd', altField: '#qr_idate0alt', altFormat: 'M yy-d' })
		.change(function(){
			date0=$('#qr_idate0').val().toLowerCase().split('-');
			date0alt=$('#qr_idate0alt').val().toLowerCase().split('-');
			$('.jquery-selectbox-currentItem:eq(0)').html(date0alt[0]);
			$('.jquery-selectbox-currentItem:eq(1)').html(date0alt[1]);
			$('#monthyear0').val(date0[0]);
			$('#day0').val(date0[1]);
			updateCheckOut();
		})
	$('#qr_idate0').datepicker('setDate', '+0');
	$('#qr_date0').click(function(){$('#qr_idate0').datepicker('show')});
	$('#qr_idate1')
		.datepicker({ showAnim: 'slideDown', minDate: '+1', maxDate: '+1Y +1D', dateFormat: 'mm y-dd', altField: '#qr_idate1alt', altFormat: 'M yy-d' })
		.change(function(){
			date1=$('#qr_idate1').val().toLowerCase().split('-');
			date1alt=$('#qr_idate1alt').val().toLowerCase().split('-');
			$('.jquery-selectbox-currentItem:eq(2)').html(date1alt[0]);
			$('.jquery-selectbox-currentItem:eq(3)').html(date1alt[1]);
			$('#monthyear1').val(date1[0]);
			$('#day1').val(date1[1]);
		})
	$('#qr_idate1').datepicker('setDate', '+1');
	$('#qr_date1').click(function(){$('#qr_idate1').datepicker('show')});


	$('#monthyear0, #day0').change(function(){
		mnyr = $('#monthyear0').val().split(' ');
		dte = new Date('20'+mnyr[1], mnyr[0]-1, $('#day0').val());
		$('#qr_idate0').datepicker('setDate', dte);
		updateCheckOut();
	});
	$('#monthyear1, #day1').change(function(){
		mnyr = $('#monthyear1').val().split(' ');
		dte = new Date('20'+mnyr[1], mnyr[0]-1, $('#day1').val());
		$('#qr_idate1').datepicker('setDate', dte);
		updateCheckOut();
	});

// cancel reservations form submission and open an url instead
	$('#checkAvail').submit(function(){
		uriel  = 'http://www.hyatt.com/HICBooking?pid=FTLHP&adults=';
		uriel += $('#adults').val();
		uriel += '&kids=';
		uriel += $('#kids').val();
		uriel += '&rooms=';
		uriel += $('#number').val();
		uriel += '&monthyear1=';
		uriel += $('#monthyear0').val();
		uriel += '&day1=';
		uriel += $('#day0').val();
		uriel += '&monthyear2=';
		uriel += $('#monthyear1').val();
		uriel += '&day2=';
		uriel += $('#day1').val();
		uriel += '&icamp=RH-reservations-ss-ftlhp';
		window.open(uriel);
		return false;
	})


// input default value
	var active_color = '#fff';
	var inactive_color = '#ccc';
	$('input.default-value').css('color', inactive_color);
	var default_values = new Array();
	$('input.default-value').focus(function() {
		if (!default_values[this.id]) {
			default_values[this.id] = this.value;
		}
		if (this.value == default_values[this.id]) {
			this.style.color = active_color;
			this.value = '';
		}
		$(this).blur(function() {
			if (this.value == '') {
				this.style.color = inactive_color;
				this.value = default_values[this.id];
			}
		});
	});

// 
	if(!setHeight)
		$('#footer .c_inner').css({clear: 'both'});
});

$(window).load(function(){
// purple background
  if(setHeight){
	cHeight = $('.slider').height();
	cOfset = $('.slider').offset();
	cTop = cOfset.top;
	mHeight = cHeight + cTop;
	$('#container').height(mHeight);
  } else if ($('#gallery .images').length < 1) {
	$('#footer .c_inner').css({clear: 'none'}).css({clear: 'both'});
  }
});

// show/hide image for reveal the photo functionality
function showImage() {
	$('#main_content').css({height: $('#main_content').height()});
	$('#main_content .slider').slideUp();
	$('#positioning_copy_link .inner').animate({top: '-75px', height: '75px'}, 'fast');
	$('.heading').animate({opacity: '.85'});
	$('.reveal').addClass('revealed');
}
function hideImage() {
	$('#positioning_copy_link .inner').animate({top: '0px', height: '0px'}, 'fast', function(){});
	$('#main_content .slider').slideDown();
	$('.heading').animate({opacity: '1'});
	$('.reveal').removeClass('revealed');
}

// check-out date should be updated automatically when check-in is changed
months = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'];
function updateCheckOut() {
	checkIn = $('#qr_idate0').datepicker('getDate').getTime();
	tomorrow = new Date();
	tomorrow.setTime(checkIn + 86400000);
	checkOut = $('#qr_idate1').datepicker('getDate').getTime();
	$('#qr_idate1').datepicker( 'option' , 'minDate' , tomorrow );
	if ( checkIn >= checkOut ) {
		$('.jquery-selectbox-currentItem:eq(2)').html(months[tomorrow.getMonth()] + ' ' + tomorrow.getFullYear());
		$('.jquery-selectbox-currentItem:eq(3)').html(tomorrow.getDate());
		mnt = tomorrow.getMonth()+1;
		yer = tomorrow.getFullYear() % 100;
		if(mnt < 10) mnt = '0' + mnt;
		if(yer < 10) yer = '0' + yer;
		$('#monthyear1').val(mnt + ' ' + yer);
		tmrw = tomorrow.getDate()<10 ? '0' + tomorrow.getDate() : tomorrow.getDate();
		$('#day1').val(tmrw);
	}
}
