var imageColoring = new Class({
    initialize: function(curlbl) {

        var imageC = [];
        var legendC = [];
        var infoCenter = $("menu").getElement("div.infoCenter").set("id", "infoCenter");
        $("menu").getElement("div.infoLeft").set("id", "infoLeft");
        $("menu").getElement("div.infoRight").set("id", "infoRight");
        new Element("div", { "id": "bigImageContainer", "class": "booksImages" }).inject(infoCenter, "top");

        $$("div.bookPage .infoRight a").each(function(lkelt) {
            var curTrigClass = lkelt.get("class");
            lkelt.addEvents({ "mouseover": this._addLegendClass.pass([curTrigClass, "active"], this),
                //"mouseout" : this._addLegendClass.pass(["none","active"],this),
                "click": this._getBook.pass(curTrigClass, this)
            });
            if (!lkelt.hasClass("current")) lkelt.addEvent("mouseout", this._addLegendClass.pass(["none", "active"], this));
            lkelt.set("href", "javascript:void(0)");
            imageC.include(lkelt);
        }, this);
        $$("div.bookPage .infoLeft a").each(function(legendElt) {
            var curTrigClass = legendElt.get("class");
            legendElt.addEvents({ "mouseover": this._addImagesClass.pass([curTrigClass, "visited"], this),
                "mouseout": this._addImagesClass.pass(["none", "visited"], this),
                "click": this._getBook.pass(curTrigClass, this)
            });
            legendElt.set("href", "javascript:void(0)");
            legendC.include(legendElt);
        }, this);
        //$("menu").store("marginFx", new Fx.Tween(bigImageContainer, { "property": "margin-top" }));
        $("menu").store("scrollFx", new Fx.Scroll($("aspnetForm")));
        $("menu").store("imageC", imageC);
        $("menu").store("legendC", legendC);
        if ($chk(curlbl)) this._getBook(curlbl);
    },
    _addImagesClass: function(triggerclass, classNameString) {
        $("menu").retrieve("imageC").each(function(imgLk) {
            if (imgLk.hasClass(triggerclass)) imgLk.addClass(classNameString).addClass("visited");
            else imgLk.removeClass(classNameString);
        }, this)
        return;
    },
    _addLegendClass: function(triggerclass, classNameString) {
        $("menu").retrieve("legendC").each(function(imgLk) {
            if (imgLk.hasClass(triggerclass)) imgLk.addClass(classNameString);
            else imgLk.removeClass(classNameString);
        }, this)
        return;
    },
    _getBook: function(label) {
        $("bigImageContainer").empty().set("text", "loading...");
        this._addImagesClass(label, "current");
        this._addLegendClass(label, "current");
        var imageBooks = $("infoRight");
        imageBooks.getElement("a." + label).setStyle("background-position", "left top");
        var lienBooks = $("infoLeft");
        var lienBook = lienBooks.getElement("a." + label);
        if (!$chk(this.legendRequest)) this.legendRequest = new Request.HTML({ 'url': '/ajax/returnBookImages.aspx',
            'update': $("bigImageContainer")
        });
        this.legendRequest.removeEvents("success");
        this.legendRequest.addEvent("success", function() {
            if (lienBook) {
                var coord = lienBook.getCoordinates(lienBooks);
                (function(to) {
                    $("menu").retrieve("scrollFx").cancel().start(0, to);
                }).delay(200, this, coord.top + 70);
            }
        } .bind(this));
        this.legendRequest.cancel().get({ 'lbl': label });
        if (lienBook) {
            var coord = lienBook.getCoordinates(lienBooks);
            $("bigImageContainer").setStyle("margin-top", coord.top);
        } else {
            $("bigImageContainer").setStyle("margin-top", 0)
        }

    }
});

var backColoring = new Class({
    initialize: function(){
        this.c = new Color([255, 255, 255]);
        $("aspnetForm").setStyle("display","none");
        $(document.body).addEvents({"mousemove" : this.changeColor.bindWithEvent(this),
                                    "mousedown" : this.showMenu.bindWithEvent(this)});
        $(document.body).setStyle("background-color",this.c);
    },
    changeColor : function(ev) {
        this.c = this.c.setHue(ev.page.x/window.getSize().x*360).setSaturation(100 - ev.page.y/window.getSize().y*100);
        $(document.body).setStyle("background-color",this.c);
        document.bgColor = this.c;
    },
    showMenu : function(){
        if(!$chk(this.colorRequest)) this.colorRequest = new Request.HTML({'url':'/ajax/setColor.aspx'});
        this.colorRequest.cancel().get({'color': "rgb("+this.c.toString()+")"});
        $("aspnetForm").setStyle("display","block");
        $(document.body).removeEvents("mousemove");
        $(document.body).removeEvents("mousedown");
    }

});
var textPageRef = new Class({
    initialize: function() {
        var globalInfo = new Hash();
        $("menu").getElements("div.textPage").each(function(textPageElt, txtPageInd) {
            textPageElt.getElements("div.infoCenter div.contentText div.texte a[rel=internal]").each(
                function(linkElt) {
                    var lbl = linkElt.get("href").replace("#", "");
                    var curHash;
                    if (!globalInfo.get(lbl)) {
                        curHash = new Hash();
                    } else {
                        curHash = globalInfo.get(lbl);
                    }
                    var curlinks = curHash.get("links") ? curHash.get("links") : [];
                    linkElt.set("href", "javascript:void(0)");
                    linkElt.addEvent("click", this._highlightObj.pass([lbl, linkElt], this));
                    curlinks.push(linkElt);
                    curHash.set("links", curlinks)
                    globalInfo.set(lbl, curHash);
                }, this);
            textPageElt.getElements("div.infoRight div.rightPeople a.imgRef, " +
                                      "div.infoRight div.rightPeople a.peopleRef").each(
                function(linkElt) {
                    var lbl = linkElt.get("rel");
                    var curHash;
                    if (!globalInfo.get(lbl)) {
                        curHash = new Hash();
                    } else {
                        curHash = globalInfo.get(lbl);
                    }
                    if (linkElt.hasClass("imgRef")) {
                        curHash.set("src", linkElt.get("href") + "?maxwidth=589&amp;maxheight=589");
                        curHash.set("type", "image");
                    } else {
                        curHash.set("type", "people");
                    }
                    linkElt.set("href", "javascript:void(0)");
                    linkElt.removeProperty("target");

                    linkElt.addEvent("click", this._highlightObj.pass(lbl, this));
                    curHash.set("link", linkElt);
                    globalInfo.set(lbl, curHash);
                }, this);
            textPageElt.getElements("div.infoRight div.rightPeople img").each(
                function(imgElt) {
                    var lbl = imgElt.get("alt");
                    imgElt.addEvent("click", this._highlightObj.pass(lbl, this));
                }, this);
        }, this);
        $("menu").store("globalInfo", globalInfo);
        return;
    },
    _highlightObj: function() {
        var curObj = arguments && arguments[0] ? $("menu").retrieve("globalInfo").get(arguments[0]) : false;
        if (!curObj) {
            this._reset();
            return;
        }
        var linkRef = curObj.get("link");
        if (linkRef) {
            if (linkRef.hasClass("reftogHover")) {
                this._reset();
                return;
            }
        }
        this._reset();
        if (linkRef) linkRef.addClass("reftogHover");
        var caller = arguments && arguments[1] ? arguments[1] : false;
        var linksIntxt = curObj.get("links");
        // type 
        switch (curObj.get("type")) {
            case "image":
                var src = curObj.get("src");
                if (src && linksIntxt && linksIntxt.length > 0)
                    this._createImage(curObj.get("src"), caller ? caller : linksIntxt[0]);
                if (linksIntxt) {
                    linksIntxt.each(function(eltLnk) {
                        eltLnk.addClass("refHover");
                    });
                }
                break;
            case "people":
                if (linksIntxt) {
                    linksIntxt.each(function(eltLnk) {
                        eltLnk.addClass("refHover");
                    });
                }
                break;
        }
    },
    _reset: function() {
        $("menu").getElements("img.imgTextHover, a.refHover,  a.reftogHover").each(function(activeLk) {
            if (activeLk.hasClass("imgTextHover")) activeLk.destroy();
            else activeLk.removeClass("refHover").removeClass("reftogHover");
        });
    },
    _createImage: function(imgsrc, link) {
        new Element("img", { "src": imgsrc,
            "class": "imgTextHover",
            "events": { "click": this._reset.bind(this) },
            "styles": { "top": link.getCoordinates($("menu")).top, "left": (Browser.Engine.trident && Browser.Engine.version < 7) ? -40 : 0}
        }).inject($("menu"));
    }
});
