//$(document).ready(function() {  This is the same as the next line below
$(function() {
	activateActiveX();
	$.id = function(id){ return document.getElementById(id); }
	$("div#js-warning").hide();
	$("div#js-warning-spacer").hide();
	$("select#choose_hotel").change( function() {
			//window.open(this.value, "", "width=800, height=650, menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
			/*window.location.href = this.value;*/
			var s = this.options[this.selectedIndex].value;window.open(s);
			if (true) this.selectedIndex=0;
		}
	);
	
	$('.coupon').click ( function() {
			window.open(this.href, '', 'width=500, height=500');
			return false;
		}
	);
	
	$("div#photo-gallery a").click ( function() {
			window.open(this.href, '', 'width=800, height=470');
			return false;
		}
	);
	$("div#photo-gallery2 a").click ( function() {
			window.open(this.href, '', 'width=800, height=470');
			return false;
		}
	);
	
	
// Finds a form on the page and instantiates a Calendar Popup
// if it is present
	$(this).find("form").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.addDisabledDates( null, formatDate( yesterday, "yyyy-MM-dd" ) );
			
			// create the <div> for calendar popup
			$("body").append("<div id=\"cp\"></div>");
		}
		// hide all "other" fields
		$("ul.checkboxgroup input.other").filter(".textfield").hide();
		$("ul.checkboxgroup input").click(function() {
			// if the input has a class of other, show it's "other" field
			if ($(this).is(".other")){
				$(this).siblings(".other").show();
				// if it's a checkbox, remove the "other" field when it is unchecked
				if (!this.checked) {
					$(this).siblings(".other").filter("input[@type='text']").hide();
				}
			}
			// if it's a radio button group, hide "other" fields if not needed
			else if ($(this).is(".radio")) {
				$(this).parent().parent().find(".other").filter(".textfield").hide();
			}
		});
	});

// For Input Elements that need a calendar, but no calendar Icon
// Simply put a class on each input and put that class name in the
// function below
	$(".makeitpopup").click(function() {
		var o = this;
		cal.select( o, this.id, "MM/dd/yyyy" );
		return false;
	});


// sIFR'ize headers 	
	$('#content h2, #hotel-name, #hotel-location, #back-to-list').sifr(
		{ strSWF: 'flash/sifr/frutiger-65-bold.swf', strColor: '#337ecb', strWmode: 'transparent', strHoverColor: '#337ecb' },
		{ expressInstall: true }
	);




// For Input Elements w/a calendar icon
// Put the class name of the span where the Icon will leave
// preferrably behind the input
	$(this).find(".form-cal").each(function() {
		// finds each form-cal instance and 
		// replaces <span> tags with calendar image
		
		// creates the calendar icon with a link and attributes
		var calLink = document.createElement( "a" );
		calLink.href = "#";
		calLink.id = "form-cal-" + $(this).siblings ("input").attr("id");
		calLink.name = "form-cal-" + $(this).siblings ("input").attr("id");
		calLink.title = "Click to select a date";

		// creates the icon and adds it
		var calImg = document.createElement( "img" );
		calImg.src = "images/icon-cal.gif";
		calLink.appendChild( calImg );
		
		// adds the icon and removes the span
		this.parentNode.appendChild( calLink );
		this.parentNode.removeChild( this );

		// when the icon is clicked show the popup
		$(calLink).click(function(){
			var o = this.previousSibling;
			while ( o.nodeType != 1 ) {
				o = o.previousSibling;
			}
			cal.select( o, this.id, "MM/dd/yyyy" );
			return false;
		});
	});





// Validates the Forms and lets the user know which
// form fields they have problems with
	$(".validate").submit(function() {
		var counter = 0;
		var first_error = "";
		
		// find all elements w/field class name
		$(this).find(".field").each(function() { 
			// find all elements w/required class name
			$(this).find(".required").each(function() { 
				// if the element is not a label do it
				$.removeWarning($(this));
				if ( ($(this).is("input")) || ($(this).is("select")) ) {
					// This part is for input, select, and textareas

					var valid = true;
					var o = $(this).val();
	
					if ($(this).is(".email")) {
						// if it's an email address make sure the email is valid using both regular expressions
						var regExp = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
						var regExp2 = /(\s+)|(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
						if ( ( o.search( regExp ) ) == -1 || o.search( regExp2 ) != -1 ) {
							valid = false;
						}
					} else if (o.replace( /(^\s+)|(\s+$)/g, '' ).length < 1) {
						// if not an email address take out funky characters and see if its still blank
						valid = false;
					}
					
					// if the data is not valid show warning icon
					if (!valid) {
						$.addWarning($(this));
						counter++;
						if ( first_error == "" ) {
							first_error = $(this).attr("id");
						}		
					}
				}
				else if ( $(this).is("ul") ) {
					// This is for checkboxes
					$(this).each(function() { 
						// find all elements w/required class name
						var valid = false;
						$(this).find("li").each(function() {
							// checkboxes in the list
							var testdis = $(this).children("input");
							// if it is checked do this
							if(testdis.attr("checked") == true){
								valid = true;
							}
						});
						if(!valid) { 
							$.addWarning($(this));
							counter++;
							if ( first_error == "" ) {
								first_error = $(this).attr("id");
							}
						}
					});			
				}
			});
		});
		if ( counter > 0 ) {
			alert( "One or more required fields were not correctly supplied. Please double-check the noted fields." );
			$.id(first_error).focus();
			return false;
		}		
	});


// Removes the Warning Icon
	jQuery.removeWarning = function(el) {
		// find img elements, if it's present, remove it
		var testwar = $(el).parents(".field").find("img.validation-error").remove();
	};
	
// Adds the Warning Icon
	jQuery.addWarning = function(el) {
		var img = "<img />";
		// adds the source attribute
		img = $(img).attr("src","images/icon-warning.gif");
		// adds the validation error class
		img = $(img).attr("class","validation-error");	
		// Add the image after the input field
		//if inputObject is part of checkbox group put it after the first list item
		if( $(el).is("ul") ) {
			$(el).find("li:first").append(img);
		}
		// if not throw it at the end
		else {
			$(el).after(img);
		}
	};


// For Each Package Wrapper add A Toggle Function if Neccessary
	$("div.package-wrapper").each(function() {
	// Div for the Toggler to be Held
	$(this).append("<div class=\"package-toggle\" style=\"clear:both;\">");

		// If this is a package that is going to be expanded
		// add a control for it
		if( $(this).is(".package-expandable") ) {
			// Since this is expandable hide the details
			$(this).find("div.package-long").hide();
			// Add the Control
			$(this).find(".package-toggle").append("<a class=\"view_details\" href=\"#\">View Details</a>");
		}
	});


// Once the Package Toggler Control is clicked
// Toggle the details and chance the text
	$("div.package-toggle a").toggle(function() {
		$(this).parents("div.package-wrapper").find("div.package-long").animate({
			height: 'show',
			opacity: 'show'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("Hide Details");
	}, function() {
		$(this).parents("div.package-wrapper").find("div.package-long").animate({
			height: 'hide',
			opacity: 'hide'
		}, 'slow');
		// Changes the Text of the Control
		$(this).html("View Details");
	});
});

/* Source : http://sixtwothree.org/blog/archives/2006/05/20/activateactivex-11/ */
function activateActiveX() {
	if ( !document.getElementsByTagName || !document.body.outerHTML || !document.compatMode ) return false;
	var elems = new Array( "object", "applet" );
	for ( i = 0, j = elems.length; i < j; i++ ) {
		var objects = document.getElementsByTagName(elems[i]);
		for ( k = 0, l = objects.length; k < l; k++ ) {
			var params = "";
			for ( m = 0, n = objects[k].childNodes.length; m < n; m++ ) {
				params += objects[k].childNodes[m].outerHTML;
			}
			objects[k].outerHTML = objects[k].outerHTML.replace( "</" + elems[i].toUpperCase() + ">", params + "</" + elems[i].toUpperCase() + ">" );
		}
	}
}
