$(document).ready(function() {
	
	// --- Definitions ---
	var Properties = PHOTOZIG.Properties;
	
	var $navigator = "#content .main .navigator";
	var $thumbsCover = "#content .main .thumbs .cover";
	var $newCover = "#content .main .new .thumbs .cover.item";
	var $popularCover = "#content .main .popular .thumbs .cover.item";
	var $topCover = "#content .main .top .thumbs .cover.item";
	
	var newAlbums = [];
	var popularAlbums = [];
	var topAlbums = [];
	
	function handleAlbumsPanel(index) {
		$(this).mouseover(function() {
			
			// Get data of current pointed album and show info panel, when mouse is over
			var albums = [];
			if ($(this).parents(".navigator").hasClass("new")) albums = newAlbums;
			if ($(this).parents(".navigator").hasClass("popular")) albums = popularAlbums;
			if ($(this).parents(".navigator").hasClass("top")) albums = topAlbums;		
			
			if (albums.length > 0) {
				var title = albums[index].name;
				var desc = albums[index].desc;
				if (albums[index].city_name && albums[index].country_name) {
					var location = 
						albums[index].city_name + 
						(albums[index].city_province ? ", " + albums[index].city_province : "") + 
						" - " + albums[index].country_name;
				} else {
					var location = "";
				}
			
				$(this).addClass("current");
				$("#info .title").html((title ? title.pad(18) : "(No Title)"));				
				$("#info .location").html((location ? location.pad(18) : ""));				
				$("#info .description").html((desc ? desc.pad(36) : ""));			
				$("#info").show();
			}
		});

		$(this).mouseout(function() {
			$(this).removeClass("current");				
			$("#info").hide();
		});
		
		$(this).mousemove(function(event) {
			$("#info").
				css("top", event.pageY + 10).
				css("left", event.pageX + 10);
		});
	}
	
	
	
	// --- Initializations ---
	
	// Info Panel Appearence
	$("#info").css("opacity", 0.90);	
	
	// Getting JSONs
	$.getJSON("/proxy.php?url=" + escape(Properties.SOCIAL_ROOT + "/yurisnight/newAlbums.json?" + Math.random()), function(json) {
		newAlbums = json;
	});
	
	$.getJSON("/proxy.php?url=" + escape(Properties.SOCIAL_ROOT + "/yurisnight/popularAlbums.json?" + Math.random()), function(json) {
		popularAlbums = json;
	});
	
	$.getJSON("/proxy.php?url=" + escape(Properties.SOCIAL_ROOT + "/yurisnight/topAlbums.json?" + Math.random()), function(json) {
		topAlbums = json;
	});	
	
	
	
	// --- Listeners ---
	$($navigator + " .previous").click(function() {
		var masked = $(".masked", this.parentNode);
		var thumbs = $(".cover .wrapper", this.parentNode);
		
		if (thumbs.length > 3 && masked.css("left") !== "0px") {
			masked.animate({"left": "+=332px"}, "medium");
		}
	});

	$($navigator + " .next").click(function() {
		var masked = $(".masked", this.parentNode);
		var thumbs = $(".cover .wrapper", this.parentNode);
		
		if (thumbs.length > 3 && masked.css("left") !== "-332px") {
			masked.animate({"left": "-=332px"}, "medium");
		}
	});	
	
	$($newCover).each(handleAlbumsPanel);
	$($popularCover).each(handleAlbumsPanel);
	$($topCover).each(handleAlbumsPanel);
});