$(document).ready(myOnDomReady);
$(window).load(myDocumentLoad);

var isIE,isIE6;
var sMainMenu =
{
	m_h			: 0,
	m_catHeight	: 150,
	m_animate	: false,
	m_expanding	: $("#menu .expanded"),
	m_delayTip	: null,
	M_init		: function()
	{


		//jQuery.each(jQuery.browser, function(i, val) { $("<div>" + i + " : <span>" + val + "</span>").appendTo("#tipDocumentation");});


		// initial menu position
		if( this.m_animate)
		{
			$("#menu").css({left:-160});			
		}

		//$("#menu .children1").css('display',"none");
		var $categories = $("#menu .children0>li>div");
		if( isIE)
			$categories.not(".expanded").children("ul").css('display',"none");
		else
			$categories.not(".expanded").children("ul").css('height',0);				// doesn't work on IE for some weird reason
			
		$categories.filter(".expanded").children("ul").css('height',this.m_catHeight);

		this.m_h = $("#menu .content").height() +40;

		// categories expanding
		
		$categories.children("a")
		.mouseover(
			function()
			{	
				var parentDiv =$(this).parent();

				if(!parentDiv.is(".expanded"))
				{
					
					if( sMainMenu.m_expanding && (sMainMenu.m_expanding!=parentDiv ))
					{
						if( sMainMenu.m_delayTip) { self.clearTimeout(sMainMenu.m_delayTip); sMainMenu.m_delayTip=null;}
						sMainMenu.m_expanding = null;
						$("#menu .expanded").removeClass("expanded").children("ul").stop().mySlide("up");
					}
					
					if( $(this).siblings("ul").is("ul"))
					{
						sMainMenu.m_expanding = parentDiv;
						
						// unexpand expanded items
						//$("#menu .expanded").toggleClass("expanded").children("ul").slideUp();
						
						// expand this item
						parentDiv.addClass("expanded").children("ul").mySlide("down");
						sMainMenu.m_delayTip= self.setTimeout( "sMainMenu.sM_showTip()",500);
					}						
					
				}
			}
		);


	}
	, sM_showTip: function()
	{
		//var o=sMainMenu.m_expanding.offset();
		//$('#tipVenir').animateTip({show:true}).animateTip({ex:o.left+100, ey:o.top+10});
	}
	, M_start :function()
	{
		// initial animation
		if( this.m_animate)
		{
			$("#menu")
			.animate({left:-160},200,"linear", function()
			{ $(this).animate( { left:165 }, { queue:true, duration:500 ,easing:"easeOutCirc"} );
			});
		}

	}
	,M_alignContainer:function()
	{

		this.m_h = $("#menu .content").height() +40;
		var h =this.m_h;
		$("#menu .content").animate( { height:h }, { queue:false, duration:2500 ,easing:"easeOutElastic"} );
	}
}


jQuery.fn.mySlide = function(dir)
{
	return this.each( function(i,e)
	{
		if (dir=="up")
		{
			if( isIE)
				$(this).slideUp();
			else
				$(this).animate({height:0,opacity:0},{queue:false, duration:500});
		}
		else if( dir=="down")
		{	
			$(this).stop().css({opacity:1}).animate({height:sMainMenu.m_catHeight},{queue:false, duration:500});

		
		}

	});
}


// imageOver
// performs an image rollover
// -------------------------------
jQuery.fn.imageOver =function(src)
{
	return this.each( function(i,e)
	{
		var im,o
		if( $(this).is("area"))
			im=$("[usemap=#"+$(this).parent().attr('name')+"]");
		else if( $(this).is("a")) 
			im=$(this).find('img');
		else if( $(this).is("img")) 
			im=$(this);
		if(o=im.get())
		{	if( src)
			{	im.attr('oldIm', im.attr('src')); 
				im.attr('src',src);
			}
			else if( src=im.attr('oldIm'))
			{	im.attr('src',src);
			}
		}
	});

}

// animateTip
// tooltips
// -------------------------------
jQuery.fn.animateTip = function(opts)
{	
	if(!opts) opts={show:true}
	return this.each( function(i,e){

		if( opts.show)
		{	$(this).stop().show().animate( { opacity:1 }, { queue:false, duration:200 ,easing:"linear"} )			
		}
		else
		if( opts.ex && opts.ey)
		{
			//$(this).css({left: 20+opts.ex, top:(opts.ey-40) })
			var a=$(this).find(".arrow");
			
			var x,y
			if( $(this).is(".ib1"))
			{	x=50+opts.ex;
				y=opts.ey-parseInt(a.css("marginTop"))-parseInt(a.css("height"))/2 ;
			}
			else if( $(this).is(".ib2"))
			{	x=opts.ex-parseInt(a.css("marginLeft"))-parseInt(a.css("width"));
				y=opts.ey-$(this).height()-30 ;
			}
			$(this).animate({left: x, top:y}, { queue:false, duration:1000 ,easing:"easeOutElastic"}); 

		}
		else
		{
			$(this).animate( { opacity:0 }, 500,"linear",function(){$(this).hide().stop();}  )
			//$(this).fadeOut("slow")
		}

	});
}


// animeMoi
// my animation function
// -------------------------------
var sTooltips=new Array()
sTooltips['ib1'] ="<div class=\"arrow\"></div><div class=\"TL\"><div class=\"TR\"><div class=\"BR\"><div class=\"BL\"><div class=\"TM\">&nbsp;</div><div class=\"MR\"><div class=\"MM\"><div class=\"inner\"></div></div></div><div class=\"BM\">&nbsp;</div></div></div></div></div>";
sTooltips['ib2'] ="<div class=\"TL\"><div class=\"TR\"><div class=\"TM\">&nbsp;</div><div class=\"MR\"><div class=\"MM\"><div class=\"inner\"></div></div></div><div class=\"BR\"><div class=\"BL\"><div class=\"BM\"><div class=\"arrow\"></div></div></div></div></div></div>";
sTooltips['ib1ie6'] ="<div class=\"MM\"><div class=\"arrow\"></div><div class=\"inner\"></div></div>";
sTooltips['ib2ie6'] ="<div class=\"MM\"><div class=\"inner\"></div></div><div class=\"arrow\"></div>";
function sMakeTooltip(tipName)
{
	var tip
	if( (tip=$("#"+tipName)) && !tip.find(".inner").is("div") )			// Check if a .inner class is present. If not, it installs the imagebloc structure
	{		var a=tip.html();
			tip.html("").append(sTooltips[tip.attr('class')+(isIE6?"ie6":"")]).addClass("tooltip"+(isIE6?" ie6":"")).find(".inner").html(a);
			// TEMP !!!! TEST ONLY !!!
			//tip.html("").append(sTooltips[tip.attr('class')+(true?"ie6":"")]).addClass("tooltip"+(true?" ie6":"")).find(".inner").html(a);

	}
}
jQuery.fn.animeMoi = function(opts)
{	return this.each( function(i,e){
		// install tooltip
		if( opts.tip)
		{	sMakeTooltip(opts.tip);
		}

		// mouveover
		$(this).mouseover(
			function()
			{
				if( opts.y0 && opts.y1 )
				{	if( opts.oneshot)
						$(this).animate({top:opts.y1}, { queue:true, duration:300 ,easing:"easeOutCirc"}).animate({top:opts.y0}, { queue:true, duration:500 ,easing:"easeInOutBack"}); 
					else
						$(this).animate({top:opts.y1}, { queue:false, duration:1000 ,easing:"easeOutElastic"}); 
					

				}
				if( opts.tip)
				{	$("#"+opts.tip).animateTip( {show:true} );
				
					$(this).mousemove( function(e)
						{	var o=$(this).offset();
							//$("#"+opts.tip).animateTip( {ex:(e.pageX+o.left),ey:(e.pageY+o.top)} );
							$("#"+opts.tip).animateTip( {ex:(opts.tipx?(o.left+opts.tipx):e.pageX),ey:(opts.tipy?(o.top+opts.tipy):e.pageY)} );
						}
					);

				}
				if( opts.imageOver)
					$(this).imageOver(opts.imageOver);
				//$(this).css("cursor","url(Data/Img/icPointer.png),auto" );
			}
		) 
		.mouseout(
			function()
			{
 				if( opts.y0 && opts.y1 && !opts.oneshot)
					$(this).animate({top:opts.y0}, { queue:false, duration:1000 ,easing:"easeOutElastic"}); 
				if( opts.tip)
					$("#"+opts.tip).animateTip({show:false}) ;
				if( opts.imageOver)
					$(this).imageOver();

				//$(this).css("cursor","default");
			}
		) 
	});
}

// addDecoration
// to decorate a div with a png
jQuery.fn.addDecoration = function(opts)
{	return this.each( function(i,e){
		var o=$(this).offset();
		var h=$(this).height();
		var x=o.left;
		var y=o.top;
		if( opts.xLeft)
		{	x += opts.xLeft;
		}
		if( opts.yBottom)
		{	y += h -opts.yBottom;
		}
		if( opts.yTop)
		{	y += opts.yTop;
		}

		var a=$(opts.element);
		a.css({position:"absolute", zIndex:500, left: x , top: y});
		$("#content").after(a);


	});
}

// myOnDomReady
function myOnDomReady()
{
	// browser detection
	isIE = jQuery.browser.msie?true:false;
	isIE6 = (isIE && (jQuery.browser.version.charAt(0)<="6" ))?true:false;


	sMainMenu.M_init();


	// animate deco

	$("#decoFleur")				.animeMoi( { tip:"tipFleur",y0:543+7	, y1:532 } );
	$("#decoChaisePhotoeil")	.animeMoi( { tip:"tipPhotoeil", y0:523		, y1:500 } );
	$("#decoBouleDePoils")		.animeMoi( { tip:"tipPeluche", y0:572		, y1:538 } );
	$("#decoGirafe")			.animeMoi( { tip:"tipGirafe", y0:498		, y1:530, oneshot:true } );
	$("#decoNainVert")			.animeMoi( { tip:"tipNainVert", y0:530		, y1:500 } );
	$("#decoChaise")			.animeMoi( { tip:"tipAccueil", y0:570		, y1:558 } );
	$("#areaSalon")				.animeMoi( { tip:"tipSalon", imageOver:"Data/Img/mapSalonOver2.gif" } ) ;
	$("#areaDocumentation")		.animeMoi( { tip:"tipDocumentation" , imageOver:"Data/Img/mapDocumentationOver.gif" } ) ;
	$("#zoneChantecrit")		.animeMoi( { tip:"tipChantecrit", imageOver:"Data/Img/decoChantecrit1Over.gif" } ) ;
	$("#zoneChantecrit2")		.animeMoi( { tip:"tipChantecrit", imageOver:"Data/Img/decoChantecrit2Over.gif" } ) ;


	sMakeTooltip("rqtooltip");
	if( 0 )
	{
		$("#Location,#JeuEnGroupe").animeMoi( { tip:"tipVenir",tipx:90,tipy:10});	
		$("#Jeu").animeMoi( { tip:"tipVenir",tipx:120,tipy:10});	
		$("#Animations").animeMoi( { tip:"tipRetrouver",tipx:130,tipy:10});	
		$("#CafeEtAteliers").animeMoi( { tip:"tipParticiper",tipx:80,tipy:10});	
		$("#Projets,#PoleRessources").animeMoi( { tip:"tipDecouvrir",tipx:100,tipy:10});	
	}


	// Paragraphes sous-pages

	// auto select
	//sousPageBastide ... ?
	$(".paragrapheSliderMenu").show().find("a").each(function(i,e){
		$(this).css({cursor:"pointer"});
		if( $(this).parent().is(".selected")) $(this).parent().css({fontSize:"18px"});
		$(this).click(function(){
			$(".paragrapheSliderMenu>span").removeClass('selected').animate({fontSize:"11px"},{duration:300,queue:false});
			$(this).parent().addClass('selected').animate({fontSize:"18px"},{duration:300,queue:false});
			$(".sousPage").hide();
			//alert(".paragrapheSlider ."+$(this).attr("className"));
			$(".paragrapheSlider ."+$(this).attr("className") ).fadeIn("slow");		
		} )
	});
	if( sSelectedOnglet)
	{	var sp=$("#sousPage"+sSelectedOnglet);
		if( sp.is('.sousPage'))
		{	var cs=sp.attr('className').split(' ')[1];
			if( cs) $(".paragrapheSliderMenu a."+cs).click();
		}
	}

	// Paragraphe plan: habillage
	$("#paragraphMapLudotheque p").prepend("<img class=\"wrapRight\" src=\"Data/Img/void.gif\" width=\"185\" height=\"119\" /><img class=\"wrapRight\" src=\"Data/Img/void.gif\" width=\"222\" height=\"111\" /><img class=\"wrapRight\" src=\"Data/Img/void.gif\" width=\"386\" height=\"106\" /><img class=\"wrapRight\" src=\"Data/Img/void.gif\" width=\"374\" height=\"41\" /><img class=\"wrapRight\" src=\"Data/Img/void.gif\" width=\"355\" height=\"30\" /><img class=\"wrapRight\" src=\"Data/Img/void.gif\" width=\"313\" height=\"98\" /><img class=\"wrapRight\" src=\"Data/Img/void.gif\" width=\"181\" height=\"44\" />");


	// IE6 patches
	if( isIE6)
		$(".paragraphNormal .noAuthor .inner").append("<br /><img src=\"Data/Img/void.gif\" width=\"1\" height=\"10px\" /> ");

	// Links that get tooltip 
	//$("a.link, .paragraph p a, a.calLnk,#menu .alevel1, #menu .alevel2").addClass('hasTip');
	$("a.link, .paragraph p a, a.calLnk").addClass('hasTip');
}
var sSelectedOnglet = "";
function sSelectOnglet(name)
{	sSelectedOnglet = name;
}

function PNGSrc(src,width,height)
{
	var s
	if(isIE6)
		s=" src=\"Data/Img/void.gif\" width=\""+width+"\" height=\""+height+"\" style=\"filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale');\"";
	else
		s="src=\""+src+"\"";
	return s;
}

function ShowNewsLetterForm()
{
	//$(".NewsletterInviteMessage").hide();
	$('#btnShowNewsletterForm').hide();
	$('#NLFormFields').show();
	$('.NLButtonSendNewsletterDiv a').addClass("link");
}

function myDocumentLoad()
{	


	sMainMenu.M_start();

	// Paragraphe photoeil
	$(".paragraphPhotoeil").addDecoration({ xLeft:505, yBottom:230, element:"<img class=\"paragraphPhotoeilDeco\" "+PNGSrc("Data/Img/photOeil.png",153,256)+" />" });


	$(".paragraphEncart .themeHoraire").each( function(){ if( !$(this).parents(".sousPage").length) $(this).addDecoration({ xLeft:97, yBottom:93, element:"<img class=\"paragraphDonaldDeco\" "+PNGSrc("Data/Img/encartDonald.png",230,173)+" />" });});
	RQTooltip.M_install("a.hasTip","rqtooltip");
}

var sStrLoading="Chargement...";

