// $Id: navigation.js,v 1.1.1.1 2010-04-11 11:56:55 lars Exp $

// helper function

String.prototype.printf = function(anz) {
    var val = this;
    var n = '';
    if(isNaN(val)) return 'Not a valid Number';
    var z = parseFloat(val);
    if(z < 0) {
        n += '-';
        z = Math.abs(z); }
    var g = z.toString().split('.');
    while(anz > g[0].length) { 
        n += '0';
        anz--;
    }
    return n + g.join('.');
}

var active_link             = -1;
var active_image            = 1;
var active_descr_id         = "";
var show_descr              = 0;
var show_i                  = 0; // Impressum an/aus
var max_image_bilder        = 32;
var max_image_album         = 23;
var max_image_portraits     = 45;
var max_image_dokumentation = 112;
var max_reportage_texts     = 37;

var reportage_was_visited   = 0;
var album_was_visited       = 0;
var ms_show_clickbar        = 350; // MS to show clickbar border
var click_hint              = '1px solid magenta';

// var act_reportage_text      = Math.floor(Math.random()*max_reportage_texts) + 1; // init
var act_reportage_text      = 1;

var color_space             = 0;
var max_color_spaces        = 2;
var imgbase                 = 'navigation/';
// var loader_image            = imgbase + '/load.gif';
var loader_image            = imgbase + '/dummy.gif';

var date_hash    = 'cad8bcee8538dccc8b6a2859f4d2cffb';
var image_prefix = 'pic_' + date_hash + '_';

// set gamma value

var gamma;

// Mac
if ((navigator.platform == 'MacIntel' || navigator.platform == 'MacPPC')
    && navigator.appName != 'Opera' && navigator.appName != 'Netscape') {
    gamma = 180;
}
// all others
else {
    gamma = 220;
}


var title_base = document.title;
var title = new Array(5);
title[0]  = 'Bilder';
title[1]  = 'Album';
title[2]  = '"Auf der Suche"';
title[3]  = 'Portraits';
title[4]  = 'Dokumentation';

var cs_map = new Array(5);
cs_map[0] = 0;
cs_map[1] = 1;
cs_map[2] = 1;
cs_map[3] = 0;
cs_map[4] = 0;

var cs_bg               = new Array(max_color_spaces);
var cs_tc               = new Array(max_color_spaces);
var cs_link_activated   = new Array(max_color_spaces);
var cs_link_deactivated = new Array(max_color_spaces);
var cs_tc_impressum     = new Array(max_color_spaces);
var cs_tc_post          = new Array(max_color_spaces);

// background
cs_bg[0] = '#231e22';
cs_bg[1] = '#d3d2cf';

// text
cs_tc[0] = '#ddd6f1';
cs_tc[1] = '#767473';

cs_link_activated[0]   = '#2c9ca6';
cs_link_activated[1]   = '#d38ede';

cs_link_deactivated[0] = '#9e9aa2';
cs_link_deactivated[1] = '#8c8b8e';

cs_tc_impressum[0] = cs_link_activated[0];
cs_tc_impressum[1] = cs_link_deactivated[1];

cs_tc_post[0]      = '#e76903';
cs_tc_post[1]      = cs_link_deactivated[1];

var mouse_over_piclink = 0;

var c, i;

// build image objects, divided into color spaces 0 - 1
var m_over_active   = new Array(max_color_spaces);
var m_over_inactive = new Array(max_color_spaces);
var m_out_active    = new Array(max_color_spaces);
var m_out_inactive  = new Array(max_color_spaces);
var t_out           = new Array(max_color_spaces);
var t_over          = new Array(max_color_spaces);
var t_line          = new Array(max_color_spaces);

for (c = 0; c <= max_color_spaces-1; c++) {
    m_over_active[c]   = new Array(5);
    m_over_inactive[c] = new Array(5);
    m_out_active[c]    = new Array(5);
    m_out_inactive[c]  = new Array(5);

    var cstr = c + "";

    t_out[c]        = new Image();
    t_over[c]       = new Image();
    t_line[c]       = new Image();
    t_out[c].src    = imgbase + 'menu_title_' + cstr.printf(2) + '_0.gif';
    t_over[c].src   = imgbase + 'menu_title_' + cstr.printf(2) + '_1.gif';
    t_line[c].src   = imgbase + 'menu_line_'  + cstr.printf(2) + '.gif';

    for (i = 0; i <= 4; i++) {
        m_over_active[c][i]   = new Image();
        m_over_inactive[c][i] = new Image();
        m_out_active[c][i]    = new Image();
        m_out_inactive[c][i]  = new Image();

        m_out_active[c][i].src    = imgbase + '/menu_button_' + cstr.printf(2) + '_0_0_' + i + '.gif';
        m_out_inactive[c][i].src  = imgbase + '/menu_button_' + cstr.printf(2) + '_0_1_' + i + '.gif';
        m_over_active[c][i].src   = imgbase + '/menu_button_' + cstr.printf(2) + '_1_0_' + i + '.gif';
        m_over_inactive[c][i].src = imgbase + '/menu_button_' + cstr.printf(2) + '_1_1_' + i + '.gif';
    }
}
// EOF image building

// colorspace 0 / BILDER


function m_over_titlelink() {
    var piclink = document.getElementById('link_m');
    piclink.src = t_over[color_space].src;
    mouse_over_piclink = 1;
}

function m_out_titlelink() {
    var piclink = document.getElementById('link_m');
     piclink.src = t_out[color_space].src;
    mouse_over_piclink = 0;
}

function m_click_titlelink() {
    if (active_link == 'tanja_thomsen') {
        deactivate_item('tanja_thomsen');
    }
    else {
        activate_item('tanja_thomsen');
        if (show_i) {
            i_click();
        }
    }
}

function m_over_piclink(num) {
    var piclink = document.getElementById('link' + num);
    if (active_link == num) {
        piclink.src = m_over_active[color_space][num].src;
    }
    else {
        piclink.src = m_over_inactive[color_space][num].src;
    }
    mouse_over_piclink = 1;
}

function m_out_piclink(num) {
    var piclink = document.getElementById('link' + num);
    if (active_link == num) {
        piclink.src = m_out_active[color_space][num].src;
    }
    else {
        piclink.src = m_out_inactive[color_space][num].src;
    }
    mouse_over_piclink = 0;
}

function m_click_piclink(num) {
    if (active_link == num) {
        deactivate_item(num);
    }
    else {
        activate_item(num);
        if (show_i)
            i_click();
        else if (num != 0 && num != 3 && num != 4) 
            document.getElementById('link_impressum').style.visibility = 'hidden';

        // ablbum, reportage
        if (num == 1 || num == 2)
            document.getElementById('mainmenu').style.visibility = 'hidden';
    }
}

function activate_item(num) {
    var new_color_space;

    if (active_link != -1) {
        if (active_link != 'tanja_thomsen') {
            document.getElementById("box" + active_link).style.visibility = 'hidden';
            document.getElementById("link" + active_link).src 
             = m_out_inactive[color_space][active_link].src;
        }
        else {
            document.getElementById('tanja_thomsen').style.visibility = 'hidden';
            document.getElementById("link_m").src = t_out[color_space].src;
        }
    }
    else if (show_i) {
        i_click();
    }

    active_link = num;
    if (num != 'tanja_thomsen') {
        new_color_space = cs_map[active_link];
    }
    else {
        new_color_space = color_space;
    }

    if (color_space != new_color_space) {
        if (num != 'tanja_thomsen') {
            document.getElementById('link' + num).src = m_over_active[new_color_space][num].src;
        }
        else {
            document.getElementById('link_m').src = t_over[new_color_space];
        }

        for (i = 0; i <= 4; i++) {
            if (num != i) {
                if (num != 'tanja_thomsen') {
                    document.getElementById('link' + i).src = m_out_inactive[new_color_space][i].src;
                }
                else {
                    document.getElementById('link_m').src = t_out[new_color_space];
                }
            }
        }
        document.getElementById('link_m').src = t_out[new_color_space].src;
        document.getElementById('mainmenu_line').src = t_line[new_color_space].src;
        color_space = new_color_space;
        activate_color_space(color_space);
    }
    else {
        if (num != 'tanja_thomsen') {
            document.getElementById('link' + num).src = m_over_active[color_space][num].src;
        }
        else {
            document.getElementById('link_m').src = t_over[color_space].src;
            document.getElementById('tanja_thomsen').style.color = cs_tc_post[color_space];
        }
    }

    if (num != 'tanja_thomsen') {
        document.getElementById("box" + active_link).style.visibility = 'visible';
        document.title = title_base + ': ' + title[num];
    }
    else {
        document.getElementById("tanja_thomsen").style.visibility = 'visible';
        document.title = title_base + ': post';
    } 

    // specials for ALBUM
    if (num == 1) {
        set_image('album',1);

        // short graphical hint where to click...
        if (album_was_visited == 0) {
            document.getElementById('image_clickbar_album').style.border = click_hint;
            window.setTimeout('set_album_click_border()',ms_show_clickbar);
            album_was_visited = 1;
        }

        // set background specific to platform
        var album_background = 'background_gray_g' + gamma + '.gif';
        document.body.style.backgroundImage = 'url(' + album_background + ')'; 
    }

    // specials for REPORTAGE
    else if (num == 2) {
        impressum_off();
        set_text('reportage' ,act_reportage_text);
        set_image('reportage',act_reportage_text);
        document.getElementById('image_box_reportage').style.visibility = 'visible';

        // short hint where to click...
        if (reportage_was_visited == 0) {
            document.getElementById('image_clickbar_reportage').style.border = click_hint;
            window.setTimeout('set_reportage_click_border()',ms_show_clickbar);
            reportage_was_visited = 1;
        }
    }

    // specials for PORTRAITS
    else if (num == 3) {

    }
}
// EOF activate_item

function set_album_click_border() {
    document.getElementById('image_clickbar_album').style.border = '0px';
}

function set_reportage_click_border() {
    document.getElementById('image_clickbar_reportage').style.border = '0px';
}

function deactivate_item(num) {
    if (mouse_over_piclink) {
        if (num != 'tanja_thomsen') {
            document.getElementById('link' + num).src = m_over_inactive[color_space][num].src;
        }
        else {
            document.getElementById('link_m').src = t_over[color_space].src;
        }
    }
    else {
        if (num != 'tanja_thomsen') {
            document.getElementById('link' + num).src = m_out_inactive[color_space][num].src;
        }
        else {
            document.getElementById('link_m').src = t_out[color_space].src;
        }
    }
    
    if (num != 'tanja_thomsen') {
        document.getElementById('box' + num).style.visibility = 'hidden';
    }
    else {
        document.getElementById('tanja_thomsen').style.visibility = 'hidden';
    }
    document.title = title_base;
    active_link = -1;

    // album
    if (num == 1) {
        document.body.style.backgroundImage = 'url()';
    }

    // reportage
    else if (num == 2) {
        document.getElementById('image_box_reportage').style.visibility = 'hidden';
    }
}
// EOF deactivate_item

function m_over_textlink(object) {
    object.style.color     = cs_link_activated[color_space];
    object.style.fontStyle = 'italic';
}

function m_out_textlink(object) {
    object.style.color = cs_link_deactivated[color_space];
    object.style.fontStyle = 'normal';
}

function activate_color_space(cs) {
    document.body.style.backgroundColor = cs_bg[cs];
    document.body.style.color = cs_tc[cs];
}


// BILDER-Funktionen

function activate_thumb(id_name) {
    document.getElementById(id_name).style.marginTop = '-1px';
    document.getElementById(id_name).style.borderTop = '1px solid ' + cs_link_activated[color_space];
}

function deactivate_thumb(id_name) {
    document.getElementById(id_name).style.marginTop = '0px';
    document.getElementById(id_name).style.borderTop = 'none';
}

function click_thumb(id_name,category) {
    var regex = /(\d+)$/;
    regex.exec(id_name);
    var rnum = RegExp.$1;
    var image_name = 'images/' + category + '/' + image_prefix +  rnum + '.jpg';


    // Achtung: 001 wird von parseInt als Oktalzahl aufgefasst(!)
    var image_num  = rnum * 1;

    document.getElementById(id_name).style.borderTop = 'none';

    if (category == "bilder") {
        document.getElementById('image_clickbar_bilder').style.visibility = 'visible';
    }

    document.getElementById('image_box_' + category).style.visibility = 'visible';
    document.getElementById('link_impressum').style.visibility = 'hidden';
    document.getElementById('image_content_' + category).src = image_name;

    show_descr = 1;
    set_image(category,image_num);
}
// EOF click_thumb

function image_switch(direction,category) {
    var max_image;

    switch (category) {
        case 'bilder'        : max_image = max_image_bilder;        break;
        case 'album'         : max_image = max_image_album;         break;
        case 'reportage'     : max_image = max_reportage_texts;     break;
        case 'portraits'     : max_image = max_image_portraits;     break;
        case 'dokumentation' : max_image = max_image_dokumentation; break;
    }

    if (direction == 'right') {
        if (active_image+1 > max_image) {
            set_image(category,1);
        }
        else {
            set_image(category,active_image+1);
        }

        // special handler REPORTAGE
        if (category == 'reportage') {
            if (act_reportage_text+1 > max_reportage_texts) {
                set_text(category,1);
            }
            else {
                set_text(category,act_reportage_text + 1);
            }
        }
    }
    else if (direction == 'left') {
        if (active_image-1 < 1) {
            set_image(category,max_image);
        }
        else {
            set_image(category,active_image-1);
        }

        // special handler REPORTAGE
        if (category == 'reportage') {
            if (act_reportage_text-1 < 1)
                set_text(category,max_reportage_texts);
            else
                set_text(category,act_reportage_text - 1);
        }
    }
}
// EOF image_switch

function set_image(category,num) {
    active_image = num; // set global value

    // the function itself
    var image_name = active_image + '';
    var image_new  = 'images/' + category + '/' + image_prefix + image_name.printf(3) + '.jpg';

    var img_obj = document.getElementById('image_content_' + category);
    img_obj.src = image_new;


    // special handlers
    if (active_descr_id != "") {
        document.getElementById(active_descr_id).style.visibility = 'hidden';
    }


    // set trigger: text is enable if images was loaded completeley
    if (show_descr == 1) {
    img_obj.onload = function () {
        if (show_descr == 0)
            return true;

        if (category == 'bilder') {
            var descr_name = category + '_descr_pic' + image_name.printf(3);
            if (active_image == 1 || active_image == 15) {
                var descr_name = 'bilder_descr_pic' + image_name.printf(3);
                document.getElementById(descr_name).style.visibility = 'visible';
                active_descr_id = descr_name;
            }
        }

        else if (category == 'portraits') {
            if (active_image == 1 || active_image == 12 || active_image == 34) {
                var descr_name = 'portraits_descr_pic' + image_name.printf(3);
                document.getElementById(descr_name).style.visibility = 'visible';
                active_descr_id = descr_name;
            }
        }

        else if (category == 'dokumentation') {
            if (   active_image ==  1 || active_image ==  5 || active_image ==  7 
                || active_image == 17 || active_image == 45 || active_image == 66
                || active_image == 86) {
                var descr_name = 'dokumentation_descr_pic' + image_name.printf(3);
                document.getElementById(descr_name).style.visibility = 'visible';
                active_descr_id = descr_name;
            }
        }

    }
    }
    // EOF anonymous onload function
}
// EOF set_image

function get_triple_id(prefix,num) {
        var numstring = num + '';
        var triple_id = prefix + numstring.printf(3);

        return triple_id;
}

// use num == -1 to switch text and description off
//
function set_text(category,num) {
    if (category == 'reportage') {
        var id_text_old  = get_triple_id('reportage_text_',act_reportage_text);
        var id_descr_old = get_triple_id('reportage_description_',act_reportage_text);

        if (num != -1) {
            var id_text_new  = get_triple_id('reportage_text_',num);
            var id_descr_new = get_triple_id('reportage_description_',num);
        }

        document.getElementById(id_text_old).style.visibility  = 'hidden';
        document.getElementById(id_descr_old).style.visibility = 'hidden';

        if (num != -1) {
            document.getElementById(id_text_new).style.visibility  = 'visible';
            document.getElementById(id_descr_new).style.visibility = 'visible';
            act_reportage_text = num;
        }
    }
    else {
        alert('not implemented category for set_text(): ' + category);
    }

    show_descr = 1;
}
// EOF set_text

function close_images(category) {
    var id_name;

    if (active_descr_id != "") {
        document.getElementById(active_descr_id).style.visibility = 'hidden';
    }

    if (category == 'album') {
        id_name = 'box1';
        deactivate_item(active_link);
    }
    else if (category == 'reportage') {
        id_name = 'box2';
        set_text(category,-1);
        deactivate_item(active_link);
    }
    else if (category == 'dokumentation') {
        active_descr_id = "";
        active_image = 0;
        id_name = 'image_box_' + category;
    }
    else {
        id_name = 'image_box_' + category;
    }

    if (category == 'bilder')
        document.getElementById('image_clickbar_bilder').style.visibility = 'hidden';

    document.getElementById(id_name).style.visibility = 'hidden';
    document.getElementById('link_impressum').style.visibility = 'visible';

    if (id_name != 'box2')
        document.getElementById('image_content_' + category).src = loader_image;
    // active_image = 1;
    document.getElementById('mainmenu').style.visibility = 'visible';

    if (active_descr_id)
        document.getElementById(active_descr_id).style.visibility = 'hidden';
    show_descr = 0;
}
// EOF close_images()

function impressum_off() {
    document.getElementById('impressum').style.visibility = 'hidden';
    show_i = 0;
}

// div#impressum_line
//
function i_click() {
    if (show_i) {
        document.getElementById('impressum').style.visibility = 'hidden';
        document.title = title_base;
        show_i = 0;
    }
    else {
        if (active_link != -1)
            deactivate_item(active_link);
        document.getElementById('impressum').style.visibility = 'visible';
        document.getElementById('impressum').style.color = cs_tc_impressum[color_space];
        document.getElementById('impressum_line').style.borderTop = '1px solid ' +  cs_tc_impressum[color_space];
        document.title = title_base + ': Impressum';
        show_i = 1;
    }
}
// EOF i_click()

