YUI().add("imageuploader",function(Y){
    var BP = BrowserPlus;

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

    imgup.NAME = "imageuploader";

    imgup.ATTRS = {
        droptarget: {
            value: "content"
        },
        phpsession: {
            value: "PHPSESSID="+Y.Cookie.get("PHPSESSID"),
            readOnly: true
        },
        url: {
            value: "/news/add/"
        }
    };

    Y.extend(imgup,Y.Widget,{
        initializer: function() {
            this.publish("initBP",{
                defaultFn: this._initBP
            });
            BP.init(Y.bind(this.BPInited,this));
        },
        destructor : function() {

        },
        renderUI : function() {
        },
        bindUI : function() {

        },
        syncUI : function() {

        },
        BPInited: function(res){
            if(res.success){
                this.fire("initBP");
            }
            else{
                Y.log(res.error);
            }
        },
        BPRequiresLoaded: function(res){
            if (res.success) {
                BP.DragAndDrop.AddDropTarget(
                    {
                        id: this.get("droptarget")
                    },
                    Y.bind(this.setDropTarget,this)
                );
            } else {
                alert("Error Loading: " + res.error);
            }
        },
        setDropTarget: function(){
            BP.DragAndDrop.AttachCallbacks({
                        id: this.get("droptarget"),
                        drop: Y.bind(this._elementsdropped,this)
                },
                function(){}
            );
        },
        _initBP: function(){
            BP.require({services: [{service: 'DragAndDrop'},{service:'FileAccess'},{service: 'Uploader'}]},Y.bind(this.BPRequiresLoaded,this));
        },
        _elementsdropped: function(arg){
            for(var i = 0;i< arg.length; i++)
                this.loadPicture(arg[i]);
        },
        UploadPicture: function(pic,text){
            BP.Uploader.upload({
                url: this.get("url"),
                files: {imagefile: pic},
                postvars: {imagetitle: text},
                cookies: this.get("phpsession")
            },function(r){
                if(r.success){
                }
                else{
                    alert("error: "+r.error);
                }
            });
        },
        loadPicture: function(pic){
            BP.FileAccess.GetURL({file:pic},Y.bind(this._newurl,this,pic));
        },
        _newurl: function(pic,url){
            if(url.success){
                var node = Y.Node.create("<div class='picupload'>");
                var img = Y.Node.create("<img src="+url.value+" />");
                var text = Y.Node.create("<input type='text' value='"+pic.name+"' />");
                var button = Y.Node.create("<input type='button' class='upload' value='upload' />");
                var cancel = Y.Node.create("<input type='button' class='cancel' value='cancel' />");
                button.on("click",Y.bind(function(){
                    this.UploadPicture(pic,text.get("value"));
                    node.remove();
                },this));
                cancel.on("click",Y.bind(function(){
                    node.remove();
                },this));
                node.append(img);
                node.append(text);
                node.append(button);
                node.append(cancel);
                this.get("contentBox").append(node);
                this.show();
            }
            else{
                alert(url.error);
            }
        }
    });
    
    Y.ImageUploader = imgup;
},"0",{
    requires: ["widget","cookie"]
    });

YUI({
    modules:{
        "gallery-lightbox":{
            name: "lightbox-js",
            type:"js",
            fullpath: "/js/gallery-lightbox-min.js",
            requires: ["widget", "widget-position-ext", "widget-stack","node","event"]
        }
    }
}).use("anim","gallery-lightbox","event",function(Y){
    Y.on("domready",function(){
        var LB = new Y.Lightbox({
            zIndex:255
        });
        LB.render();
    });
});


/**
 * @todo refaire complêtement et proprement
 */
YUI().use("anim",function(Y){
    var hidden = true;
    Y.all("div.menutoggle").on("click",function(){
        if(hidden){
            hidden = false;
            var animobj = {
                node: "div.menu",
                to:{
                    left: 0
                },
                duration: 0.1
            };
            var anim = new Y.Anim(animobj);
            anim.run();

            var animobj = {
                node: "#content",
                to:{
                    marginLeft: 250
                },
                duration: 0.1
            };
            animobj.node = "#content";
            var anim = new Y.Anim(animobj);
            anim.on("end",function(){
                Y.one("#content").setStyle("marginLeft","auto");
            });
            anim.run();


            animobj.node = "#navigbartitle";
            var anim = new Y.Anim(animobj);
            anim.run();

            animobj.node = "#subnavigbar";
            var anim = new Y.Anim(animobj);
            anim.run();
        }
        else{
            hidden = true;
            var animobj = {
                node: "div.menu",
                to:{
                    left: -250
                },
                duration: 0.1
            };
            var anim = new Y.Anim(animobj);
            anim.run();

            var animobj = {
                node: "#content",
                to:{
                    marginLeft: 0
                },
                duration: 0.1
            };
            animobj.node = "#content";
            var anim = new Y.Anim(animobj);
            anim.run();

            animobj.node = "#navigbartitle";
            var anim = new Y.Anim(animobj);
            anim.run();

            animobj.node = "div.menu";
            var anim = new Y.Anim(animobj);
            anim.run();
        }
    });
});

YUI().use("anim","cookie",function(Y){
    try{
        var small = Y.Cookie.get("resized");
        if(small){
            Y.one("#content").setStyle("width","1000px");
        }

        Y.all("#contentwidth").on('click',function(){
            if(small){
                var anim = new Y.Anim({
                    node: "#content",
                    to:{
                        width: Y.one("body").get("winWidth")
                    }
                });
                anim.on("end",function(){
                    Y.one("#content").setStyle("width","auto");
                })
                anim.run();
                small = false;
            }
            else{
                var anim = new Y.Anim({
                    node: "#content",
                    to:{
                        width: 1000
                    }
                });
                anim.run();
                small = true;
            }
            Y.Cookie.set("resized",small,{
                expires: new Date("January 12, 2025"),
                path: "/"
            });
        });
    }
    catch(e){
        Y.log(e.message);
    }
});

/*YUI().use("node",function(Y){
    var list = Y.Node.create("<ul class='tabview'></ul>");
    Y.one("#content").prepend(list);
    Y.all(".indexelement").setStyle("display","none");
    Y.all(".indexelement > h2,.indexelement > h3").batch(function(node){
        Y.log(node.get("innerHTML"));
        var id = Y.guid();
        if(node.one("a")){
            node = node.one("a");
        }
        node.ancestor(".indexelement").set("id","tab-"+id);
        list.append("<li id='"+id+"'>"+node.get("innerHTML")+"</li>");
    });
    Y.all(".tabview li").on("click",function(e){
        e.preventDefault();
        Y.all(".indexelement").setStyle("display","none");
        Y.log(e.currentTarget.get("id"));
        Y.one("#tab-"+e.currentTarget.get("id")).setStyle("display","block");
    });
});*/
/**
* GG Analytics
*/
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
try {
    var pageTracker = _gat._getTracker("UA-7271294-2");
    pageTracker._trackPageview();
} catch(err) {}


