$(document).ready(function() {
	
	Media('coverflow','<object id="coverflow-flash" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="100%" height="100%"><param name="movie" value="/Portal_MenuFlash/coverflow.swf"><param name="allowScriptAccess" value="always"><param name="wmode" value="transparent"><param name="quality" value="high"><embed id="coverflow-flash" src="/Portal_MenuFlash/coverflow.swf" allowScriptAccess="always" wmode="transparent" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"  width="100%" height="100%"></embed></object>');
	
	if ($("#animation")) {
		$("#animation a").click(function(){
			var img = $("<img src='" + $(this).attr("rel") + "' />");
			$("#animation-image").html(img);
			$("#animation li").removeClass("selected");
			$(this).parent().addClass("selected");
			return false;
		});
	}
	
	var _current = 0;
	var _data;
	var canv;
	
	function getColor()
	{
		var color = new Array();
		color.push("#aba2b5");
		color.push("#e4e87f");
		return color[Math.round(Math.random())];
	}
	
	if ($("#clouds").length > 0)
	{
		var u = true;
		$("#clouds").append($("<div class='globe' />"));
		$("#clouds").parseClouds({complete:function(object){
			for (var i=0; i<object.length; i++)
			{
				var ob = object[i];
				var a = $("<span />")
					.css('font-size', ob.taille)
					.css('color', getColor())
					.html(ob.titre + " ");
				if (u)
				{
					$("#cloud-1").append(a);
				}
				else
				{
					$("#cloud-2").append(a);
				}
				u = !u;
			}
		}});
	}
	
	if ($("#ref-label").length > 0) {
		var champs = new Array();
		$("body").parseAnimation({
			url: '/Portal_Bandeaux/fluxxml.asp?zoneid=2',
			champs: 1,
			complete: function(object){
				_data = object;
				canv = $("<div class='ref-animation-2' />");
				for (var i=0; i<object.length; i++)
				{
					var ob = object[i];
					var html = "<div class='ref-animation'>";
					html += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
					html += '<tr>';
					html += '<td align="left" valign="top">&nbsp;</td>';
					html += '<td>&nbsp;</td>';
					html += '<td>&nbsp;</td>';
					html += '</tr>';
					html += '<tr>';
					html += '<td width="130" rowspan="2" align="left" valign="top"><img alt="" src="' + ob.image + '" /></td>';
					html += '<td width="10">&nbsp;</td>';
					html += '<td width="150">&nbsp;</td>';
					html += '</tr>';
					html += '<tr>';
					html += '<td align="left" valign="top">&nbsp;</td>';
					html += '<td align="left" valign="top">';
					html += ob.texte;
					html += '</tr>';
					html += '</table>';
					html += '</div>';
					canv.append(html);
				}
				$("#animation-ref").html($("<div id='ref-animation-canvas' />").html(canv));
				
				$(".ref-animation-2").css("width", 200 + (280 * object.length));
				var d = _data[_current];
				$("#ref-label").html(d.titre + "<br /><strong>" + d.description0 + "</strong>");
				$("#animation-ref").delay(d.delay * 1000, function(){
					move();
				});
			}
		});
		
	}
	
	function move()
	{
		_current++;
		$("#ref-label").html("");
		if (_current >= _data.length)
		{
			_current = 0;
		}
		var d = _data[_current];
		
		$(".ref-animation-2").animate({
			left: -(_current * 278)
		}, 1000, function(){
			$("#ref-label").html(d.titre + "<br /><strong>" + d.description0 + "</strong>");
		});
		
		$("body").delay(d.delay * 1000, function(){
			move();
		});
	}
	
	$(".navigation").parseMenu({
		url: '/Portal_MenuFlash/menu_fluxxml.asp',
		complete: function(object){
			var ul = $("<ul />");
			var u = "1";
			
			for (var i=0; i<object.length; i++)
			{
				var item = object[i];
				var li = $("<li />");
				var div = $("<div />").addClass("handle");
				var div2 = $("<div />");
				var a = $("<a />").html(item.text);
				if (item.url)
				{
					a.attr("href", item.url);
				}
				
				if (item.selected)
				{
					u = i + 1;
					a.addClass('active');
				}
				
				li.html(div.html(div2.html(a)));
				
				if (item.child)
				{
					var ul2 = $("<ul />").addClass("child");
					for (var j=0; j<item.child.length; j++)
					{
						var item2 = item.child[j];
						var a = $("<a />").html(item2.text).attr("href", item2.url)
						if (item2.selected)
						{
							u = i + 1;
							a.addClass('active');
						}
						ul2.append($("<li />").html(a));	
					}
					li.append(ul2);
				}
				ul.append(li);

			}
			$('#navigation').html(ul.addClass("navigation"));

			$(".navigation").hrzAccordion({
				containerClass : 		"container",
				listItemClass : 		"listItem",					
				contentWrapper : 		"contentWrapper",
				contentInnerWrapper : 	"contentInnerWrapper",
				handleClass : 			"handle",
				handleClassOver : 		"handleOver",
				handleClassSelected	: 	"handleSelected",
				handlePosition		: 	"left",
				openOnLoad			: 	u,
				fixedWidth			:	141
			});
			
			$(".handle a").click(function(){
				var str = $(this).attr('href');
				var isSelected = false;
				
				if ($(this).hasClass("active"))
				{
					isSelected = true;
				}
				
				if (str != "#")
				{
					if (isSelected)
					{
						return true;
					}
					else
					{
						if (str)
						{
						document.location.href = str;
						}
						else
						{
							$(this).parent().parent().parent().trigger('click');
						}
					}
					return false;
				}
				else
				{
					$(this).parent().parent().parent().trigger('click');
					return false;
				}
			});
		}
	});
	
	$.fn.delay = function(delay, method){
	    var node = this;
	    if (node.length){
	        if (node[0]._timer_) clearTimeout(node[0]._timer_);
	        node[0]._timer_ = setTimeout(function(){ method(node); }, delay);
	    }
	    return this;
	};

});
