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

Ydata.namespace("dior");
Ydata.use("base", "node", "io-base", function(Y) {

	var _this;

	var transactionData = {
		complete : function(id, o) {
		},
		success : function(id, o, args) {
			_this.loadData(o.responseXML);
		},
		failure : function(id, o) {
			alert(id + ": Transaction Event Failure.  The status text is: "
					+ o.statusText + ".");
		},
		abort : function(id) {
			alert(id + ": Transaction Event Aborted.");
		}
	}

	function DataController(url) {
		DataController.superclass.constructor.apply(this, arguments);
		var contents = Y.get("#data").queryAll('.content');
		this.set("resultSet", contents);
		/*	Y.io(url + "data/", {
			on : {
				complete :transactionData.complete,
				success :transactionData.success,
				failure :transactionData.failure
			},
			context :transactionData
		});*/
	}

	DataController.NAME = "datacontroller";

	DataController.ATTRS = {
		file : {
			value :"file"
		},
		resultSet : {
			value :null
		},
		dataSet : {
			value:[]
		},
		avaiableItems : {
			value:[]
		},
		blocItems:{
			value:[]
		}
	}

	Y.extend(DataController, Y.Base, {
		initilizer:function(cfg){
		
		},
		destructor : function() { 

			for(var i=0; i<this.get("dataSet").length; i++){
				this.get("dataSet")[i].destroy();
				delete this.get("dataSet")[i];
			}
		},
		loadData : function() {
			var tmpDataSet = [];
			var tmpBlocDataSet = [];
			if(!Y.Lang.isNull(this.get("resultSet"))){
				for ( var i = 0; i < this.get("resultSet").size(); i++) {
					var tmpDataItemNode = this.get("resultSet").item(i);
					var tmpDataItemConfig = {node:tmpDataItemNode};
					var tmpDataItemTitle = tmpDataItemNode.query("h1");
					if(tmpDataItemTitle.size()){
						tmpDataItemConfig.title = tmpDataItemTitle.item(0);
					}
					
					var tmpDataItemLink = tmpDataItemNode.query("a");
					if(tmpDataItemLink){
						tmpDataItemConfig.href = tmpDataItemLink.item(0).getAttribute("href");
					}
	
					var tmpDataItemImage = tmpDataItemNode.query("img");
					if(tmpDataItemImage.size()){
						var src =  tmpDataItemImage.item(0).getAttribute("src");
						src = src.substring(src.lastIndexOf("/")+1);
						src = src.substring(0,src.indexOf("_"));
						tmpDataItemConfig.image = src;
						if(tmpDataItemImage.item(0).hasClass("zoom")){
							tmpDataItemConfig.zoom =true;
						}
					}
					if(tmpDataItemNode.hasClass("isbloc")){
						var blocNameString = tmpDataItemNode.get("className").replace("content isbloc bloc_", "");
						tmpBlocDataSet[blocNameString] = new YdataItem.dior.init(tmpDataItemConfig);
					}else{
						tmpDataSet.push(new YdataItem.dior.init(tmpDataItemConfig));
					}
				}
			}
			var avaiableTmpDataSet = tmpDataSet.slice();
			this.set("dataSet", tmpDataSet);
			this.set("blocItems", tmpBlocDataSet);
			this.set("avaiableItems", avaiableTmpDataSet);
			this.fire("loaded");
		},
		getData : function(item){
			var toReturn = null;
			if(!Y.Lang.isUndefined(this.get("blocItems")[item.get("xmlId")])){
				toReturn = this.get("blocItems")[item.get("xmlId")];
			}else{
				if(this.get("avaiableItems").length){
					toReturn = this.get("avaiableItems")[0];
					this.get("avaiableItems").shift();
				}else{
					if(this.get("dataSet").length){
						toReturn = this.get("dataSet")[0];
					}
				}
			}
			if(!Y.Lang.isNull(toReturn)){
			toReturn.loadPicture(item);
			}
			return toReturn;
			
		},
		pushDataItem:function(dataitem){
			for(var i=0; i<this.get("blocItems").length; i++){
				if(this.get("blocItems")[i] == dataitem){
					return true;
				}
			}
			this.get("avaiableItems").push(dataitem);
		}
	});

	Ydata.dior = {
		init :DataController
	};

});
var dataInstance;