
function Action() 
{ 
	this.id;
	this.url = "/";
	this.projet = null;
	this.index = 0;
	this.direct = false;
	
	this.preDispatchFunction;
	this.actionFunction;
	this.postDispatchFunction;
} 

Action.prototype = { 
   
	preDispatch : function()
	{
		this.preDispatchFunction ();
	},
	
	action : function()
	{
		this.actionFunction ();
	},
	
	postDispatch: function(action)
	{
		if (this.direct)
			this.actionFunction ();
		else
			this.postDispatchFunction (action);
	},
	
	resize: function()
	{
		this.actionFunction (true);
	}
} 



/*************
	PRE DISPATCH
**************/

function _preDispatchaccueil()
{

	$("#container-accueil").fadeIn();
	
	$("#home-projet").css("display", "block");
	$("#home-projet .home-projet").css("opacity", 0);
	
		
	setTimeout("_endPreDispatchAccueil()", 400);
	
}

function _endPreDispatchAccueil()
{

	$("#presentation").fadeIn("fast");

	
	var showNext = function (section) {
		
		var top = parseInt(section.css( "top").replace("px", ""));
		section.css( "top", function(index, value) {var val = parseInt(value.replace("px", "")) + 60; return val; })

		if(!Modernizr.cssanimations)
		{
			section.animate(
				{
					opacity: 1 ,
					top:top 
				},
				{
					duration: 400, 
					easing : 'easeOutExpo',
					complete:function()
					{
						var next = $(this).next('.home-projet');
						
						if (next[0] != undefined)
							showNext(next);
						else
							dispatcher.isTweening(false);
					}
				}
			)
		
		}
		else
		{
		
			section.css("top",top);
			section.css("opacity", 1);
			
			var next = section.next('.home-projet');
	
			setTimeout(function ()
			{
				if (next[0] != undefined)
					showNext(next);
				else
					dispatcher.isTweening(false);
			},400);
			
		}
		
	}
	
	showNext( $("#home-projet .home-projet:first-child") );
}

function _preDispatchportfolio()
{
	$("#gallery-all").css("display", "block");
	
	dispatcher.isTweening(false);
	
	_showProjets(true);
}

function _preDispatchprojet()
{
	//console.log("projet url", this.projet);
	
	// Direct access
	if (this.projet == null)
	{
		$("#projet").fadeIn();
		dispatcher.isTweening(false);
	}
	else
	{
		
		//addchild loading gif
		var loadingProjet = '<div id="loading-projet"><img src="/img/loading-projet.gif" /></div>';
		$("body").append(loadingProjet);
		
		//Load the project
		if ($("#projet")[0] != undefined)
		{
			//load data
			$.getJSON("/projets/getprojetcontent/"+this.projet, function(data)
			{
				  var projet = data.projet;
				  
				  $("#loading-projet").fadeOut("fast", function(){$(this).remove();});
				  
				  //Clean
				  $("#projet article").html("");
				  
				  $("#projet article").append("<hgroup><h1>"+projet.title+"</h1></hgroup>")
				  $("#projet article").append(projet.description);
				  
				  if (projet.url != null)
					  $("#projet article").append('<a id="visit-site" href="'+projet.url+'" >Check this out !</a>');
				  
				  $("#projet .media-projet > img").attr("src", projet.mediaPath);
				  $("#projet .media-projet > img").attr("alt", projet.title);
				  
				  $("div#slideshow span#date").html(projet.annee);
				  $("div#slideshow span#client").html("<strong>"+projet.client+"</strong>");
				 				  
				  $("div#slideshow span#kw").html(projet.techno);
				  
				  var image = new Image();
				  image.src = projet.mediaPath;
					
				  $(image).load(function()
				  {
					  $("#projet").fadeIn("fast", function()
					  {
						   dispatcher.isTweening(false);
					  });
				  });
				  
				  //_onResize(null);
				  
				  
			});
			
			
		}
		else
		{
			
			//load HTML
			$.ajax({
				  url: "/projets/seeprojetpartial/"+this.projet ,
				  success: function(data)
				  {
					  $("#loading-projet").fadeOut("fast", function(){$(this).remove();});
					  
					    $("section.page[rel=projets]").append(data);
					    $("#projet").hide();
					    _onResize(null);
					    $("#projet").fadeIn("fast", function()
					    {
							  dispatcher.isTweening(false);
						});
						    
				  }
				});
			
		}
	}
	
	
}

function _preDispatchcontact()
{
	$("#container-contact").fadeIn();
	
	dispatcher.isTweening(false);
}

/*************
	ACTIONS
**************/

function _move(left, top, o, resize)
{
	//console.log("move", o, resize);
	if (!o.direct || resize == null)
	{
		if (!Modernizr.cssanimations)
		{
			$("#container").animate(
				{
					left: left,
					top:  top
				},
				{
					duration: 700, 
					easing : 'easeOutExpo'
					
				}
			);
			$("#footer").animate(
				{
					left: left
				},
				{
					duration: 700, 
					easing : 'easeOutExpo',
					complete: function()
					{
						if (resize == null)
							o.preDispatch();
					}
				}
			);
		}
		else
		{
			$("#container").css("left", left);
			$("#container").css("top", top);
			$("#footer").css("left", left);
			
			o.preDispatch();
		}
	}
	else
	{
		$("#container").css("left", left);
		$("#footer").css("left", left);
		if (resize == null)
			o.preDispatch();
	}
}

function _actionaccueil(resize)
{
	resize = resize || null;
	
	var left =  - parseInt(this.index * pageWidth);
	_move(left, 0, this, resize);
}

function _actionportfolio(resize)
{
	resize = resize || null;
	
	var left =  - parseInt(this.index * pageWidth);
	_move(left, 0, this, resize);
}

function _actioncontact(resize)
{
	resize = resize || null;
	
	var left =  - parseInt(this.index * pageWidth);
	_move(left, 0, this, resize);
}

function _actionprojet(resize)
{
	resize = resize || null;
	
	var left =  - parseInt(this.index * pageWidth);
	_move(left, -pageHeight, this, resize);
}



/*************
	POST DISPATCH
**************/

function _postDispatchaccueil(action)
{
	/*
	if (!Modernizr.cssanimations)
		$("#presentation").css("opacity", 0);
	else
		$("#presentation").fadeOut(400);
	*/
	
	var first = $("#home-projet .home-projet:first-child");
	var last = $("#home-projet .home-projet:last-child");
		
	var topFirst = parseInt(first.position().top);
	var topLast = parseInt(last.position().top);
	
	if (!Modernizr.cssanimations)
	{
		$("#home-projet .home-projet:last-child").animate(
			{
				opacity: 0,
				top:topLast
			},
			{
				duration: 400, 
				easing : 'easeOutExpo',
				complete:function()
				{
					action.action();
				}
			}
		)
		
		$("#home-projet .home-projet:first-child").animate(
			{
				opacity: 0,
				top:topFirst
			},
			{
				duration: 400, 
				easing : 'easeOutExpo'
			}
		)
	}
	else
	{
		
		
		//var replace_top = parseInt(top.replace("px", "")) + 60;
		//section.offset(  {top:replace_top} );
		
		console.log("topFirst", topFirst);
		console.log("topFirst+60", parseInt(topFirst+60));

		first.css("opacity", 0);
		first.position(  {top:parseInt(topFirst+60)} );
		last.css("opacity", 0);
		last.position(  {top:parseInt(topLast+60)} );
		
		setTimeout(function ()
		{
			first.position(  {top:parseInt(topFirst)} );
			last.position(  {top:parseInt(topLast)} );
			console.log("end", first.position().top);
			action.action();
		},400);
	}
	
	
	//hidePrev( $("#home-projet .home-projet:last-child"));
	
	//A la fin, on fait l'action
	
}

function _postDispatchportfolio(action)
{
	
	_hideProjets(true, action);
	//action.action();
}

function _postDispatchprojet(action)
{

	$("#projet").fadeOut("fast", function(){
		action.action();
	});
	
}

function _postDispatchcontact(action)
{
	
	action.action();
}
