$(document).ready(function() {
	var $mainPhoto = $("#main-photo");
	var $thumbPhoto = $(".thumb-photo"); 
    var map = {};
	var marker = {};
	var myOptions = {
    	mapTypeId: google.maps.MapTypeId.ROADMAP,
		center: latLng,
		zoom: 13
	 };
	if(showvideo)
	{
		$("#main-photo").fadeOut(function() {
			$("#video-toggle").hide();
			$("#video-image-toggle").show();
			$("#video-viewer").fadeIn();
		});
	}
	$("#map-toggle").click(function(e) {
		e.preventDefault();
		$("#photo-viewer").fadeOut(function() {
			$("#map-toggle").hide();
			$("#image-toggle").show();
			$("#map-viewer").fadeIn(function() {
				map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
				marker = new google.maps.Marker({
					position: latLng,
					map: map
				});
			});
		});	
	});
	
	$("#video-image-toggle").click(function(e){
		e.preventDefault();
		$("#video-viewer").fadeOut(function() {
			$("#video-toggle").show();
			$("#video-image-toggle").hide();
			$("#main-photo").fadeIn();
			$("#thumbnails-area").show();
		});
	});
	$("#video-toggle").click(function(e) {
		e.preventDefault();
		$("#main-photo").fadeOut(function() {
			$("#video-toggle").hide();
			$("#thumbnails-area").hide();
			$("#video-image-toggle").show();			
			$("#video-viewer").fadeIn();
			
		});
	});
	
	
	
	$("#image-toggle").click(function(e) {
		e.preventDefault();
		$("#map-viewer").fadeOut(function() {
			$("#image-toggle").hide();
			$("#map-toggle").show();
			$("#photo-viewer").fadeIn();
		});
	});
	$("#thumb-toggle").toggle(function() {
		$("#thumbnails").fadeOut();
		$(this).html("Show Thumbnails");
	}, function() {
		$("#thumbnails").fadeIn();
		$(this).html("Hide Thumbnails");
	});
	
	var totalPhotoCount = $(".thumb-photo").length;
	var currentPhoto = totalPhotoCount;
	
	var adjustPhoto = function($currentThumb) {
		if ($currentThumb.attr("height") > $currentThumb.attr("width")) {
			$mainPhoto.attr("width", portPhotoWidth);
			$mainPhoto.attr("height", mainPhotoHeight);
		}
		else {
			$mainPhoto.attr("width", "440");
			$mainPhoto.attr("height", mainPhotoHeight);
		}
	};
	var postPhotoLoad = function() {
		mainPhotoHeight = $mainPhoto.attr("height");
		portPhotoWidth =  (mainPhotoHeight * mainPhotoHeight) / 440;
		$thumbPhoto.each(function(i) {
			/*if (i == 0) {
				if ($(this).attr("height") == 0) {
					var $that = $(this);
					$(this).one("load", function(){
						adjustPhoto($that);
					});	
				}
				else {
					adjustPhoto($(this));
				}
			}*/
			$(this).click(function(e) {
				currentPhoto = i;
				changePhoto($(this));
			});				
		});
		
	};
	var mainPhotoHeight = "";
	var portPhotoWidth = "";
	if ($mainPhoto.attr("height") == 0) {
		$mainPhoto.one("load", postPhotoLoad);
	}
	else {
		postPhotoLoad();
	}
	
	var changePhoto = function($currentThumb) {
		adjustPhoto($currentThumb);
		$mainPhoto.attr("src", $currentThumb.attr("src"));
	};
	
	var movePhoto = function(dir) {
		currentPhoto = (((currentPhoto + totalPhotoCount) + dir) % totalPhotoCount);
		changePhoto($thumbPhoto.eq(currentPhoto));
	};				
	
	$("#prev-photo").click(function(e) {
		e.preventDefault();
		movePhoto(-1);
		
	});
	$("#next-photo").click(function(e) {
		e.preventDefault();
		movePhoto(1);
	});
});
