var Ajax = new Hash({
	autoLoad: function(){
		$$(".w3bz-container[action]").each(function(container){
			var url = window["baseURI"]+container.getProperty("action");	
			new Request({
				url: url,
				method: "get",
				onRequest: function(){
					var indicator = new Element("div",{"class":"w3bz-indicator", "opacity":"0"});
					container.adopt(indicator);
					indicator.fade(0.5);
				},
			
				onSuccess: function(response) {
					container.set("html",response);
				}
			}).send();
		});		
	},
	
	link: function(url,where){
		var container = $$(where);
		new Request({
			url: window["baseURI"]+url,
            method: "get",
            
			onRequest: function(){
				container.setStyle("position","relative");
				var indicator = new Element("div",{"class":"w3bz-indicator", "opacity":"0"});
				container.adopt(indicator);
				indicator.fade(0.5);
			},
	
			onSuccess: function(response) {
				container.set("html",response);
			}
		}).send();
	}
});

var Uploader = new Hash({
    files: new Array(),
    updateUrl: null,
	swiffObj: new Swiff(window["webroot"]+'js/widgets/uploader.swf', {
		width: 200,
		height: 20,
		vars: {
			uploadURL: window["baseURI"]+"/admin/uploads/receive/sid:"+window["sessionID"]
		},
		callBacks: {
			complete: function(response){
                var nel = new Element("li");
                nel.set("html", response);
                $$(".w3bz-gallery ul").grab(nel);
                var photoId = $$(".w3bz-gallery li:last-child img").getProperty("alt");
                Uploader.files.push(photoId[0]);
                console.log(this.files);
                $("uploads").setProperty("value",Uploader.files.toString());
                if($$("form").getProperty("id")[0].indexOf("Edit") != -1){
                    $$("form").send();
                }
            }
		}
	}),

	init: function(controller,action,id){
        this.updateUrl = window["baseURI"]+"/admin/"+controller+"/"+id;
		$$("div.w3bz-uploader").adopt(this.swiffObj);
        this.files = $("uploads").getProperty("value").split(",").erase("");
	},

    remove: function(value){
        console.log(this.files);
        var vi = this.files.erase(value);
        $("uploads").setProperty("value",vi.toString());
        if($$("form").getProperty("id")[0].indexOf("Edit") != -1){
            $$("form").send();
        }
    }
});