navImages = {
	on : {
		home : 'index_on_04.jpg',
		about : 'index_on_05.jpg',
		leadership : 'index_on_06.jpg',
		press : 'index_on_07.jpg',
		contact : 'index_on_08.jpg'
	},	
	off : {
		home : 'index_04.jpg',
		about : 'index_05.jpg',
		leadership : 'index_06.jpg',
		press : 'index_07.jpg',
		contact : 'index_08.jpg'
	}
};

navPages = 5;
showMoreInfo = null;
_pageNum = null;
	
locations = {
	"allure" : {
		name : 'Allure Las Vegas',
		lat : 36.144286,
		lng : -115.158842
	},
	"turnberryPlace" : {
		name : 'Turnberry Place',
		lat : 36.136768,
		lng : -115.156798
	},
	"turnberryTowers" : {
		name : 'Turnberry Towers',
		lat : 36.141029,
		lng : -115.152533
	},
	"parkTowers" : {
		name : 'Park Towers',
		lat : 36.136748,
		lng : -115.155537
	}
};

	
$(document).ready(function() {  		
	
	$("img.off").hover(  
		function() { $(this).animate({"opacity": "0"}, "slow"); },  
		function() { $(this).animate({"opacity": "1"}, "slow"); }
	);  
	
	$('#floorplanPane').jScrollPane({showArrows:true});
	$('#aboutPane').jScrollPane({showArrows:true});
	$('#pressPane').jScrollPane({showArrows:true});
	
	var $leaderPane = $('#leaderPane');
	$leaderPane.jScrollPane({showArrows:true,animateTo:true});
	$('a.scroll-to-element').click(function() {
		var targetElementSelectorString = $(this).attr('rel');
		$leaderPane[0].scrollTo(targetElementSelectorString);
		return false;
	});
	
	watchVideo = function() { 									
		$('.swfintro').hide();
		$('.flowplayer').removeClass('flowplayerHidden');	
		$('.flowplayer').addClass('flowplayerVisible');
		$('.flowplayer').show();
		$('.flowscreen').show();		
		$('.sidemenu').show();
		flowplayer("player", "swf/flowplayer-3.1.5.swf");
	};
	
	$('.home').click(function() { slideTo(0); });	
	$('.about').click(function() { slideTo(1); });
	$('.leadership').click(function() { slideTo(2, 'leadership'); $('#leaderPane')[0].scrollTo('#p1'); });
	$('.press').click(function() { slideTo(3, 'press'); });
	$('.contact').click(function() { slideTo(4, 'contact'); });	
	
	function slideTo(pageNum) {		
		$('#content').show();
		$('#propview').hide();
		if (pageNum == navPages) 
			pageNum = 0;
		else if (pageNum == -1) 
			pageNum = navPages - 1;
		var pageName = null;
		var pageCount = 0;
		for (var p in navImages.off) {
			if (pageNum == pageCount) pageName = p;
			$('.' + p + ' .off').attr('src', 'images/' + navImages.off[p]); 
			pageCount++;
		}
		if (pageName) {
 			$('.' + pageName + ' .off').attr('src', 'images/' + eval('navImages.on.' + pageName)); 
			if (pageNum != 0) {		
	       		closeVideo();
			}
			
			animate(pageNum);	
			
			if (pageNum == 0) {
				setTimeout(function() {
			 	$('.swfintro').show();
		   		$('.sidemenu').hide(); }, 500);
			}			
			
			_pageNum = pageNum;
		} else {
			alert(pageNum);	
		}
		
	};
	
	closeVideo = function() {
		$('.swfintro').hide();
		$('.flowplayer').hide();
		$('.sidemenu').hide();
		$('.flowscreen').hide();
		$('#player').html();				
		$('.flowplayer').removeClass('flowplayerVisible');	
		$('.flowplayer').addClass('flowplayerHidden');
	}
	
	goHome = function() {
		closeVideo();
		slideTo(0);
	};
	
	showMoreInfo = function(p) {
		$('#content').hide();			
		$('#propview').fadeIn(800);
	};
	
	showListings = function() {		
		window.location.hash = 'showListingsAnchor';
	};
	
	$('.logoLink').click(function() {
		slideTo(0);								  
	});
	
	// initialize scrollable  
    $("div.scrollable").scrollable({ 
        vertical:true,  
        size: 2 
         
    // use mousewheel plugin 
    }).mousewheel();  
	
	$("#slider").easySlider({
      auto: false, 
      continuous: true,
	  controlsShow: true,
	  numeric: false,
	  callback: slideTo
    });
	
	slideTo(0, 'home');
	
	// select the thumbnails and make them trigger our overlay
	$(".triggers a").overlay({

	// each trigger uses the same overlay with the id "gallery"
	target: '#gallery',

	// optional exposing effect
	expose: '#f1f1f1'
	
	// let the gallery plugin do its magic!
	}).gallery({	
		// the plugin accepts its own set of configuration options
		speed: 600
	});
	
	$("a[rel]").overlay({ 	 
		expose: 'black', 
		effect: 'apple', 
 
		onBeforeLoad: function() { 
 
			// grab wrapper element inside content 
			var wrap = this.getContent().find(".contentWrap"); 
 
			// load the page specified in the trigger 
			wrap.load(this.getTrigger().attr("href")); 
			
			if (_pageNum == 0) 
				$('.swfintro').hide();
		} ,
		
		onBeforeClose : function() { 
 			if (_pageNum == 0)
				goHome();
		} 
 
	}); 
	
	var map = new GMap2(document.getElementById('map'));
	var burnsvilleMN = new GLatLng(36.062109,-115.187058);
	map.setCenter(burnsvilleMN, 12);
	
	
	var bounds = new GLatLngBounds();
	var geo = new GClientGeocoder(); 
	
	var reasons=[];
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address";
	reasons[G_GEO_BAD_KEY]            = "Bad API Key";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries";
	reasons[G_GEO_SERVER_ERROR]       = "Server error";
	
	// initial load points
	/*
	$.getJSON("php/map-service.php?action=listpoints", function(json) {
		if (json.Locations.length > 0) {
			for (i=0; i<json.Locations.length; i++) {
				var location = json.Locations[i];
				addLocation(location);
			}
			zoomToBounds();
		}
	});
	*/
	$("#add-point").submit(function(){
		geoEncode();
		return false;
	});
	
	function savePoint(geocode) {
		var data = $("#add-point :input").serializeArray();
		data[data.length] = { name: "lng", value: geocode[0] };
		data[data.length] = { name: "lat", value: geocode[1] };
		$.post($("#add-point").attr('action'), data, function(json){
			$("#add-point .error").fadeOut();
			if (json.status == "fail") {
				$("#add-point .error").html(json.message).fadeIn();
			}
			if (json.status == "success") {
				$("#add-point :input[name!=action]").val("");
				var location = json.data;
				addLocation(location);
				zoomToBounds();
			}
		}, "json");
	}
	
	function geoEncode() {
		var address = $("#add-point input[name=address]").val();
		geo.getLocations(address, function (result){
			if (result.Status.code == G_GEO_SUCCESS) {
				geocode = result.Placemark[0].Point.coordinates;
				savePoint(geocode);
			} else {
				var reason="Code "+result.Status.code;
				if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				} 
				$("#add-point .error").html(reason).fadeIn();
				geocode = false;
			}
		});
	}
	
	function addLocation(location) {
		var point = new GLatLng(location.lat, location.lng);		
		var marker = new GMarker(point);
		map.addOverlay(marker);
		bounds.extend(marker.getPoint());
		/*
		$("<li />")
			.html(location.name)
			.click(function(){
				showMessage(marker, location.name);
				map.panTo(marker.getLatLng());
			})
			.appendTo("#list");
		*/
		GEvent.addListener(marker, "click", function(){
			showMessage(this, location.name);
		});
	}
	
	function zoomToBounds() {
		map.setCenter(bounds.getCenter());
		map.setZoom(map.getBoundsZoomLevel(bounds)-1);
	}
	
	$("#message").appendTo( map.getPane(G_MAP_FLOAT_SHADOW_PANE) );
	
	function showMessage(marker, text){
		var markerOffset = map.fromLatLngToDivPixel(marker.getPoint());
		$("#message").hide().fadeIn()
			.css({ top:markerOffset.y, left:markerOffset.x + 5 })
			.html(text);
	}
	
	$('.locationSelector').click(function() {
		var id = $(this).attr('id');
		var point = new GLatLng(locations[id].lat, locations[id].lng);		
		var marker = new GMarker(point);
		showMessage(marker, locations[id].name);
		map.panTo(marker.getLatLng());
		return false;
	});
	
	for (var key in locations) {
		addLocation(locations[key]);
	}
	zoomToBounds();
});

