// Andre Lei - mouseOver_pagination
// v4 2003.Jan.07

var ROWS_PER_PAGE = 20;
var bActiveMenu = false;
var hideIntervalId;
var hideTime = 500; // milliSeconds

/*
Menu( _id, _targetId, _imgTargetId, _width, _offsetX, _offsetY )
var m0 = new Menu("m0","menuFloater","imgAttribute_0",100, 7, 10);
m0.addMenuItem("Acura", "custom('foo bar')");
m0.addMenuItem("BMW", "alert('BMW')");

var m1 = new Menu("m1","menuFloater","imgAttribute_1",100, 7, 10);
m1.addMenuItem("Convertible", "custom('Convertible')");
m1.addMenuItem("Sedan", "alert('Sedan')");
m1.addMenuItem("Station Wagon", "alert('Station Wagon')");
m1.addMenuItem("SAV", "alert('SAV')");
m1.addMenuItem("SUV", "alert('SUV')");
m1.addMenuItem("Crossover", "alert('Crossover')");
m1.addMenuItem("Truck", "alert('Truck')");
m1.addMenuItem("RV", "alert('RV')");
*/

// support for browsers not supporting Array.push
if( typeof Array.prototype.push == 'undefined' ) {
    Array.prototype.push=function(){
        var i=0;
        b=this.length,a=arguments;
        for( i; i<a.length; i++ ) {
            this[b+i]=a[i];
        }
        return this.length;
    };
}

function Menu( _id, _targetId, _imgTargetId, _width, _offsetX, _offsetY ) {
    this.id = _id;
    this.targetId = _targetId;
    this.imgTargetId = _imgTargetId;
    this.width = _width;
    this.offsetX = _offsetX;
    this.offsetY = _offsetY;
    this.labels = new Array();
    this.actions = new Array();
    this.addMenuItem = addMenuItem;
    this.positionMenu = positionMenu;
    this.writeMenu = writeMenu;
//introspectObj(this);
}
// need to search and replace the single and/or double quotess
function addMenuItem(label, action) {
    this.labels[this.labels.length] = label;
    this.actions[this.actions.length] = action;
}

function writeMenu( divTarget, pageNum ) {
    if( pageNum == null ) {
        pageNum = 1;
    }
    var totalPages = Math.ceil(this.labels.length / ROWS_PER_PAGE);
    var startCount = (pageNum - 1) * ROWS_PER_PAGE;
    var endCount = pageNum * ROWS_PER_PAGE;
    var aFullMenu = new Array();
    var tempVar = 0;

    for( var i=startCount; i<endCount; i++ ) {
        // out label unless it's over the array count
        if( i<this.labels.length ) {
            tempVar = aFullMenu.push("<div class='row' style='width:" + this.width + "px;'>&nbsp;<a href=\"javascript:" + this.actions[i] + ";\" class='rowItem'>" + this.labels[i] + "</a> </div>");
        } else {
            // buffer till pagination to have consistent level, Netscapes wants &nbsp;
            // don't buffer if only 1 page of data
            if( totalPages > 1 ) {
                tempVar = aFullMenu.push("<div class='row' style='width:" + this.width + "px;'>&nbsp;</div>");
            }
        }
    }

    //add the pagination div(s)
    // display pagination only if more than 1 page
    if( totalPages > 1 ) {
        var pageClassName;
        tempVar = aFullMenu.push("<div class='row' style='width:" + this.width + "px;'>");
        // previous link
        if( pageNum != 1 && totalPages > 1 ) {
            tempVar = aFullMenu.push(" <a href=\"javascript:showMenu('" + this.id + "'," + (pageNum-1) + ");\"  class='page'>Prev</a>");
        }

        for( var p=1; p<=totalPages; p++ ) {
            if( pageNum == p ) {
                pageClassName = "curPage";
            } else {
                pageClassName = "page";
            }
            // some trouble here without menu reference, need the quotes in this format to avoid conflicts
            tempVar = aFullMenu.push(" <a href=\"javascript:showMenu('" + this.id + "'," + p + ");\"  class='" + pageClassName + "'>" + p + "</a> ");
        }

        // next link
        if( pageNum != totalPages && totalPages > 1 ) {
            tempVar = aFullMenu.push(" <a href=\"javascript:showMenu('" + this.id + "'," + (pageNum+1) + ");\"  class='page'>Next</a> ");
        }
        tempVar = aFullMenu.push("</div>");
    }
//alert(aFullMenu.join("\n"));
    divTarget.innerHTML = aFullMenu.join("");
}

function showMenu( menuList, pageNum ) {
    var x = eval(menuList);
    var menu = document.getElementById(x.targetId);
    menu.style.display = "block";
    //introspectObj(x);
    x.positionMenu(menu);
    x.writeMenu(menu, pageNum);
    retainMenu();
}

function positionMenu( divTargetObj ) {
    var pos = getXYcoord( this.imgTargetId );
    divTargetObj.style.left = pos.x + this.offsetX;
    divTargetObj.style.top = pos.y + this.offsetY;
}

function getXYcoord ( imgName ) {
    var elm = document.images[imgName];
    // NS4 images contain x and y values
    if ( document.layers ) {
        return elm;
    }

    var coordObj = { x:0 ,y:0 };
    do {
        coordObj.x += parseInt( elm.offsetLeft );
        coordObj.y += parseInt( elm.offsetTop );
        elm = elm.offsetParent;
    }
    while ( elm );
    return coordObj
}

function retainMenu() {
	//alert('test');
    bActiveMenu = true;
    if( hideIntervalId ) {
        clearInterval(hideIntervalId);
    }
}

function hideMenu() {
    //alert(bActiveMenu);
    // call to hide
    if( bActiveMenu ) {
        hideIntervalId = setInterval('doHideMenu()', hideTime);
        bActiveMenu = false;
    }
}

function doHideMenu() {
    var menu = document.getElementById("menuFloater");
    menu.style.display = "none";
    if( hideIntervalId ) {
        clearInterval(hideIntervalId);
    }
}

function custom( txt ) {
    alert("some user defined function passing in: " + txt);
}

function hilite(e, _className) {
    e.className = _className;
    var children = e.childNodes;
    //alert(e.childNodes.length);
    for( i=0; i<children.length; i++ ) {
        var curNode = children[i];
        if( curNode.tagName == "A" ) {
            //alert(curNode.className);
                curNode.className="navItem_on";
                //alert(curNode.getAttribute("type"));
                //alert('i=' + i + ' j=' + j + ' tagName=' + grandChildren[j].tagName);
        }
    }

/*
    if( ie ) {
        while (E.tagName!="TR")
        {E=E.parentElement;
        alert(E);}
    } else {
        while (E.tagName!="TR")
        {E=E.parentNode;}
    }
    E.className = "normalRow";

    // loop through child elements and mark the input as strike through
    var children = E.childNodes;
    //alert(E.childNodes.length);
    // tr loop for tds
    for( i=0; i<children.length; i++ ) {
        //alert(children[i].tagName);
        var grandChildren = children[i].childNodes;
        // td loop for input

        //alert(grandChildren.length);
        for( j=0; j<grandChildren.length; j++ ) {
            var curNode = grandChildren[j];
            if( curNode.tagName == "INPUT" && curNode.getAttribute("type") == 'text' ) {
                curNode.className="input";
                //alert(curNode.getAttribute("type"));
                //alert('i=' + i + ' j=' + j + ' tagName=' + grandChildren[j].tagName);
            }
        }
    }
    */
}