var search_queue = {};
var searching_obj = null;
var photo_obj = null;
var photo_cache = new Array();
var photo_list = new Array();
var current_index = 0;
var rs_id = "";
var now = '';
var property_cache = {};
var see_lodging = {};

$(function()
{
	startHomeGallery();
	setupFormSubmits();
	setupDetailImages();
	setupInquireForms();
});

	
	function setupDetailImages()
	{
		if ($('.details-thumb').length)
		{
			cachePhotos();
			$('.details-thumb').mouseover(function(){
				var id = $(this).attr('id');
				var temp = id.split('_');
				var index = temp[1];
						      
				if (typeof(photo_cache[index]) != "undefined")
					$('#main_photo').attr('src', photo_cache[index].src);
			});
			
			$('.details-thumb').click(function(){
				var id = $(this).attr('id');
				var temp = id.split('_');
				var index = temp[1];
						      
				setPhoto(parseInt(index));
			});
			
			$('#main_photo').click(function(){
			  setPhoto(0);
			});
			
			$('#photo_gallery_overlay .prev').click(prevPhoto);
			$('#photo_gallery_overlay .next').click(nextPhoto);
			$('#photo_gallery_overlay .close').find('img').click(function(){
				$('#photo_gallery_overlay').fadeOut(200);
			});	
		}
	}

	function cachePhotos()
	{
		var args_len = photo_list.length;
		for (i = 0;i<args_len; i++)
		{
		  var cacheImage = document.createElement('img');
		  cacheImage.src = photo_list[i].photo;
		  photo_cache[i] = cacheImage;
		} 
	}

	
	function nextPhoto()
	{
		current_index++;
		if (current_index >= photo_list.length)
		{
			current_index = 0;
		}
		setPhoto(current_index);
	}
	
	function prevPhoto()
	{
		current_index--;
		if (current_index < 0)
		{
			current_index = photo_list.length - 1;
		}
		setPhoto(current_index);	
	}
	
	function setPhoto(index)
	{
		//if (!photo_obj.isOpened())
	//	  photo_obj.load();
		
		window.scroll(0,0);
		$('#large_image_src').fadeOut(100, function()
		{
			// figure out our index
			if (typeof index != 'number')
			{
				var temp = index.target.id.split('_');
				index = parseInt(temp[1]);
			}
			$("#large_image_src").attr("src", photo_cache[index].src);
			$('#photo_gallery_overlay .caption').html(photo_list[index].caption);
			
			var current_page = index + 1;
			
			$('#photo_gallery_overlay .gallery_status').html(current_page + '/' + photo_list.length);
			current_index = index;						
			$("#large_image_src").fadeIn(100);
			
			if (!$('#photo_gallery_overlay').is(":visible"))
				$('#photo_gallery_overlay').fadeIn(200);
			
		});
		
			
	}

	function setupFormSubmits()
	{
		$('.form_submit').click(function() {
			$('#'+$(this).attr('rel')).submit();
			return false;
		});
	}
	
	function setupInquireForms()
	{

	
		if ($('#inquire_form').length > 0)
		{
			$("#inquire_form").validationEngine()
			$("#inquire_form :text").addClass('validate[required] text-input');
		}
		
	}

	function startHomeGallery()
	{
		if ($('#gallery').length)
		{
			$('#main_photo').attr('src', $('.gallery_thumb:first').attr('alt'));
			$('.gallery_thumb:first').addClass('current_thumb hover_thumb');
			
			$('.gallery_thumb').hover(
				function() {
					$(this).addClass('hover_thumb');
				},
				function() {
					if (!$(this).hasClass('current_thumb'))
						$(this).removeClass('hover_thumb');
				}
			);
			
			$('.gallery_thumb').click(function()
				{
					$('#main_photo').attr('src', $(this).attr('alt'));
					$('.gallery_thumb').removeClass('hover_thumb');
					$('.gallery_thumb').removeClass('current_thumb');
					$(this).addClass('hover_thumb');
					$(this).addClass('current_thumb');
				});
			
			setInterval(nextSlide, 3000);
		}
	}
	
	function nextSlide()
	{
	  if ($('#gallery .current_thumb').index() == $('#gallery .gallery_thumb:last').index())
	  {
	      $('#gallery .current_thumb').removeClass('current_thumb').removeClass('hover_thumb');
	      $($('#gallery .gallery_thumb').get($('#gallery .gallery_thumb:first').index())).addClass('current_thumb').addClass('hover_thumb');
	      $('#main_photo').attr('src', $($('#gallery .gallery_thumb').get($('#gallery .gallery_thumb:first').index())).attr('alt'));
	  }
	  else
	  {
	      $('#gallery .current_thumb').removeClass('current_thumb').removeClass('hover_thumb').next().addClass('current_thumb').addClass('hover_thumb');
	      $('#main_photo').attr('src', $('#gallery .current_thumb').attr('alt'));
	  }
	}



