var Yitem = YUI( {
	base:'/scripts/yui/',
	timeout :10000
});

Yitem.namespace("dior");
Yitem.use("base", "node", "anim", function(Y) {

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

	ItemController.NAME = "itemcontroller";

	ItemController.ATTRS = {
		priority : {
			value :'1'
		},
		xmlId : {
			value :'0'
		},
		animations : {
			value : []
		},
		size : {
			width : {
				value :'0'
			},
			height : {
				value :'0'
			},
			wsize : {
				value :'0'
			},
			hsize : {
				value :'0'
			},
			string:{
				value : "0x0"
			}
		},
		coordinates : {
			x : {
				value :'0'
			},
			y : {
				value :'0'
			}
		},
		dataItem : {
			value : null
		},
		htmlItem : {
			value:null
		},
		open : {
			value :false
		},
		preloadImg:{
			value:false
		},
		preloadImgZoom:{
			value:false
		},
		tooltip:{
			value:'0'
		}
	}

	Y.extend(ItemController, Y.Base, {
		animStart:function(timer){
			this.createInDom();
			var anim = new Yanimation.dior.init(this, this.get("animations")[timer]);
			anim.subscribe("animcontroller:deleteInDom", function(e){this.deleteInDom()}, this);
			anim.subscribe("animcontroller:startRel", function(e){
				dataInstance.pushDataItem(this.get("dataItem"));
				}, this);
			anim.run();
		},
		destructor:function(){
			this.unsubscribeAll("collectioncontroller:timeline0");
			this.unsubscribeAll("collectioncontroller:timeline1");
		},
		deleteInDom:function(){
			
			Y.Event.purgeElement(this.get('htmlItem'));
			this.get('htmlItem').get("parentNode").removeChild(this.get('htmlItem'));
			this.set("htmlItem", null);
		},
		createInDom:function(){
			if(this.get("htmlItem") == null){
				var data = dataInstance;
				var map = mapInstance;
				this.set("dataItem", data.getData(this));
				
				var pos = map.getPosition(this.get("coordinates.x"), this.get("coordinates.y"));
				var _item = Y.get(document.createElement("div"));
				_item.setAttribute("id", "container_" + this.get("xmlId"));
				_item.setStyle("position", "absolute");
				_item.setStyle("top", pos[1] + "px");
				_item.setStyle("left", pos[0] + "px");
				_item.setStyle("width", this.get("size.width") + "px");
				_item.setStyle("height", this.get("size.height") + "px");
				_item.setStyle("display", "none");
				_item.addClass("container_" + map.get("typeName") + "_Bg_Anim");
				_item.addClass("container_" + map.get("typeName") + "_"+ this.get("size.string"));
				_item.on("mouseover", function(e){
					e.stopImmediatePropagation();
					if(!this.get("open"))
						this.showTips(this.get("tooltip"));
					
					if(this.get("preloadImgZoom") != false){
						this.get("htmlItem").query(".content").setStyle("background", 'url('+this.get("preloadImgZoom").src+') no-repeat center center' );
					}
					
					this.fire("itemcontroller:stopTimeline")
					}, this);
				_item.on("mouseout", function(e){
					e.stopImmediatePropagation();
					if(this.get("open"))
						this.showTips('0');
					
					if(this.get("preloadImgZoom") != false){
						this.get("htmlItem").query(".content").setStyle("background", 'url('+this.get("preloadImg").src+') no-repeat center center' );
					}
					
					this.fire("itemcontroller:startTimeline");
					}, this);
				
				
				
				var background = this.get("preloadImg").src;
				if(!Y.Lang.isNull(this.get("dataItem"))){
					_item.set("innerHTML", '<div class="content" style="background:url('+background+') no-repeat center center">'+this.get("dataItem").get("node").get("innerHTML")+'</div>');
				}else{
					_item.set("innerHTML", this.get("xmlId"));
					_item.setStyle("visibility", "hidden");
				}
				this.set("htmlItem", _item);
				
				Y.get("#dior_mosaique").appendChild(_item);
				
				var nodeLinks = Y.get("#container_"+this.get("xmlId")).queryAll("div.links");
				if(nodeLinks){
					nodeLinks = nodeLinks.item(0);
					Y.get("#container_"+this.get("xmlId")).setStyle("display", "block");
					var region = nodeLinks.get("region");
					Y.get("#container_"+this.get("xmlId")).setStyle("display", "none");
					nodeLinks.setStyle("height", "0");
					this.set("tooltip", (parseInt(region.bottom) - parseInt(region.top)));
				}
			}
		},
		showTips:function(to){
			var nodeEl = this.get("htmlItem").queryAll(".links");
			if(nodeEl != null){
				nodeEl = nodeEl.item(0);
				var animInfos = new Y.Anim(
						{
							node:nodeEl,
							to:{
								height : to
							},
							duration:0.2,
							easing:Y.Easing.easeOut
						}		
				);
				animInfos.run();
				this.set("open", !this.get("open"))
			}
		}
	});

	Yitem.dior = {
		init :ItemController
	};

});