var Ymenu = YUI( {
	base:'/scripts/yui/',
	timeout :10000
});
Ymenu.namespace("dior");
Ymenu.use("event", "node", "anim", "slider", function(Y) {

	function MenuController() {

		MenuController.superclass.constructor.apply(this, arguments);
		this.getItems();
		
		
		if(Y.get("#data") == null){
			this.showMenu();
		}
	}

	MenuController.NAME = "menucontroller";

	MenuController.ATTRS = {
		items : {
			value :null
		},
		nodeList : {
			value :null
		},
		active : {
			value :false
		},
		url:{
			value:null
		}
	}

	Y.extend(MenuController, Y.Base, {
		showMenu:function(){
			opacityInstance.showMenu();
		},
		getItems : function() {
			
			//first levels items
			this.set("nodeList", Y.get("#diorMenu").queryAll("ul.item"));
			var arrayMenuItem = [];
			for ( var i = 0; i < this.get("nodeList").size(); i++) {
				var item = new Ymenu.dior.menuItemInit( {
					node :this.get("nodeList").item(i)
				})
				item.subscribe("menuitemcontroller:menuover", function() {
					this.set("active", true)
					this.setLevel();
				}, this);
				item.subscribe("menuitemcontroller:menuout", function() {
					this.set("active", false)
					Y.later(300, this, "setNoLevel", null, false);
				}, this);
				arrayMenuItem.push(item);
			}
			this.set("items", arrayMenuItem);
			swfobject.embedSWF(this.getUrl() + "swf/storeloc_"+Y.get("#lg").get("text")+".swf",
					"dior_eshoplocator_flash", "675", "450", "9.0.0", this.getUrl() 
							+ "swf/expressInstall.swf", {}, {
								menu :"false",
								scale :"noScale",
								allowscriptaccess :"always"
							});
			
			//service levels
			var opener = Y.get("#diorMenu").queryAll("ul.service li a");
			for ( var i = 0; i < opener.size(); i++) {
				var item = new Ymenu.dior.menuSubItemInit( {
					node :opener.item(i)
				});

				this.subscribe("menucontroller:menuopen", function(e) {
					this.hide();
				}, item);
				this.subscribe("menucontroller:menuall", function(e) {
					this.selecter("block");
				}, item);
				
				opener.item(i).on("click", function(e, args){e.halt();this.fire("menuopen");args.show();}, this, item);
			}
		},
		setLevel : function() {
			this.get("nodeList").addClass("out");
			for ( var i = 0; i < this.get("items").length; i++) {
				var itemMenu = this.get("items")[i];
				if (itemMenu.get("active")) {
					itemMenu.get("node").removeClass("out");
				}
			}
		},
		setNoLevel : function() {
			if (!this.get("active")) {
				this.get("nodeList").removeClass("out");
			}
		},
		getUrl : function() {
			if (this.get("url") == null) {
				var linksNodeList = Y.all("link");
				for ( var i = 0; i < linksNodeList.size(); i++) {
					var link = linksNodeList.item(i).getAttribute('href');
					if (link.indexOf("styles") >=0) {
						this.set("url", link.substring(0, link.indexOf("styles")));
						break;
					}
				}
			}
			return this.get("url");
		},
		hideSub:function(){
			this.fire("menuopen");	
			this.fire("menuall");	
		}
	});

	function MenuItemController(config) {
		MenuItemController.superclass.constructor.apply(this, arguments);
		this.configure();
	}

	MenuItemController.NAME = "menuitemcontroller";

	MenuItemController.ATTRS = {
		node : {
			value :null
		},
		height : {
			value :0
		},
		animation : {
			value :null
		},
		animationUl:{
			value:null
		},
		active : {
			value :false
		}
	}

	Y.extend(MenuItemController, Y.Base, {
		configure : function() {
			var subItems = this.get("node").query("ul");

			if (subItems) {
				var anim = new Y.Anim( {
					node :subItems,
					duration :0.2,
					to : {},
					easing :Y.Easing.easeOut
				});
				Y.log(this.get("node"))
				var animUl = new Y.Anim( {
					node :this.get("node"),
					duration :0.2,
					to : {},
					easing :Y.Easing.easeOut
				});
				this.set("animation", anim);
				this.set("animationUl", animUl);
				subItems.setStyle("display", "block");
				this.set("height", subItems.get("offsetHeight")+23);
				subItems.setStyle("height", 0);
				this.get("node").setStyle("height", 0);
				subItems.setStyle("display", "block");
				subItems.setStyle("position", "absolute");
				subItems.setStyle("width", "400px");
				subItems.setStyle("top", "55px");
				if(subItems.hasClass("service")){
					subItems.setStyle("right", "0px");
				}else{
					subItems.setStyle("left", "0px");
				}
			}
			
			if(!this.get("node").hasClass("search")){
				this.get("node").on("mouseover", function() {
					this.show();
				}, this);
				this.get("node").on("mouseout", function() {
					this.hide();
				}, this);
			}
		},
		show : function() {
			this.set("active", true);
			if (this.get("animation") != null) {
				this.get("animation").stop();
				this.get("animation").set("to.height", this.get("height"));
				this.get("animation").run();
				this.get("animationUl").stop();
				this.get("animationUl").set("to.height", this.get("height"));
				this.get("animationUl").run();
				
			}
			this.fire("menuover");
		},
		hide : function() {
		this.set("active", false);
			if (this.get("animation") != null) {
				this.get("animation").stop();
				this.get("animation").set("to.height", 0);
				this.get("animation").run();
				this.get("animationUl").stop();
				this.get("animationUl").set("to.height", 23);
				this.get("animationUl").run();
			}
			this.fire("menuout");
		}
	});
	
	
	
	function MenuSubItemController(config) {
		MenuSubItemController.superclass.constructor.apply(this, arguments);
		this.configure();
	}
	
	MenuSubItemController.NAME = "menusubitemcontroller";
	
	MenuSubItemController.ATTRS = {
			node : {
				value :null
			},
			height : {
				value :0
			},
			animation : {
				value :null
			},
			active : {
				value :false
			},
			nodeOpen:{
				value : null
			},
			pastActive : {
				value : null
			}
	}
	
	Y.extend(MenuSubItemController, Y.Base, {
		configure : function() {
			
			this.set("nodeOpen", "#dior_"+this.get("node").get("id")+"_panel");
			
			var nodeOpen = Y.get(this.get("nodeOpen"));
			if (nodeOpen) {
				var anim = new Y.Anim( {
					node :this.get("nodeOpen"),
					duration :0.4,
					to : {},
					easing :Y.Easing.easeOut
				});
				anim.on("start", function(){this.get("node").setStyle("display", "block");})
				anim.on("end", function(){if(this.get("node").get("offsetHeight") == 0){this.get("node").setStyle("display", "none");}})
				this.set("animation", anim);
				nodeOpen.setStyle("display", "block");
				this.set("height", nodeOpen.get("offsetHeight")+30);
				nodeOpen.setStyle("overflow", "hidden");
				nodeOpen.setStyle("height", 0);
				nodeOpen.setStyle("visibility", "visible");
				nodeOpen.setStyle("display", "none");
			}
			
			
		},
		show : function() {
			if(this.get("pastActive") == true){
				this.hide();
				this.selecter("block");
				return;
			}
			
			if (this.get("animation") != null) {
				this.set("active", true);
				if(this.get("node").get("id") != "eshop")
					new Ajax.dior.io(menuInstance.getUrl()+'service/'+this.get("node").get("id"), {displayer:this.get("nodeOpen") });
				this.get("animation").stop();
				this.get("animation").set("to.height", this.get("height"));
				this.get("animation").run();
				this.selecter("none");
			}
		},
		hide : function() {
			if (this.get("animation") != null) {
				this.set("pastActive", this.get("active"));
				this.set("active", false);
				this.get("animation").stop();
				this.get("animation").set("to.height", 0);
				this.get("animation").run();
			}
		},
		selecter:function(statut){
			if(Y.UA.ie || Y.UA.ie<7){
				var select = Y.get(".infos_col");
				if(select){
					select.queryAll("select").setStyle("display", statut)
				}
				var select = Y.get("#product_detail");
				if(select){
					select.queryAll("select").setStyle("display", statut)
				}
			}
		}
	});
	
	
	function MenuSearchItemController() {
		MenuSearchItemController.superclass.constructor.apply(this, arguments);
	}

	MenuSearchItemController.NAME = "menusearchitemcontroller";

	MenuSearchItemController.ATTRS = {
		items : {
			value : null
		},
		animation : {
			value :null
		},
		height: {
			value : 0
		},
		active:{
			value : false
		}
	}

	Y.extend(MenuSearchItemController, Y.Base, {
		initializer : function(cfg) {
		/*	var item = Y.get("#diorSearch");
			this.set("items", item.queryAll("li"));
			this.get("items").on("click", function(e){this.changeState(e.target)}, this);
			item.setStyle("display", "block");
			var height = item.get("offsetHeight");
			item.setStyle("display", "none");
			this.set("height", height);
			item.setStyle("height", 0);
			item.setStyle("display", "block");
			//Y.get("#advSearch").on("click", function(){this.show()}, this);
			
			var anim = new Y.Anim( {
				node :"#diorSearch",
				duration :0.5,
				to : {},
				easing :Y.Easing.easeOut
			});

			this.set("animation", anim);
			*/
		},
		changeState:function(target){
			if(target.hasClass("no")){
				target.removeClass("no");
			}else{
				target.addClass("no");
			}
		},
		show:function(){
			if (this.get("animation") != null) {
				this.get("animation").stop();
				var to = (this.get("active") ? 0 : this.get("height"));
				this.get("animation").set("to.height", to);
				this.get("animation").run();
			}
			this.set("active", !this.get("active"))
		}
	});
	
	
	function OpacityController() {
		OpacityController.superclass.constructor.apply(this, arguments);
	}

	OpacityController.NAME = "opacitycontroller";

	OpacityController.ATTRS = {
		displayed : {
			value : false
		},
		nodeList : {
			value : null
		}
	}

	Y.extend(OpacityController, Y.Base, {
		initializer : function(cfg) {
			this.set("nodeList", Y.get(document.body).queryAll(".opacityItem"));
		
		},
		displayAll:function(){
			if(this.get("nodeList") != null){
				for(i=0; i<this.get("nodeList").size(); i++){
					var anim = new Y.Anim( {
						node :this.get("nodeList").item(i),
						to : {
							opacity : 1
						},
						easing:Y.Easing.easeOut
					})
					anim.on("end", function(){
						this.get("node").removeClass("opacityItem");
						this.get("node").removeAttribute("style");})
					anim.run();
				}
			}
		},
		showMenu:function(){
			if(Y.get("#loader") != null){
			var loaderAnim = new Y.Anim( {
				node :"#loader",
				to : {
					opacity :0
				},
				easing:Y.Easing.easeOut
			});
			loaderAnim.on("end", function() {
				var menuAnim = new Y.Anim( {
					node :"#menu",
					to : {
						xy : [ 0, 0 ]
					},
					easing:Y.Easing.easeOut
				});
				if(Y.UA.ie || Y.UA.ie<7){
					var select = Y.get(document.body).queryAll("select");
					if(select){
						select.setStyle("display", "block")
					}
				}
				menuAnim.on("end", function() {
					Y.get("#loader").get("parentNode").removeChild(Y.get("#loader"));
					if(!Y.UA.ie || Y.UA.ie>6){
						Y.get(document.body).setStyle("overflow", "auto")
					}
					this.fire("opacitycontroller:animend");
				}, this);
				menuAnim.on("start", function(){
					this.displayAll();
				}, this);
				menuAnim.run();
			}, this);
			loaderAnim.run();
			}else{
				this.fire("opacitycontroller:animend");
			}
		},
		displayFlv:function(video){
			//alert(video.substring(0,6));
			
			var ratio = "16/9";
			var playerName = "player_1024x576.swf";

			if(video.substring(0,6) == "DIOR60") {
				ratio = "4/3";
				playerName = "player.swf";
			}
			
			//alert(ratio);
			
			var flashvars = {
				videoUrl : menuInstance.getUrl()+"flv/"+video,
				flv : menuInstance.getUrl()+"flv/"+video,
				autoplay: 1,
				autoload: 1,
				margin: 0,
				showvolume: 1,
				showtime: 1,
				buffershowbg: 1,
				buffercolor: "ffffff",
				bufferbgcolor: "000000",
				format: ratio
			};

			var params = {
				menu :"false",
				scale :"noScale",
				wmode :"opaque",
				allowscriptaccess :"always"
			};
			
			var element = Y.get(document.createElement("div"));
			element.setAttribute("id", "flash_area_container");
			var bclose = Y.get("#bclose").get("text");
			bclose = bclose.toUpperCase();
			var fbclose = '<a href="javascript:void(0);" id="btnoff" class="buttonStyle" style="cursor:pointer;">'+bclose+'</a>';
			if (Y.UA.ie && Y.UA.ie < 7) {
				fbclose = '<a href="javascript:void(0);" id="btnoff"  style="cursor:pointer;"><img src="' + menuInstance.getUrl() + 'images/' + bclose + '_OFF.png" style="padding-left: 30px";></a>';
			
			}
			element.set("innerHTML", '<div id="flash_area_container_mask"></div><div id="flash_area"></div><div id="flash_btn_close" align="center">'+fbclose+'</div>');
			Y.get(document.body).appendChild(element);
			Y.get("#btnoff").on("click", function(){Y.get("#flash_area_container").get("parentNode").removeChild(Y.get("#flash_area_container"));})
			swfobject.embedSWF(menuInstance.getUrl() + "swf/"+playerName, "flash_area", "100%", "100%", "9.0.0", menuInstance.getUrl() + 
"swf/expressInstall.swf", flashvars, params);
			if(ratio == "4/3") {
				Y.get("#flash_area").setStyle("width", "768px");
				Y.get("#flash_area").setStyle("margin", "-288px 0 0 -384px");
			}			
		},

		displaySWF:function(swf_file){
			//alert(video.substring(0,6));
			

			var params = {
				menu :"false",
				scale :"noScale",
				wmode :"transparent",
				allowscriptaccess :"always"
			};
			
			swfobject.embedSWF(menuInstance.getUrl() + "swf/"+swf_file, "flash_mos", "100%", "100%", "9.0.0", menuInstance.getUrl() +  "swf/expressInstall.swf", flashvars, params);
		}
				
	});
	
	function FormController() {
		FormController.superclass.constructor.apply(this, arguments);
	}

	FormController.NAME = "formcontroller";

	FormController.ATTRS = {}

	Y.extend(FormController, Y.Base, {
		initializer : function(cfg) {
	
			var checkboxNodeList = Y.get(document.body).queryAll("label.formcontroller_checkbox_label");
			if(checkboxNodeList != null){
				Y.Event.purgeElement(checkboxNodeList, true); 
				checkboxNodeList.on("click", function(e){this.changeCheckboxState(e);}, this);
			}
			
			var selectboxNodeList = Y.get(document.body).queryAll(".formcontroller_select_label select");
			if(selectboxNodeList != null){

				Y.Event.purgeElement(selectboxNodeList, true); 
				selectboxNodeList.on("change", function(e){this.changeSelectboxState(e);}, this);
				for(var i=0; i<selectboxNodeList.size(); i++){
					var e = {target:selectboxNodeList.item(i)}
					this.changeSelectboxState(e);
				}
			}
		},
		changeCheckboxState : function(e){
			var parent = e.target;
			if(parent.hasClass("formcontroller_checkbox_label_uncheck")){
				parent.removeClass("formcontroller_checkbox_label_uncheck");
			}else{
				parent.addClass("formcontroller_checkbox_label_uncheck");
			}
			
			if(parent.query("input")){
				if(parent.query("input").getAttribute("checked")  == undefined){parent.query("input").setAttribute("checked", true);}
				parent.query("input").setAttribute("checked", !parent.query("input").getAttribute("checked") );
			}
		},
		changeSelectboxState:function(e){
			var parent = e.target.get("parentNode");
			var sel =e.target;
			parent.query(".value").set("innerHTML", sel.get("options").item(sel.get("selectedIndex")).get("text"))
		}
	});
	

	Ymenu.dior = {
		menuInit :MenuController,
		menuItemInit :MenuItemController,
		menuSubItemInit :MenuSubItemController,
		searchItemInit : MenuSearchItemController,
		opacityItems : OpacityController,
		formInit:FormController
	};

	Y.on("domready", function() {
	
		
		formInstance = new Ymenu.dior.formInit();
		if(!Y.Lang.isObject(opacityInstance)){
			opacityInstance = new Ymenu.dior.opacityItems();
		}
		if(!Y.Lang.isObject(menuInstance)){
			menuInstance = new Ymenu.dior.menuInit();
		}
	
		if(Y.get("#dior_mosaique") && !Y.get("#collection_detail")){
			mosaiqueInstance = new Ymosaique.dior.init()
		}
		
		if(Y.get("#collection_detail")){
			collectionInstance = new Ycollection.dior.init();
			collectionInstance.subscribe("collectioncontroller:end", function(){
				opacityInstance.showMenu();
			}, this);
			collectionInstance.config();
		}
		
		if(Y.get("#product_map")){
			productMapInstance = new Yproductmap.dior.init();
		}
		
		if(Y.get("#product_fiche")){
			productInstance = new Yproduct.dior.init();
		}
		/*
		var legals_slider = new Y.Slider({ 
			    axis: 'y',
			    value: 0, 
			    min : 0,
			    max:100,
			    railSize: '500px', 
			    thumbImage: menuInstance.getUrl()+'images/icons/scroller.gif' 
		}); 
		legals_slider.after("valueChange", function(e){
			var region = Y.get("#dior_legals_content").query("span").get("region");
			Y.get("#dior_legals_content").set("scrollTop", (e.newVal/100)*(region.bottom-region.top))
		}, this);
		legals_slider.render("#dior_legals_slider");
		*/
		var centeredEls = Y.get(document.body).queryAll(".centered");
		
		if(centeredEls){
			if(!Y.Lang.isObject(mapInstance)){
				mapInstance = new Ymap.dior.load();
				mapInstance.config();
			}

			Y.get(window).on("resize", function(){mapInstance.getSize()}, this)
			
		}
		
		if(Y.get("#dior_slider_static_content")){
			var legals_slider = new Y.Slider({ 
			    axis: 'y',
			    value: 0, 
			    min : 0,
			    max:100,
			    railSize: '230px', 
			    thumbImage: menuInstance.getUrl()+'images/icons/scroller2.gif' 
			}); 
			legals_slider.after("valueChange", function(e){
				var height = Y.get("#dior_slider_static_content").query("span").get("offsetHeight");
				Y.get("#dior_slider_static_content").set("scrollTop", (e.newVal/100)*(height))
			}, this);
			if(Y.get("#dior_slider_static")) {
				legals_slider.render("#dior_slider_static");
				if (Y.UA.ie > 0) {
					Y.get("#dior_slider_static").setStyle("top", "100px");
				}
			}
			else {
				legals_slider.render("#dior_slider_static2");	
			}
			
			Y.get("#dior_slider_static_content").setStyle("overflow", "hidden");
		}
	}, window);	

	
});
var menuInstance;
var opacityInstance;
var formInstance;
