var Ycarousel = YUI( {
	base:'/scripts/yui/',
	timeout :10000
});
Ycarousel.namespace("dior");
Ycarousel.use("event", "node", "anim", function(Y) {

	function CarouselController() {

		CarouselController.superclass.constructor.apply(this, arguments);
		

	}

	CarouselController.NAME = "carouselcontroller";

	CarouselController.ATTRS = {
		container:{
			value:null
		},
		containerObj:{
			value:null
		},
		arrowleft:{
			value:null
		},
		arrowright:{
			value:null
		},
		mask:{
			value:null
		},
		maskcontainer:{
			value:null
		},
		width:{
			value:null
		},
		ratio:{
			value:0
		}
	}

	Y.extend(CarouselController, Y.Base, {
		
		initializer:function(cfg){
			this.set("containerObj", Y.get(this.get("container")));
			if(this.get("containerObj") != null){
				this.set("arrowleft", this.get("containerObj").query(".arrowleft"));
				this.set("arrowright", this.get("containerObj").query(".arrowright"));
				this.set("mask", this.get("containerObj").query(".carousel"));
				this.set("maskcontainer", this.get("containerObj").query(".carouseldata"));
				var itemList = this.get("maskcontainer").queryAll("div.area");
				if(itemList != null){
					var width = itemList.item(0).get("offsetWidth")+2;
					if(parseInt(this.get("ratio")) !=0)
						itemList.item(0).setStyle("display", "none");
					this.set("width", width*(itemList.size()+parseInt(this.get("ratio"))));
					this.get("maskcontainer").setStyle("width", this.get("width")+"px");
					this.get("arrowleft").on("click", function(event){this.slide(-width); event.preventDefault(); }, this);
					this.get("arrowright").on("click", function(event){this.slide(width); event.preventDefault(); }, this);
					this.checkPosition();
				}else{
					this.get("containerObj").setStyle("opacity", "0");
				}
			}
		},
		slide:function(value){
			var node = this.get("container")+" .carousel";
			var animSlide = new Y.Anim( {
				node :node,
				to : {
					scroll:function(node){
						return [node.get("scrollLeft") + value, 0]
						}
					},
				duration : 0.2,
				easing:Y.Easing.easeOut
			});
			animSlide.on("end", function(){this.checkPosition()}, this);
			animSlide.run();
		},
		checkPosition:function(){

			this.get("arrowleft").setStyle("opacity", "1");
			this.get("arrowright").setStyle("opacity", "1");
			
			if(this.get("mask").get("scrollLeft") == 0){
				this.get("arrowleft").setStyle("opacity", "0");
			}
			
			Y.log(this.get("mask").get("offsetWidth") + " => "+this.get("width"))
			if((this.get("mask").get("scrollLeft") + this.get("mask").get("offsetWidth") ) == this.get("width") || this.get("mask").get("offsetWidth")>this.get("width")){
				this.get("arrowright").setStyle("opacity", "0");
			}
			
		}
	});

	Ycarousel.dior = {
		init :CarouselController
	};
});