var images = [
	"alice_tully",
	"american_museum",
	"brooklyn_museum",
	"celeste",
	"central_park",
	"february",
	"laguardia",
	"lehman",
	"metro_museum",
	"miller",
	"moma",
	"paley",
	"prince_george",
	"schomburg",
	"st_george",
	"walter_reade",
	"whitman"
];

for(var i = 0; i < images.length; i++) {
	$("<img>").attr("src", "/images/map-bubble-image-" + images[i] + ".jpg");
}

function start(id) {
	bind_events();
	if(id) { click_sidebar(id); }
}

function bind_events() {
	var id;
	$events = $("#content .middle .map-nav .nav-row");
	$events.click(function() {
		id = $(this).attr("id").replace(/^e/, "");
		//close_gallery();	
		click_sidebar(id);
		vars.venue = $(this).text();
	});
}

function close_gallery() {
	if($("#dark-blind").length && $("#dark-blind").css("display") == "block") {
		$("#blind .galleryRow .galleryPic .active").removeClass("active").removeClass("no-border");
		$("#dark-blind").fadeOut(150, function() {
			$("#blind").css({ overflowY: "auto" });
		});
	}
}

function gallery(id) {
	close_gallery();
	
	// If the gallery isn't displayed, show it.
	if(!$("#blind").length) {
		$blind	= $("<div>").attr({ id: "blind" }).css({ display: "none" });
		$blind.append($("<div>").html("<a href=\"javascript:void(0);\" onclick=\"gallery()\">&laquo; Go Back</a><div class=\"loader\"><img src=\"/images/ajax-loader.gif\" /></div>").attr({ "class": "controls" }));
		$darkBlind	= $("<div>").attr({ id: "dark-blind" }).css({ display: "none" });
		$blind.append($darkBlind);
		$("#map").append($blind);
		$("#blind").fadeIn(150);
		loadGallery(id);
	} else {
		$("#blind").fadeOut(150, function() { $(this).remove(); });
	}
}

function loadGallery(id) {
	var galleries	= {
		"12" : "gallery-metro_museum.php",
		"13" : "gallery-brooklyn_museum.php",
		"15" : "gallery-american_museum.php",
		"16" : "gallery-moma.php"
	};
	
	$.ajax({
		url: "/include/" + galleries[id],
		type: "GET",
		success: function(response) {
			$("#blind .loader").fadeOut(100, function() {
				$(this).remove();
				$("#blind").append(response);
				bindGalleryEvents();
			});
		},
		error: function() {
			alert("There was an error.  Please try again.");
		}
	});
}

// Binds click events for gallery thumbnails
function bindGalleryEvents() {
	$("#blind .galleryRow .galleryPic").click(function(e) {
		$("#dark-blind").fadeIn();
		$img	= $(this).children("img");
		if(parseInt($img.height()) == 85) {
			$img.addClass("active").addClass("no-border");
			$("#blind").css({ overflowY: "hidden" });
		} else {
			$("#dark-blind").fadeOut();
			$(this).children("img").removeClass("active");
		}
	});
}

function forward(id) {
	window.open("./sendtofriend.php?id=" + id, "nyco_forward", "menubar=no, width=300, height=400, toolbar=no, resizable=no, scrollbars=yes");
}

function remind(id) {
	window.open("./reminder.php?id=" + id, "nyco_reminder", "menubar=no, width=400, height=200, toolbar=no, resizable=no, scrollbars=yes");
}

function validate() {
	$name	= $("#name");
	$email	= $("#email");
	$fname	= $("#friendname");
	$femail	= $("#friendemail");
	$msg	= $("#message");
	errors	= [];
	
	if(exists($name) && !$name.val().replace(/\s/g, "")) {
		errors.push("- Please enter your name.");
	}
	
	if(exists($email) && !$email.val().match(/^[\w-._+]+@([\w\-].?)+\.[a-z]{2,4}$/)) {
		errors.push("- Please enter a valid email.");
	}
	
	if(exists($fname) && !$fname.val().replace(/\s/g, "")) {
		errors.push("- Please enter your friend's name.");
	}
	
	if(exists($femail) && !$femail.val().replace(/\s/g, "")) {
		errors.push("- Your friend's email must be valid.");
	}
	
	if(exists($msg) && !$msg.val().replace(/\s/g, "")) {
		errors.push("- Please enter a message.");
	}
	
	if(exists(errors)) {
		msg	 = "Please fix the following errors:\n";
		msg	+= errors.join("\n");
		alert(msg);
		return false;
	}
	return true;
}

function exists(obj) {
	return obj.length;
}

function fixAddress() {
	$("#gmapDirLabelTo").html("Starting address (include street address, city, state/region)<br />");
}

$(document).ready(function() {
	vars = new function config() {
		this.current_id	= null;
		this.venue		= null;
	};
});