/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

/* VideoObj
*************************************************************/
function SetupVideo(){
	var self = this;
	
	self.init = function(){
		$('.page_video a').unbind('click').click(function(){
			self.setup_media_overlay($(this).attr('href'));
			return false;
		});
	}
	
	self.setup_media_overlay = function(to_link){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
		$('body').append('<div id="page_popover"></div>');
		if(to_link.indexOf('youtube.com') > 0){
			$("#page_popover").html('<object width="560" height="340"><param name="movie" value="' + to_link + '"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="' + to_link + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object>');
			
			$("#page_popover").dialog({
				width: 570,
				height: 360,
				top: 88,
				moveable: false,
				resizable: false,
				modal: true
			});

		} else {
			$("#page_popover").html("<a href='" + to_link + "'>&nbsp;</a>");
			
			$("#page_popover").dialog({
				width: 500,
				height: 400,
				top: 88,
				moveable: false,
				resizable: false,
				modal: true
			});
			$("#page_popover a").addClass("media {width:480,height:375}");
			$("#page_popover a.media").media();
		}
		$('.ui-dialog').css('top','73px');
		$('.ui-dialog').css('overflow','visible');
		$('.ui-widget-overlay').fadeTo('slow',0.95);
			
		$(".ui-widget-overlay").unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
		$(".ui-dialog-titlebar-close").click(function(){
			self.close_overlay();
			return false;
		});
	}
	self.close_overlay = function(){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
	}
	
	self.init();
}

/* OverlayObj
*************************************************************/
function Overlay(){
	var self = this;
	
	self.init = function(){
		$('a.overlay').unbind('click').click(function(){
			self.setup_overlay($(this).attr('href'));
			return false;
		});
	}
	
	self.setup_overlay = function(to_link){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
		$('body').append('<div id="page_popover"></div>');
		$("#page_popover").html('<img src="' + to_link + '" />');
		$("#page_popover").dialog({
			width: 590,
			height: 470,
			top: 150,
			moveable: false,
			resizable: false,
			modal: true
		});
		
		$('.ui-dialog').css('top','73px');
		$('.ui-dialog').css('overflow','visible');
		$('.ui-widget-overlay').fadeTo('slow',0.95);
			
		$(".ui-widget-overlay").unbind('click').click(function(){
			self.close_overlay();
			return false;
		});
		$(".ui-dialog-titlebar-close").click(function(){
			self.close_overlay();
			return false;
		});
	}
	self.close_overlay = function(){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
	}
	
	self.init();
}

/* REGISTRATION OVERLAY
*************************************************************/
function RegisterOverlay(){
	var self = this;
	self.form_location = "/_lib/request_form.php";
	
	self.init = function(){
		$("#register_now").unbind('click').click(function(){
			self.setup_request();
			return false;
		});
	}
	
	self.setup_request = function(){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
		$('body').append('<div id="page_popover"></div>');
		$.post(self.form_location,'&from_link='+window.location,function(data){
			if(data){
				$("#page_popover").html(data);
				$("#page_popover").dialog({
					width: 690,
					moveable: false,
					resizable: false,
					modal: true
				});
				$('#request_form').validate({	
					messages: {
						first_name: "Please enter your first name",
						last_name: "Please enter your last name",
						phone_number: "Please enter your phone number",
						email: "Please enter a valid email address",
						number_of_guests: "Please enter in the number of guests",
						arrive_month: "Please enter in your anticipated arrival month",
						arrive_day: "Please enter in your anticipated arrival day",
						arrive_year: "Please enter in your anticipated arrival year",
						nights_to_stay: "Please enter in the number of nights you wish to stay",
						suite: "Please enter which suite you plan on staying in"
					},
					errorLabelContainer: "#form_errors ul",
  					wrapper: "li",

					invalidHandler: function(form, validator) {
					  var errors = validator.numberOfInvalids();
					  if (errors) {
						$("#form_errors").show();
					  } else {
						$("#form_errors").hide();
					  }
					},
					submitHandler: function() {
						self.ajaxSubmit();
					}
				});
				$('.ui-dialog').css('top','73px');
				$('.ui-dialog').css('overflow','visible');
				$('.ui-widget-overlay').fadeTo('slow',0.95);
			
				$(".ui-widget-overlay").unbind('click').click(function(){
					self.close_overlay();
					return false;
				});
				$(".ui-dialog-titlebar-close").click(function(){
					self.close_overlay();
					return false;
				});
			}
		});
	}
	
	self.ajaxSubmit = function(){
		var content = $("#page_popover form").serializeArray();
		if(self.ajax_call){
			self.ajax_call.abort();
		}
		self.ajax_call = $.post(self.form_location,content,function(data){
			if(data){
				$("#page_popover").html(data);
				$('#request_form').validate({	
					messages: {
						first_name: "Please enter your first name",
						last_name: "Please enter your last name",
						phone_number: "Please enter your phone number",
						email: "Please enter a valid email address",
						number_of_guests: "Please enter in the number of guests",
						arrive_month: "Please enter in your anticipated arrival month",
						arrive_day: "Please enter in your anticipated arrival day",
						arrive_year: "Please enter in your anticipated arrival year",
						nights_to_stay: "Please enter in the number of nights you wish to stay",
						suite: "Please enter which suite you plan on staying in"
					},
					errorLabelContainer: "#form_errors ul",
  					wrapper: "li",

					invalidHandler: function(form, validator) {
					  var errors = validator.numberOfInvalids();
					  if (errors) {
						$("#form_errors").show();
					  } else {
						$("#form_errors").hide();
					  }
					},
					submitHandler: function() {
						self.ajaxSubmit();
					}
				});
			}
		});
	}
	self.close_overlay = function(){
		if($("#page_popover").length > 0 ){
			$("#page_popover").dialog("destroy");
			$("#page_popover").remove();
		}
	}
	self.init();
}

/* HomeSlides
*************************************************************/
function HomeSlides(){
	var self = this;
	self.current_image = 0;
	self.num_images = 0;
	self.image_container = "";
	self.interval = null;
	self.timer = 5000;
	
	self.init = function(){
		self.image_container = "video_slides";
		if($("#"+self.image_container+" img").length > 1){
			self.num_images = $("#"+self.image_container+" img").length;
			self.setup();
		}
		
	}
	
	self.setup = function(){
		$("#"+self.image_container+" img").hide();
		$("#"+self.image_container+" img:eq(0)").show();
		self.interval = setTimeout("HomeSlidesObj.show_image()",self.timer);
	}
	
	self.show_image = function (){
		next_image = self.current_image + 1;
		prev_image = self.current_image;
		if(next_image >= self.num_images){
			next_image = 0;
		}
		if(prev_image < 0){
			prev_image = (self.num_images-1);
		}
		self.current_image = next_image;
		
		$("#"+self.image_container+" img:eq("+next_image+")").css("z-index","200").fadeIn('slow', function(){
			self.interval = setTimeout("HomeSlidesObj.show_image()",self.timer);
			$("#"+self.image_container+" img:eq("+prev_image+")").css("z-index","100").hide();
			$("#"+self.image_container+" img:eq("+next_image+")").css("z-index","100")
		});
	}
	
	self.init();
}

/* MEDIA PLAYER DEFAULTS
*************************************************************/
$.fn.media.defaults.flvPlayer= '/_lib/player.swf';
$.fn.media.defaults.flashvars={overstretch: 1, controlbar: 'over',autostart: 'true'};
$.fn.media.defaults.mp3Player= '/_lib/player.swf';
$.fn.media.defaults.params= {allowFullScreen: "true"};

/* DOCUMENT READY FUNCTION
*************************************************************/
$(document).ready(function(){
	if($('.page_gallery').length == 1){
		$('.page_gallery a').lightBox();
	} else {
		$('.page_gallery').each(function(){
			var id = $(this).attr('id');
			$('#' + id + ' a').lightBox();
		});
	}
	var videoObj = new SetupVideo();
	var OverlayObj = new Overlay();
	var RegisterOverlayObj = new RegisterOverlay();
});