$(document).ready(function(){
	attach_handlers();
	$('a').autoAnalytics(pageTracker);

	$("#newletter_signup").bind("submit", function() {
		var firstmail  = $("#recipient_email").val();
		var secondmail  = $("#recipient_email_confirm").val();
		if (firstmail.length <= 0){
			alert ("You must enter an email address.");
			return false;
		}
		if (firstmail != secondmail){
			alert ("Email addresses must match.");
			return false;
		}
	});
});

function vars_from_string(str) {
	var vars = new Object();
	var pairs = str.split('_');
	if (pairs.length > 0) {
		for (var i=0; i < pairs.length; i++) {
			var pair = pairs[i].split(':');
			vars[pair[0]] = pair[1];
		}
	}
	return vars;
}

var tabInitCount = 0;
var allTabsInitialized = false;
function onInitTabs() {
	tabInitCount++;
	var allTabs = $('ul.jt-tabs');
	//When all tabs have been initialized and the page layout has settled, navigate to our anchor if there is one
	if (tabInitCount >= allTabs.length && !allTabsInitialized) {
		var anchorPos = window.location.href.indexOf('#');
		if (anchorPos > -1) {
			var anchorId = window.location.href.substr(anchorPos);
			if (anchorId != '#') {
				//$.scrollTo($(anchorId),0);
				$('html, body').animate({
					scrollTop: $(anchorId).offset().top
				}, 0);
			}
		}
		allTabsInitialized = true;
	}
}

function attach_handlers() {
	//Tab functionality
	$('div.domtab > div#modernbricksmenu > ul.domtabs').each(function(){
		$(this).tabs( $(this).parents('div.domtab').children('div#modernbricksmenuline').nextAll() );
	});

	//Newer Tab Functionality
	$('ul[class*="jt-tabs"]').each(function(){
		$(this).tabs($(this).next('div.panes').children(),{onBeforeClick:onInitTabs});
	});
	$('ul[class*="jt-tabs"] > li > a').click(function(event){
		event.preventDefault();
	});
	//Night tab matching
	$('div.package-seasons').find('ul.nights > li a').click(function(){
		//Determine the index that's been selected
		var index = $(this).parents('ul.nights:first').children('li').index($(this).parents('li:first'));
		//For each season...
		$(this).parents('div.package-seasons').children('div.pane').each(function() {
			//Remove any 'current' classes on all but the desired link
			var night_lis = $(this).children('ul.nights').children('li');
			$(night_lis).children('a').removeClass('current');
			$(night_lis).eq(index).children('a').addClass('current');
			//Hide all nights but the desired one
			var night_panes = $(this).children('div.panes').children('div.pane');
			$(night_panes).hide();
			$(night_panes).eq(index).show();
		});
	});

	//Datepickers
	$('.datepicker').datepicker();

	$('.display-more-amenities').click(function() {
		$('.extra-amenities').toggle();
		if( $('.extra-amenities').is(':visible') ) {
			$('.display-more-amenities span').text('Hide Extra');
		} else {
			$('.display-more-amenities span').text('View All');
		}
	});

	//Colorbox Functionality
	$('.colorbox').each(function(){
		var vars = vars_from_string($(this).attr('name'));
		if (!vars.hasOwnProperty('innerWidth')) {
			vars.innerWidth = 650;
		}
		if (!vars.hasOwnProperty('innerHeight')) {
			vars.innerHeight = 500;
		}
		if (!vars.hasOwnProperty('onComplete')) {
			vars.onComplete = attach_handlers;
		}
		if (!vars.hasOwnProperty('rel')) {
			vars.rel = 'nofollow';
		}
		$(this).colorbox(vars);
	});

	//Selects that act as links
	$('select.direct-link').change(function(){
		var url = $(this).val();
		window.location = url;
	});

	//Popup code
	$('a.Popup').bind('click',function(){ return false;});
	$('a.Popup').click(function(){
		var URL = $(this).attr('href');
		var wPos = URL.indexOf('w=');
		var hPos = URL.indexOf('h=');
		var width;
		var height;
		if (wPos != -1) {
			width = URL.substr(wPos+2,4);
		} else {
			width = 600;
		}
		if (hPos != -1) {
			height = URL.substr(hPos+2,4);
		} else {
			height = 600;
		}

		var parameters = 'height='+height+', width='+width+', scrollbars=yes, location=no, menubar=no';

		window.open(URL, 'CalendarPopup', parameters);
	});

	//Show javascript only elements
	$('.script-only').each(function() {
		$(this).css('display','block');
	});
	$('.script-only-i').each(function() {
		$(this).css('display','inline');
	});
	$('.script-hide').each(function() {
		$(this).hide();
	});

	//Assign toggle functionality
	$('div.toggle').css('display','none');
	$('a.toggle').each(function() {
		$(this).bind("click", function() {return false;});
		var id = $(this).attr('href').substr(1);
		//Check for anchor in url to automatically open the container
		var windowUrl = new String(window.location);
		var urlAnchorPos = windowUrl.indexOf('#');
		if (urlAnchorPos > -1) {
			var anchorId = windowUrl.substr(urlAnchorPos+1);
			if (anchorId == id || anchorId == 'see-'+id) {
				$('#'+id).show();
			}
		}
		$(this).click(function() {
			//var target = $('div.toggle').filter(function () {return $(this).id('title') == id;});
			var target = $('div#'+id);
			$(target).slideToggle();
		});
	});

	//Glider Tracking
	$('div#scroller-controls').children().mousedown(function(){
		$.get($(this).attr('href'));

		return false;
	});

	//Countdown
	$('input.countdown').each(function(){
		var countdownTarget = new Date($(this).val());

		countdownVars = new Object();
		countdownVars.until = countdownTarget;
		countdownVars.timezone = -6;
		countdownVars.expiryText = 	$(this).siblings('input.expiryText').val();
		var customFormat = $(this).siblings('input.format').val();
		if (customFormat) {
			countdownVars.format = customFormat;
		}
		var expiryHide = $(this).siblings('input.expiryHide').val();
		countdownVars.alwaysExpire = true;
		countdownVars.onExpiry = function(){
			$('#'+expiryHide).hide();
		}

		if ($(this).countdown) {
			$(this).parent().countdown(countdownVars);
		}
	});

	$('.swap-for-flv').click(function(){
		var imageSrc = $(this).find('img:first').attr('src');
		var videoSrc = $(this).siblings('input[name="video"]').val();
		var videoTracker = $(this).siblings('input[name="video_tracker"]').val();
		var flashVars = 'videoFile='+videoSrc+'&showMoreVideos=true&trackingUrl='+videoTracker;
		var nocache = '3';

		var flvHtml = '<div style="margin-top:10px">';
		flvHtml += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="185" height="137" id="mini_player" align="middle">'
		flvHtml += '<param name="allowScriptAccess" value="sameDomain" />';
		flvHtml += '<param name="allowFullScreen" value="false" />';
		flvHtml += '<param name="movie" value="/swf/mini_video_player.swf?nocache='+nocache+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />';
		flvHtml += '<param name="flashvars" value="'+flashVars+'" />';
		flvHtml += '<embed src="/swf/mini_video_player.swf?nocache='+nocache+'" flashvars="'+flashVars+'" quality="high" bgcolor="#ffffff" width="185" height="137" name="mini_player" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />';
		flvHtml += '</object>';
		flvHtml += '</div>';
		$(this).after(flvHtml);
		$(this).remove();
		return false;
	})
}
