var active_menu       = null ;
var selected_menu     = null ;
var menu_out_time_out = -1 ;

var mouse_x = 0 ;
var mouse_y = 0 ;

var is_mac  = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false ;
var is_ie   = (document.all)                            ? true : false ;

function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3.0 Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    }
    // 3.3 ... Opera changes colors set via HTML to rgb(r,g,b) format so fix it
    if (currentColor.indexOf("rgb") >= 0) 
    {
        var rgbStr = currentColor.slice(currentColor.indexOf('(') + 1,
                                     currentColor.indexOf(')'));
        var rgbValues = rgbStr.split(",");
        currentColor = "#";
        var hexChars = "0123456789ABCDEF";
        for (var i = 0; i < 3; i++)
        {
            var v = rgbValues[i].valueOf();
            currentColor += hexChars.charAt(v/16) + hexChars.charAt(v%16);
        }
    }

    // 4.0 Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // Garvin: deactivated onclick marking of the checkbox because it's also executed
            // when an action (like edit/delete) on a single item is performed. Then the checkbox
            // would get deactived, even though we need it activated. Maybe there is a way
            // to detect if the row was clicked, and not an item therein...
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
            // document.getElementById('id_rows_to_delete' + theRowNum).checked = false;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function


function set_mouse_pos ( e ) {
    if (is_ie) {
        mouse_x = event.clientX + document.body.scrollLeft ;
        mouse_y = event.clientY + document.body.scrollTop ;
    } else {
        mouse_x = e.pageX
        mouse_y = e.pageY
    }
    if (mouse_x < 0){mouse_x = 0}
    if (mouse_y < 0){mouse_y = 0}
    return true
}



function menu_item_onmouseover () {

    this.className =  'menu_item_over' ;

    if ( active_menu != null ) {
        active_menu.style.visibility = 'hidden' ;
        selected_menu.className = 'menu_item' ;
    }
    var menu_name = 'menu_' + this.id ;
    var menu_element = document.getElementById ( menu_name ) ;

    var x_cour = 0 ;
    var x_rel = 0 ;
    var elem_cour = this ;
    while ( elem_cour ) {
        x_cour += elem_cour.offsetLeft ;
        if ( elem_cour.id == 'menu' ) {
            x_rel = x_cour ; // break ;
        }
        elem_cour = elem_cour.offsetParent ;
    }
    if ( x_rel + menu_element.offsetWidth > 775 ) {
        var _width = menu_element.offsetWidth ;
        if ( is_ie ) {
            menu_element.style.pixelLeft = x_cour - x_rel + 770 - menu_element.offsetWidth ;
        }
        else {
            menu_element.style.left = x_cour - x_rel + 770 - menu_element.offsetWidth ;
        }
        // menu_element.style.width = _width ;
    }
    else {
        // menu_element.style.width = 770 - x_rel ;
        if ( is_ie ) {
            menu_element.style.pixelLeft = x_cour ;
        }
        else {
            menu_element.style.left = x_cour ;
        }

    }

    //if ( is_mac ) {
    //    var main_page = document.getElementById( 'main_page' ) ;
    //    menu_element.style.pixelLeft = main_page.offsetLeft + 50 ;
    //}

    var menu2 = document.getElementById ( "menu2" ) ;
    menu_element.style.pixelTop = 0 ;

    elem_cour = menu2 ;
    while ( elem_cour ) {
        if ( elem_cour.offsetTop > 0 ) {
            menu_element.style.pixelTop += elem_cour.offsetTop ;
            //menu_element.style.pixelLeft += elem_cour.offsetLeft ;
        }
        elem_cour = elem_cour.offsetParent ;
    }

    if ( ! is_ie ) {
        menu_element.style.top = menu_element.style.pixelTop ;

    }

    menu_element.style.visibility = 'visible' ;
    active_menu = menu_element ;
    selected_menu = this ;
}



function sub_menu_item_onmouseover () {
    if (is_mac) {
        active_menu.style.pixelTop = 29 ;
        active_menu.style.pixelLeft = selected_menu.offsetLeft + 50 ;
    }
    this.className = ( this.className == 'sub_menu_item' ) ? 'sub_menu_item_over' : 'sub_menu_first_item_over' ;
}



function sub_menu_item_onmouseout () {
    this.className = ( this.className == 'sub_menu_item_over' ) ? 'sub_menu_item' : 'sub_menu_first_item' ;
    if (is_mac) {
        active_menu.style.pixelTop = 29 ;
        active_menu.style.pixelLeft = selected_menu.offsetLeft + 50 ;
    }
}



function sub_menu_item_onclick () {
    var href =  this.id ;
    // var href = (this.href) ? this.href : this.getAttribute( 'href' ) ;

    if ( this.target ) {
        window.open( href , this.target ) ;
    }
    else {
        self.location = href ;
    }
}



function hide_menu () {
    active_menu.style.visibility = 'hidden' ;
    selected_menu.className = 'menu_item' ;
}



function menu_out () {
    if ( menu_out_time_out != -1 ) {
        clearTimeout ( menu_out_time_out ) ;
        menu_out_time_out = -1 ;
    }
    var menu_table = document.getElementById ( 'menu' ) ;
    var x = 0 ;
    var y = 0 ;
    var elem_cour = menu_table ;
    while ( elem_cour ) {
        x += elem_cour.offsetLeft ;
        y += elem_cour.offsetTop ;
        elem_cour = elem_cour.offsetParent ;
    }
    if ( active_menu != null ) {
        if ( mouse_y > ( y + menu_table.offsetHeight + active_menu.offsetHeight ) ) {
            active_menu.style.visibility = 'hidden' ;
            selected_menu.className = 'menu_item' ;
            //active_menu = null ;
            //selected_menu = null ;
        }
        else {
            menu_ou_time_out = setTimeout ( "menu_out()", 500 ) ;
        }
    }
}



function btn_lang_over ( ) {
    this.className = ( this.className == 'lang_available' ) ? 'lang_over' : this.className ;
}
function btn_lang_out ( ) {
    this.className = ( this.className == 'lang_over' ) ? 'lang_available' : this.className ;
}
function btn_lang_click ( ) {
    window.location = this.id ;
}



function initialize () {
   var menu_elements = document.getElementsByTagName("td") ;
   for (i=0 ; i<menu_elements.length ; i++) {
       var element = menu_elements[i] ;
       if ( element.className == "menu_item" ) {
           element.unselectable = "on";
           element.onmouseover = menu_item_onmouseover ;
           element.onmouseout  = menu_out ;
       }
       if ( element.className == "sub_menu" ) {
           element.unselectable = "on" ;
           element.onmouseout  = menu_out ;
       }
       if ( element.className == "sub_menu_item" || element.className == "sub_menu_first_item" ) {
           element.unselectable = "on";
           element.onmouseover = sub_menu_item_onmouseover ;
           element.onmouseout  = sub_menu_item_onmouseout ;
           element.onclick     = sub_menu_item_onclick ;
       }
   }
}



function pop_window( url, name, prop ) {
  var popit = window.open(url,name,prop);
}



document.onmousemove = set_mouse_pos ;




