$(function() {
	if($('body').is('.home')) {
		checkCookie();
		$('#modal-wrapper').jqm({closeClass:'jqmClose'}).jqmShow();
	}

	$('#modal-popunder-wrapper').jqm({trigger:'.video-modal', closeClass:'close-modal'});
	$("div#concept-movie").flash(
		{src:"/flash/citizenm.swf",
		 width:590,
		 height:472,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"8"});

$('form#booking-form').submit(function() {
	    var location = $('#location'),
		val = location.val(),
		exists = location.parent().find('#location-icon').length,
		icon = $('<img />')
		    .attr('src', '/images/booking-validation.gif')
		    .attr('id', 'location-icon');

	    if ( !val ) {
		if ( !exists ) {
		    location.parent().append( icon );
		} else {
		    $('#location-icon').show();
		}
		return false;
	    } else {
		icon.hide();
	    }
	});

/*	$('select#location').change(function() {
	    var icon = $('#location-icon').length;

	    if ( this.value == 'WTC' ) {
		if ( icon ) {
		    $('#location-icon').hide();
		}
		var date = $('input#arrive').val(), reset = true;
		if ( date != 'dd/mm/yyyy' ) {
		    var temp = date.split('/'),
			 dateTimeStamp = new Date(temp[1] + '/' + temp[0] + '/' + temp[2]).getTime(),
			 openingDate = new Date('04/16/2009').getTime();

		    if ( dateTimeStamp > openingDate ) {
			reset = false;
		    }
		}
		if ( reset ) {
		    $('#arrive').val('16/04/2009');
		}
	    } else if ( this.value != '' ) {
		if ( icon ) {
		    $('#location-icon').hide();
		}
	    }
	});
*/
$('#check-availability-wrapper').tabs();
	$('#tab-citz-login a').click(function() {
		$('#tab-check-avail').removeClass("tabs-selected");
		$('#check-avail-form').hide();
		$('#tab-citz-login').addClass("tabs-selected");
		$('#citz-login-form').show(); 
		return false;
        });

        $("#tab-check-avail a").click(function() {
		$('#citz-login-form').hide();
		$('#check-avail-form').show();
		$('#tab-check-avail').addClass("tabs-selected");
		$('#tab-citz-login').removeClass("tabs-selected");
	});
	
	$('a.open-login').click(function() {
			if ($('#tab-check-avail').hasClass("tabs-selected"))
				{
					$('#tab-check-avail').removeClass("tabs-selected");
					$('#check-avail-form').hide();
					$('#tab-citz-login').addClass("tabs-selected");
					$('#citz-login-form').show(); 
				} 
			else {
					$('#check-avail-form').hide();
					$('#tab-citz-login').addClass("tabs-selected");										
					$('#citz-login-form').show(); 
			}
        });


		
	$('body.home div#content-wrapper h1').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff', strWmode: 'transparent' }, { expressInstall: true } );
	$('body.home div#content-wrapper h2').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#e2003d;', strWmode: 'transparent' }, { expressInstall: true } );

	
	$('div#content h1').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff', strWmode: 'transparent' }, { expressInstall: true } );
	$('div#content h2').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#E2003D', strLinkColor: '#E2003D', strHoverColor: '#E2003D', strWmode: 'transparent' }, { expressInstall: true } );
	$('div#content h2#map-bottom').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#E2003D', strLinkColor: '#E2003D', strHoverColor: '#E2003D', strWmode: 'transparent', strFlashVars: 'textalign=right&' }, { expressInstall: true } );
	$('div#login-welcome h4').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff', strWmode: 'transparent' }, { expressInstall: true } );

	$('body#press div#content h3').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#e2003d;', strWmode: 'transparent' }, { expressInstall: true } );

	$('div.form-help').hide();
	$('a.help-link').toggle( function() {
		$(this).parents('div.field').children('div.form-help').show();
		return false;
	},function(){ 
	  $(this).parents('div.field').children('div.form-help').hide();
	  return false;
	});
	
	// For Each Package Wrapper add a Toggle Function if Neccessary
	$("div.comment-body").each(function() {
		if( $(this).children().is('.comment-long') ) {
			$('.comment-long').hide();
			$(this).children('div.comment-author').before('<span class="comment-more" ><a href="#">(read more...)</a></span>');
		}
	});
	
	$('ul#categories li a').hover(function() {
		$(this).children('span').show();
	}, function() {
		$(this).children('span').hide();
	});
	
	$("span.comment-more a").toggle(function() {
		$(this).parents("div.comment").find("div.comment-long").animate({
			height: 'show'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("(read less...)");
	}, function() {
		$(this).parents("div.comment").find("div.comment-long").animate({
			height: 'hide'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("(read more	...)");
	});
	
	$('.hotel-fact-sheet').each(function() {
		var el = this;
		var moreLess = $('<div></div>').html('View more...')
						.bind('click', toggleFaq)
						.css({cursor: 'pointer', position: 'absolute', right: '45px', top: '-40px',
							 backgroundColor: '#e2003d', fontWeight: 'bold', padding: '2px'});
						
		$('dt:gt(0)', el).hide();
		$('dd:gt(0)', el).hide();
		$(el).prepend(moreLess);
		
	});
	
	$('#thank-you').hide();
	
	$('.faq-desc').hide();
	$('.faq-toggle').toggle(function() { $(this).next().show(); },
							function() { $(this).next().hide(); });

	//Used for the change moods select box
	$("select#auto-select").change(function(){
		var value = $("option:selected").val();
		if($(this).is('.latest')) {
			parent.location = 'community-latest.php?channel=' + value;
		} else {
			parent.location = value;
		}
	});

	$("#interactive-map").flash(
		{src:"/flash/interactive-map.swf",
		 width:487,
		 height:236,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"}
	);
		
	$("div#image-gallery").flash(
		{src:"/flash/photos.swf",
		 width:600,
		 height:450,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"}
	);
	
	/*$("div#home-movie-push").flash(
		{src:"flash/banner.swf",
		 width:160,
		 height:64,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"}
	);*/


	$("div#room-tour").flash(
		{src:"/flash/citizen_m_room.swf",
		 width:650,
		 height:610,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"});
	
		$("div#concept-movie").flash(
		{src:"/flash/citzenm.swf",
		 width:590,
		 height:472,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"});


//Special Packages JS

	$(".package-wrapper").each( function() {
		var parent = $(this);
		$(".package-image", parent).hover( function() {
			parent.addClass("package-wrapper-hover");
			}, function() {
			parent.removeClass("package-wrapper-hover");											
		});
		$(".package-details a", parent).hover( function() {
			parent.addClass("package-wrapper-hover");
			}, function() {
			parent.removeClass("package-wrapper-hover");											
		});
	});


// Finds a form on the page, instantiates a Calendar Popup and hides "other" fields if necessary
	$(this).find("form#comsearchform").each(function() {
		if ( window.CalendarPopup ) {
			// instantiate CalendarPopup and set some params
			
			// Sets starting date by yesterdays date
			var yesterday = new Date();
			yesterday.setDate( yesterday.getDate() - 1 );
			
			// new instance of Calendar popup
			cal = new CalendarPopup( "cp" );
			cal.offsetX = 0;
			cal.offsetY = -158;
			cal.addDisabledDates( formatDate( yesterday, "MM/dd/yyyy" ), null );
			
			// create the <div> for calendar popup
			$("body").append("<div id=\"cp\"></div>");
		}
	});

// Input Elements that need a calendar popup, but no calendar Icon, need a class of "makeitpopup"
	$(".makeitpopup").click(function() {
		cal.select( this, this.id, "MM/dd/yyyy" );
		return false;
	});


// Finds all elements with a class of "form-cal" and replaces them with a calendar icon, link, and onclick handler
// Only difference between euro and regular formats is the default values in boxes, formatDate() does not need to change
	$(this).find(".form-cal-euro").each(function(i) {
		var icon = '<a href="#" class="icon-cal" id="form-cal-euro-' + i + '" name="form-cal-euro-' + i + '" title="Click to select a date"><img src="/images/icon-cal.gif" /></a>';
		$(icon).appendTo($(this).parent()).click(function(){
			//hide all dropdowns for IE6 and lower
			if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","hidden"); }

			var o = this.previousSibling;
			while (o.nodeType != 1) { o = o.previousSibling; }
			
			// check to see if it is an arrival/departure pair
			if($(o).is(".date-end")) {
				var arrive_string = $(o).parent().prev(".field").children(".date-begin").attr("value");
				var arrive_label = $(o).parent().prev(".field").children("label").html().replace(":","");
				//check to see if begin date has been set already and adjust disabled/current dates
				if (arrive_string != "dd/mm/yyyy" && arrive_string) {
					var arrive_array = arrive_string.split('/');
					var arrive_date = new Date(arrive_array[1]+'/'+arrive_array[0]+'/'+arrive_array[2]);
					var arrive_date = formatDate(arrive_date, "MM/dd/yyyy" );
					cal.clearDisabledDates();
					cal.addDisabledDates( null, arrive_date );
					cal.select(o,$(this).attr("id"),"dd/MM/yyyy", arrive_date);
					return false;
				}
				// begin date not set first, so pull label text and alert
				else {
					alert("Please select \"" + arrive_label + "\" first");
					if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","visible"); }
					return false;
				}
			}
			
			if($(o).is("#arrive")) {
				cal.clearDisabledDates();
				var yesterday = new Date();
				yesterday.setDate( yesterday.getDate() - 1 );
				cal.addDisabledDates( null, formatDate( yesterday, "MM/dd/yyyy" ));
				yesterday.setDate( yesterday.getDate() + 1 );
				cal.select( o,$(this).attr("id"), "dd/MM/yyyy", formatDate( yesterday, "dd/MM/yyyy" ) );
				return false;
			}

			if($(o).is(".archive-begin")) {
				cal.clearDisabledDates();
				var yesterday = new Date();
				yesterday.setDate( yesterday.getDate() + 1 );
				cal.addDisabledDates( formatDate( yesterday, "MM/dd/yyyy" ), null );
				cal.select( o,$(this).attr("id"), "dd/MM/yyyy", yesterday );
				return false;
			}

			//searching dates in the past
			if($(o).is(".archive-end")) {
				var arrive_string = $(o).parent().prev(".field").children(".archive-begin").attr("value");
				var arrive_label = $(o).parent().prev(".field").children("label").html();
				//check to see if begin date has been set already and adjust disabled/current dates
				if (arrive_string != "dd/mm/yyyy" && arrive_string) {
					var arrive_array = arrive_string.split('/')
					var arrive_date = new Date(arrive_array[1]+'/'+arrive_array[0]+'/'+arrive_array[2]);
					var arrive_date = formatDate(arrive_date, "dd/MM/yyyy" );
					cal.clearDisabledDates();
					var yesterday = new Date();
					yesterday.setDate( yesterday.getDate() + 1 );
					cal.addDisabledDates( null, arrive_date );
					cal.addDisabledDates( formatDate( yesterday, "dd/MM/yyyy" ), null );
					cal.select(o,$(this).attr("id"),"dd/MM/yyyy", arrive_date);
					return false;
				}
				// begin date not set first, so pull label text and alert
				else {
					alert("Please select \"" + arrive_label + "\" first");
					if (document.all && window.ActiveXObject && !window.opera) { $(this).parents("form").find("select").css("visibility","visible"); }
					return false;
				}
			}
			// it's just a plain calendar popup
			else {
				cal.clearDisabledDates();
				var yesterday = new Date();
				yesterday.setDate( yesterday.getDate() - 1 );
				cal.addDisabledDates( null, formatDate( yesterday, "dd/MM/yyyy" ) );
				cal.select( o,$(this).attr("id"), "dd/MM/yyyy", yesterday );
				return false;
			}
		});
		$(this).remove();
	});

// Sample sifr usage - remove if not needed
	/*
	// intPadding array order is: left, top, right, bottom - must match css for corresponding element
	$('div#content h2').sifr(
		{ strSWF: 'flash/font-name.swf', strColor: '#cc0000', strLinkColor: '#cccc00', strHoverColor: '#cc00cc', strWmode: 'transparent', intPadding: [35, 0, 0, 0], strFlashVars: 'textalign=center&', strCase: 'upper' },
		{ expressInstall: true }
	);
	*/


// Sample usage of jquery.flash plugin - remove if not needed
	/*	If given the following markup:

	<div id="masthead">
		<img src="images/mastheads/flash-alt.jpg" alt="" /><br />
	</div>
	
	you would use the following to replace the alternate content:

	$("div#masthead").flash(
		{src:"flash/home.swf",
		 width:600,
		 height:300,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"});
	*/
});

function toggleFaq(e) {
	var el = e.target;
	var faqItem = $(el).parent('.hotel-fact-sheet');
	if($('dt:gt(0)', faqItem).is(':visible')) {
		$('dt:gt(0)', faqItem).hide();
		$('dd:gt(0)', faqItem).hide();
		$(el).html('View more...');
	} else {
		$('dt:gt(0)', faqItem).show();
		$('dd:gt(0)', faqItem).show();
		$(el).html('View less...');
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length,c.length);
			}
	}
	return null;
}

function checkCookie() { 
	var visited = readCookie('visited');

	if (visited!=null && visited!="")  {
		$("div#home-masthead").each(function(){
			var flash = new Array();
			var rNum = Math.floor(Math.random()*6);
			flash[0] = "chair";
			flash[1] = "bed";
			flash[2] = "shower";
			flash[3] = "toothbrush";
			flash[4] = "wifi";
			flash[5] = "window";
			$(this).flash(
				{src:"/flash/home_" + flash[rNum] +".swf",
				 width:650,
				 height:610,
				 wmode:"transparent",
				 quality:"high"},
				 {version:"9"}
			);
		});
	} else {
		createCookie('visited',"visited",365);
		$("div#home-masthead").flash(
			{ src:"/flash/manifesto.swf",
			 width:650,
			 height:610,
			 wmode:"transparent",
			 quality:"high"},
			 {version:"9"}
		);
	}
}
