/*
 * jQuery Color Animations
 * Copyright 2007 John Resig
 * Released under the MIT and GPL licenses.
 */
 
 

(function(jQuery){
	
	// We override the animation for all of these color styles
	jQuery.each(['backgroundColor', 'borderBottomColor', 'borderLeftColor', 'borderRightColor', 'borderTopColor', 'color', 'outlineColor'], function(i,attr){
		jQuery.fx.step[attr] = function(fx){
			if ( fx.state == 0 ) {
				fx.start = getColor( fx.elem, attr );
				fx.end = getRGB( fx.end );
			}

			fx.elem.style[attr] = "rgb(" + [
				Math.max(Math.min( parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1]), 255), 0),
				Math.max(Math.min( parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2]), 255), 0)
			].join(",") + ")";
		}
	});

	// Color Conversion functions from highlightFade
	// By Blair Mitchelmore
	// http://jquery.offput.ca/highlightFade/

	// Parse strings looking for color tuples [255,255,255]
	function getRGB(color) {
		var result;

		// Check if we're already dealing with an array of colors
		if ( color && color.constructor == Array && color.length == 3 )
			return color;

		// Look for rgb(num,num,num)
		if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color))
			return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

		// Look for rgb(num%,num%,num%)
		if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color))
			return [parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55];

		// Look for #a0b1c2
		if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color))
			return [parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16)];

		// Look for #fff
		if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color))
			return [parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16)];

		// Otherwise, we're most likely dealing with a named color
		return colors[jQuery.trim(color).toLowerCase()];
	}
	
	function getColor(elem, attr) {
		var color;

		do {
			color = jQuery.curCSS(elem, attr);

			// Keep going until we find an element that has color, or we hit the body
			if ( color != '' && color != 'transparent' || jQuery.nodeName(elem, "body") )
				break; 

			attr = "backgroundColor";
		} while ( elem = elem.parentNode );

		return getRGB(color);
	};
	
	// Some named colors to work with
	// From Interface by Stefan Petre
	// http://interface.eyecon.ro/

	var colors = {
		aqua:[0,255,255],
		azure:[240,255,255],
		beige:[245,245,220],
		black:[0,0,0],
		blue:[0,0,255],
		brown:[165,42,42],
		cyan:[0,255,255],
		darkblue:[0,0,139],
		darkcyan:[0,139,139],
		darkgrey:[169,169,169],
		darkgreen:[0,100,0],
		darkkhaki:[189,183,107],
		darkmagenta:[139,0,139],
		darkolivegreen:[85,107,47],
		darkorange:[255,140,0],
		darkorchid:[153,50,204],
		darkred:[139,0,0],
		darksalmon:[233,150,122],
		darkviolet:[148,0,211],
		fuchsia:[255,0,255],
		gold:[255,215,0],
		green:[0,128,0],
		indigo:[75,0,130],
		khaki:[240,230,140],
		lightblue:[173,216,230],
		lightcyan:[224,255,255],
		lightgreen:[144,238,144],
		lightgrey:[211,211,211],
		lightpink:[255,182,193],
		lightyellow:[255,255,224],
		lime:[0,255,0],
		magenta:[255,0,255],
		maroon:[128,0,0],
		navy:[0,0,128],
		olive:[128,128,0],
		orange:[255,165,0],
		pink:[255,192,203],
		purple:[128,0,128],
		violet:[128,0,128],
		red:[255,0,0],
		silver:[192,192,192],
		white:[255,255,255],
		yellow:[255,255,0]
	};
	
})(jQuery);


(function($) {
	$.fn.menuPolygone = function(options){
		var defaults = {
			appear_speed: 300,
			next_speed: 10,
			open_speed: 300,
			close_speed: 200,
			subAppear: 200,
			subDisappear: 50,
			subNext_speed: 200,
			url_param: "",
			defaultLang : "fr",
			defaultSelected : 0
		};
				
		var opt = $.extend({}, defaults, options);
		
		var obj = $(this);
		
		/* Definition des variables du script */
		var urlParam="";
		var defaultLang = opt.defaultLang // fr - en - nl
				
		// On attribut un name à chaque menu
		$(" > li > a", obj).each(function(i){
			$(this).attr("name","menu_"+i);
			//alert($(this).attr('name'));
		});
		
		// On donne un attribue "show" à la barre MYC
		$("#makeYourChoice").attr("show","false");
		
		// On crée et on cache la div de résulat de MYC
		$("<div id='makeResult' name='close'></div>").appendTo("#makeYourChoice");
		$("#makeResult").css("display","none");
		$("#subMYC").attr("name","close");
				
		// On lance un evenement "click" sur le menu
		$(" > li > a", obj).click(function(event){	
			$("a.selectedLava").removeClass("selectedLava");
			$(this).addClass("selectedLava");
			var thisName = $(this).attr("name");
			// Si le menu est différents de la home et de contact on fait apparaitre MYC
			var lang = $(this).attr("lang");
			var link = $(this).attr("lienPerso");

			showMYC(thisName, lang);
				
			/*if(thisName == "menu_0"){
				var lang = $(this).attr("lang");
				var link = $(this).attr("lienPerso");

				showMYC(thisName, lang);
			} else if(thisName == "menu_1"){
				var lang = $(this).attr("lang");
				var link = $(this).attr("lienPerso");

				showMYC(thisName, lang);
			}
			else if(thisName == "menu_1"){
				var lang = $(this).attr("lang");
				var link = $(this).attr("lienPerso");

				showMYC(thisName, lang);
			}
			else if(thisName != "menu_5"){
				var lang = $(this).attr("lang");
				var link = $(this).attr("lienPerso");

				showMYC(thisName, lang);
			
			} else {
				var lang = $(this).attr("lang");
				var link = $(this).attr("lienPerso");

				showMYC(thisName, lang);
				
				// On arrete la propagation du lien
				return false;
			}*/
			// Si on a cliquez sur la home ou sur contact, on suit juste le lien			
		});
		
		selectGoodMenu(opt.url_param);
		function selectGoodMenu(url){
			var aUrlParam = urlToArray(url);
			var num = 0;
			switch(aUrlParam[0]){
				case "group":
				case "groupe":
				case "groep":
					num = 1;
				break;
				
				case "achievements":
				case "realisations":
				case "realisaties" :
					num = 2;
				break;
				
				case "investors":
				case "investisseurs":
				case "investeerders" :
					num = 3;
				break;
				
				case "contact":
					num = 5;
				break;
	
				default:
					num = 0;
					//setAddress(defaultLang);
				break;
			}
			$(" > li > a", obj).eq(num).click();
			
		}
		function hightlightSubMenu(){
			var aUrlParam = urlToArray(opt.url_param);
			var goodUrl = defaultLang+"/"+aUrlParam[0]+"/"+aUrlParam[1];
			$("#subMYC >a").each(function(){
				if($(this).attr("href") == goodUrl){
					$(this).addClass("subOver");
				}
			});
		}
		
		
		/* Déclaration des fonctions */
		function showMYC(menuName, lang){
			var objMYC = $("#makeYourChoice");
			if(objMYC.attr("show") == "false"){
				objMYC.animate({"opacity":"1"},opt.appear_speed,"linear",function(){showSubMenu(menuName,opt.appear_speed,lang)}).attr("show","true");
			} else {
				objMYC.animate({"opacity":"1"},opt.subAppear,"linear",function(){showSubMenu(menuName,opt.subAppear,lang)});	
			}
		}
		function hideMYC(){
			var objLink = $("#subMYC");
			
			var objLinkChild = 0;
			// On verifie s'il y a deja quelque chose à l'interieur
			if(objLink.attr("name") == "open"){
				var objLinkChild = objLink.children().size();
				//alert(speed);
				var speed = speed*objLinkChild;
				//alert(speed);
				$(" > a", objLink).each(function(i){
					var diff = objLinkChild - i;
					var time = diff*opt.subDisappear;
					$(this).animate({"opacity":"1"},time,"linear",function(){
						$(this).animate({"opacity":"0"},opt.subDisappear);
					});
				});
				objLink.attr("name","close");
			}
			var objMYC = $("#makeYourChoice");
			objMYC.animate({"opacity":"0"},opt.appear_speed,"linear",function(){}).attr("show","false");
		}
		
		function loadLinkMenu(lang,titre){
		
		}
		
		function hideSubMenu(){
			var objLink = $("#subMYC");
			
			var objLinkChild = 0;
			// On verifie s'il y a deja quelque chose à l'interieur
			if(objLink.attr("name") == "open"){
				var objLinkChild = objLink.children().size();
				//alert(speed);
				var speed = speed*objLinkChild;
				//alert(speed);
				$("#makeYourChoice").animate({"backgroundColor":"#444"},500);
				$(" > a", objLink).each(function(i){
					var diff = objLinkChild - i;
					var time = diff*opt.subDisappear;
					$(this).animate({"opacity":"1"},time,"linear",function(){
						$(this).animate({"opacity":"0"},opt.subDisappear);
					});
				});
				objLink.attr("name","close");
			}	
			
			return objLinkChild;
		}
		
		function showSubMenu(menu,speed,lang){
			//alert('result');
			hideResult(opt.close_speed);
			// On déclare une variable pour cibler #subMYC
			var objLink = $("#subMYC");		
			var objLinkChild = hideSubMenu();
			$("#makeYourChoice").animate({"backgroundColor":"#212020"},500);
			objLink.animate({"opacity":"1"},(objLinkChild * opt.subDisappear)+opt.subNext_speed,function(){
				// On lui met un display none
				objLink.css("display","none");
				
				objLink.empty();
				
				$.ajax({
					type: "GET",
					url: "lang/"+lang+".php",
					data: "menu="+menu,
					success: function(result){
						
						objLink.append(result);
						//alert(result);
						hightlightSubMenu();
						$(" > a", objLink).css("opacity","0");
						objLink.css("display","inline");
						
						$(" > a", objLink).each(function(i){
							if(menu == "menu_2"){$(this).attr("name","sub_"+i).click(function(){
									showResult("sub_"+i, $(this).attr("href"));
									return false;
								});
							}
							var time = i*opt.subAppear;
							$(this).animate({"opacity":"0"},time,"linear",function(){
								$(this).animate({"opacity":"1"},opt.subAppear);
							});
							
						});
						objLink.attr("name","open").removeClass().addClass(menu);			     
					}
				 });	

				
			});
		}
		
		function showResult(menu,link){
				waitingBar(true);
				hideResult("200");
				var objMake = $("#makeResult");
				$("select").css("display","none");
				objMake.animate({"opacity":"1"},opt.next_speed,function(){
				//objMake.empty();
					switch(menu){
						case "sub_0":		// Par clients
							var theType = "client";
						break;
						
						case "sub_1":		// Par réalisations
							var theType = "expertise_expertise";
						break;
						
						case "sub_2":		// Par secteurs
							var theType = "campagne_secteur";
						break;
					}
					
					$.ajax({
						type: "POST",
						cache: true,
						url: "data-MYC.php",
						data: "type="+theType+"&lang="+defaultLang+"&link="+link,
						success: function(result){
							
							objMake.append(result);		
							objMake.slideDown(opt.open_speed,function(){
								waitingBar(false);
							}).attr("name","open");	
							objMake.mouseleave(function(){
								hideResult("200");
							});   
						}
					 });			
				
				});

		} 
		function hideResult(speed){
			$("select").css("display","inline");
				var objMake = $("#makeResult");
				if(objMake.attr("name") == "open"){
					objMake.slideUp(speed);
					objMake.animate({"opacity":"1"},speed,"linear",function(){
						objMake.empty().attr("name","close");
						
					});
					
					
				}
		}
		
		function loadContent(type){
			switch(type){
				case 1:
					$("#content").empty().append(aUrlParam[1]);
				break;
				case 2:
					//$("#content").append(urlParam);
				break;
				
			}
			//alert(aUrlParam);
			setTitle("Realisations");
			//alert(window.location.hash);
		}
		
		function waitingBar(show){
			if(show){
				$("<div id='waiting'></div>").appendTo("#makeYourChoice");
				$("<img src='images/waiting.gif'><span>Loading...</span>").appendTo("#waiting");
				$("#waiting").css({
					"background":"#000",
					"position":"absolute",
					"top":"0",
					"width":"100%",
					"height":"35px",
					"line-height":"25px",
					"color":"#FFF",
					"opacity":"0.7",
					"text-align":"left"
				});
				$("#waiting > img").css({
					"margin-top":"10px",
					"margin-left":"550px"
				});	
				
				$("#waiting > span").css({
					"padding":"0 20px 0 10px"
				});			
				
			}else {
				$("#waiting").remove();			
			}
		}
		//waitingBar(true);
		
		
	};
})(jQuery);