﻿var rollover = new Class ({
    initialize: function(){
        this.elt = $$("a.bgimage");
        //console.log(this.elt);
        //this.rollimage = elt.get("rel");
        var arrayImg = [];
        this.elt.each(function (elt){
            var relimg = elt.get("rel")
            if (!$chk(relimg))  
                return;
            arrayImg.push(relimg) 
            elt.set({"rel": "url("+relimg+")",
                     "events" : {'mouseover':this._toggle.pass(elt),
                           'mouseout':this._toggle.pass(elt)}})
        
                                              
                       
        },this)
        new Asset.images(arrayImg);
    },
    
    _toggle : function(elt){
        //console.log("ok");
        var firstimg = elt.getStyle("background-image");
        var secondimg = elt.get("rel");
        elt.set({"rel": firstimg, "styles" :{"background-image":  secondimg }});
    }
});
var divtoggler = new Class ({
    initialize: function(openedLayer){
        this.links =  ["kontaktlink",   "uberunslink",  "newslink"];
        this.closes = ["closekont",     "closeuber",    "closenews"];
        this.layers = ["layerKontakt",  "layerUberUns", "overlayNews"];
        this.links.each(function(linkElt, ind){
        if(!$(linkElt) || linkElt == "newslink") return;
            $(linkElt).set({"href" : "javascript:void(0)", "events" : {'click' : this._toggle.pass(ind, this)}})
                 
        }, this);
        this.closes.each(function(closeElt, ind){
            if(!$(closeElt)) return;
            $(closeElt).set({"href" : "javascript:void(0)", "events" : {'click' : this._toggle.pass(-1,this)}})     
        }, this);
        this.layers.each(function(layerElt){
         if(!$(layerElt) || (layerElt == "overlayNews")) return; 
            $(layerElt).setStyle("opacity", 0.80);
        });
        if($chk(openedLayer)) this._toggle(this.layers.indexOf(openedLayer));
        
    },
    _toggle : function(curind){
        this.layers.each(function(layerElt, ind){
            if(!$(layerElt)) return;
            $(layerElt).setStyle("display", ind == curind ? "block" : "none");
            new scollMainContent();
        }, this);
    }
});

var scollMainContent = new Class({
    initialize : function () {
     this.c = $("newsContent");
     if(!$chk(this.c)) return;
     if(this.c.retrieve("ready")) return;
     if(!this.checkSize()) window.addEvent("load", this.checkSize.bind(this));
    },
    checkSize : function() {
        if(this.c.getSize().y == 0) return;
        var t = this.c.getScrollSize().y / this.c.getSize().y;
        
        if(t <= 1) return false;
        return this.addFunction(t);
    },
    addFunction : function(t){
         if(this.c.retrieve("ready")) return;
         t = Math.round(t*1000)/1000;
         this.f = new Fx.Scroll(this.c, {"duration" : (t*1500)});
         this.ff = new Fx.Scroll(this.c, {"duration" : (t*400)});
         
         this.c.setStyles({"overflow": "hidden"});
         this.u = new Element("a", {"class" : "up", "html" : "up"}).addEvent("mouseleave",this.cancel.bindWithEvent(this)).addEvent("mouseenter",this.goUp.bindWithEvent(this, this.f)).addEvent("mousedown",this.goUp.bindWithEvent(this, this.ff)).inject($("ctrlcont"));
         this.d = new Element("a", {"class" : "down", "html" : "down"}).addEvent("mouseleave",this.cancel.bindWithEvent(this)).addEvent("mouseenter",this.goDown.bindWithEvent(this, this.f)).addEvent("mousedown",this.goDown.bindWithEvent(this, this.ff)).inject($("ctrlcont"));
         this.c.addEvent("mousewheel",this.wheel.bindWithEvent(this));
         this.c.store("ready", true);
         return true;
    },
    wheel : function(ev) {
        this.cancel();
        this.f.set(0, this.c.getScroll().y - 5*ev.wheel);
    },
    goUp : function (ev, ef) {
        this.cancel();
        ef.toTop();
    },
    goDown : function (ev, ef) {
        this.cancel();
        ef.toBottom();
    },
    cancel : function () {
        this.f.cancel();
        this.ff.cancel();
        
    }
});

window.addEvent("domready", function(){
    new scollMainContent();
    new rollover();
}) 
    
