// This is mathieu.js

/* store geometry of central box for further restoring */
var centralBoxHeight = 0;

/* * * * * * * * * * * * */
/* Few globals          */
/* * * * * * * * * * * * */
var optionCollapsedWidth = 100;
var optionExpandedWidth = 400;

function Wip()
{
	var maxW = 100;
	var maxH = 400;
	var minH = 44;
	var w = $(".wip-item");
	var actualW = w.width();
	var actualH = w.height();
	
	w.hide();
	w.css("width",optionCollapsedWidth);
	w.css("height", (optionCollapsedWidth * actualH) / actualW);
	w.addClass("wip-item-collapsed");
	w.each(function(i){
		var t = Math.floor((Math.random() * (maxH - minH)) + minH);
		var l = Math.floor(Math.random()*maxW);
		var that = $(this);
		that.css("top", t);
		that.css("left", l);
		that.attr("id", "wip"+i);
	});
	w.show();
	
	w.resizable({ 
		aspectRatio: true,
		autoHide: true,
		ghost: true,
		handles: "ne, se, sw, nw"
	});
	w.parent(".ui-wrapper").draggable({ cursor: 'move' });
	
	w.dblclick(function(){
		var that = $(this);
		var ratio = that.height() / that.width() ;
		that.parent(".ui-wrapper").draggable("destroy");
		that.resizable('destroy');
		if(that.hasClass("wip-item-collapsed"))
		{
			that.width(optionExpandedWidth);
			that.height(optionExpandedWidth * ratio);
			that.removeClass("wip-item-collapsed");
		}
		else
		{
			that.width(optionCollapsedWidth);
			that.height(optionCollapsedWidth * ratio);
			that.addClass("wip-item-collapsed");
		}
		that.resizable({ 
				aspectRatio: true,
				autoHide: true,
				ghost: true,
				handles: "ne, se, sw, nw"
			});
		that.parent(".ui-wrapper").draggable({ cursor: 'move' });
	});
}

function showNext()
{
		var that = $(this);
		that.css("visibility", "hidden");
		$(".nav-prev").css("visibility", "visible");
		var cur = $(".gallery-item:visible");
		var next = cur.next(".gallery-item");
		if(next.length > 0)
		{ 
			cur.hide();
			next.show();
			if(next.next(".gallery-item").length  > 0)
			{
				that.css("visibility", "visible");
			}
		}
}

function showPrev()
{
		var that = $(this);
		that.css("visibility", "hidden");
		$(".nav-next").css("visibility", "visible");
		var cur = $(".gallery-item:visible");
		var prev = cur.prev(".gallery-item");
		if(prev.length > 0)
		{ 
			cur.hide();
			prev.show();
			if(prev.prev(".gallery-item").length > 0)
			{
				that.css("visibility", "visible");
			}
		}
}

function Gallery()
{
	$(".nav-prev").live("click",showPrev);
	$(".nav-next").live("click",showNext);
}

function Fixe()
{
	$(".menu-fixe").click(function(){
		var c= $("#central");
		var f = "fixe/" + $(this).attr("id") + ".html";
		c.load(f);
		c.css("height", "auto");
	});
}

function InitAll()
{
	centralBoxHeight = $("#central").css("height");
	$(".menu-item").click(function(){
		var e = $(this);
		var f = "menu/" + e.attr("id") + ".php";
		var c= $("#central");
		c.load(f);
		c.css("height", "auto");
	});
	
	$(".media-close").live("click",function(){
		var c= $("#central");
		c.empty();
		c.css("height", centralBoxHeight);
	});
	
	Wip();
	Gallery();
	Fixe();
}

$(document).ready(InitAll);