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

Ycollection.namespace("dior");
Ycollection.use("base", "io-base", "event", function(Y) {

	var transactionCollection = {
		complete : function(id, o) {
		},
		success : function(id, o, args) {
			Y.get("#data").set("innerHTML", o.responseText);
			collectionInstance.swap();
		},
		failure : function(id, o) {
			alert(id + ": Transaction Event Failure.  The status text is: "
					+ o.statusText + ".");
		},
		abort : function(id) {
			alert(id + ": Transaction Event Aborted.");
		}
	}

	var cfg = {
		on : {
			complete :transactionCollection.complete,
			success :transactionCollection.success,
			failure :transactionCollection.failure
		},
		context :transactionCollection
	};

	function CollectionController(url) {
		CollectionController.superclass.constructor.apply(this, arguments);
	}

	CollectionController.NAME = "collectioncontroller";
	CollectionController.ATTRS = {
		url : {
			value:null
		},
		xmlRoot : {
			value :''
		},
		items : {
			value : []
		},
		init:{
			value:true
		},
		template : {
			value : [
			         {
			        	 id:4,
			        	 size:{w:12 , h:16},
			        	 animations:[{from:{x:-4, y:8},to:{x:0, y:8}, effect:"EffectFadeInPos"},{to:{x:-4, y:8}, effect:"EffectFadeOutPos"}]
			         },
			         {
			        	 id:2,
			        	 size:{w:12 , h:16},
			        	 animations:[{from:{x:12, y:-5},to:{x:12, y:0}, effect:"EffectFadeInPos"},{to:{x:12, y:-5}, effect:"EffectFadeOutPos"}]
			         },
			         {
			        	 id:1,
			        	 size:{w:15 , h:22},
			        	 animations:[{from:{x:24, y:2},to:{x:24, y:-3}, effect:"EffectFadeInPos"},{to:{x:24, y:2}, effect:"EffectFadeOutPos"}]
			         },
			         {
			        	 id:3,
			        	 size:{w:12 , h:16},
			        	 animations:[{from:{x:39, y:2},to:{x:39, y:7}, effect:"EffectFadeInPos"},{to:{x:39, y:2}, effect:"EffectFadeOutPos"}]
			         },
			         {
			        	 id:5,
			        	 size:{w:12 , h:16},
			        	 animations:[{from:{x:55, y:-2},to:{x:51, y:-1}, effect:"EffectFadeInPos"},{to:{x:55, y:-2}, effect:"EffectFadeOutPos"}]
			         }
			         ]
		},
		page:{
			value :1
		},
		formParam:{
			value:null
		}
	}


	Y.extend(CollectionController, Y.Base, {
		initializer:function(cfg){
			mapInstance = new Ymap.dior.load();
			mapInstance.set("xboxnum",  63);
			mapInstance.set("top",  260);
			mapInstance.config();
			dataInstance = new Ydata.dior.init(menuInstance.getUrl());

			Y.get("#prev").on("click", function(){this.page((this.get("page")-1))}, this);
			Y.get("#next").on("click", function(){this.page((this.get("page")+1))}, this);
			Y.get(document.body).queryAll(".formcontroller_select_label select").on("change", function(){this.page(1)}, this);
			Y.get(document.body).queryAll("a.filter").on("click", function(e){this.setOption(e.target);this.page(1)}, this);
			this.subscribe("loaded", function(){this.setCollectionItems();}, this);
			this.subscribe("endloaded", function(){
				this.pagination();
				opacityInstance.subscribe("opacitycontroller:animend", function(){this.fire("collectioncontroller:timeline0");}, this);
				opacityInstance.showMenu();
				}, this);
		},
		loadData:function(){
			Y.io(menuInstance.get("url") + "collection/data/"+this.get("page")+(this.get("formParam") != null ? this.get("formParam") : '' ), cfg);
		},
		setOption:function(target){
			if(target.hasClass("activefilter")){
				target.removeClass("activefilter")
			}else{
				Y.get(document.body).queryAll("a.filter").removeClass("activefilter");
				target.addClass("activefilter")
				
			}
		
		},
		setFormParam:function(){
			var array_get = [];
			this.set("formParam", null);
			
			var taille = Y.get("#taille_collection").get("options").item(Y.get("#taille_collection").get("selectedIndex")).get("value");
			if(taille != -1){
				array_get.push("size="+taille);
			}
			
			var matiere = Y.get("#matiere_collection").get("options").item(Y.get("#matiere_collection").get("selectedIndex")).get("value");
			if(matiere != -1){
				array_get.push("materials="+matiere);
			}
			
			var mouvement = Y.get("#mouvement_collection").get("options").item(Y.get("#mouvement_collection").get("selectedIndex")).get("value");
			if(mouvement != -1){
				array_get.push("mouvement="+mouvement);
			}
			
			var functions = Y.get("#fonction_collection").get("options").item(Y.get("#fonction_collection").get("selectedIndex")).get("value");
			if(functions != -1){
				array_get.push("function="+functions);
			}
			
			var filter = Y.get(document.body).query("a.activefilter");
			if(!Y.Lang.isNull(filter)){
				array_get.push("filter="+filter.get("id"));
			}
			
			
			if(array_get.length){
				this.set("formParam", "?"+array_get.join("&"));
			}
			
		},		
		setCollectionItems:function () {
			var map = mapInstance;
			items = this.get("template");
			var arrayItems=[];
			for ( var i = 0; i < items.length; i++) {
				var itemObj = new Yitem.dior.init();
				var template = items[i];
				itemObj.set("xmlId",  template.id);
				
				var animation = template.animations;
				var animArray = [];
				for(var j=0; j<animation.length; j++){
					
					var from = animation[j].from;
					var fromObj = {};
					if(!Y.Lang.isUndefined(from)){
						fromObj = from;
						if(j==0){
							itemObj.set("coordinates", {});
							itemObj.set("coordinates.x",fromObj.x);
							itemObj.set("coordinates.y",fromObj.y);
						}
					}
					
					var to = animation[j].to;
					var toObj = {};
					if(!Y.Lang.isUndefined(to)){
						toObj = to;
					}
					
					animArray[j] = {
						start:		j,
						duration:	0.4,
						effect:		animation[j].effect,
						from:		fromObj,
						to:			toObj
					};
				    var	event = "collectioncontroller:timeline"+j;
					this.subscribe(event, function(e, args){this.animStart(args)}, itemObj,	j);
				}
				itemObj.set("animations" , animArray);

				var size = template.size
				if(!Y.Lang.isUndefined(size)){
					itemObj.set("size", {});
					itemObj.set("size.width",Math.round(size.w * map.get("xbox")));
					itemObj.set("size.wsize",size.w);
					itemObj.set("size.height",Math.round(size.h* map.get("ybox")));
					itemObj.set("size.hsize",size.h);
					itemObj.set("size.string",size.w+"x"+size.h);
				}
				 arrayItems.push(itemObj);
			}
			this.set("items",arrayItems);
			this.fire("endloaded");
			
			
		},
		config:function(){
			dataInstance.on("loaded", function() {
				this.fire("loaded");
			}, this);
			dataInstance.loadData();
		},
		page:function(page){
			this.setFormParam();
			this.set("page", page);
			this.fire("collectioncontroller:timeline1");
			Y.later(450, this, "loadData", [], false)
		},
		swap:function(){
			for(var i=0; i<this.get("items").length; i++){
				this.get("items")[i].destroy();
				delete this.get("items")[i];
			}
			
			this.unsubscribeAll("collectioncontroller:timeline0");
			this.unsubscribeAll("collectioncontroller:timeline1");
			this.unsubscribeAll("endloaded");
			this.unsubscribeAll("loaded");

			this.subscribe("loaded", function(){this.setCollectionItems();}, this);
			this.subscribe("endloaded", function(){
				this.pagination();
				this.fire("collectioncontroller:timeline0");
			}, this);
			
			dataInstance.destroy();
			delete dataInstance;
			
			dataInstance = new Ydata.dior.init(menuInstance.getUrl());
			this.config();
		},
		pagination:function(){

			var totalNumber = Y.get("#collection_detail").query(".number").get("text");
			var totalPage = Math.ceil(totalNumber / 5);
			if(this.get("page") ==1 ){
				Y.get("#prev").setStyle("display", "none");
			}else{
				Y.get("#prev").setStyle("display", "inline");
			}
		
			Y.get("#collection_detail div.pagination span").set("innerHTML", "");
			for(var i=1; i<=totalPage; i++){
				var page = Y.get(document.createElement("a"));
				page.setAttribute("href", "javascript:void(0)");
				page.setAttribute("class", "page");
				page.set("innerHTML", i);
				
				if(this.get("page") == i){
					page.addClass("active");
				}else{
					page.addClass("nonactive");
				}
				
				page.on("click", function(e, args){this.page(args)}, this, i);
				
				Y.get("#collection_detail div.pagination span").appendChild(page);
			}
			
			if(this.get("page") ==totalPage ||  totalNumber ==0){
				Y.get("#next").setStyle("display", "none");
			}else{
				Y.get("#next").setStyle("display", "inline");
			}
		}
	})

	Ycollection.dior = {
		init :CollectionController
	};
});
var collectionInstance;