/* EMEDAGAL
- - - - - - - - - - - - - - - - - - - - */
$.fn.emedagal = function() {
	return this.each(function() {
		var zoom = false;
		var newImg;
		var $this = $(this);
		var firstImg = $this.find(".zoom img.zoom").attr("src");
		var _top;
		$this.find(".items img:eq(0)").addClass("on");
		
		$this.find(".carat_open a").click(function(){

			if ($(window).height() < 660) {
				_top = $(window).height() - 110 + "px";
			} else {
				_top = "549px";
			}
			
			if ($(this).is(".open")) {
				$this.find(".dati").animate({"top":"0px"},400);
				$(this).parent().animate({"top":_top},400,function(){
					$(this).find("a").addClass("close").removeClass("open").css({"marginLeft":"-" + parseInt($this.find(".carat_open a").width()/2) + "px"});
				});
			} else {
				$this.find(".dati").animate({"top":"-576px"},400);
				$(this).addClass("open").removeClass("close").css({"marginLeft":"-" + parseInt($this.find(".carat_open a").width()/2) + "px"}).parent().css({"top":_top});
				$(this).parent().animate({"top":"-1px"},370);
			}
			if ($(".dati").parent().is("a")) {
				$(".dati").parent().css({"cursor":"default"}).click(function(){
					return false;
				});
			}
			return false;
		});
		
		$this.find(".zoom").click(function(){
			if (!zoom) {
				$(this).css("cursor","default");
				$this.find(".group:eq(0) img:eq(0)").click();
			}
		});
		
		$this.css({"position":"absolute","right":"10px","top":"47px","width":"280px"});
		$(this).parents("#right").css({"padding":"340px 0px 0px 0px"});
		
		$this.find(".items a").each(function(){
			$(this).children("img").attr("rel",$(this).attr("href"));
			$(this).removeAttr("href");
		});
		
		$this.find(".items img").click(function(){	
			$this.find(".carat_open a.close").click();
			$this.append('<div class="photo_loading"></div>');
			newImg = $(this).attr("rel");
			function rqAjax() {
				zoomma();
				$this.find(".zoom img.zoom").attr({"src":newImg}).css({"width":"auto"});
				setTimeout(function(){
					$this.find(".photo_loading").fadeOut(100,function(){
						$(this).remove();
					});
				},500);
			}
			
			var imgW = $this.find(".zoom img.zoom").width();
			var imgH = $this.find(".zoom img.zoom").height();
			var imgS = $this.find(".zoom img.zoom").attr("src");
			$this.find(".zoom").find("img.zoom").hide();
			if (!zoom) {
					$this.find("div.zoom").css({"width": imgW + "px", "height": imgH + "px"});
			}
			$.ajax({
				url: $(this).attr("rel"),
				cache: true,
				success: function(){
					rqAjax();
				},
				error : function(){
					rqAjax();
				}
			});
			
		});
		
		
		var _height;
		var _width;
		function zoomma () {
			if (!zoom) {
				// controllo dimensioni finestra
				if ($(window).height() < 660) {
					_height = $(window).height() - 84 + "px";
					_width = parseInt((parseInt(_height) * 940) / 576) + "px";
				} else {
					_height = "576px";
					_width = "940px"
				}
				
				$this.find("img.zoom").css({"cursor":"default"});
				$this.animate({"width":"980px", "top":"33px"},1000);
				$this.find(".items div").css({"width":"auto"});
				$this.find("div.zoom").animate({"width":"940px","height":_height},1000).addClass("big");
				$this.find("img.zoom").animate({"width":_width,"height":_height},1000);
				$this.find(".nav_next, .nav_prev").fadeIn(500);
				$this.find(".scrollable").animate({"width":"882px"},1000);
				$this.find(".items").animate({"paddingLeft":"408px"},1000);
				$this.find("div.zoom img.zoom").fadeIn(800,function(){
					$this.find("div.zoom").css({"background":"url(img/bg_zoom.gif) repeat"});
					$this.find(".carat_open").show().find("a").css({"marginLeft":"-" + parseInt($this.find(".carat_open a").width()/2) + "px"}).hide().fadeIn(300);
				});
				overlay.open();
				zoom = true;
			} else {
				$this.find("img.zoom").css({"width":_width,"height":_height});
				$this.find("div.zoom").css({"background":"url(img/bg_zoom.gif) repeat"});	
				setTimeout(function(){
					$this.find(".zoom img.zoom").fadeIn(800);
				}, 500);
			}
			$this.find(".items a.on").removeClass("on");
			$this.find(".items img.on").parent().addClass("on");
			if ($this.find(".scheda li").size()) {
				$this.find(".dati").html($this.find(".items img.on").parents(".group").children("span.scheda").html());
			} else {
				$this.find(".carat_open").addClass("hidden").remove();
			}
			
		}
		
		
		overlay = {
			open : function(){
				$.scrollTo("#left", 500);
				$this.find("h3").hide();
				$("#left h1").css({"color":"#fff"});
				olay = $('<div id="overlay"></div>');
				olay.appendTo($("#corpo")).hide().css({"width":$("body").width() + "px", "height":$(document).height() + "px", "position":"absolute", "background":"#000", "opacity":0.7, "zIndex":"1", "top":"-" + $("#corpo").offset().top + "px", "left":"-" + $("#corpo").offset().left + "px"}).fadeIn(400);
				chiudi = $('<div id="chiudi_gal"></div>');
				chiudi.appendTo($this);
				olay.click(function(){
					overlay.close();
				});
				chiudi.click(function(){
					overlay.close();
				});
				
				zoom = true;
			},
			close : function(){
				$this.find(".carat_open a.close").click();
				$("#left h1").css({"color":"#000"});
				$this.find("h3").show();
				$this.animate({"width":"280px", "top":"47px"},1000);
				$this.find(".items div").css({"width":"66px"});
				$this.find("img.on").removeClass("on").parent().removeClass("on");
				$this.find(".items div:eq(0)").click();
				$this.find(".items div:eq(0) img:eq(0)").addClass("on");
				$this.find(".items").animate({"paddingLeft":"57px"},500);
				$this.find(".zoom img.zoom").hide();
				$this.find(".nav_next, .nav_prev").fadeOut(500);
				$this.find(".zoom").css({"background":"none"});
				$this.find(".zoom").animate({"width":"240px","height":"147px"},1000,function(){
					$this.find(".zoom img.zoom").css({"cursor":"pointer"}).hide().attr("src",firstImg).fadeIn(1000);
				}).removeClass("big");
				$this.find(".scrollable").animate({"width":"182px"},900);
				$this.find(".carat_open").fadeOut(300);
				zoom = false;
				$("#overlay").fadeOut(400,function(){
					$(this).remove();
				});
				$("#chiudi_gal").fadeOut(400,function(){
					$(this).remove();
				});
			}
		};

			$this.find(".items div:eq(0)").click();
	});
}


/* INIT GALLERY
- - - - - - - - - - - - - - - - - - - - */
$(function(){
	if ($("#photogallery").size()){
		$("#photogallery").emedagal();
		var api = $("#photogallery .scrollable").scrollable({
			'size': 1,
			//'interval': 5000,
			api: true,
			clickable: false
		});
		api.onSeek(function(){
			$("#photogallery").find(".active").removeClass("active");
			if ($("#overlay").size()) {
				$("#photogallery").find(".items").find("div:eq(" + this.getIndex() + ")").addClass("active");
				$("#photogallery").find("img.on").parent().addClass("on");
			} else {
				$("#photogallery").find(".items img.on").removeClass("on");
				$("#photogallery").find(".items").find("div:eq(" + this.getIndex() + ")").addClass("active").find("img:eq(0)").addClass("on");
			}		
		});
		$("#photogallery").find(".items div").click(function(){
			$this = $(this);
			setTimeout(function(){
				api.seekTo($("#photogallery .items div").index($this));
			},200);
		});
		$("#photogallery").find(".items img").click(function(){
			$("#photogallery").find(".items img.on").removeClass("on");
			$(this).addClass("on");
		});
		$("#photogallery").find(".items div:eq(0)").click();
		api.seekTo(0);
		
		// click freccie thumb
		$("#photogallery .next, #photogallery .prev").click(function(){
			$(".items").stop();
			$(".items .active a:eq(0) img").click();
		});
		
		// click freccie zoom
		$(".nav_next, .nav_prev").appendTo($("#photogallery"));
		$(".nav_next a, .nav_prev a").click(function(){
			thumbSize = $("#photogallery").find(".items a").size();
			i = $("#photogallery").find(".items a").index($("#photogallery").find(".items a.on"));
			if ($(this).parent().attr("class") == "nav_next") {
				(i == thumbSize - 1) ? i = 0 : i++;
			} else {
				(i == 0) ? i = (thumbSize - 1) : i--;
			}
			$("#photogallery").find(".items a:eq(" + i + ") img").click();
			return false;
		});
	}
});

window.onresize = function(){
	if ($("#overlay").size()){
		$("#overlay").css({"width":$("body").width() + "px", "height":$(document).height() + "px", "position":"absolute", "background":"#000", "opacity":0.7, "zIndex":"1", "top":"-" + $("#corpo").offset().top + "px", "left":"-" + $("#corpo").offset().left + "px"});
	}
};