/****************************************************************************************/
/*  Copyright (C) 2004 Rob Johnson <robjohnson@black-hole.com>                          */
/*                                                                                      */
/*  This program IS NOT free software; you may not use, redistribute or modify          */
/*  without the permission of the author.  For licensing and purchasing information     */
/*  please visit http://www.script-source.net/ .                                        */
/*                                                                                      */
/*  For more information and a demo please visit http://www.script-source.net/ .        */
/****************************************************************************************/


function menuClass() {
        this.menus = new Array();
        this.rolldowndelay = 25;
        this.rollupdelay = 25;
        this.menuheadClassName = '';
        this.menuItemClassName = '';
        this.menuitemselClassName = '';
        this.menuItemBullet = '';
        this.menuItemSpacing = 0;
        this.cookiename = 'nav';
        this.withid = 0;
        this.initflag = false;
        this.uniquekey = '';
        this.menustate = new Array();

        if(!window.menulist) window.menulist = new Array();
        if(!window.menuitemlist) window.menuitemlist = new Array();

        this.init = function() {
                if(this.initflag) return;

                // make a unique id for this menu
                var sum = len = 0;
                for(var m=0; m<this.menus.length; ++m) {
                        len += this.menus[m].menutitle.length;
                        sum += this.stringsum(this.menus[m].menutitle);
                         for(var i=0; i<this.menus[m].menuitems.length; ++i) {
                                sum += this.stringsum(this.menus[m].menuitems[i].itemtitle);
                                len += this.menus[m].menuitems[i].itemtitle.length;
                        }
                }
                this.uniquekey = sum + len;

                // parse cookies
                var ckname = this.cookiename + this.uniquekey;
                var start = document.cookie.indexOf(ckname);
                if(start > -1) {
                        start += ckname.length + 1;
                        var end = document.cookie.indexOf(';', start); if(end == -1) end = document.cookie.length;
                        var data =  document.cookie.substring(start, end);
                        var bits = data.split('|');

                        var menustate = new Array();
                        for(var b=0; b<bits.length; ++b) {
                                var keyval = bits[b].split(':');
                                keyval[1] *= 1;
                                menustate[keyval[0]] = keyval[1];
                        }
                        this.menustate = menustate;


                }

                // set display status for menus
                for(var m=0; m<this.menus.length; ++m) {
                        this.menus[m].status = (this.menustate[m]) ? 1 : 0;
                }

                // highlight current page link
                if(this.menuitemselClassName) {
                        var keyname = this.cookiename + 'click=';
                        var start = document.location.href.indexOf(keyname);
                        if(start > -1) {
                                start += keyname.length;
                                var end = document.location.href.indexOf('&', start);
                                if(end < 0) end = document.location.href.indexOf('#', start);
                                if(end < 0) end = document.location.href.length;
                                var data = document.location.href.substring(start, end);
                                var bits = data.split('-');

                                if(bits.length == 3 && (1 * bits[0]) == this.uniquekey) {
                                        bits[1] *= 1; bits[2] *= 1;
                                        if(typeof(this.menus[bits[1]]) != 'undefined' && typeof(this.menus[bits[1]].menuitems[bits[2]]) != 'undefined') {
                                                this.menus[bits[1]].status = 1;
                                                this.menus[bits[1]].menuitems[bits[2]].selected = 1;
                                        }
                                }
                        }
                }
        }
        this.stringsum = function(str) {
                val = 0; for(var i=0; i<str.length; ++i) { val += str.charCodeAt(i); } return val;
        }

        this.write = function() {
                this.init();

                var buf = '';
                buf += '<table style="border:none;" cellpadding="0" cellspacing="'+this.menuItemSpacing+'">';

                var bullet = '';
                if(this.menuItemBullet) {
                        bullet = '<img src="'+this.menuItemBullet+'" border="0" alt="" align="absmiddle"> ';
                }
                for(var m=0; m<this.menus.length; ++m) {

var url=document.URL;
var url_array = url.split(".php");
url=url_array[0];
var url_array = url.split(".com/");
url=url_array[1];

var site_array  = new Array("Referenzen","News");
for ( i = 0; i <= 1; i++)

if (site_array[i] == url)
this.menus[i].classname = "menuheadsel"
else {
if (this.menus[i].status==1)
this.menus[i].classname = "menuhead2"
else {this.menus[i].classname = "menuhead"}
};

var display = (this.menus[m].status) ? '' : 'display:none;';
buf +='<tr><td class="'+this.menus[m].classname+'" id="'+site_array[m]+'"><a href="javascript:void(0);" onclick="window.menulist['+this.menus[m].menuid+'].click()">'+this.menus[m].menutitle+'</a></td></tr>';
                         for(var i=0; i<this.menus[m].menuitems.length; ++i) {
                                var classname = (this.menuitemselClassName && this.menus[m].menuitems[i].selected) ? this.menuitemselClassName : this.menus[m].menuitems[i].classname;
                                buf += '<tr id="menuitem_'+this.menus[m].menuitems[i].menuitemid+'" style="'+display+'"><td id="td_menuitem_'+this.menus[m].menuitems[i].menuitemid+'" class="'+classname+'">'+bullet+'<a href="'+this.menus[m].menuitems[i].itemhref+'" target="'+this.menus[m].menuitems[i].itemhreftarget+'" onclick="return window.menuitemlist['+this.menus[m].menuitems[i].menuitemid+'].click(this)">'+this.menus[m].menuitems[i].itemtitle+'</a></td></tr>';
                        }
                }
                buf += '</table>';

                document.write(buf);
        }

        this.savestate = function() {
                var ckname = this.cookiename + this.uniquekey;
                var ckval = '';
                var menustate = new Array();
                for(var m=0; m<this.menus.length; ++m) {
                        ckval += m + ':' + this.menus[m].status + '|';
                        menustate[m] = this.menus[m].status;
                }
                ckval = ckval.substr(0, ckval.length-1);
                document.cookie = ckname +'='+ ckval + ';';
                this.menustate = menustate;
                }

        this.createMenu = function(menutitle, classname) {
                this.createHeading(menutitle, classname);
        }
        this.createHeading = function(menutitle, classname) {
                this.withid = this.menus.length;
                this.menus[this.withid] = new menu(this, menutitle, classname, this.withid);
                this.menus[this.withid].menuid = window.menulist.length;
                window.menulist[this.menus[this.withid].menuid] = this.menus[this.withid];
        }
        this.addItem = function(itemname, itemhref, itemhreftarget, classname) {
                this.menus[this.withid].addItem(itemname, itemhref, itemhreftarget, classname);
        }
}

function menu(parent, menutitle, classname, index) {
        this.parent = parent;
        this.menutitle = menutitle;
        this.classname = (classname) ? classname : this.parent.menuheadClassName;
        this.menuid = 0;
        this.index = index;
        this.menuitems = new Array();
        this.status = 0;
        this.addItem = function(itemname, itemhref, itemhreftarget, classname) {
                var id = this.menuitems.length;
                this.menuitems[id] = new menuItem(this, itemname, itemhref, itemhreftarget, classname, id);
                this.menuitems[id].menuitemid = window.menuitemlist.length;
                window.menuitemlist[this.menuitems[id].menuitemid] = this.menuitems[id];
        }
        this.click = function() {
                if(!this.status) this.expand();
                else this.contract();
        }
        this.expand = function() {
                var curdelay = 0;
                        for(var i=0; i<this.menuitems.length; ++i) {
                        curdelay += this.parent.rolldowndelay;
                        setTimeout("document.getElementById('menuitem_"+this.menuitems[i].menuitemid+"').style.display = '';", curdelay);
        }
                this.status = 1;
                this.parent.savestate();
                var delay = curdelay+this.parent.rolldowndelay;
                //liest den Menutitel aus und springt zur entsprechend benamten URL
                var url=menutitle
                var url_array = url.split("<");
                url=url_array[0]
                setTimeout("document.location.href = '"+url+".php'", delay);
        }
        this.contract = function() {
                var curdelay = 0;
var menuno=menutitle;
var men_array = menuno.split("<");
menuno=men_array[0];
document.getElementById(menuno).style.backgroundImage = "url(./gfx/buttondw.jpg)";
                for(var i=this.menuitems.length-1; i>=0; --i) {
                curdelay += this.parent.rollupdelay;
                setTimeout("document.getElementById('menuitem_"+this.menuitems[i].menuitemid+"').style.display = 'none';", curdelay);
                }

                this.status = 0;

                this.parent.savestate();
        }

}
function menuItem(menuref, itemtitle, itemhref, itemhreftarget, classname, index) {
        this.menu = menuref;
        this.classname = (classname) ? classname : this.menu.parent.menuItemClassName;
        this.itemtitle = itemtitle;
        this.itemhref = itemhref;
        this.itemhreftarget = (itemhreftarget) ? itemhreftarget : '_self';
        this.menuitemid = 0;
        this.selected = 0;
        this.index = index;

        this.click = function(that) {
                this.select();

                // don't bother with this if theres no selected classname
                if(!this.menu.parent.menuitemselClassName) return true;

                // determine if we're leaving this site
                var matches = that.href.match(/https?:\/\/(www\.)?([a-zA-Z0-9\-\.]+)/);
                if(matches && matches.length > 0) {
                        var dl = document.location.href + '';
                        if(dl.toLowerCase().indexOf(matches[2].toLowerCase()) < 0) return true;
                }

                // if the page uses a frameset, we may have already set this
                var keyname = this.menu.parent.cookiename + 'click=';
                if(that.href.indexOf(keyname) > -1) {
                        return true;
                }
                var urldata = keyname + this.menu.parent.uniquekey + '-' + this.menu.index + '-' + this.index;

                var bits = that.href.split('#');
                var targethref = anchor = '';

                if(bits.length > 1) {
                        targethref = bits[0];
                        anchor = bits[1];
                } else {
                        targethref = that.href;
                }

                var hasqs = (targethref.indexOf('?') > -1) ? 1 : 0;
                newhref = targethref;
                newhref += (targethref.indexOf('?') > -1) ? '&' : '?';
                newhref += urldata;
                if(anchor) newhref += '#' + anchor;

                that.href = newhref;

                return true;
        }

        this.select = function() {
                if(this.menu.parent.menuitemselClassName) {
                        for(var m=0; m<this.menu.parent.menus.length; ++m) {
                                for(var i=0; i<this.menu.parent.menus[m].menuitems.length; ++i) {
                                        this.menu.parent.menus[m].menuitems[i].deselect();
                                }
                        }
                        document.getElementById('td_menuitem_' +this.menuitemid).className = this.menu.parent.menuitemselClassName;
                        this.selected = 1;
                }
        }
        this.deselect = function() {
                if(this.menu.parent.menuitemselClassName && this.selected) {
                        document.getElementById('td_menuitem_' +this.menuitemid).className = this.classname;
                        this.selected = 0;
                }
        }
}


