var loading_img = '<img src="/images/loading.gif" alt="" title="Caricamento in corso..." />';
var defaultEasing = 'easeInExpo';
var duration = 200;
var hoverPopup = true;


var content_to_popup = function(is_gallery, the_html, the_W, the_H, loaded, callback_f) {
	if( is_gallery === undefined ) {
		is_gallery = false;
	}
	if( (loaded === undefined) ) {
		loaded = false;
	}
	
	var newH = the_H + 20;
	if( (the_H === undefined) && !is_gallery ) {
		newH = 300 + 20;
	}
	var newW = the_W;
	if( (the_W === undefined) && !is_gallery ) {
		newW = 550;
	}
	
	if( is_gallery ) {
		var all_images = new Array();
		var i = 0;
		var current = 0;
		var totale = 0;
		$('a[rel="real_photos"]').each(function(index){
			if( $(this).attr('href') == the_html ) {
				current = i;
			}
			all_images[i] = $(this).attr('href');
			i = i+1;
		})
		totale = all_images.length;
	}
	
	if( is_gallery && ((the_html == 'prev') || (the_html == 'next')) ) {
		/* Significa che ho già aperto il popup e ho richiesto una foto precedente o successiva */
		var newPos = parseInt($('#popup #current').html()) + ((the_html == 'prev') ? (0-1) : 1);
		if( newPos < 0 ) {
			newPos = parseInt($('#popup #total').html()) - 1;
		} else if ( newPos > (parseInt($('#popup #total').html()) - 1) ) {
			newPos = 0;
		}
		
		var newImg = all_images[newPos];
		
		content_to_popup(false, loading_img, 32, 32, false, function(){
			setTimeout(function(){
				$('<img />')
					.attr('src', newImg)
					.load(function(){
						content_to_popup(true, newImg, this.width, this.height, true);
						return;
					})
			}, 300);
		});
		return;
		
	} else if ( is_gallery && !(loaded) ) {
		/* Il popup sta visualizzando una gallery e the_html è il percorso di una immagine e non è precaricata: precarico e rilancio la funzione */
		img_to_load = the_html;
		content_to_popup(false, loading_img, 32, 32, false, function(){
			setTimeout(function(){
				$('<img />')
					.attr('src', img_to_load)
					.load(function(){
						content_to_popup(true, img_to_load, this.width, this.height, true);
						return;
					})
			}, 300);
		});
		return;
	}
	
	the_html = 
		'<div style="position: relative; display: block; height: 20px; text-align: center;">'
		+ (	is_gallery
			? '<a href="#" class="prev_img_link">« precedente</a> | <a href="#" class="next_img_link">successiva »</a> | '
			: '')
		+ '<a href="#" id="do_close_popup">chiudi</a>'
		+ '</div>'
		+ ( is_gallery
			? '<div style="position: relative"><div class="prev_link_block"></div><div class="next_link_block"></div><img alt="" title="" src="'+ the_html +'" /></div>' + '<div id="popup_val" style="display: none;"><div id="current">'+current+'</div><div id="total">'+totale+'</div></div>'
			: the_html);
	
	if( is_gallery ) {
		the_html += '<div id="popup_val" style="display: none;"><div id="current">'+current+'</div><div id="total">'+totale+'</div></div>';
	}
	
	$('#popup')
		.animate({
			'width' : newW + 'px',
			'height' : newH + 'px',
			'left' : Math.floor(($(window).width() / 2) - (newW/2)) + 'px',
			'top' : Math.floor(($(window).height() / 2) - (newH/2)) + 'px'
		}, duration, defaultEasing, function(){
			if( typeof callback_f == 'function' ) {
				callback_f.call();
			}
			return;
		})
		.html(the_html);
	return;
}

var do_resize_popup = function () {
	var newH = ($(window).height() > $('html').height()) ? $(window).height() : $('html').height();
	var newW = ($(window).width() > $('html').width()) ? $(window).width() : $('html').width();
	$('#hider').css('height', newH);
	$('#hider').css('width', newW);

	var newTop = (Math.floor(($(window).height() / 2) - ($('#popup').height() / 2)) < 0) ? 0 : (Math.floor(($(window).height() / 2) - ($('#popup').height() / 2)) + 'px');
	var newLeft = (Math.floor(($(window).width() / 2) - ($('#popup').width() / 2)) < 0) ? 0 : (Math.floor(($(window).width() / 2) - ($('#popup').width() / 2)) + 'px');

	$('#popup').css('top', newTop);
	$('#popup').css('left', newLeft);
}

var do_close_popup = function(do_it_fast) {
	if( do_it_fast === undefined ) {
		do_it_fast = false;
	}
	
	if( !do_it_fast ) {
		$('#popup').animate({'opacity' : 'hide'}, duration+300, defaultEasing, function(){
			$('#popup')
				.html(loading_img)
				.css('height', '32px')
				.css('width', '32px');
		});
		$('#hider').animate({'opacity' : 'hide'}, duration, defaultEasing);
		
	} else {
		$('#popup')
			.hide()
			.html('<img src="/images/loading.gif" alt="" title="Caricamento in corso..." />')
			.css('height', '32px')
			.css('width', '32px');
		$('#hider').hide();
	}
	
	do_resize_popup();
}

var do_open_popup = function(is_gallery, the_html, the_W, the_H) {
	if( is_gallery === undefined ) {
		is_gallery = false;
	}
	
	do_resize_popup();
	
	$('#hider')
		.css({'display' : 'block', 'opacity' : '0'})
		.animate({'opacity' : '0.7'}, duration, defaultEasing);
	
	$('#popup')
		.animate({'opacity' : 'show'}, duration, defaultEasing, content_to_popup(is_gallery, the_html, the_W, the_H, false));
}

$(document).ready(function(){
	
	do_close_popup(true);
	
	$(window).resize(function(){
		do_resize_popup();
	})
	
	$('a#do_close_popup').live('click', function(e){
		e.preventDefault();
		do_close_popup();
	})
	
	$('a[rel="real_photos"]').live('click', function(e){
		e.preventDefault();
		do_open_popup(true, $(this).attr('href'));
	})
	
	$('a.next_img_link').live('click', function(e){
		e.preventDefault();
		content_to_popup(true, 'next');
	})

	$('a.prev_img_link').live('click', function(e){
		e.preventDefault();
		content_to_popup(true, 'prev');
	})
	
	$('div.next_link_block, div.prev_link_block').live('mouseover', function(){
		$(this).css({'opacity' : '0.6'});
	})
	$('div.next_link_block, div.prev_link_block').live('mouseout', function(){
		$(this).css({'opacity' : '0'});
	})
	
	$('div.next_link_block').live('mouseup', function(){
		$('a.next_img_link').trigger('click');
	})
	
	$('div.prev_link_block').live('mouseup', function(){
		$('a.prev_img_link').trigger('click');
	})
	
	$('#popup').hover(function(){
		hoverPopup = true;
	}, function(){
		hoverPopup = false;
	})
	
	$('html').click(function(){
		if( hoverPopup === false ) {
			do_close_popup();
		}
	})
	
	$(document).keyup(function(e) {
		if (e.keyCode == 27) { // ESC
			do_close_popup();
		}
	})
})
