﻿ 
function fixUnload() {
        // Is there things still loading, then fake the unload event
        if (document.readyState == 'interactive') {
                function stop() {
                        // Prevent memory leak
                        document.detachEvent('onstop', stop);
 
                        // Call unload handler
                        unload();
                };
 
                // Fire unload when the currently loading page is stopped
                document.attachEvent('onstop', stop);
 
                // Remove onstop listener after a while to prevent the unload function
                // to execute if the user presses cancel in an onbeforeunload
                // confirm dialog and then presses the stop button in the browser
                window.setTimeout(function() {
                        document.detachEvent('onstop', stop);
                }, 0);
        }
};
 
function unload() {
      $(document.body).empty();
      
};
 
if(Browser.Engine.trident && Browser.Engine.version < 7) window.addEvent('unload', unload);
if(Browser.Engine.trident && Browser.Engine.version < 7) window.addEvent('beforeunload', fixUnload);
 
var menu = new Class({
    initialize: function(contentObj){
        $("menuPanel").addClass("script");
        this.menus = [];
        this.submenus = [];
        this.currentReq;
        var cO = new Hash(contentObj);
        $("menuPanel").getElements("a.mnIt").each(function(linkItem){
                            var parentLi = linkItem.getParent();
                            this.menus.push(parentLi);
                            var lbl = "";
                            if(cO.get(linkItem.get("id"))) lbl = cO.get(linkItem.get("id"));
                             if(parentLi.getElements("a.submnIt").length > 0) {
                                parentLi.getElements("a.submnIt").each(function(submenuElt){
                                        var parentSubLi = submenuElt.getParent();
                                        this.submenus.push(parentSubLi);
                                        var sublbl = "";
                                        if(cO.get(submenuElt.get("id"))) sublbl = cO.get(submenuElt.get("id"));
                                        submenuElt.set("href","javascript:void(0)").addEvent("click", function(){
                                            this._disableSubMenus();
                                            parentSubLi.removeClass("itemLink").addClass("itemLinkActive");
                                            if($chk(sublbl)) this._getPage(sublbl);
                                        }.bind(this));
                                },this);
                             }
                             linkItem.set("href","javascript:void(0)").addEvent("click", function(){
                                this._disablePrentMenus();
                                parentLi.removeClass("itemLink").addClass("itemLinkActive");
                                if($chk(lbl)) this._getPage(lbl);
                                else if(parentLi.getElement("a.submnIt"))
                                    parentLi.getElement("a.submnIt").fireEvent("click");
                                if(parentLi.getElement(".submenu")) {
                                    parentLi.getElement(".submenu").removeClass("submenu").addClass("submenuActive");
                                }
                             }.bind(this));
                         }, this);
     
    },
    _disablePrentMenus : function(){
        this.menus.each(function(elt){
                elt.removeClass("itemLinkActive").addClass("itemLink");
                if(elt.getElement(".submenuActive")) {
                   elt.getElement(".submenuActive").removeClass("submenuActive").addClass("submenu");
                } 
            },this);
    },
    _disableSubMenus : function(){
        this.submenus.each(function(elt){
                elt.removeClass("itemLinkActive").addClass("itemLink");
            },this);
    },
    _getPage : function (pageLbl){
        if(this.currentReq) this.currentReq.cancel();
        $("contentPanel").empty();
        var newDiv = new Element("div", {"id" : pageLbl, "class" : "contentpageactive"}).inject($("contentPanel"));
        this.currentReq = new Request.HTML({url:'/ajax/newReturnPage.aspx', update : newDiv}).post({"label" : pageLbl});
        
        
    },
    _showDiv : function(divtoshow){
        $("contentPanel").getElements("div.contentpageactive").removeClass("contentpageactive").addClass("contentpage");
        divtoshow.removeClass("contentpage").addClass("contentpageactive");
    }
});
var imagePage = new Class({
    initialize: function(panelId, curInd, curLeg){
        var rootDiv = $(panelId);
        this.fullDiv = rootDiv.getElement("div.imageFull").setStyle("cursor","pointer").addEvent("click", this._next.bind(this))
        this.thumbDiv = rootDiv.getElement("div.imagesThumb")
        this.legendDiv = rootDiv.getElement("div.imagesLegend")
        this.imagesArr = [];
        this.curIndex = curInd;
        this.curLegend = curLeg;
        this._showLegend(curLeg,true);
        this.thumbDiv.getElement("a.imagemoins").addEvent("click", this._prev.bind(this));
        this.thumbDiv.getElement("a.imageplus").addEvent("click", this._next.bind(this))
        this.thumbDiv.getElements("a.imageLink").each(function (thumbElt, ind){
            var legid = thumbElt.get("rel");
            var imgSrc = thumbElt.get("href");
            thumbElt.set("href","javascript:void(0)").addEvents({"mousedown":function(){this.go(ind)}.bind(this),
                                                                 "mouseenter":function(){this._showLegend(legid, false)}.bind(this),
                                                                 "mouseleave":function(){this._showLegend(this.curLegend, true)}.bind(this)
                                                                });
            this.imagesArr.push({"src":imgSrc, "leg" : legid});
        },this);
    },
    _next : function() {
        this.go(this.curIndex +1);
    },
    _prev : function() {
        this.go(this.curIndex -1);
    },
    go : function(ind){
        this.curIndex = (ind + this.imagesArr.length) % this.imagesArr.length;
        var imgobj = this.imagesArr[this.curIndex];
        
        this.fullDiv.empty();
        
        this._showLegend(imgobj.leg,true);
        new Element("img", {src : imgobj.src,"alt" : "image"}).inject(this.fullDiv);
        
    },
    _showLegend : function (legid, constant){
        this.legendDiv.getElements("div.imageLegendActive").addClass("imageLegend").removeClass("imageLegendActive");
        if($(legid)) {
            $(legid).addClass("imageLegendActive");
        }
        if(constant) this.curLegend = legid;
        
    }
    
});
