﻿
$(document).ready(function() {
    $("#FooterDiv").load("/include/footer_new.html?2");
});

// Rollover __________________________________________________
$(document).ready(function() {
    function rollover_img(selector, suffix) {
        $(selector).each(function(i) {
            var defaultImg = $(this).attr("src");
            if (!defaultImg.match((suffix))) {
                var point = defaultImg.lastIndexOf(".");
                var rolloverImg = defaultImg.slice(0, point) + suffix + defaultImg.slice(point);
                var preloadImg = new Image();
                preloadImg.src = rolloverImg;
                $(this).hover(function() {
                    $(this).attr("src", rolloverImg);
                }, function() {
                    $(this).attr("src", defaultImg);
                }
					)
            }
        })
    }
    rollover_img(".rollover", "_ov");
});


// -----------------------------------------------------
//
// teaser boxes with dhtml list-selects
//
// -----------------------------------------------------

// create select object
//
var _select = {

    obj: [],
    ref: [],
    li: [],
    sel: [],
    curr: false,

    expand: function(id) {
        this.ref[id] = document.getElementById(id);
        this.li[id] = this.ref[id].getElementsByTagName("li");
        this.sel[id] = 0;
        this.obj[id] = new list_expander(id);
    }
}

// create expand_object
//
function list_expander(id) {

    // remember if ul was clicked
    //
    this.ulclick = true;

    // if other list is open close first
    //
    if (_select.curr) {
        this.reset_list();
    }

    // remember current id
    // get parent element and set z-index
    //
    _select.curr = id;
    var parent = _select.ref[id].parentNode;
    _select.ref[id].style.display = "";
    parent.style.zIndex = "200";

    // init event handler
    //
    this.set_ul_event_handler(id);
    this.set_document_onkeyup(id);
    this.set_document_onclick(id);
    this.set_document_onkeydown(id);
}

// set eventhandler for document onkeydown
// just cancel browser's standard behavior
// because we use arrow keys for dhtml-
// select up and down
//
list_expander.prototype.set_document_onkeydown = function(id) {

    document.onkeydown = function(e) {
        var e = e ? e : window.event;
        return (e.keyCode == 38 || e.keyCode == 40) ? false : true;
    }
}

// set eventhandler for document onkeyup
// a closure keeps object and some properties
//
list_expander.prototype.set_document_onkeyup = function(id) {

    var obj = this;
    document.onkeyup = function(e) {

        obj.e = e;
        obj.id = id;
        obj.handle_document_onkeyup();
        return false;
    }
}

// execute onkeyup eventhandler
// find out which key was pressed: arrows or enter
// do tasks depending on key
//
list_expander.prototype.handle_document_onkeyup = function() {

    var id = this.id;
    var e = this.e ? this.e : window.event;

    // arrow up or down
    //
    switch (e.keyCode) {

        case 38:
            this.select_up(id);
            break;
        case 40:
            this.select_down(id);
            break;
        case 13:
            var li_url = _select.li[id][_select.sel[id]].getElementsByTagName("a")[0].href;
            window.open(li_url);
            break;
        default:
            return false;
    }
    return false;
}

// set eventhandler for document onkeyup
// a closure keeps object and some properties
//
list_expander.prototype.set_document_onclick = function(id) {

    var obj = this;
    document.onclick = function(e) {
        obj.e = e;
        obj.id = id;

        // do reset only if click outside of ul
        // set flag false
        //
        if (!obj.ulclick) {
            obj.reset_list();

            /*
            // testout
            //
            var txt =  document.createTextNode("doc_onclick");
            var br  =  document.createElement("br");
            document.getElementsByTagName("p")[0].appendChild(br);
            document.getElementsByTagName("p")[0].appendChild(txt);
            */
        }
        obj.ulclick = false;
    }
}

// reset list and hide it
// called by document onclick or in case different lists get opened
//
list_expander.prototype.reset_list = function() {

    var curr = document.getElementById(_select.curr);
    var parent = curr.parentNode;
    curr.style.display = "none";
    parent.style.zIndex = "100";
    curr.onclick = null;
    document.onclick = null;
    document.onkeyup = null;
    document.onkeydown = null;
    _select.curr = false;
    this.ulclick = true;
}

// set eventhandler for document onkeyup
// a closure keeps object and some properties
//
list_expander.prototype.set_ul_event_handler = function(id) {

    var obj = this;

    // reset flag to remember ul onclick and stop document onclick
    //
    _select.ref[id].onclick = function(e) {
        obj.ulclick = true;
    }

    // loop all li elements within list
    // set onmouseover/out and onclick eventhandler
    // blur a tag
    //
    for (var i = 0; i < _select.li[id].length; i++) {

        // set or reset classes list items
        //
        if (i == 0) _select.li[id][i].className = "sel";
        else _select.li[id][i].className = "norm";

        _select.li[id][i].onmouseover = function() {
            this.className = "sel";
        }

        _select.li[id][i].onmouseout = function(e) {
            this.className = "norm";
        }


        _select.li[id][i].onclick = function() {
            window.open(this.getElementsByTagName("a")[0].href);
        }

        var a = _select.li[id][i].getElementsByTagName("a")[0];
        a.onclick = function() {
            //this.blur();
            return false;
        }
    }
    return false;
}

// set selected list item when down key was pressed
//
list_expander.prototype.select_down = function(id) {

    _select.sel[id]++;
    if (_select.sel[id] == _select.li[id].length) _select.sel[id] = 0;
    this.select_scroll(id);
    this.select_sel(id);
}

// set selected list item when up key was pressed
//
list_expander.prototype.select_up = function(id) {

    _select.sel[id]--;
    if (_select.sel[id] < 0) _select.sel[id] = _select.li[id].length - 1;
    this.select_scroll(id);
    this.select_sel(id);
}

// reset al list element's classes exept selected item
//
list_expander.prototype.select_sel = function(id) {

    for (var i = 0; i < _select.li[id].length; i++) {
        _select.li[id][i].className = (_select.sel[id] == i) ? "sel" : "norm";
    }
}

// set scroll top by counting out ul and list hights and offset tops
//
list_expander.prototype.select_scroll = function(id) {

    var li_pos = _select.li[id][_select.sel[id]].offsetTop;
    var li_ht = _select.li[id][_select.sel[id]].offsetHeight;
    var ul_ht = _select.ref[id].offsetHeight;

    if (li_pos > ul_ht - li_ht) {
        _select.ref[id].scrollTop = li_pos - ul_ht + li_ht;
    } else {
        _select.ref[id].scrollTop = 0;
    }
}
