var Yproduct = YUI( {
	base:'/scripts/yui/',
	timeout :10000
});
Yproduct.namespace("dior");
Yproduct.use("event", "node", "anim", function(Y) {

	function ProductController() {

		ProductController.superclass.constructor.apply(this, arguments);
	}

	ProductController.NAME = "productcontroller";

	ProductController.ATTRS = {
		flashvars : {
			value :null
		},
		params : {
			value :null
		},
		animFooter : {
			value :null
		},
		currentInterval : {
			value :null
		},
		map : {
			value :null
		},
		reference : {
			value :null
		}
	}

	Y.extend(ProductController, Y.Base, {

		initializer : function(cfg) {
			var path = menuInstance.getUrl();
			this.set("map", new Ymap.dior.load());
			this.get("map").config();
			var crossCarousel = new Ycarousel.dior.init( {
				container :"#cross_carousel"
			});
			var imageCarousel = new Ycarousel.dior.init( {
				container :"#image_carousel",
				ratio:'-1'
			});

			this.set("reference", Y.get("#zoom_active").get("innerHTML"));
			

			if(Y.get("#product_picture_img")){
				
				Y.get("#product_picture_img").set("src",  path + "media/"
					+ this.get("reference") + "/" + this.get("reference")
					+ "_1_visuel_" + this.get("map").get("typeName") + ".png");
				
		/*		if(Y.UA.ie && Y.UA.ie<7){
					var ie_pic = Y.get(document.createElement("div"));
					ie_pic.setAttribute("id", "product_picture_img_ie");
					ie_pic.setStyle("width", "1px");
					Y.get("#product_picture_img").get("parentNode").appendChild(ie_pic);
					document.getElementById("product_picture_img_ie").style.filter ="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+Y.get("#product_picture_img").get("src")+"', sizingMethod='image')";
					Y.get("#product_picture_img").setStyle("display", "none");
				}*/
				
			}else{
				var views = Y.get("#image_carousel").queryAll(".carouseldata div.area a");
				if (views) {
					views.on("click", function(e) {
						e.halt();
						this.swapZoom(e.target.get("parentNode").get("parentNode"));
					}, this);
				}
			}

			this.set("flashvars", {
				little :path + "media/" + this.get("reference") + "/"
						+ this.get("reference") + "_1_visuel_L.png",
				big :path + "media/" + this.get("reference") + "/"
						+ this.get("reference") + "_1_zoom_L.png",
				padding_top :110,
				padding_right :12,
				padding_bottom :12,
				padding_left :12
			});

			this.set("params", {
				menu :"false",
				scale :"noScale",
				wmode :"transparent",
				allowscriptaccess :"always"
			});

			this.set("animFooter", new Y.Anim( {
				node :"#product_footer",
				to : {
					height :this.get("map").get("footer")
				},
				duration :0.4,
				easing :Y.Easing.easeOut
			}));

			if (Y.get("#flashZoom")) {
				swfobject.embedSWF(path + "swf/zoomer.swf", "zoom_active",
						"100%", "100%", "9.0.0", path
								+ "swf/expressInstall.swf", this
								.get("flashvars"), this.get("params"));
				this.set("currentInterval", Y.later(1000, this, "runAnim", [],
						false));
			} else {
				this.set("currentInterval", Y.later(10, this, "runAnim", [],
						false));
			}

			swfobject.embedSWF(path + "swf/storeloc_"+Y.get("#lg").get("text")+".swf",
					"eshoplocator_flash", "750", "475", "9.0.0", path
							+ "swf/expressInstall.swf", {}, this
							.get("params"));
			
			var close = Y.get(document.body).queryAll(".close")

			if (close) {
				close.on("click", function(e) {
					this.hidePanel(e.target.get("parentNode"))
				}, this);
			}
		},
		stopDrag : function() {
			this.get("animFooter").set("to.height",
					this.get("map").get("footer"));
			this.get("currentInterval").cancel();
			this.set("currentInterval", Y.later(2000, this, "runAnim", [],
					false));
		},
		startDrag : function() {
			if(Y.get("#flashZoom")){
				Y.get("#flashZoom").setStyle("background", "transparent");
			}
			this.get("currentInterval").cancel();
			this.get("animFooter").set("to.height", 0);
			this.runAnim();
		},
		hideLoader:function(){
			if(Y.get("#flashZoom")){
				Y.get("#flashZoom").removeClass("loaderIco")
			}
		},
		runAnim : function() {
			this.get("animFooter").run();
		},
		swapZoom : function(target) {
			if(Y.get("#flashZoom")){
				Y.get("#flashZoom").addClass("loaderIco");
			}
			if (target.get("nodeName") != "IMG") {
				target = target.query("img");
			}
			target.get("parentNode").get("parentNode").get("parentNode").get("parentNode").queryAll("div.area").setStyle("display", "block")
			target.get("parentNode").get("parentNode").get("parentNode").setStyle("display", "none");
			var number = target.get("className");
			var path = menuInstance.getUrl();
			this.get("flashvars").big = path + "media/" + this.get("reference")
					+ "/" + this.get("reference") + "_" + number
					+ "_zoom_L.png";
			
			/*this.get("flashvars").little = path + "media/"
					+ this.get("reference") + "/" + this.get("reference") + "_"
					+ number + "_visuel_L.png";*/

			swfobject.embedSWF(path + "swf/zoomer.swf", "zoom_active", "100%",
					"100%", "9.0.0", path + "swf/expressInstall.swf", this
							.get("flashvars"), this.get("params"));
		},
		swap : function(node1, node2) {
			var animZoom = new Y.Anim( {
				node :node1,
				to : {
					opacity :0
				},
				duration :0.4,
				easing :Y.Easing.easeOut
			});
			animZoom.on("end", function() {
				animZoom.get('node').setStyle("display", "none");
				animZoom.get('node').setStyle("left", "-100%");
				var animDetail = new Y.Anim( {
					node :node2,
					to : {
						opacity :1
					},
					duration :0.4,
					easing :Y.Easing.easeOut
				});
				animDetail.on("start", function() {
					animDetail.get('node').setStyle("left", "0");
					animDetail.get("node").setStyle("display", "block")
				}, this);
				animDetail.run();

			}, this);
			animZoom.run();
		},
		showDetail : function() {
			this.swap("#flashZoom", "#product_detail");
		},
		showZoom : function() {
			this.swap("#product_detail", "#flashZoom");
		},
		hidePanel : function(panel) {
			this.stopDrag();
			var animHide = new Y.Anim( {
				node :panel,
				to : {
					opacity :0
				},
				duration :0.4,
				easing :Y.Easing.easeOut
			});
			if (Y.get(panel).hasClass("panel")) {
				animHide.set("to.left", function(node) {
					return -node.get("offsetWidth");
				});

			} else {
				var animContent = new Y.Anim( {
					node :"#product_content",
					to : {
						opacity :1
					},
					duration :0.4,
					easing :Y.Easing.easeOut
				});
				animHide.on("end", function() {
					animHide.get("node").setStyle("display", "none");
					animContent.get("node").setStyle("display", "block");
					animContent.run();
				}, this);
			}

			animHide.run();
		},
		showPanel : function(panel) {
			this.startDrag();
			var animShow = new Y.Anim( {
				node :panel,
				to : {
					opacity :1
				},
				from : {
					opacity :0
				},
				duration :0.4,
				easing :Y.Easing.easeOut
			});
			animShow.on("start", function() {
				animShow.get("node").setStyle("display", "block")
			}, this);
			if (Y.get(panel).hasClass("panel")) {
				animShow.set("to.left", 0);
				animShow.set("from.left", function(node) {
					return -node.get("offsetWidth");
				});
				animShow.run();
			} else {
				var animContent = new Y.Anim( {
					node :"#product_content",
					to : {
						opacity :0
					},
					duration :0.4,
					easing :Y.Easing.easeOut
				});
				animContent.on("end", function() {
					animContent.get("node").setStyle("display", "none");
					animShow.run();
				}, this);
				animContent.run();
			}
		}
	});

	Yproduct.dior = {
		init :ProductController
	};


});
var productInstance;