var Site={

    init:function(){
        $(document.body).addClass('js');
        Site.draw_labels();
        Site.addCorners($$("#nav, #top .lcol"));
        Site.popups();
        Site.slider_init();
        Site.faq();
        $$(".label .inner").each(function(el, i){
            el.innerHTML = '<div class="bg">'+el.innerHTML + '</div>';
        });

        if($("print")) window.print();
    },

    slider_init:function(){
        $$("#slider li, .oftheday li").addEvent("mouseover", function(e){
            this.addClass("hover");
        });
        $$("#slider li, .oftheday li").addEvent("mouseout", function(e){
            this.removeClass("hover");
        });
        if (!$$("#slider ul").length) return;
        Site.slider = $$("#slider ul")[0];
        Site.slider_value = 3;
        Site.slider_step = Site.slider.getElements('li')[0].offsetWidth;
        Site.slider_link = Site.slider.getElements('a')[0].offsetWidth;
        $("slider").style.height = Site.slider.getElements('a')[0].offsetHeight+'px';
        Site.slider.set({'styles':{'width':($$("#slider ul li").length * Site.slider_step), 'height':Site.slider.getElements('a')[0].offsetHeight + 'px'}});
        Site.frame = new Element('div', {'id':'frame', 'styles':{'width':(Site.slider_value * Site.slider_step) - (Site.slider_step - Site.slider_link) + 'px', 'height':Site.slider.getElements('a')[0].offsetHeight+'px'}});
        $("slider").appendChild(Site.frame);
        $("frame").adopt($$("#slider ul"));
        $$("#rarr, #larr").addEvent("click", Site.slider_move)
        $$("#rarr, #larr").each(function(el,i){Site.slider_bounds(el)});
        $$("#rarr, #larr").addEvent("select", stopit);
        $$("#rarr, #larr").addEvent("dblclick", stopit);
    },

    slider_bounds:function(el){
        var links = $$("#rarr, #larr");
        links.removeEvent("click", stopit);
        links.removeClass('disabled');
        links.addEvent('click', Site.slider_move);

        if((Site.slider.style.left.toInt() + Site.slider.style.width.toInt()) - 240 <= $('frame').style.width.toInt()){ 
            $("rarr").addClass("disabled");
            $("rarr").removeEvent("click", Site.slider_move);
            $("rarr").addEvent("click", stopit);
        }else if(!Site.slider.style.left || Site.slider.style.left.toInt()>=0){
            $("larr").addClass("disabled");
            $("larr").addEvent("click", stopit); 
            $("larr").removeEvent("click", Site.slider_move);
        }

    },

    slider_move:function(e){
        e = new Event(e);
        e.stop();
        var step = (Site.slider.style.left||0).toInt() + (((this.id=="rarr")?'-':'+') + Site.slider_step).toInt();
        var myfx = new Fx.Tween(Site.slider)
        myfx.start('left', step).chain(function(){Site.slider_bounds(this)})
        $$("#larr, #rarr").removeEvent("click", Site.slider_move);
    },

    faq:function(){
        if(!$("faq")) return;
        $$(".faq li").each(function(el, i){
            var button = el.getElement("h3");
            var slider = el.getElement("div");
            var myfx = new Fx.Slide(slider).hide();
            button.addEvent("click", function(){myfx.toggle();});
        });
    },

    draw_labels:function(){
        if($("print")) return;
        $$(".label").each(function(el, i){
            el.className.split(" ").each(function(classname, i){
                //if(Site.colors[classname]){
                    //$(el).CanvasULElemet(new Site.label(Site.colors[classname]));
                //}else{
                    //$(el).CanvasULElemet(new Site.label(Site.colors['brown']));
                //}
            });

            $(el).addEvent("mouseover", function(){$(this).addClass('hover')});
            $(el).addEvent("mouseout", function(){$(this).removeClass('hover')});
            $(el).addEvent("click", function(e){
                var e = new Event(e);
                if(typeof(e.target.href) == String) location.href = e.target.href;
                location.href = $(this).getElement("a").href;
            });
        });

/*        $$(".add, .del").each(function(el, i){*/
            //el.addEvent("click", function(e){
                //var e = new Event(e);
                //e.stop();
                //alert(el);
                //location.href = el.href;
            //});
        /*});*/
    },

    label:function(colorObj){
        var label = {
            glow: 9,
            shadow: 0,
            border: 0,
            corners: [14, 14, 14, 14],
            bodyStartColor: colorObj.startColor,
            bodyStopColor: colorObj.stopColor,
            glowColor: [50, 50, 50],
            shadowColor: [0, 0, 0],
            gradientMode: 'vertical',
            zindex: -1
        }
        return label;
    },

    close:function(){
        var fadeout = new Fx.Tween($('fader'));
        fadeout.start('opacity', 0);
        $("popup").dispose();
    },

    popup:function(href, i){
        var fader = $("fader");
        var popup = $("popup");
        var classname = 'popup'+i;
        if(!fader) {
            fader = new Element("div", {'id':'fader'});
            $(document.body).adopt(fader);
        }
        if(!popup){ 
            popup = new Element("div", {'id':'popup', 'class':classname});
            $(document.body).adopt(popup);
            popup.adopt(new Element('div', {'id':'iefix'}), new Element('div', {'id':'loader'}), new Element('a', {'href':'#','id':'close', 'html':'<span>&times;</span>'}));
            $('close').addEvent('click', Site.close);
            $(document.body).addEvent('keypress', function(e){
                if(e.code==27) Site.close();
            });
        }
        //document.body.set({'styles':{'position':'relative','overflow':'hidden','padding-right':18}});
        Site.faderFx = new Fx.Tween(fader,{'duration':1000}).set('opacity', 0);
        //Site.popupFx = new Fx.Tween(popup).set('opacity', 0);
        Site.faderFx.start('opacity',0.7);
        //Site.popupFx.start('opacity', 1);
        $("loader").load(href);
    },

    popups:function(){
        $$("#login_f .ajx").each(function(el, i){
            el.addEvent("click", function(e){
                e = new Event(e);
                e.stop();
                Site.popup(el.href, i);
            })
        })
    },

    addCorners:function(els){
        $$(els).each(function(e,i){
            e.innerHTML = '<b class="b0"></b><b class="b1"></b><b class="b2"></b><b class="b3"></b><span class="inner">'+e.innerHTML+'</b>';
        });
    },

    colors:{
        'red':{
            'startColor':[199, 74, 21, 1],
            'stopColor':[163, 33, 23, 1]
        },
        'blue':{
            'startColor':[84, 160, 200, 1],
            'stopColor':[81, 126, 179, 1]
        },
        'orange':{
            'startColor':[238, 186, 79, 1],
            'stopColor':[211, 168, 79, 1]
        },
        'green':{
            'startColor':[207, 225, 52, 1],
            'stopColor':[166, 174, 24, 1]
        },
        'sky':{
            'startColor':[137, 221, 227, 1],
            'stopColor':[93, 175, 215, 1]
        },
        'bog':{
            'startColor':[113, 174, 147, 1],
            'stopColor':[34, 116, 118, 1]
        },
        'pink':{
            'startColor':[239, 126, 177, 1],
            'stopColor':[238, 82, 131, 1]
        },
        'purple':{
            'startColor':[202, 64, 149, 1],
            'stopColor':[186, 56, 121, 1]
        },
        'brown':{
            'startColor':[200, 191, 144, 1],
            'stopColor':[163, 151, 77, 1]
        }
    }
};

function stopit(e){
    var e = new Event(e);
    e.stop();
}

window.addEvent('domready', Site.init);
