// JavaScript Document

var fade_duration = 1;
var photo_duration = 5000;
var homelink;
var swap_int;
var photo_num = 0;
var photos = [];
var z_photo = 500;

var map;
var gdir;
var geocoder = null;
var addressMarker;

function init()
{
	$('a.roomrates').bind('click', showRates);
	$('a.lightbox').lightBox();
	
	if($('#homephotos').length)
	{
		photos = $('#homephotos').val().split('|');
		if($('#homepage').length)
		{
			homelink = $('#homelink');
			swap_int = setInterval(swapPhoto,photo_duration);
			
			for(i=0;i<photos.length;i++)
			{
				//alert(photos[i]);
				var img = $$('img','',{ id:'img_'+photos[i].split('.')[0], src:'/images/billboards/home/'+photos[i] });
				homelink.append( img );
				img.hide();
			}
		}
	}
	
	$('a.tab').bind('click', toggle_tab);
}

function toggle_tab ()
{
	var id = $(this).attr('rel');
	
	$('a.tab').addClass('inactive');
	$('div.album').hide();
	
	$(this).removeClass('inactive');
	$('#'+id).fadeIn(200);
	
	return false;
}

function createMap(id,lat,lon)
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById(id));
		//map.addControl(new GSmallMapControl());
		gdir = new GDirections(map, document.getElementById("route"));
		var point = new GLatLng(lat, lon);
		map.setCenter(point, 15);
		map.addOverlay(new GMarker(point));
	}
}

function setDirections()
{
	var fromAddress 		= document.getElementById('fromAddress').value;
	var locale					= 'en';
  	gdir.load("from: " + fromAddress + " to: 9844 Water Street 54211",
            { "locale": locale });
}

function handleErrors(){
   if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
     alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
   else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
     alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
   
   else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
     alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);

//   else if (gdir.getStatus().code == G_UNAVAILABLE_ADDRESS)  <--- Doc bug... this is either not defined, or Doc is wrong
//     alert("The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.\n Error code: " + gdir.getStatus().code);
     
   else if (gdir.getStatus().code == G_GEO_BAD_KEY)
     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);

   else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    
   else alert("An unknown error occurred.");
   
}

function onGDirectionsLoad(){ 
  // Use this function to access information about the latest load()
  // results.

  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

function showRates()
{
	var atag = $(this);
	var id = atag.attr('rel');
	
	var rates = $('#roomrates_'+id);
	if( rates.is(':hidden') )
	{
		rates.show();
		// atag.html('<img src="/images/buttons/hide_rates.jpg" alt="Click to hide rates" />');
	}
	else
	{
		rates.hide();
		// atag.html('<img src="/images/buttons/show_rates.jpg" alt="Click here for rates" />');
	}
	return false;
}

function swapPhoto()
{
	var num = ++photo_num%photos.length;
	var id = 'img_'+photos[num].split('.')[0];
	if(window.console) window.console.log(id);
	
	$('#'+id).css({ 'z-index': z_photo++ }).fadeIn();
	var nextnum = (num+1)%photos.length;
	var nextid = 'img_'+photos[nextnum].split('.')[0];
	$('#'+nextid).hide();
}

/* form validation */

function css_validation(form)
{
	if(!form) form = document.forms[0];
	var submit_it = true;
	var email_pattern = new RegExp(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i);
	
	$(form).find(".REQUIRED").each(function(req)
	{
		if(($(this).attr('type')!='checkbox' && this.value == '') || 
			($(this).attr('type')=='checkbox' && !this.checked) || 
			($(this).hasClass('email') && !email_pattern.test($(this).val()))) 
		{
			$(this).parents("div.formfield").addClass('error');
			submit_it = false;
		}
		else
		{
			$(this).parents("div.formfield").removeClass('error');
		}			
	});
	if( !submit_it ) $('#error_message').fadeIn(250);
	return submit_it;
}

$(document).ready(function() {
	if($('#map.googlemap').length) createMap('map',45.148686, -87.17845);
	init();
});
