var tim;
var jump;
$(function() {
	setInterval('pollicino()', 400);
	storia();
});

function storia() {
	if(location.hash != "") {
		id = location.hash.replace("#", "");
		
		$.post("/ajax/new/info:id=" + id, function(a) {
			categoria = a.informazioni.catUtile[0];
			$('li[cId="'+categoria+'"]').trigger("click");
			$("body").append('<img id="a" style="display: none" opera="'+id+'" />');
			$("#a").trigger("click");
		}, "json");
		
		$("#ajx").hide();
	}
}

$("ul#menu li").live("click", function() {

	$(".selected").removeClass("selected");
	$(this).addClass("selected");
	$("#opera").fadeOut();
	if(!$(this).hasClass("text") && !$(this).hasClass("no")) {
	
		if($("#content").css("display") == "none")
			$("#content").fadeIn();
	
		$.get("/ajax/categoria:id=" + $(this).attr("cid"), function(a) {
			
			if($("#underline").height() > 0) {
				
				$("#underline img").fadeOut(function() {
					$("#underline div").html('');
				});
				
				$("#underline").animate({
					height: 0
				}, function() {
					$("#underline").animate({
						height: 155
					}, function() {
//						portfolio(a);
						creaPortfolio(a);
					});
				});
				
			} else {
				
				$("#underline").animate({
					height: 155
				}, function() {
//					portfolio(a);
					creaPortfolio(a);
				});
				
			}
		
		}, "json");
		
	} else if(!$(this).hasClass("no")) {
		
		$("#opera").fadeOut(function() {
			
			$("#underline").append('<img src="" style="display: none" />');
			
			$("#left, #right").hide();
			
			if($("#underline").height() > 0) {
				$("#underline img").fadeOut();
				
				$("#underline").animate({
					height: 0
				}, function() {
					$("#underline div").html('<div style="width: 901px; padding-left: 20px">' + $("#about").html() + '</div>');
					$("#underline").animate({
						height: $(window).height() - 111
					});
				});
				
			} else {
				$("#underline div").html('<div style="width: 901px; padding-left: 20px">' + $("#about").html() + '</div>');
				$("#underline").animate({
					height: $(window).height() - 111
				});
				
			}
		});
		
	}
});

function creaPortfolio(a) {
	$(a).each(function(id) {
		$("#underline div").append('<img height="146" opera="'+a[id].id+'" src="http://www.sugarkane.it/'+a[id].thumb+'" style="display: none; margin: 5px; margin-right: 0px" />');	
	});
	
	var elementi = $("#underline img").size();
	var contatore = 0;
	
	$("#underline img").bind("load", function(a){
		contatore++
		$(this).fadeIn();
	});
	
	if(elementi > 5) {
		var xy = $("#underline").position();
		
		if($("#left").html() == null)
			$("body").append('<div id="left"><img src="/next.png" /></div><div id="right"><img src="/prev.png" /></div>');
		
		$("#left").css({
			left: ($(document).width()/2) + 455,
			top: 111,
			position: 'absolute',
			width: 20,
			height: 75
		}).fadeIn();
		
		$("#right").css({
			left: ($(document).width()/2) + 455,
			top: 191,
			position: 'absolute',
			width: 20,
			height: 75
		}).fadeIn();
		
	} else {
		$("#right, #left").fadeOut();
	}
	
	$("#underline div").animate({
		marginLeft: 0
	});
	
	if(jump == 0)
		$($("#underline img").get(0)).trigger("click");
	else
		jump = 0;
	
}

$("#left").live("mouseover", function() {
	clearInterval(tim);
	tim = setInterval('mLeft()', 400);
})

$("#right").live("mouseover", function() {
	clearInterval(tim);
	tim = setInterval('mRight()', 400);
})

function mLeft() {
	if(parseInt($("#underline div").css("marginLeft")) >= -1*($("#underline div img").size()-6) * 200) {
		$("#underline div").animate({
			marginLeft: '-=240'
		});	
	}
}

$("#left, #right").live("mouseout", function() {
	clearInterval(tim);
})

function mRight() {
	
	if(parseInt($("#underline div").css("marginLeft")) < 0) {
		$("#underline div").animate({
			marginLeft: '+=240'
		});	
	}
	
}
/*
$("img[opera]").live("click", function() {
	
	var id = $(this).attr("opera");
	
	$("#ajx").fadeOut(function() {
		
		$("#ajx").fadeIn().html('<div align="center"><br /><br /><img src="/ajax-loader.gif" /></div>');
		$.get("/ajax/opera:id=" + id, function(e) {
			
		});
		
	});
	
	location.hash = id;
	
});

// Questo viene chiamato in corrispondenza della storia.
$(".moreInfo").live("click", function() {
	
	var id = $(this).attr("oId");
	var elemento = $(this).attr("elemento");
	$("#ajx").fadeOut(function() {
		
		$("#ajx").fadeIn().html('<div align="center"><br /><br /><img src="/ajax-loader.gif" /></div>');
		
		$.get("/ajax/opera:id=" + id + "&tipo=2&elemento=" + elemento, function(e) {
			$("#ajx").html(e);
		});
	});
	
});
*/


$("img[opera]").live("click", function() {
	
	$("#ajx").slideUp();
	
	var id = $(this).attr("opera");
	
	var informazioni;
	var thumb;
	var contenuto;
	
	// Prelevo le informazioni generali sull'opera!
	$.post("/ajax/new/info:id=" + id, function(a) {
		
		informazioni = a.informazioni;
		
		$("#opera #collaterale #descrizione p").html(informazioni.descrizione);
		$("#opera #collaterale #descrizione h1").text(informazioni.titolo);
		$("#opera #collaterale #descrizione h2").text(informazioni.autore);
		
		if(informazioni.tipo == 2) {
			
			$("#opera #collaterale #thumb").html('');
			
			$(a.thumb).each(function(a,b) {
				$("#opera #collaterale #thumb").append('<img sSrc="http://www.sugarkane.it/tx/' + b.immagine + '" src="http://www.sugarkane.it/thumb:immagine=/' + b.immagine + '" style="margin-right: 10px; margin-bottom: 10px"/>');
			});
			
			$($("#opera #collaterale #thumb img").get(0)).trigger("click");
			
		} else {
			$("#opera #show").html(a.video.video);
			if(a.video.thumb != "") {
				$("#opera #collaterale #thumb").html('<img width="270" src="'+a.video.thumb+'" />');
			} else
				$("#opera #collaterale #thumb").html('');
			
		}
		
		$("#opera").fadeIn();
		
	}, "json");
	
	location.hash = id;
	
});

$("#opera #collaterale #thumb img").live("click", function() {
	$("#opera #collaterale #thumb img").fadeTo(200, 1);
	$(this).fadeTo(200, 0.5);
	$("#opera #show").html('<img width="640" style="display: none" src="' + $(this).attr("sSrc") + '" />').find("img").bind("load", function() {
		$(this).fadeIn();
	});
})
