if (window != top)
{
	top.location.href = location.href;
}

$(document).ready(function()
{
	//addclass active to active link
	var thisClass = $('a').filter(function() { return this.href === window.location.href; }).attr('class');
	$('a').filter(function() { return this.href === window.location.href; }).removeClass(thisClass);	
	$('a').filter(function() { return this.href === window.location.href; }).addClass(thisClass + ' active');

	//homepage slideshow
	if($('div').hasClass('homeslide'))
	{
		$('.homeslide img').removeClass('hidden');
		$('.homeslide').cycle({fx: 'fade', timeout: 4000, delay: -2000});
	}

	//homepage featured specials packages
	if($('div').hasClass('packages'))
	{
		$('.packages img').removeClass('hidden');
		$('.packages').cycle({fx: 'scrollLeft', timeout: 4000, delay: -2000, pause: true});
	}

	//embassy photo gallery
	if($('div').hasClass('photo_gallery'))
	{
		$('.photo_gallery').after('<div class="photo_gallery_nav"><ul class="photo_gallery_nav">').cycle(
		{
			fx: 'fade',
			timeout: 4000,
			pager: '.photo_gallery_nav',
			cleartype: true,
			pause: true,
			next: '#next',
			prev: '#prev',
			before: function(idx, slide)
			{
				var alt = $('img', slide).attr('alt');
				$('.photo_gallery_cap').html(alt).css('display','none');
			},
			after: function(idx, slide)
			{
				var alt = $('img', slide).attr('alt');
				$('.photo_gallery_cap').html('<span>' + alt + '</span>').css({opacity: '0.7'}).slideDown();
			},
			pagerAnchorBuilder: function(idx, slide)
			{
				var src = $('img', slide).attr('src'); 
				return '<li><a href="#"><img src="' + src + '" width="50" height="37" /></a></li>'; 
			},
			updateActivePagerLink: function(pager, activeIndex) { 
				$(pager).find('li:eq('+activeIndex+')').addClass('activeLI').siblings().removeClass('activeLI');
								
				//photo gallery nav scroll to active LI
				$('div.photo_gallery_nav').scrollTo('li:eq('+activeIndex+')', 1000, {axis:'x', offset: -190});
			}
		});

		//photo gallery nav mouse scroller
		var divWidth = $('div.photo_gallery_nav').width();
		var lastLi = $('ul.photo_gallery_nav').find('li:last-child');

		$('div.photo_gallery_nav').css({overflow: 'hidden'});

		$('div.photo_gallery_nav').mousemove(function(e)
		{
			$('.photo_gallery').cycle('pause');

			var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + 15;
			var left = (e.pageX - $('div.photo_gallery_nav').offset().left) * (ulWidth-divWidth) / divWidth;

			$('div.photo_gallery_nav').scrollLeft(left);
		});

		$('div.photo_gallery_nav').mouseleave(function(e)
		{
			$('.photo_gallery').cycle('resume');
		});

		//photo gallery controls
		$('#pause').click(function()
		{
			$('.photo_gallery').cycle('pause'); return false;
		});
		
		$('#play').click(function()
		{
			$('.photo_gallery').cycle('resume'); return false;
		});

		$('.photo_gallery_container').hover(
			function() { $('#controls').fadeIn(); },
			function() { $('#controls').fadeOut(); }
		);
	}

	if(!$('div').hasClass('photo_gallery'))
	{
		//lazy load images if not on gallery page 
		$('img').lazyload({effect : 'fadeIn'});
	}

		//reservation widget datepicker
		var dates = $( '#arrivalDate, #departureDate' ).datepicker({
			dateFormat: 'dd M yy',
			showOn: 'both',
			buttonImage: '/images/calendar.png',
			buttonImageOnly: true,
			defaultDate: '+1D',
			changeMonth: true,
			changeYear: true,
			minDate: '0',
			onSelect: function( selectedDate ) {
				var option = this.id == 'arrivalDate' ? 'minDate' : 'maxDate',
					instance = $( this ).data( 'datepicker' ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( 'option', option, date );
			checkIt(this.id);
			updateDates();
			}
		});

	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash();
	$.localScroll({ hash: true });

	//translations
	var MyURL = '';
	if (location.host == 'www.embassytahoe.com')
	{
		MyURL = location.href;
	}
	else
	{
		MyURL = 'http://www.embassytahoe.com/';
	}

	$('#translations').html(
		'<a href="' + MyURL + '"><img src="http://www.embassytahoe.com/assets/UNST0001.GIF" border="0" width="20" height="15" /></a>&nbsp;&nbsp;'
		+ '<a href="http://translate.google.com/translate?u=' + MyURL + '&amp;hl=en&amp;ie=UTF-8&amp;sl=en&amp;tl=es"><img src="http://www.embassytahoe.com/assets/SPAN0001.GIF" border="0" width="20" height="15" /></a>&nbsp;&nbsp;'
		+ '<a href="http://translate.google.com/translate?u=' + MyURL + '&amp;hl=en&amp;ie=UTF-8&amp;sl=en&amp;tl=de"><img src="http://www.embassytahoe.com/assets/GERM0001.GIF" border="0" width="20" height="15" /></a>&nbsp;&nbsp;'
		+ '<a href="http://translate.google.com/translate?u=' + MyURL + '&amp;hl=en&amp;ie=UTF-8&amp;sl=en&amp;tl=ja"><img src="http://www.embassytahoe.com/assets/JAPA0001.GIF" border="0" width="20" height="15" /></a>&nbsp;&nbsp;'
		+ '<a href="http://translate.google.com/translate?u=' + MyURL + '&amp;hl=en&amp;ie=UTF-8&amp;sl=en&amp;tl=ko"><img src="http://www.embassytahoe.com/assets/KORE0001.GIF" border="0" width="20" height="15" /></a>&nbsp;&nbsp;'
		+ '<a href="http://translate.google.com/translate?u=' + MyURL + '&amp;hl=en&amp;ie=UTF-8&amp;sl=en&amp;tl=zh-CN"><img src="http://www.embassytahoe.com/assets/CHIN0001.GIF" border="0" width="20" height="15" /></a>&nbsp;&nbsp;'
	);

	//get pixel code on reservation form submit
	$('#quicksearch').submit(function()
	{
		$.ajax(
		{
			url : '/global/conversion_pixel.php', success : function (data)
			{		
				$('body').append(data);
			}
		});
	});
});

function checkIt(item)
{
	if(item == 'arrivalDate')
	{
		var arrival = $('#arrivalDate').datepicker('getDate').getDate();
		var departure = $('#departureDate').datepicker('getDate').getDate();
		
		if(arrival == departure)
		{
			var nextDayDate = $('#arrivalDate').datepicker('getDate', '+1d');  
			nextDayDate.setDate(nextDayDate.getDate() + 1);  
			$('#departureDate').datepicker('setDate', nextDayDate);  
		}
	}
}

function updateDates()
{
	var arrivalDay = $('#arrivalDate').datepicker('getDate').getDate();
	var arrivalMonth = $('#arrivalDate').datepicker('getDate').getMonth() + 1;
	var arrivalYear = $('#arrivalDate').datepicker('getDate').getFullYear();

	$("input[name='arrivalDay']").val(arrivalDay);
	$("input[name='arrivalMonth']").val(arrivalMonth);
	$("input[name='arrivalYear']").val(arrivalYear);

	var departureDay = $('#departureDate').datepicker('getDate').getDate();
	var departureMonth = $('#departureDate').datepicker('getDate').getMonth() + 1;
	var departureYear = $('#departureDate').datepicker('getDate').getFullYear();

	$("input[name='departureDay']").val(departureDay);
	$("input[name='departureMonth']").val(departureMonth);
	$("input[name='departureYear']").val(departureYear);
}

function updateTime()
{
	$.ajax(
	{
		url : '/global/time.php', success : function (data)
		{		
			$('#time').html(data);
		}
	});
	setTimeout('updateTime()', 15000);
}
updateTime();
