var successivePhoto;

$(document).ready( function () {
	/*$('#wrapper li').bind('mouseenter mouseleave', function () {$(this).toggleClass('over')});*/
	$('#wrapper li').bind('click', function () { 
		var action = this.id.split("_");
		switch (action[0]) {
			case "col":
				document.location=this.id.replace("col_","?c=");
				break;
			case "colset":
				var newobj=this.id.replace("colset_","?c=");
				document.location=newobj.replace("set_","&s=");
				break;
			case "set":
				document.location=this.id.replace("set_","?s=");
				break;
			case "tag":
				document.location=this.id.replace("tag_","?t=");
				break;
			case "context":
				var url = "pages/context.php";
				if ($('#dialogcontainer').css("display") == "none") {
					loadContext (url, {'context': action[1]});
				} else {
					$('#dialogcontainer').slideUp('fast', function() {
						loadContext (url, {'context': action[1]});
					});
				}
				break;
			default:
				break;			
		}
	});
	$('#dialogcontainer').live ('click',function() {
		$(this).slideUp('fast');
	});
	$("#contenuto span img").live('mouseenter mouseleave', function() {
		$(this).parent('span').toggleClass('over');
	});
	$("#contenuto span img").live('click', function() {
		if (this.id.indexOf("id_") == 0) {
			photos = jQuery.parseJSON ( $('#contenuto').attr('photos'));
			var currentno = $(this).attr('no');
			thisPhoto = photos[currentno];
			var url = "pages/picture.php";
			if ($('#imagecontainer').css("display") == "none") {
				loadImage (url, {'id': thisPhoto});
			} else {
				$('#imagecontainer').fadeOut('fast', function() {
					loadImage (url, {'id': thisPhoto});						
				});
			}
		}
	});
	$.ajax({
		url: 'mosaic.php',
		async: true,
		success: function(msg) {
			$('#contenuto').html( msg );
		}
	});
	$('#imagecontainer').live('mousemove', function (e) { 
		//$('#cursor').removeClass("prev next close");
		$('#cursor').offset({top: e.pageY, left: e.pageX - ( $('#cursor').width() /2 )});;
		var im = $('#image div img');
		if (im.offset() == undefined) return false;
		var x = e.pageX - im.offset().left;
		var y = e.pageY - im.offset().top;
		if ( y > - $('#cursor').height()/2 && y <  im.height() - $('#cursor').height()/2 ) { 
			if ( x < im.width()/2 && x > 0  && photos.indexOf(thisPhoto) > 0 ) {
				$('#cursor').html("&lt;");
				//$('#cursor').addClass("prev");
				successivePhoto = photos.indexOf(thisPhoto) - 1;
			} else if ( x >= im.width()/2 && x < im.width() && photos.indexOf(thisPhoto) < photos.length -1 ) {
				$('#cursor').html("&gt;");
				//$('#cursor').addClass("next");
				successivePhoto = photos.indexOf(thisPhoto) + 1;
			} else {
				$('#cursor').html("x");
				//$('#cursor').addClass("close");
				successivePhoto = null;
			}
		} else {
			$('#cursor').html("x");
			//$('#cursor').addClass("close");
			successivePhoto = null;
		}
	});
	$('#imagecontainer').live('click', function () { 
		if ( successivePhoto ) {
			thisPhoto = photos[successivePhoto];
			loadImage ("pages/picture.php", {'id': thisPhoto});						
		} else {
			$('#imagecontainer').fadeOut('fast', function () { $('#image').html( "" )});
		}
		return false;
	});	
	$(document).scroll( function() {
		$('#header').css('bottom', -$(document).scrollTop()+'px');
		$('#dialogcontainer').css('top', $(document).scrollTop()+'px');
	});
});

loadContext = function (url, vars) {
	$.ajax({
		type: "POST",
		url: url,
		data: vars,
		async: true,
		success: function(msg) {
			$('#context').html( msg );
			$('#dialogcontainer').css("margin-top", 0);	
			$('#dialogcontainer').slideDown('slow');
			$('#dialogcontainer').bind('mousemove', scroller);
		}
 	});
}
loadImage = function (url, vars) {
	if ($('#imagecontainer').css("display") == "none") $('#imagecontainer').fadeIn('slow');
	$('#image').html( "<br /><br /><br /><br /><br /><img src='resources/wait_red.gif' width='32' height='33' />" );
	$.ajax({
		type: "POST",
		url: url+"?r="+Math.random(),
		data: vars,
		async: true,
		success: function(msg) {
			$('#image').html( msg );
		}
 	});
}
scroller = function(e) {
	var ypos = e.pageY-$(document).scrollTop();
	var viewport = $('#header').offset().top-$(document).scrollTop();
	var contexth = $('#context').height();
	var dy = (viewport < contexth && ypos > 0) ? -Math.round(ypos*(contexth-viewport)/viewport): 0;
	$('#dialogcontainer').css("margin-top", dy+'px');	
	//$('#debug').html( "viewport: "+viewport+"<br>"+"scrolltop: "+$(document).scrollTop()+"<br>"+"mouseY: "+e.pageY+"<br>"+"contexth: "+contexth+"<br>"+"dy: "+dy);
}

