/**
 * Globals/constants
 */
	var map;
	var pois = [];
	var currentMarkerIndex = -1;
	var isSliding = false;

/**
 * Initial setup / event bindings
 */
	$(document).ready(function()
	{
	  $('#alert').css({left: ($(window).width() - 340) / 2});
	  $('#close_overlay').click(closeOverlay);
		$('#rooms_overview .rooms a').click(gotoRoom).hover(highlightRoom, unhighlightRoom);
		$('.previous').click(previousRoom);
		$('.next').click(nextRoom);
		$('.room_nav').css({opacity: 0}).show();
		$('.room_nav li a').click(clickRoomBlock).hover(showTooltip, hideTooltip);
		$('.scrollbar').scrollbar({buttonWidth: 0, buttonHeight: 0});
		$('.slideshow .controls a').click(switchImage).hover(showThumb, hideThumb);
		$('.slideshow .more').click(toggleImageDetails);
		
		$('.panel').click(panelClick);
		
		// Pan pacific park		
		$('.park_dropdown').dropdown({css: true, change: filterParkPOIs});
		$('#markers .marker').click(parkMarkerClick).hover(highlightParkPOI, unhighlightParkPOI);
		$('.park_pois a').click(parkPoiClick).hover(showParkMarkerHighlight, hideParkMarkerHighlight);
		$('#park_info_window .close').click(closeParkInfoWindow);

		// Surrounding areas
		$('.dropdown').dropdown({css: true, change: filterMapPOIs});
		$('.map_pois a').click(poiClick).hover(showMarkerHighlight, hideMarkerHighlight);
		$('#info_window .close').click(closeInfoWindow);
		
		// Init google maps
		if ($('#map').length > 0)
		{
			initMap();
		}
	});


/**
 * Event handlers
 */
  function closeOverlay()
  {
    createCookie('hide_virtualtour_alert', '1', 0);
    
    $('#overlay, #alert').fadeOut(200, function()
    {
      $(this).remove();
    });
    
		enableFlashMap();
    
    return false;
  }

	function enableFlashMap()
	{
		if ($('#interior_map').length > 0)
		{
			var flash = getFlashMovie('interior_map_flash');

	    if (flash)
	    {
	      flash.enableMap();
	    }
		}
	}
	
	function mapReady()
	{
		if ($('#overlay').length == 0)
		{
			enableFlashMap();
		}
	}
  
	function showTooltip()
	{
		if (!$(this).parent().hasClass('selected'))
		{			
			var tooltip = $(this).parents('.room_nav').find('.tooltip');
			var leftOffset = $(this).parent().position().left + ($(this).width() / 2) - 9;
			tooltip.find('.point').css('backgroundPosition', leftOffset + 'px 35px');
		
			tooltip.find('p').html($(this).parent().find('.title').html());
			tooltip.css({opacity: 0}).show().stop().animate({opacity: 1}, {duration: 500});
		}
	}
	
	function hideTooltip()
	{
		if (!$(this).hasClass('selected'))
		{
			var room_nav = $(this).parents('.room_nav');
			room_nav.find('.tooltip').stop().animate({opacity: 0}, 250, function() { $(this).hide(); });
		}
	}
	
	function clickRoomBlock()
	{
		if (!$(this).parent().hasClass('selected'))
		{
			slideToRoom($('.panel').not('.main').eq($(this).parent().index()));
		}
		
		return false;
	}
	
	function panelClick()
	{
		if (!$(this).hasClass('selected') && !isSliding)
		{
			slideToRoom($(this));
		}
	}


	function gotoRoomAtIndex(index)
	{
		var panel = $('.panel').eq(index + 1);

		if (panel.length > 0)
		{
			$('#vt_nav li.selected').find('a').addClass('selected').end().removeClass('selected');
			slideToRoom(panel);
		}	
	}
	
	function gotoRoom()
	{
		var panel = $($(this).attr('href'));
		
		if (panel.length > 0)
		{
			$('#vt_nav li.selected').find('a').addClass('selected').end().removeClass('selected');

			slideToRoom(panel);
		}
		
		return false;
	}
	
	function highlightRoom()
	{
		var flash = getFlashMovie('interior_map_flash');
		var index = $(this).parent().index();
		flash.showRoom(index);
	}
	
	function unhighlightRoom()
	{
		var flash = getFlashMovie('interior_map_flash');
		var index = $(this).parent().index();
		flash.hideRoom(index);
	}
	
	function previousRoom()
	{
		var panels = $('.panel');
		var currentIndex = $(this).parents('.panel').index();
		var previousIndex = (currentIndex == 0) ? panels.length : currentIndex - 1;
		
		slideToRoom(panels.eq(previousIndex));
		
		return false;
	}
	
	function nextRoom()
	{
		var panels = $('.panel');
		var currentIndex = $(this).parents('.panel').index();
		var nextIndex = (currentIndex == panels.length) ? 0 : currentIndex + 1;
		
		slideToRoom(panels.eq(nextIndex));
		
		return false;
	}
	
	function slideToRoom(panel)
	{
		if (panel.length > 0)
		{
			isSliding = true;
			var position = panel.position().left * -1;
			
			$('#panels .panel.selected').removeClass('selected').find('.room_nav').animate({opacity: 0}, 100);
			
			$('#panels').animate({left: position}, {duration: 350, easing: 'easeInOutQuint', complete: function()
			{
				$(panel).addClass('selected').find('.room_nav').animate({opacity: 1});
				isSliding = false;
			}});
		}
	}
	
	/**
	 * Slideshows
	 */
	function toggleImageDetails()
	{
		var slideshow = $(this).parents('.slideshow');
		var bottom = slideshow.find('.bottom');
		
		if (bottom.hasClass('expanded'))
		{
			collapseImageDetails(bottom)
		}
		else
		{
			bottom.find('.more').html('CLOSE');
			bottom.find('.description').fadeIn();
			bottom.animate({height: 119}, 150, function()
			{
				$(this).addClass('expanded');
			});
		}

		return false;
	}
	
	function collapseImageDetails(bottom)
	{
		bottom.find('.more').html('MORE');
		bottom.find('.description').fadeOut(100);
		bottom.animate({height: 38}, 150, function()
		{
			$(this).removeClass('expanded');
		});
	}
	
	function switchImage()
	{
		if (!$(this).hasClass('selected'))
		{
			var slideshow = $(this).parents('.panel').find('.slideshow');
			var index = $(this).parent().index();
			slideshow.find('.controls li').eq(index).find('.thumb').css({opacity: 0}).hide();
			
			var bottom = slideshow.find('.bottom');
			
			if (bottom.hasClass('expanded'))
			{
				bottom.find('.more').html('MORE');
				bottom.find('.description').hide();
				bottom.animate({height: 38}, 100, function()
				{
					$(this).removeClass('expanded');
					setTimeout(function()
					{
						loadImage(slideshow, index);
					}, 120);
					
				});
			}
			else
			{
				loadImage(slideshow, index);
			}
		}
		
		return false;
	}
	
	function loadImage(slideshow, index)
	{
		slideshow.find('.controls a.selected').removeClass('selected');
		slideshow.find('.controls a').eq(index).addClass('selected');
		
		// Crossfade images
		var image = slideshow.find('img').eq(index);
		slideshow.find('img.selected').removeClass('selected').fadeOut(500);
		image.addClass('selected').fadeIn(500);	

		// Update image details
		var imageInfo = slideshow.find('.controls li').eq(index);
		var details = slideshow.find('.details');
		details.find('.caption span').html(imageInfo.find('.caption').html());
		
		if (imageInfo.find('.description').html() !== '')
		{
			details.find('.more').fadeIn();
			details.find('.description').html(imageInfo.find('.description').html());
		}
		else
		{
			details.find('.more').fadeOut();
			details.find('.description').html('');
		}
	}
	
	function showThumb()
	{
		if (!$(this).hasClass('selected'))
		{
			$(this).parent().find('.thumb').css({opacity: 0}).show().stop().animate({opacity: 1});
		}
	}
	
	function hideThumb()
	{
		$(this).parent().find('.thumb').stop().animate({opacity: 0}, function()
			{
				$(this).hide();
			}
		);
	}
	
	/**
	 * Interior map
	 */
	function showNavigationHover(index)
	{
		$('#rooms_overview .rooms li a').eq(index).addClass('hover');
	}
	
	function hideNavigationHover(index)
	{
		$('#rooms_overview .rooms li a').eq(index).removeClass('hover');
	}
	
	/**
	 * Pan Pacific Park map
	 */
	function parkPoiClick()
	{
		if (!$(this).hasClass('selected'))
		{
			$('.park_pois a.selected').removeClass('selected');
			var marker = $($(this).attr('href'));
			$(this).addClass('selected');
			
			$('#markers .marker.selected').removeClass('selected');
			marker.addClass('selected');
			
			showParkInfoWindow($(this).parent().index());
		}
		
		return false;
	}
	
	function showParkMarkerHighlight()
	{
		var marker = $($(this).attr('href'));
		marker.addClass('hover');
	}
	
	function hideParkMarkerHighlight()
	{
		var marker = $($(this).attr('href'));
		marker.removeClass('hover');
	}
	
	function highlightParkPOI()
	{
		$('.park_pois a[href=#' + $(this).attr('id') + ']').addClass('hover');
	}
	
	function unhighlightParkPOI()
	{
		$('.park_pois a[href=#' + $(this).attr('id') + ']').removeClass('hover');
	}
	
	function filterParkPOIs(event, ui)
	{
		var category = ui.value;
		
		if (category == 'all')
		{
			$('.park_pois li').show();
			$('#markers .marker').show();
		}
		else
		{
			$('.park_pois li').not('.' + category).hide();
			$('#markers .marker').not('.' + category).hide();
			
			$('.park_pois li.' + category).show();
			$('#markers .' + category).show();
		}
		
		$('.park_pois a.selected').removeClass('selected');
		$('#markers .selected').removeClass('selected');
		$('#park_info_window').fadeOut();
		
		$('.scrollbar').scrollbar('scrollToBeginning');
		$('.scrollbar').scrollbar('refresh');
	}

	function parkMarkerClick()
	{
		if (!$(this).hasClass('selected'))
		{
			$('.park_pois a.selected').removeClass('selected');
			$('.park_pois a[href=#' + $(this).attr('id') + ']').addClass('selected');
			
			$('#markers .marker.selected').removeClass('selected');
			$(this).addClass('selected');
			
			showParkInfoWindow($(this).index());
		}
		
		return false;
	}
	
	function showParkInfoWindow(index)
	{
		var poi = pois[index];
			
		if (poi)
		{
			var info_window = $('#park_info_window');
			
			info_window.find('h3').html(poi.title);
			info_window.find('p').html(poi.description);

			if (poi.image)
			{
				info_window.find('img').attr('src', poi.image).show();
			}
			else
			{
				info_window.find('img').hide();
			}

			if (!info_window.is(':visible'))
			{
				info_window.fadeIn();
			}
		}
	}
	
	function closeParkInfoWindow()
	{
		$('#park_info_window').fadeOut();
		$('.park_pois li a.selected').removeClass('selected');
		$('#markers .selected').removeClass('selected');
		
		return false;
	}

	/**
	 * Surrounding Areas map
	 */
	function initMap()
	{
		var latlng = new google.maps.LatLng(34.074595, -118.356067);
    var myOptions = {
      zoom: 15,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map($('#map_canvas')[0], myOptions);
		
		// Add LAMH marker
	  var main = new google.maps.Marker({position: new google.maps.LatLng(34.074595, -118.356067), map: map, icon: markerImageForCategory('main')});
		
		// Add markers for other points of interests
		addMarkers();
	}
	
	// Loop through all the POIs and add markers
	function addMarkers()
	{	
		for (var i = 0; i < pois.length; i++)
		{
			var latlon = new google.maps.LatLng(pois[i].lat, pois[i].lon);
			var marker = new google.maps.Marker({position: latlon, map: map, icon: markerImageForCategory(pois[i].category)});
			pois[i].marker = marker;
			bindClickEvent(marker, i);
		}
	}
	
	// Closure to allow click event to carry though index of poi
	function bindClickEvent(marker, index)
	{
		google.maps.event.addListener(marker, 'click', function()
		{
		  $('#map_poi .map_pois a.selected').removeClass('selected');
			$('#map_poi .map_pois li').eq(index).find('a').addClass('selected');
			showInfoWindow(index);
		});
	}
	
	function poiClick()
	{
		$('#map_poi .map_pois a.selected').removeClass('selected');
		var index = $(this).addClass('selected').parent().index();
		showInfoWindow(index);
		
		return false;
	}
	
	function showInfoWindow(index)
	{
		var poi = pois[index];
			
		if (poi)
		{
			removeHighlightFromMarker(currentMarkerIndex);
			currentMarkerIndex = index;

			poi.marker.setIcon(markerImageForCategory('highlight'));

			var info_window = $('#info_window');
			
			info_window.find('h3').html(poi.title);
			info_window.find('p').html(poi.address);
      info_window.find('a').attr('href', poi.directions_url);
      
			if (poi.image)
			{
				info_window.find('img').attr('src', poi.image).show();
				info_window.removeClass('no_image');
			}
			else
			{
				info_window.find('img').hide();
				info_window.addClass('no_image');
			}

			if (!info_window.is(':visible'))
			{
				info_window.fadeIn();
			}
		}
		
		return false;
	}
	
	function closeInfoWindow()
	{
		$('#info_window').fadeOut();
		$('#map_poi .map_pois li a.selected').removeClass('selected');
		removeHighlightFromMarker(currentMarkerIndex);
		
		return false;
	}
	
	function showMarkerHighlight()
	{
		var poi = pois[$(this).parent().index()];
		
		if (poi)
		{
			poi.marker.setIcon(markerImageForCategory('highlight'));
		}
	}
	
	function hideMarkerHighlight()
	{
		var poi = pois[$(this).parent().index()];
		
		if (poi && !$(this).hasClass('selected'))
		{
			poi.marker.setIcon(markerImageForCategory(poi.category));
		}
	}
	
	function removeHighlightFromMarker(index)
	{
		// Change icon back of previously highlighted marker
		if (index >= 0)
		{
			var poi = pois[index];
			poi.marker.setIcon(markerImageForCategory(poi.category));
		}
	}
	
	function markerImageForCategory(category)
	{
		return WWW_ROOT + 'images/vt/marker_' + category + '.png';
	}
	
	function filterMapPOIs(event, ui)
	{
		var category = ui.value;
		
		if (category == 'all')
		{
			$('.map_pois li').show();
		}
		else
		{
			$('.map_pois li').not('.' + category).hide();
			$('.map_pois li.' + category).show();
		}
		
		removeHighlightFromMarker(currentMarkerIndex);
		closeInfoWindow();
		
		displayMarkers(category);
		$('.scrollbar').scrollbar('scrollToBeginning');
		$('.scrollbar').scrollbar('refresh');
	}
	
	function displayMarkers(category)
	{	
		for (var i = 0; i < pois.length; i++)
		{
			pois[i].marker.setVisible((pois[i].category == category) || category == 'all');
		}
	}
	
	function getFlashMovie(movieName) 
	{
		if (window.document[movieName])
		{
			return window.document[movieName];
		}
	
		if (navigator.appName.indexOf("Microsoft Internet") == -1)
		{
			if (document.embeds && document.embeds[movieName])
			{
				return document.embeds[movieName];
			}
		}
		else
		{
			return document.getElementById(movieName);
		}
	}
	
	jQuery.extend( jQuery.easing,
	{
		def: 'easeOutQuad',
		swing: function (x, t, b, c, d) {
			//alert(jQuery.easing.default);
			return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
		},
		easeInQuad: function (x, t, b, c, d) {
			return c*(t/=d)*t + b;
		},
		easeOutQuad: function (x, t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		},
		easeInOutQuad: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t + b;
			return -c/2 * ((--t)*(t-2) - 1) + b;
		},
		easeInQuart: function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t + b;
		},
		easeOutQuart: function (x, t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		},
		easeInOutQuart: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
			return -c/2 * ((t-=2)*t*t*t - 2) + b;
		},
		easeInQuint: function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t*t + b;
		},
		easeOutQuint: function (x, t, b, c, d) {
			return c*((t=t/d-1)*t*t*t*t + 1) + b;
		},
		easeInOutQuint: function (x, t, b, c, d) {
			if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
			return c/2*((t-=2)*t*t*t*t + 2) + b;
		}
	});
	
	
  /**
   * Cookie handling functions - via http://www.quirksmode.org/js/cookies.html
   */
   function createCookie(name,value,days) {
   	if (days) {
   		var date = new Date();
   		date.setTime(date.getTime()+(days*24*60*60*1000));
   		var expires = "; expires="+date.toGMTString();
   	}
   	else var expires = "";
   	document.cookie = name+"="+value+expires+"; path=/";
   }

   function readCookie(name) {
   	var nameEQ = name + "=";
   	var ca = document.cookie.split(';');
   	for(var i=0;i < ca.length;i++) {
   		var c = ca[i];
   		while (c.charAt(0)==' ') c = c.substring(1,c.length);
   		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   	}
   	return null;
   }

   function eraseCookie(name) {
   	createCookie(name,"",-1);
   }
   