/* This notice must be untouched at all times.

menu.js    v.1.0

Copyright (c) 2005 Mikrobeta Software Corp. All rights reserved.
Created by Milas Development Team (Web: http://www.milasweb.com)
Last modified: 10.6.2005

*/

/* browser detection will be done in main.js
var agt=navigator.userAgent.toLowerCase();
var is_ie   = (agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1);
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
        && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
        && (agt.indexOf('webtv')==-1));
var is_opera = (agt.indexOf("opera") != -1) && (agt.indexOf("msie") == -1);
*/


// search option functions

function options_title_over(id)
{
    document.getElementById(id).className = "search_option_title_hover";
}

function options_title_out(id)
{
    if (document.getElementById(id + "_container").style.display) {
        document.getElementById(id).className = "search_option_title";
    }
}

function options_title_clicked(id)
{
    document.getElementById(id).className = "search_option_title_hover";

    if (document.getElementById(id + "_container").style.display) {
        foldOpen(id);
    } else {
        foldClose(id);
    }

}

function foldClose(id)
{
    document.getElementById(id).className = "search_option_title";
    document.getElementById(id + "_container").style.display = 'none';
    document.getElementById(id + "_icon").className = "fold_close";
}

function foldOpen(id)
{
    document.getElementById(id).className = "search_option_title_hover";
    document.getElementById(id + "_container").style.display = '';
    document.getElementById(id + "_icon").className = "fold_open";
}

function resetFolds(foldList)
{
    foldNum = foldList.length;
    for (i=0; i<foldNum; i++) {
        foldList[i][1]? foldOpen(foldList[i][0]):  foldClose(foldList[i][0]);
    }
}


/* --- grid menu --- */

var MENU_OBJ = [];
var gridMenuCounter = 0;
var lastActiveMenuIdx = null;
var windowSpecs = {width: 0, height: 0, scrollTop: 0, scrollLeft: 0};

function milas_grid_menu(cssPrefix, menuItems)
{
    gridMenuCounter++;
    
    this.idx = gridMenuCounter;

    this.id = "menu_" + this.idx;
    this.selected = false;
    this.timeoutID = null;
    this.Add = menu_add;
    this.cssPrefix = cssPrefix + "_";
    this.resetTimer = menu_reset_hide_timer;
    this.titleOver = menu_title_over;
    this.titleOut = menu_title_out;
    this.itemsShow = menu_items_show;
    this.itemsHide = menu_items_hide;
    this.itemOver = menu_item_over;
    this.itemOut = menu_item_out;

    MENU_OBJ[this.idx] = this;
    this.Add(menuItems);
}

function locateWindowSpecs()
{
    if (document.body && ( document.body.scrollTop || document.body.scrollLeft )) {
        windowSpecs.scrollLeft = document.body.scrollLeft;
        windowSpecs.scrollTop = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft )) {
        windowSpecs.scrollLeft = document.documentElement.scrollLeft;
        windowSpecs.scrollTop = document.documentElement.scrollTop;
    }

    if (window.innerWidth || window.innerHeight) {
        windowSpecs.width = window.innerWidth;
        windowSpecs.height = window.innerHeight;
    } else if (document.body.clientWidth || document.body.clientHeight) {
        windowSpecs.width = document.body.clientWidth;
        windowSpecs.height = document.body.clientHeight;
    }
}

function menu_add(menuItems) {
    var menuID = "menu_" + this.idx;
    
    writeThis = "";
    writeThis += "<div id=\"" + menuID + "\" class=\"" + this.cssPrefix + "menu_title\" ";
    writeThis += "onClick=\"MENU_OBJ[" + this.idx + "].itemsShow()\"";
    writeThis += "onMouseOver=\"MENU_OBJ[" + this.idx + "].titleOver()\"";
    writeThis += "onMouseOut=\"MENU_OBJ[" + this.idx + "].titleOut()\"";
    writeThis += ">" + menuItems[0] + "</div>";

    writeThis += "<div id=\"" + menuID + "_items\" class=\"" + this.cssPrefix + "menu_items\" ";
    writeThis += "onMouseOver=\"MENU_OBJ[" + this.idx + "].itemOver()\" onMouseOut=\"MENU_OBJ[" + this.idx + "].itemOut()\">";

    writeThis += "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">";
    for (i=0; i<menuItems[1].length; i++) {
        writeThis += "<tr><td>";
        writeThis += "<table class=\"" + this.cssPrefix + "menu_items\"><tr><td class=\"items\">";
        writeThis += "<a class=\""+this.cssPrefix+"menu_item\" href=\""+menuItems[1][i][1]+"\">"+menuItems[1][i][0]+"</a>";
        writeThis += "</td></tr></table>";
        writeThis += "</td></tr>";
    }
    writeThis += "</table></div>";
    document.write(writeThis);
}

function menu_reset_hide_timer() {
    clearTimeout(this.timeoutID);
    this.timeoutID = setTimeout("MENU_OBJ[" + this.idx + "].itemsHide()", 200);
}

function menu_title_over()
{
    if (!this.selected) {
        if (lastActiveMenuIdx != null) MENU_OBJ[lastActiveMenuIdx].itemsHide();
        document.getElementById(this.id).className = this.cssPrefix + "menu_title_hover";
    }
}

function menu_title_out()
{
    if (this.selected) {
        this.resetTimer();
    } else {
        document.getElementById(this.id).className = this.cssPrefix + "menu_title";
    }
}

function menu_items_show()
{
    this.selected = true;

    document.getElementById(this.id).className = this.cssPrefix + "menu_title_selected";
    var itemsDiv = document.getElementById(this.id + "_items");
    var titleDiv = document.getElementById(this.id);
    
    locateWindowSpecs();

    if (itemsDiv.offsetWidth < 200) {
        itemsDiv.style.width = "200px";
    }

    if (titleDiv.offsetWidth > itemsDiv.offsetWidth) {
        itemsDiv.style.width = titleDiv.offsetWidth + "px";
    }
    
    if ((itemsDiv.offsetLeft + itemsDiv.offsetWidth) > (windowSpecs.width + windowSpecs.scrollLeft)) {
        itemsDiv.style.left = (itemsDiv.offsetLeft - itemsDiv.offsetWidth + titleDiv.offsetWidth) + "px";
    }
    
    lastActiveMenuIdx = this.idx;
    itemsDiv.style.visibility = "visible";
    
    if (is_ie) {
        var iframeObj = document.getElementById("tooltip_iframe");

        iframeObj.style.top    = itemsDiv.offsetTop + "px";
        iframeObj.style.left   = itemsDiv.offsetLeft + "px";
        iframeObj.style.width  = itemsDiv.offsetWidth + 3 + "px";
        iframeObj.style.height = itemsDiv.offsetHeight + 3 +"px";
        
        iframeObj.style.visibility = "visible";
    }
}

function menu_items_hide()
{
    document.getElementById(this.id).className = this.cssPrefix + "menu_title";
    document.getElementById(this.id + "_items").style.visibility = "hidden";
    if (is_ie) document.getElementById("tooltip_iframe").style.visibility = "hidden";
    this.selected = false;
}

function menu_item_over()
{
    clearTimeout(this.timeoutID);
}

function menu_item_out()
{
    this.resetTimer();
}

/* --- fold menu --- */

var fMenuIdx=0;
var fMenuList = [];

function foldMenu(independentFolds, cssPrefix)
{
    fMenuIdx++; 
    fMenuList[fMenuIdx] = this;
    
    this.fMenuFoldIdx = 0;
    
    this.idx = fMenuIdx;
    this.cssPrefix = cssPrefix;
    this.independentFolds = independentFolds? true: false;
    this.menuFolds = new Array();
    this.titleClicked = fmenu_title_clicked;
    this.titleMouseOver = fmenu_title_mouse_over;
    this.titleMouseOut = fmenu_title_mouse_out;
    this.itemMouseOver = fmenu_item_mouse_over;
    this.itemMouseOut = fmenu_item_mouse_out;
    this.itemClicked = fmenu_item_clicked;
    this.Add = fmenu_add;
    this.Open = fmenu_open;
    this.Close = fmenu_close;
    this.toggleStatus = fmenu_toggle_fold_status;
    this.CloseOthers = fmenu_close_others;
    this.openLink = fmenu_open_link;
}

function fmenu_add(label, link, items, foldOpen)
{
    this.fMenuFoldIdx++;
    this.menuFolds[this.fMenuFoldIdx] = new Object();
    
    var writeThis = '';

    writeThis += "<div class=\"fmenu_container\">";
    writeThis += "<div id=\"fmenu_" + fMenuIdx + "_" + this.fMenuFoldIdx + "\"";
    writeThis += "class=\"" + this.cssPrefix + "fmenu_title_normal\" ";
    writeThis += "onclick=\"fMenuList[" + fMenuIdx + "].titleClicked(this.id";
    if (((link !=null) && typeof link == 'string')) {
        this.menuFolds[this.fMenuFoldIdx]['type'] = "link";
    	writeThis += ", '" + link.replace(/([^\\])(['])/g, "$1\\$2") + "'";
    }
    writeThis += ")\" ";
    writeThis += "onmouseover=\"fMenuList[" + fMenuIdx + "].titleMouseOver(this.id)\" ";
    writeThis += "onmouseout=\"fMenuList[" + fMenuIdx + "].titleMouseOut(this.id)\">";
    writeThis += label;
    writeThis += "</div>";
    if ((link == null)  && ((items != null) && (typeof items == "object"))) {
        this.menuFolds[this.fMenuFoldIdx]['type'] = "fold";
        
    	writeThis += "<div id=\"fmenu_" + fMenuIdx + "_" + this.fMenuFoldIdx + "_items\" ";
    	writeThis += "class=\"" + this.cssPrefix + "fmenu_items\">";
    	for (i=0; i < items.length; i++) {
            writeThis += "<div id=\"fmenu_" + fMenuIdx + "_" + this.fMenuFoldIdx + "_" + i + "_item\" ";
            writeThis += "class=\"" + this.cssPrefix + "fmenu_item_normal\" ";
            writeThis += "onclick=\"fMenuList[" + fMenuIdx + "].itemClicked('";
            writeThis += items[i][1].replace(/([^\\])(['])/g, "$1\\$2");
            writeThis += "')\" ";
            writeThis += "onmouseover=\"fMenuList[" + fMenuIdx + "].itemMouseOver(this.id)\" ";
            writeThis += "onmouseout=\"fMenuList[" + fMenuIdx + "].itemMouseOut(this.id)\">";
            writeThis += items[i][0] + "</div>";
        }
        writeThis += "</div>";
    }
    writeThis += "</div>";
    
    document.write(writeThis);
    
    this.menuFolds[this.fMenuFoldIdx]['status'] = 'closed';
    if (this.menuFolds[this.fMenuFoldIdx].type == "fold") {
        if (foldOpen) {
            this.Open(this.fMenuFoldIdx);
        } else {
            this.Close(this.fMenuFoldIdx);
        }
    } else {
        this.Close(this.fMenuFoldIdx);
    }
}

function fmenu_open_link(link)
{
    parent.location = link;
}

function fmenu_open(foldIdx)
{
    if (this.menuFolds[foldIdx].type == 'fold') {
        this.menuFolds[foldIdx].status = 'opened';
        
        document.getElementById("fmenu_" + this.idx + "_" + foldIdx).className = this.cssPrefix + "fmenu_title_hover";
        document.getElementById("fmenu_" + this.idx + "_" + foldIdx + "_items").style.display = '';
    }
}

function fmenu_close(foldIdx)
{
    if (this.menuFolds[foldIdx].type == 'fold') {
        this.menuFolds[foldIdx].status = 'closed';
        document.getElementById("fmenu_" + this.idx + "_" + foldIdx).className = this.cssPrefix + "fmenu_title_normal";
        document.getElementById("fmenu_" + this.idx + "_" + foldIdx + "_items").style.display = 'none';
    }
}

function fmenu_toggle_fold_status(foldIdx)
{
    if (this.menuFolds[foldIdx].type == 'fold') {
        if (this.menuFolds[foldIdx].status == 'closed') {
            this.Open(foldIdx);
        } else {
            this.Close(foldIdx);
        }
    }
}

function elementGetGroupID(elementID)
{
    return null;
}

function id2foldIdx(id)
{
    if (typeof id != "string") {
        return false;
    }

    if (id.match(/fmenu_([0-9]+)_([0-9]+)/)) {
        return id.replace(/fmenu_([0-9]+)_([0-9]+)/, "$2");
    } else {
        return false;
    }
}

function fmenu_close_others(foldIdx)
{
    for (var i in this.menuFolds) {
        if (i != foldIdx) {
            this.Close(i);
        }
    }
    if (this.menuFolds[foldIdx].status == 'closed') {
        this.Open(foldIdx);
    } else {
        this.Close(foldIdx);
    }
}

function fmenu_title_mouse_over(id)
{
    var foldIdx = id2foldIdx(id);
    
    if (this.menuFolds[foldIdx].status == 'closed') {
        document.getElementById(id).className = this.cssPrefix + "fmenu_title_hover";
    }
}

function fmenu_title_mouse_out(id)
{
    var foldIdx = id2foldIdx(id);
 
    if (this.menuFolds[foldIdx].status == 'closed') {
        document.getElementById(id).className = this.cssPrefix + "fmenu_title_normal";
    }
}

function fmenu_title_clicked(id, link)
{
    var foldIdx = id2foldIdx(id);
    
    if (link == null) {
        if (this.independentFolds) {
            this.toggleStatus(foldIdx);
        } else {
            this.CloseOthers(foldIdx);
        }
    } else {
        this.openLink(link);
    }
}

function fmenu_item_mouse_over(id)
{
    if (id.substr(id.length - 5, 5) == "_item") {
        document.getElementById(id).className = this.cssPrefix + "fmenu_item_hover";
    }
}

function fmenu_item_mouse_out(id)
{
    if (id.substr(id.length - 5, 5) == "_item") {
        document.getElementById(id).className = this.cssPrefix + "fmenu_item_normal";
    }
}

function fmenu_item_clicked(link)
{
    this.openLink(link);
}


