/**
* Boutons reserv, infos
**/

function rangeReserv()
{
	link_open = false;
	reserv.css({"cursor":"pointer"});
	reserv.children("a").attr('href',base_href_reserv);
	dispProg($liens, 2, function()
	{
		reserv.animate({width:base_width_reserv}, 500, "", function()
		{
			$liens.remove();
			reserv.children("div").remove();
			reserv.animate({top:0}, 500, "", onInfoClick);
		});
	});
}

function rangeInfos()
{	
	link_open = false;
	infos.css({"cursor":"pointer"});
	infos.children("a").attr('href',base_href_infos);
	dispProg($boutons, $boutons.length - 1, function()
	{
		$("#infoscontent").fadeOut(500, function()
		{
			infos.animate({width:base_width_infos, height:base_height_infos}, 500, "", function()
			{
				$boutons.remove("*");
				$("#infoscontent").remove();
				infos.animate({top:0}, 500, "", onReservClick);
			});
			infos.parent().parent().animate({top:base_top_dd}, 500);
		});
	});
}

function onInfoClick(e)
{
	if(e != undefined) e.preventDefault();
	
	stopCentralMotion($(this).parent().get(0));
	if(anim_playing) return;
	
	if(link_open)
	{
		rangeReserv();
		e.preventDefault();
		return;
	}
	
	anim_playing = 1;
	
	if(e == undefined) reserv.bind('click', onReservClick);
	
	link_open = true;
	
	var attr = infos.children("a").attr('href');
	infos.children("a").removeAttr("href");
	infos.unbind("click");
	infos.css({"cursor":"auto"});
	infos.animate({"top":30}, 500, "", function()
	//infos.animate({"top":0}, 0, "", function()
	{
		var $bloc = infos;
		infos.animate({width:600}, 500, "", function()
		{
			$.get(attr+"nometa=true", function(data)
			{
				$bloc.append("<div id='infoscontent' style='display:none;'></div>");
				$bloc.children("#infoscontent").append(data);
				//alert($bloc.children("p").outterHeight());
				var totalHeight = ($bloc.children("#infoscontent").outerHeight() + infos.outerHeight());
				if(totalHeight > 333)
				{
					totalHeight = 333;
					infos.find("div.content").css('overflow','auto');
					infos.find("div.content").height(310);
					infos.find("div.content").width(590);
					//infos.find("div.content").jScrollPane({scrollbarWidth:20});
				}
				infos.animate({height:totalHeight}, 500, "", function()
				{
					if(attr.indexOf('actu') == -1)
					{
						$.get("dyn/btn_info.php?id="+(attr.substr(attr.lastIndexOf("_") + 1).substr(0, attr.indexOf(".") - attr.indexOf("_")))+"&nometa=true", function(data)
						{
							$bloc.append(data);
							$bloc.children("#infoscontent").fadeIn(1000, function()
							{
								$boutons = $bloc.children(".boutons");
								putPrettyPhoto($("*[rel^='prettyPhoto']"));
								for(var i = 0; i < $boutons.length; i++)
								{
									$($boutons[i]).css({"top":60 * i, "left":-60});
								}
								affProg($boutons, 0);
								anim_playing = 0;
								
								$("."+thenWhat).click();					
							});
							
							$(".ident").click(function(e)
							{
								e.preventDefault();
								
								showIdent($(this).attr("href"), "popup");
							})
						});
					}
					else
					{
						anim_playing = 0;
						$bloc.children("#infoscontent").fadeIn(1000);
					}
				});
				if(totalHeight > 150)
				{
					var $dd = $bloc.parent().parent();
					var coord = $dd.offset();
					$dd.css("position","absolute");
					var posY = base_top_dd + (150 - totalHeight);
					if(posY < 55) posY = 55;
					//alert(totalHeight);
					//alert("Move to "+posY);
					$dd.animate({"top": posY}, 500);
				}
			});
		});
	});
}

function onReservClick(e)
{
	if(e != undefined) e.preventDefault();
	
	stopCentralMotion($(this).parent().get(0));
	if(anim_playing) return;
	
	if(link_open)
	{
		rangeInfos();
		e.preventDefault();
		return;
	}
	
	anim_playing = 1;
	
	if(e == undefined) infos.bind('click', onInfoClick);

	link_open = true;
	
	var attr = reserv.children("a").attr('href');
	reserv.children("a").removeAttr("href");
	reserv.unbind("click");
	reserv.css({"cursor":"auto"});
	
	var $dd = reserv.parent().parent();
	$dd.animate({"top":68}, 500, "", function()
	{
		reserv.animate({"top":30}, 500, "", function()
		{
			base_width_reserv = reserv.width();
			reserv.animate({width:"500"}, 500, "", function()
			{
				var $bloc = reserv;
				if(attr == '')
				{
					alert("Attr est vide, onReservClick fin.");
					return;
				}
				$.get(attr+"&nometa=true", function(data)
				{
					$bloc.append("<div class='blocbutton'></div>");
					
					var $bb = $bloc.find('.blocbutton');
					
					$bb.append(data).css({"top":30, "left":0});
					$liens = $bb.children("div");
					
					for(var i = 0; i < $liens.length; i++)
					{
						opened = "";
						
						var $this = $($liens[i]);
						
						$this.find('.closed').css('opacity','0.5');
						if($this.find('.closed').length == 0)
						{
							$this.click(openlink);
						}
						else
						{
							$this.css('cursor','wait')
						}
					}
					affProg($liens, 0);
					
					anim_playing = 0;
					
					$(".buttonvalide").css('cursor','pointer');
				});
			});
		});
	});
}

function openlink()
{
	if(opened != "")
	{
		if($(opened).children("div").length > 0)
		{
			$(opened).children("div").fadeOut(500, function()
			{
				$(this).parent().animate({height:30});
				$(this).parent().bind('click', openlink);
				$(this).parent().css('cursor','pointer');
			});
		}
		else
		{
			$(opened).animate({height:30});
			$(opened).bind('click', openlink);
			$(opened).css('cursor','pointer');
		}
	}
	
	if($(this).children('a').attr('onclick') != undefined) return false;
	
	opened = this;
	$(opened).unbind('click');
	$(opened).css('cursor','auto');
	$(this).animate({height:120}, 500, "", function()
	{
		$(this).children("div").fadeIn();
	});
}