var logoPlacer = new Class({
    initialize: function(menuObj){
        
        this.c = new Color([255, 255, 255]);
        this.menuObj = menuObj;
        Cookie.dispose('commuun');
        $("mF").addEvent("mousemove",this.changeColor.bindWithEvent(this));
        $("mF").addEvent("click",this.placeLogo.bindWithEvent(this));        
        $("mPH").setStyle("display","none");
        $$('ul.sm').destroy();
    },
    changeColor : function(ev) {
        this.c = this.c.setBrightness((ev.page.x + ev.page.y)/(window.getSize().x+window.getSize().y)*100);
        $(document.body).setStyle("background-color",this.c);
    },
    placeLogo : function(ev) {
        $("mF").setStyle("cursor","default");
        $("mF").removeEvents("mousemove");
        $("mF").removeEvents("click");
        this.c = this.c.setBrightness((ev.page.x + ev.page.y)/(window.getSize().x+window.getSize().y)*100);
        $(document.body).setStyle("background-color",this.c);
        new main({ x: ev.page.x, y : ev.page.y},this.c, this.menuObj);
    }

});
var subMenus = new Class({
    initialize: function(){
    
        if($("rPH")) {
                        if(Browser.Engine.trident && Browser.Engine.version < 7) $("mPHt").setStyle("width",window.getSize().x - 400);
            $$("div.mRc").each(function(divel,ind){
                var submenu = divel.getElement("ul.mRs");
                submenu.setStyle("display","none");                
                divel.addEvent("mouseenter", function() {submenu.setStyle("display","block")});

                divel.addEvent("mouseleave", function() {submenu.setStyle("display","none")});
                
            },this);
        }
    }
});
window.addEvent("domready",function () {new subMenus();});
var main = new Class({
    initialize: function(p,c,menuObj){
        this.fix = new fixColors({p0 : p, c0 : c})
        this.cats = [];
        this.loadMenu.delay(1000,this);
        this.menuObj = new Hash(menuObj);
        this.menuObj.each(function(objcat, keycat){
            var catob = new Hash(objcat)
            var newSub = $("sm_"+keycat)
            if(!$chk(newSub)) newSub = new Element("ul",{"id" : "sm_"+keycat,"class":"sm", "styles" : {"display" : "none"}}).inject($("m"),"after");
            catob.each(function(pageObj, pagekey){
                var li = new Element("li").inject(newSub);
                if($chk(pageObj.lbl)) new Element("a",{"href" : pageObj.url,'styles' : {'color' : this.fix.getnc()},"text" : pageObj.title}).inject(li);
                else li.addClass("desc").set("text",pageObj.title);
            }, this);
            this.menuObj.set(keycat,catob);
            if($chk($(keycat))) {
                var ob = {"ul" : newSub, "a" :$(keycat)};
                $(keycat).set({'href' : 'javascript:void(0);',
                                'alt' : '',
                               'styles' : {"border-bottom-color" : this.fix.getnc()},
                               'events' : {"click" : this.resetCats.pass(ob,this)}});
                this.cats.include(ob);
             }
        },this);
    },
    resetCats : function(curcat){
        this.cats.each(function(el){
                        el.ul.setStyle("display", "none").removeClass("active");
                        el.a.removeClass("active")});
        curcat.ul.setStyle("display", "block").addClass("active");
        curcat.a.addClass("active");
    },
    loadMenu : function() {
        if($("mPH")) $("mPH").setStyles({"display" : "block"});
    }
});
var fixColors = new Class({
        initialize : function(defaultVal){
            this.lp = new Hash();
            
            if(!$defined(defaultVal)){
                var defaultVal = {}
                defaultVal.c0 = [50,50,50];
            }
            
            var c0 = defaultVal.c0;
            if(!this.lp.get('lp')) {this.lp.set('c', c0);}
            var c = new Color(this.lp.get("c"));
            var p = 100-c.hsb[2];
            if(Math.abs(p-50) < 10) p = p > 50 ? p + 20 : p - 20;
            var nc = c.setBrightness(p);
            
            $(document.body).setStyle("background-color",c);
            
            $(document.body).setStyle("color",nc);
            
            if($("aspnetForm")) $("aspnetForm").setStyle("color",nc);
            $("lPH").setStyles({"display" : "block"});
            if($("lPH").getElements("a")) $("lPH").getElements("a").setStyles({"color" : nc});
            if($("mPH")) {
                $("mPH").setStyle("color",nc);
                $("mPH").getElements("a").each(function(el){el.setStyle("color",nc);})
            };    
            if($("rPH")) {
                $("rPH").setStyle("color",nc);
                $("rPH").getElements(".mRc ul li a").each(function(el){el.set({"title" : ""});el.setStyle("color",nc);})
                $("rPH").getElements(".mRc").each(function(cElt){
                                                        var ul = cElt.getElement(".mRs");
                                                        cElt.addEvent("mouseenter", function(){cElt.addClass("active");});
                                                        cElt.addEvent("mouseleave", function(){cElt.removeClass("active");});
                                                        })
                
            };
            this.c = c; this.nc = nc;
            new Request.HTML({url:'/ajax/fixColor.aspx'}).post({'c' : c.toString(),'nc':nc.toString()});
        },
        getc : function(){
            return this.c;
        },
        getnc : function(){
            return this.nc;
        }
})
var imgViewer = new Class({
    initialize:function(obj, curPos, imagePanel,reg){
        if(!$defined(imagePanel)) imagePanel = "imgV"
        if(!$defined(reg)) reg = "i"
        this.panel = $(imagePanel);
        this.reg = reg;
        if(!this.panel) return;
        this.imgArray = [];
        var aelt = this.panel.getElement("a")
        aelt.getElement("img").inject(this.panel,"top")
        aelt.destroy();
        obj.each(function (elt){
            this.imgArray.include(new Hash(elt));
        },this);
        this.history = new HistoryManager();
		this.history.register(this.reg,[""], this.getPos.bind(this),function(values) {
						return [this.reg, '(', values[0], ')'].join('');
					}.bind(this),this.reg + '\\((\[^)]+)\\)');
        this.cur = curPos < obj.length ? curPos : 0;
        if(this.imgArray.length > 1) this.panel.setStyle("cursor","pointer").addEvent("click",this.getNext.bind(this));
        this.history.start();
    }, 
    getPos : function (pos){
        this.panel.empty();
        pos = (pos + this.imgArray.length) % this.imgArray.length;
        if($chk(this.imgArray[pos].get("i"))) this.imgArray[pos].get("i").inject(this.panel);
        else new Element("img", {"src" : this.imgArray[pos].get("s"), "title" : this.imgArray[pos].get("t")}).inject(this.panel);
        new Element("span", {"text" : this.imgArray[pos].get("l")}).inject(this.panel);
        this.preload((pos+1) % this.imgArray.length);
        this.history.setValue(this.reg,0, pos);
        return pos;
    },
    preload : function(pos){
        if(!$chk(this.imgArray[pos].get("i"))) this.imgArray[pos].set("i", new Asset.image(this.imgArray[pos].get("s")));
    },
    getNext: function(){
        this.cur = this.getPos(this.cur+1);
    }

});
var Video = new Class({
    initialize : function (urlVideo, contId, w, h) {
        this.o =  $chk($(contId).getElement("a.img")) ? $(contId).getElement("a.img") : new Element("a",{"styles":{"width": "100%", "height" : "100%", "display":"block"}}).inject($(contId));
        this.o.addEvent("click",this.show.bind(this));
        this.h = $pick(h,240).toInt();
        this.w = $pick(w,320).toInt();
        this.c = $chk($(contId).getElement("div.vid")) ? $(contId).getElement("div.vid") : new Element("div",{"class":"vid", "styles" : {"width": this.w, "height" : this.h,"background-color" : "#000000"}}).inject($(contId));
        this.urlVideo = urlVideo;
        this.show();
    },
    show : function () {
        window.fireEvent("hideAll");
        this.o.setStyle("display", "none");
        this.c.setStyle("display", "block");
        if(Browser.Plugins.Flash.version >= 9)  new Swiff('/video.swf', {"width": this.w +"px","height": this.h + "px", vars : {'urlVideo' : this.urlVideo, 'autoPlay' : 'true','w' : this.w,'h' : this.h},params : {'scale' : 'showall','allowFullScreen' : 'true','allowScriptAccess' :'sameDomain'}}).inject(this.c);
        else new Element("a",{'text' : "please install the last flash player here","target" : "_blank","href" : "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=shockwaveFlash", "styles" : {"color" : "white", "width": "100%", "height" : "100%", "font-size" : "10px", "display" : "block", "display" : "block"}}).inject(this.c);
    }
});

