$(function() {

	$('div#home-modal').each(function() {
		$(this).jqm().jqmShow();
	});


	$('#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 } );
	$('h3#press-title').sifr( { strSWF: 'flash/chalet-newyorknineteensixty.swf', strColor: '#e2003d;', strWmode: 'transparent' }, { expressInstall: true } );


//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");											
		});
	});


	$('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#sidebar h4').sifr( { strSWF: '/flash/chalet-newyorknineteensixty.swf', strColor: '#ffffff', 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').after('<span class="comment-more" ><a href="#">(read more...)</a></span>');
		}
	});*/
	
	/*$("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...)");
	});*/
	
	$('.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;
		}
	});
	
	$("div#image-gallery").flash(
		{src:"flash/photos.swf",
		 width:600,
		 height:450,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"}
	);


	$("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"}
		);
	});
	
	$("div#room-tour").flash(
		{src:"flash/citizen_m_room.swf",
		 width:650,
		 height:610,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"7"});


	//Used for the change moods select box
	$("select#mood-select").change(function(){
		var value = $("option:selected").val();
		parent.location = value;
	});


//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();
	});


	//social media sharing
	$('.share-link').live('click',function(event) {
		$('.higherStacked').removeClass('higherStacked');
		event.preventDefault();
		$('.share-panel').hide();
                var p = $(this).parents('.rss-item');
                if ( !p.length ) {
                    p = $(this).parents('.package-popup-content');
		    if ( !p.length ) {
			p = $(this).parents('.share-container');
		    }
                }
		p.find('.share-panel').css('display','inline');
		$(this).closest('.contains-share').addClass('higherStacked')
	});
	$('.share-close').live('click',function(event){
		event.preventDefault();
                var p = $(this).parents('.rss-item');
                if ( !p.length ) {
                    p = $(this).parents('.package-popup-content');
		    if ( !p.length ) {
			p = $(this).parents('.share-container');
		    }
                }
		p.find('.share-panel').slideUp('fast');
	});

	//Community Details
	$('.comment').each(function() {
		$('.comment-more a', this).toggle(function(event) {
			event.preventDefault();
			$('.share-panel').hide();
			var el = $(this);
			$.get('/includes/community-details.php', { event_id: this.id.substr(3), channel: this.rel },function(data) { 
				el.html('(read less...)').parent().addClass('active').parent().children('.comment-long').hide().html(data).slideDown('slow');
			});
		},function(event) { 
			event.preventDefault();
			$('.share-panel').hide();
			$(this).html('(read more...)').parent().removeClass('active').parent().children('.comment-long').slideUp('slow', function() { 
				$(this).html(''); 
			});
		});
	});


/*	$('.package-wrapper').each(function() {
		$('.package-details a', this).toggle(function(event) {
			event.preventDefault();
			$('.share-panel').hide();
			var el = $(this);
			$.get('/includes/package-details.php', { package_id: this.id.substr(3), channel: this.rel },function(data) { 
				el.html(opts.hideDetails).parent().addClass('active').parent().find('.package-long').hide().html(data).slideDown('slow');
			});
		},function(event) { 
			event.preventDefault();
			$('.share-panel').hide();
			$(this).html(opts.viewDetails).parent().removeClass('active').parent().find('.package-long').slideUp('slow', function() { 
				$(this).html(''); 
			});
		});
	});*/


	$('.package-wrapper').each(function() {
		$('.package-details a', this).click(function(event) {
			event.preventDefault();
			$('.share-panel').hide();
			var el = $('div#package-popup-wrapper');
			/*$.get('/includes/package-details.php', { package_id: this.id.substr(3), channel: this.rel },function(data) { 
				el.html(data);				
			});*/
			tb_show('Thickbox Title', 'includes/package-details.php?package_id='+this.id.substr(3)+'&channel='+this.rel+'&width=515&inlineId=package-popup-wrapper', null);
		});
	});

/*	$.fn.postPreview = function(options) {
		var defaults = {
			hideDetails : '(read less...)',
			viewDetails : '(read more...)'
		},
		opts = $.extend(defaults, options);
		return this.each(function() {
								  alert("here");
			$('.comment-more a', this).toggle(function(event) {
				event.preventDefault();
				$('.share-panel').hide();
				var el = $(this);
				$.get('community-details.php', { event_id: this.id.substr(3), channel: this.rel },function(data) { 
					el.html(opts.hideDetails).parent().addClass('active').parent().children('.comment-long').hide().html(data).slideDown('slow');
				});
			},function(event) { 
				event.preventDefault();
				$('.share-panel').hide();
				$(this).html(opts.viewDetails).parent().removeClass('active').parent().children('.comment-long').slideUp('slow', function() { 
					$(this).html(''); 
				});
			});
		});
	};*/

	if($('body').is('.home')) {
		$('#modal-wrapper').jqm({closeClass:'jqmClose'}).jqmShow();
	}

	// Don't let users enter a date
	$('#arrivehack').each(function(){
		$(this).click(function(){
			$('.icon-cal').click();
		});
	});
	
});

