window.addEvent('domready', function() {
    
    if (Browser.Engine.presto) {
        $('content').setStyle('margin-left', 10);
    }
    
    var content_pane_width = 470;
    var bottom_bar_height = 32;
    
    var _page_num = 1;
    
    var content_pane = $('content-pane'), content = $('content');
    var content_scroll_fx = new Fx.Scroll(document.body, {
        transition: 'cubic:out'
    });
    
    var scroll_handle = $empty;
    
    var content_pane_scroll_fx = new Fx.Tween(content_pane, {
        property: 'margin-left',
        transition: 'cubic:out',
        onComplete: function() {
            scroll_handle();
        }
    }).set(0);
    
    function hide_content() {
        content_pane_scroll_fx.start(-content_pane_width);
    }
    
    function show_content() {
        content_pane_scroll_fx.start(0);
    }
    
    function get_url(el) {
        var rel = el.getAttribute('rel');
        return '/white/' + rel.replace('-', '') + '/';
        //return '/white/page' + href.replace(/#[^:]+:/,'') + '/';
    }
    
    function top_link_click_handle(e) {
        if (this.hasClass('current')) return;
        if (! this.getAttribute('rel')) return;
        if (! this.getNext() ) {
            empty_top_link_click_handle.bind(this)(e);
            return;
        }
        e.stop();
        if (_page_num == 1) {
		$$('#content-page-2').dispose();//setStyle('display: none;');
	} 
        scroll_handle = function() {
            var old_active = $$('#menu>ul>li>a.current');
            if (old_active.length) {
                old_active[0].removeClass('current');
                if (old_active[0].getNext())  {
                    new Fx.Tween(old_active[0].getNext(), {
                        property: 'height'
                    }).start(0);
                }
            }
            this.addClass('current');
            menu_links.removeClass('current');
            new Fx.Tween(this.getNext(), {
                property: 'height',
                onComplete: function() {
                    var first_link = this.getNext().getElements('a')[0];
                    if (! first_link.getAttribute('rel')) {
                        var url = 'none.html';
                    } else {
                        var url = get_url(first_link);
                    }
                    first_link.addClass('current');
                    new Request.HTML({
                        url: url,
                        onSuccess: function(r, l, h) {
                            _page_num = 1;
                            content_pane.innerHTML = h;
                            scroll_handle = $empty;
                            show_content();
                        }
                    }).get({t: Math.random()});
                }.bind(this)
            }).start(this.getNext().getFirst().getHeight());
            
        }.bind(this);
        
        hide_content();
    }

    function empty_top_link_click_handle(e) {
        e.stop();
        if (this.getAttribute('href') == '#') return;
        if (this.hasClass('current')) return;
	if (_page_num == 1) {
		$$('#content-page-2').dispose();//setStyle('display: none;');
	} 
        var old_active = $$('#menu>ul>li>a.current');
        old_active.removeClass('current');
        if (old_active[0].getNext())  {
            new Fx.Tween(old_active[0].getNext(), {
                property: 'height'
            }).start(0);
        }
        this.addClass('current');
        setTimeout(function(){
            new Request.HTML({
                url: get_url(this),
                onSuccess: function(r, l ,h) {
                    _page_num = 1;
                    content_pane.innerHTML = h;
                    scroll_handle = $empty;
                    show_content();
                }
            }).get({t: Math.random()});
        }.bind(this), 500);
        hide_content();
    }
    
    function menu_link_click_handle(e) {
        if (this.getAttribute('href')[0] == 'h') return;
	if (this.hasClass('current')) return;
        e.stop();
          menu_links.removeClass('current');
  
      if (_page_num == 1) {
                $$('#content-page-2').dispose();//setStyle('display: none;');
        }
        


	menu_links.removeClass('current');
        if (! this.getAttribute('rel')) {
            var url = 'none.html';
        } else {
            var url = get_url(this);
        }
        scroll_handle = function() {
            this.addClass('current');
            new Request.HTML({
                url: url,
                onSuccess: function(r, l, h) {
                    _page_num = 1;
                    content_pane.innerHTML = h;
                    scroll_handle = $empty;
                    show_content();
                }
            }).get({t: Math.random()});
        }.bind(this);
        hide_content();
    }
    
    var top_links = $$('#menu>ul>li>a');
    var menu_links = $$('#menu ul ul a');
    top_links.addEvent('click', top_link_click_handle);
    menu_links.addEvent('click', menu_link_click_handle);
    

    window._set_page = function(page_num) {
        if (_page_num == page_num) return;
        _page_num = page_num;
        $$('.content-page-controls').setStyle('display', 'none');
        $('content-page-controls-'+page_num).setStyle('display', 'block');
        if (page_num == 2) {
            new Fx.Tween('content-pages', {property: 'left'}).start(0, -500);
        } else if (page_num == 1) {
            new Fx.Tween('content-pages', {property: 'left'}).start(-500, 0);           
        } else {
            alert('not implemented');
        }
    }
    
    var bottom_bar_should_hide = true;
    var inital_height, height_delta;
    
    var bottom_bar = $('bottom-bar'), bottom_pane = $('bottom-pane');

    var bottom_bar_fx = new Fx.Tween(bottom_pane, {
        property: 'margin-top',
        wait: false,
        transition: 'cubic:out',
        onComplete: function() {
            var to = this.to[0].value;
            if (to == height_delta) {
                bottom_bar.setStyle('z-index', 100);
            }
        },
        onStart: function() {
            var to = this.to[0].value;
            if (to == 0) {
                bottom_bar.setStyle('z-index', 300);
            }
        }
    });
    
    bottom_pane.addEvents({
        click: function(e) {
            bottom_bar_should_hide = false;
            bottom_bar_fx.start(0);
        },
        mousemove: function(e) {
            bottom_bar_should_hide = false;
        },
        mouseleave: function(e) {
            bottom_bar_should_hide = true;
            (function(){
                if (bottom_bar_should_hide) {
                    bottom_bar_fx.start(height_delta);
                }
            }).delay(3000);
        }
    });
    
    var bottom_bar_div_height = 0;
    $$('#bottom-ua div, #bottom-ru div').each(function(div){
        if (div.getHeight() > bottom_bar_div_height) {
            bottom_bar_div_height = div.getHeight();
        }
    });
    $$('#bottom-ua div, #bottom-ru div').setStyle('height', bottom_bar_div_height);

    inital_height = bottom_bar.getHeight();
    height_delta = bottom_pane.getHeight() - inital_height;
    // bottom_bar.setStyles(bottom_bar.getCoordinates());
    bottom_bar.setStyle('width', bottom_bar.getWidth()); 
    bottom_bar.setStyle('height', bottom_pane.getHeight());
    bottom_bar.setStyle('top', bottom_bar.getTop() - height_delta - (Browser.Engine.trident ? 75 : 50)); // 50px body margin
    bottom_bar.setStyle('position', 'absolute');
    bottom_bar_fx.set(height_delta);
    
    $$('#menu a')[0].fireEvent('click', {stop: $empty});
    
    // $$('#menu a')[2].fireEvent('click');
    
    
});

window.addEvent('domready', function(){
    var bm = $('blue-bookmark');
    var wfx = new Fx.Tween(bm, {property: 'width', wait: false});
    var bfx = new Fx.Tween(bm, {property: 'border-width', wait: false});
    bm.addEvents({
            mouseenter: function() {
                //wfx.start(50);
                bfx.start(30);
            },
            mouseleave: function() {
                //wfx.start(26);
                bfx.start(6);
            }
        });
});


window.addEvent('resize', function() {
    $('blue-bookmark').setStyle('height', window.getScrollSize().y);
});

window.addEvent('load', function(e) {
    this.fireEvent('resize', e);
});


