var root = this;
document.addEvent("domready", function() {
    root.load = new Fx.Tween($("load-indicator"), {link: "cancel"});
    root.load.set("opacity", 0);
    root.packageBox = new MooDialog({destroyOnHide: false});
    root.packageBox.setContent($('pack_box'));
    $('pack_reset').addEvent('click', function() {
        $('pack_form').reset();
        root.packageBox.close();
    });
});
function indicateLoad() {
    //$("load-indicator").reveal();
    root.load.start("opacity", 1)
}
function indicateFinish() {
    root.load.start("opacity", 0)
}
function indicateSuccess() {
    indicateFinish();
    root.notify.alert('{{_("Success")}}.', {
             'className': 'success'
    });
}
function indicateFail() {
    indicateFinish();
    root.notify.alert('{{_("Failed")}}.', {
             'className': 'error'
    });
}
var PackageUI = new Class({
    initialize: function(url, type) {
        this.url = url;
        this.type = type;
        this.packages = [];
        this.parsePackages();
        this.sorts = new Sortables($("package-list"), {
            constrain: false,
            clone: true,
            revert: true,
            opacity: 0.4,
            handle: ".package_drag",
            onComplete: this.saveSort.bind(this)
        });
        $("del_finished").addEvent("click", this.deleteFinished.bind(this));
        $("restart_failed").addEvent("click", this.restartFailed.bind(this));
    },
    parsePackages: function() {
        $("package-list").getChildren("li").each(function(ele) {
            var id = ele.getFirst().get("id").match(/[0-9]+/);
            this.packages.push(new Package(this, id, ele))
        }.bind(this))
    },
    loadPackages: function() {
    },
    deleteFinished: function() {
        indicateLoad();
        new Request.JSON({
            method: 'get',
            url: '/api/deleteFinished',
            onSuccess: function(data) {
                if (data.length > 0) {
                    window.location.reload()
                } else {
                    this.packages.each(function(pack) {
                        pack.close();
                    });
                    indicateSuccess();
                }
            }.bind(this),
            onFailure: indicateFail
        }).send();
    },
    restartFailed: function() {
        indicateLoad();
        new Request.JSON({
            method: 'get',
            url: '/api/restartFailed',
            onSuccess: function(data) {
                this.packages.each(function(pack) {
                    pack.close();
                });
                indicateSuccess();
            }.bind(this),
            onFailure: indicateFail
        }).send();
    },
    startSort: function(ele, copy) {
    },
    saveSort: function(ele, copy) {
        var order = [];
        this.sorts.serialize(function(li, pos) {
            if (li == ele && ele.retrieve("order") != pos) {
                order.push(ele.retrieve("pid") + "|" + pos)
            }
            li.store("order", pos)
        });
        if (order.length > 0) {
            indicateLoad();
            new Request.JSON({
                method: 'get',
                url: '/json/package_order/' + order[0],
                onSuccess: indicateFinish,
                onFailure: indicateFail
            }).send();
        }
    }
});
var Package = new Class({
    initialize: function(ui, id, ele, data) {
        this.ui = ui;
        this.id = id;
        this.linksLoaded = false;
        if (!ele) {
            this.createElement(data);
        } else {
            this.ele = ele;
            this.order = ele.getElements("div.order")[0].get("html");
            this.ele.store("order", this.order);
            this.ele.store("pid", this.id);
            this.parseElement();
        }
        var pname = this.ele.getElements(".packagename")[0];
        this.buttons = new Fx.Tween(this.ele.getElements(".buttons")[0], {link: "cancel"});
        this.buttons.set("opacity", 0);
        pname.addEvent("mouseenter", function(e) {
            this.buttons.start("opacity", 1)
        }.bind(this));
        pname.addEvent("mouseleave", function(e) {
            this.buttons.start("opacity", 0)
        }.bind(this));
    },
    createElement: function() {
        alert("create")
    },
    parseElement: function() {
        var imgs = this.ele.getElements('span');
        this.name = this.ele.getElements('.name')[0];
        this.folder = this.ele.getElements('.folder')[0];
        this.password = this.ele.getElements('.password')[0];
        imgs[3].addEvent('click', this.deletePackage.bind(this));
        imgs[4].addEvent('click', this.restartPackage.bind(this));
        imgs[5].addEvent('click', this.editPackage.bind(this));
        imgs[6].addEvent('click', this.movePackage.bind(this));
        this.ele.getElement('.packagename').addEvent('click', this.toggle.bind(this));
    },
    loadLinks: function() {
        indicateLoad();
        new Request.JSON({
            method: 'get',
            url: '/json/package/' + this.id,
            onSuccess: this.createLinks.bind(this),
            onFailure: indicateFail
        }).send();
    },
    createLinks: function(data) {
        var ul = $("sort_children_{id}".substitute({"id": this.id}));
        ul.set("html", "");
        data.links.each(function(link) {
            link.id = link.fid;
            var li = new Element("li", {
                "style": {
                    "margin-left": 0
                }
            });
            
            if (link.icon == 'arrow_right.png'){
                    link.icon = 'glyphicon glyphicon-arrow-right';
            }
            if (link.icon == 'status_downloading.png'){
                    link.icon = 'glyphicon glyphicon-cloud-download';
            }
            if (link.icon == 'status_failed.png'){
                    link.icon = 'glyphicon glyphicon-exclamation-sign';
            }
            if (link.icon == 'status_finished.png'){
                    link.icon = 'glyphicon glyphicon-ok';
            }
            if (link.statusmsg == 'queued'){
                    link.icon = 'glyphicon glyphicon-time';
            }
            if (link.icon == 'status_offline.png'){
                    link.icon = 'glyphicon glyphicon-ban-circle';
            }
            
            var html = "\n".substitute({"icon": link.icon});
            html += "{name}