/*

Copyright (C) 2007 by Marian Meravy

*/

var g_content1_x_offset=20;
var g_content1_y_offset=500;

var g_content2_x_offset=1023;
var g_content2_y_offset=0;

var g_content3_x_offset=1105;
var g_content3_y_offset=500;

var g_content4_x_offset=0;
var g_content4_y_offset=0;

var g_content_startup_left_offset=0;

var width_original=null;
var first=true;

function reposition(resize)
{
}

function show_ending_line(show)
{
}

function display_background_image()
{
    $('kritika').style.background='#000000 url(./images/background.gif) top center no-repeat';
}

var dyn_content_adjust=0;

function set_content_vertical_indent()
{
    var vertical_indent=26;
    if(window.gecko)
        vertical_indent+=14;

    if(dyn_content_adjust!=0)
        return;
    var c1=$(window.ie6 ? 'wrapper_scrolling' : 'inner').getCoordinates();
    var c2=$('dyn_offset0').getCoordinates();
    dyn_content_adjust=(c1.top+c1.height)-(c2.top);
    if(0==dyn_content_adjust)
        return;
    new Fx.Style('dyn_offset1', 'top', {duration:0}).set(c2.top+dyn_content_adjust+vertical_indent);
}

function children_set_color(el,hex_color)
{
    el.style.color=hex_color;

    for(var i1=0; i1<el.getChildren().length; ++i1) {
        children_set_color(el.getChildren()[i1],hex_color);
    }
}

function update_tags()
{
    {
        var els=document.getElementsByTagName('a');
        for(i1=0; i1<els.length; ++i1) {
            if(els[i1].my_dont_touch)
                continue;
            var el=$(els[i1]);
			/*
            el.removeEvents('mouseover');
            el.addEvent('mouseover',function() { this.style.textDecoration="underline"; }.bind(el));

            el.removeEvents('mouseout');
            el.addEvent('mouseout',function() { this.style.textDecoration="none"; }.bind(el));
			/**/

            if(el.href.length>0) {
                el.style.cursor="pointer";
                if(el.id.match("^loadpage_.+")) {
                    el.removeEvents('click');
                    el.addEvent('click', function(event)
                        {
                            event = new Event(event).stop();
                            var page=this.id.substr(String('loadpage_').length);
                            my_click('load_document("'+page+'");',page);
                        }.bind(el)
                    );
                }
                if(el.hash.match("^\\#document\\((\\w+)\\)")) {
                    el.removeEvents('click');
                    el.addEvent('click', function(event)
                        {
                            event = new Event(event).stop();
                            var page=this.hash.substr(String('#document(').length,this.hash.length-String('#document(').length-1);
                            my_click('load_document("'+page+'");',page);
                        }.bind(el)
                    );
                }
            }
        }
    }

    {
        var els=document.getElementsByTagName('div');
        for(i1=0; i1<els.length; ++i1) {
            var el=$(els[i1]);
            if(String(el.id).match(/^sub_slide_.+/)) {
                if(!String(el.id).match(/.*_content$/)) {
                    if(!el.my_sl) {
                        el.my_sl=new Fx.Slide((String(el.id)+"_content"), {duration: 500});
                        el.my_display=false;
                        el.my_sl.addEvent('onComplete', function()
                            {
                                if(!this.my_display) {
                                    this.my_sl.hide();
                                    $(String(this.id)+"_content").style.display="none";
                                } else {
                                    $(String(this.id)+"_content").style.display="";
                                }
                                //update_tags(); // FIXME: do we really need to call this?
                                process_scrollers();
                            }.bind(el)
                        );
                        el.my_sl.hide();
						/*
                        el.addEvent('mouseover',function()
                            {
                                this.style.textDecoration="underline";
                                this.style.cursor="pointer";
                            }.bind(el)
                        );
                        el.addEvent('mouseout',function() { this.style.textDecoration="none"; });
						/**/
                        el.addEvent('click',function(event)
                            {
                                event = new Event(event);
                                event.stop();
                                this.my_display=!this.my_display;
                                if(this.my_display) {
                                    $(String(this.id)+"_content").style.display="";
                                    this.my_sl.hide();
                                    this.my_sl.slideIn();
                                } else {
                                    this.my_sl.slideOut();
                                }
                            }.bind(el)
                        );
                    }
                } else {
                    el.style.display="none";
                }
            }
        }
    }

    {
        var els=document.getElementsByTagName('td');
        for(i1=0; i1<els.length; ++i1) {
            if(els[i1].className.substr(0, 11) == "col_3_hover") {
                $(els[i1]).removeEvents('mouseover');
                $(els[i1]).addEvent('mouseover',function() { this.style.backgroundColor="#1f3946"; children_set_color(this,"#f2e9da"); }.bind(els[i1]));
                $(els[i1]).removeEvents('mouseout');
                $(els[i1]).addEvent('mouseout',function() { this.style.backgroundColor=""; children_set_color(this,""); }.bind(els[i1]));
            }
        }
    }
}

var scroll = null;

function init_wrapper_scrolling()
{
    scroll=new Fx.Scroll('wrapper_scrolling', {
    	wait: false,
    	duration: 2500,
    	offset: {'x': 0, 'y': 0},
    	transition: Fx.Transitions.Quad.easeInOut,
        wheelStops: false
    });
    if(window.gecko) {
        scroll.addEvent('onStart', function()
            {
                for(var i1=1; i1<=4; ++i1) {
                    var content='content'+String(i1);
                    if($defined($(content))) {
                        $(content).style.overflow="hidden";
                    }
                }
            }
        );
        scroll.addEvent('onComplete', function()
            {
                for(var i1=1; i1<=4; ++i1) {
                    var content='content'+String(i1);
                    if($defined($(content))) {
                        $(content).style.overflow="hidden";
                    }
                }
            }
        );
    }

    for(var i1=1; i1<=4; ++i1) {
        var link='link'+String(i1);
        if($defined($(link)) && $defined($('content'+String(i1)))) {
            $('link'+String(i1)).addEvent('click', function(event)
                {
                    event = new Event(event);
                    event.stop();
                    var content=$(event.target).my_content;
                    //scroll.toElement($(content));
                    switch(this+0) {
                        case 1:
                            scroll.scrollTo(g_content1_x_offset,g_content1_y_offset);
                            break;
                        case 2:
                            scroll.scrollTo(g_content2_x_offset,g_content2_y_offset);
                            break;
                        case 3:
                            scroll.scrollTo(g_content3_x_offset,g_content3_y_offset);
                            break;
                        case 4:
                            scroll.scrollTo(g_content4_x_offset,g_content4_y_offset);
                            break;
                    }
                }.bind(i1)
            );
            $('link'+String(i1)).my_content='content'+String(i1);
        }
    }

    set_content_vertical_indent();
}

function kritika1_main()
{
    //reposition(false);
    //window.addEvent('resize',function() { reposition(true); });
    //setTimeout("reposition(false);",100);
    setTimeout("main();",100);
}

function fired_complete(el)
{
    var func=el.my_slide_func;
    el.my_slide_func=null;
    func();
}

function wait_for_completition(el,script,func)
{
    el.my_slide_func=func;
    el.my_slide.addEvent('onComplete',fired_complete);
    eval(script);
}

var g_slides=new Array();
var g_slides_funcs=new Array();

function slide_element(id,slide_in,duration_ms,complete_func)
{
    if(duration_ms<20) {
        complete_func();
        return;
    }
    var got=false;
    var i;
    for(i=0; i<g_slides.length; ++i) {
        if(g_slides[i]!=null && g_slides[i].element.id===id) {
            g_slides[i].stop();
            got=true;
            break;
        }
    }
    if(!got) {
        i=g_slides.push(new Fx.Slide($(id),{ duration: duration_ms }))-1;
        g_slides[i].addEvent("onComplete", function(){slide_clean(i);});
        g_slides[i].addEvent("onCancel", function(){slide_clean(i);});
    }
    g_slides[i].stop();
    g_slides[i].duration=duration_ms ? duration_ms : 1;
    g_slides_funcs[i]=complete_func;
    if(slide_in)
        g_slides[i].slideIn();
    else {
        g_slides[i].slideOut();
    }
    return true;
}
function null_slide_element(i)
{
}
function null_function()
{
    //alert("NULL FUNCTION!");
}
function slide_clean(i)
{
    if(g_slides[i]!=null)
        null_slide_element(i);
    else
        alert("sc none");
    var func=g_slides_funcs[i];
    g_slides_funcs[i]=null_function;
    func();
}

function load_document(id)
{
    var query_obj={
        "docid": encodeURI(id)
    };
    var i=0;
    $('dyn_offset1').style.overflow="hidden";
    slide_element('dyn_content',0,500, function() {
        $('footer').style.display="none";
        slide_element('loading_wait',1,500, function() {
            show_ending_line(false);
            $('dyn_content_loader').empty();
            reposition(false);
            slide_element("dyn_content_loader",0,1,function(){
                slide_element("dyn_content",0,1,function(){
                    var a=new Ajax("index.php?dyn_content=1",
                        {
                            method: "post",
                            data: query_obj,
                            evalScripts: true,
                            evalResponse: false,
                            update: $("dyn_content_loader")
                        }
                    );
                    a.addEvent("onComplete",function(html)
                        {
                            window.addEvent('domready',function()
                                {
                                    slide_element("dyn_content_loader",1,1,function(){
                                        slide_element("dyn_content_loader",0,1,function(){
                                            slide_element("dyn_content",1,1,function(){
                                                slide_element("dyn_content",0,1,function(){
                                                    slide_element("dyn_content_loader",1,1,function(){
                                                        set_content_vertical_indent();
                                                        slide_element('dyn_content',1,500, function() {
                                                            $('dyn_offset1').style.overflow="visible";
                                                            set_content_vertical_indent();
                                                            slide_element("loading_wait",0,500,function(){
                                                                set_content_vertical_indent();
                                                                reposition(false);
                                                                show_ending_line(true);
                                                                var c1=$('dyn_content').getCoordinates();
                                                                $('footer').style.top=String(c1.top+c1.height)+"px";
                                                                $('footer').style.display="inline";
                                                                update_tags();
                                                                process_scrollers();
                                                            });
                                                        });
                                                    });
                                                });
                                            });
                                        });
                                    });
                                }
                            );
                        }
                    );
                    a.request();
                });
            });
        });
    });
}

var g_imgs=null;

function build_imgs_list(el)
{
    if(el.getTag()==='img') {
        g_imgs.push(el);
    }
    for(var i1=0; i1<el.getChildren().length; ++i1)
        build_imgs_list(el.getChildren()[i1]);
}

function check_imgs()
{
    for(i1=0; i1<g_imgs.length; ++i1)
        if(!g_imgs[i1].complete)
            return false;
    return true;
}

function preload_done()
{
    kritika1_main();
    update_tags();
    process_scrollers();
    process_zebra1();
    process_zebra2();
    process_zebra3();
    process_zebra4();

    display_background_image();

    $('all_loading_wait').empty();
    $('all_loading_wait').remove();
    $('all_loading_okay').style.display='inline';
}

function preload_wait()
{
    if($('scrolling_image').complete) {
        init_wrapper_scrolling();
    }
    if(!g_imgs) {
        var size=window.getSize();
        $('all_loading_wait').style.left=String(size.size.x/2-$('all_loading_wait').getSize().size.x/2)+"px";
        $('all_loading_wait').style.top=String(size.size.y/2-$('all_loading_wait').getSize().size.y/2)+"px";
        $('all_loading_wait').style.visibility="visible";

        g_imgs=new Array();
        build_imgs_list($('kritika'));
        display_background_image();
    }

    if(!check_imgs()) {
        setTimeout('preload_wait();',100);
        return;
    }
    preload_done();

    $('wrapper_scrolling').scrollTo(g_content_startup_left_offset,0);
}

