/*
 * Roll table element module
 * @author Alexey Budin
 *
 */


/*
 * Constans. Do not redefine please
 */

// Popup menu layer name
var DEFAULT_POPUP_MENU_NAME = "popupmenu";
var DEFAULT_ACTIVE_ROW_CLASS = "activerow";

// Colors
var DEFAULT_ACTIVE_ROW_BG_COLOR = "#A8C3FF";

/*
 * System vars. Do to use directly - use set / get methods please
 */
// store here row class of active row
var oldRowClass;

// store hre last active row
var activeRow;

// store here the previous (before activating) color of the row
var oldRowBGColor;

//store class name (defined in css file of active row)
var activeRowClass = DEFAULT_ACTIVE_ROW_CLASS;

// active popup menu
var activePopupMenuName = null;

// show if current row is blocked during current click transaction
var isBlockedRowClick = false;

/**
 * Shows which type of mouse out function to use. If value of this variable is true,
 * then system change the class only for the TR element on mouseOut event. Else if change
 * the class for all TD subelements of TR object
 */
var lazyMouseOut = false;

/**
 * This TD element should be omitted when system makes redrawing the TR
 */
var unselectableTD = null;

/*
 *
 * Functions
 *
 */

/*
 * Set the active row class name
 */
function setActiveRowClass(className) {
        activeRowClass = className;
}

/*
 * Get the active row class name
 */
function getActiveRowClass() {
        return activeRowClass;
}

/**
 * @return bg color for active row
 */
function getActiveRowBGColor() {
    return DEFAULT_ACTIVE_ROW_BG_COLOR;
}


/*
 * Change row color
 */
function onRowMouseOver(srcElem) {

    // save prev color
    if (activeRow == null || srcElem != activeRow) {

        // alert('here');

                // change color
                oldRowBGColor = srcElem.style.backgroundColor;

        // store the active row
        activeRow = srcElem;
    }

    // change color of <td> sub elements
    changeRowBGColor(srcElem, getActiveRowBGColor());

    // change cursor
    srcElem.style.cursor="hand";
}

/*
 * Change row color
 */
function onRowMouseOut(srcElem) {
    // restore color
    changeRowBGColor(srcElem, oldRowBGColor);

    // clear the active row value
    activeRow = null;

    // change cursor
    srcElem.style.cursor="default";
}

/**
 * This function should be used for TD elements containing
 * some clickable elements (buttons, checkboxes, other input controls or
 * clickable images - this function prevent coloring the appropriative TR
 * and block whole row clicking
 */
function onUnSelectableTDMouseOver(srcElem) {
    // restore color
    srcElem.style.backgroundColor = oldRowBGColor;

    // set flag
    unselectableTD = srcElem;

    // prevent clicking the row
    blockRowClick();

    // change the cursor
    srcElem.style.cursor="default";
}

/**
 * This function should be used for TD elements containing
 * some clickable elements (buttons, checkboxes, other input controls or
 * clickable images - this function return coloring the appropriative TR
 * and unlock row clicks
 */
function onUnSelectableTDMouseOut(srcElem) {
    // restore color
    srcElem.style.backgroundColor = getActiveRowBGColor();

    unselectableTD = null;

    // prevent clicking the row
    unlockRowClick();
}


/*
 * Display context menu with name
 */
function showContextMenu(popupMenuName) {
        if (showContextMenu.arguments.length == 0) popupMenuName = DEFAULT_POPUP_MENU_NAME;
        showPopupMenu(popupMenuName);
        return false;
}

/*
 *  Display popup menu at mouse position
 */
function showPopupMenu(popupMenuName) {
        if (isPopupMenuEnabled()) {
        setActivePopupMenu(popupMenuName);
        moveLayerToMouse(getActivePopupMenu());
        showLayer(getActivePopupMenu());                
    } else {
        // do nothing
    }
}

function setActivePopupMenu(popupMenuName) {
        activePopupMenuName = popupMenuName;
}

function getActivePopupMenu() {
        return activePopupMenuName;
}

function isPopupMenuEnabled() {
        return (!isNS());
}

function changeRowBGColor(srcElem, color) {
    // change color of <td> sub elements
    srcElem.style.backgroundColor = color
    if (!lazyMouseOut && srcElem!=null) {
        for (i = 0; i < srcElem.cells.length; i++) {
            if (srcElem.cells[i] != unselectableTD ) {
                srcElem.cells[i].style.backgroundColor = color;
            }
        }
    }
}

/*
 * Open url
 */
function onRowClick(url, srcElem) {

        if (!isBlockedRowClick) {
        // check & clear row blocking
        if (isBlockedRowClick) {
            isBlockedRowClick = false;
            return false;
        }
        isBlockedRowClick = false;

        // check if menu is visible
        if (!isMenuVisible()) {
            // change the color of selected row
            changeRowBGColor(srcElem, "red");

            window.location = url;
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/*
 * Open url in new window
 */
function onRowClickOpenWindow(url, srcElem, windowName) {
        // check & clear row blocking
        if (isBlockedRowClick) {
                isBlockedRowClick = false;
                return false;
        }
    isBlockedRowClick = false;
        
        // check if menu is visible
        if (!isMenuVisible()) {
                openDialogWindow(url, windowName);
                return false;
        } else {
                return false;
        }
}


/*
 * A method that block the operation that performed when user clicks the row.
 * Clocking is cleared after each click (see onRowClick method)
 */
function blockRowClick() {
        isBlockedRowClick = true;
}

/**
 * A method that unlock the operation that performed when user clicks the row.
 * Clocking is cleared after each click (see onRowClick method)
 */
function unlockRowClick() {
        isBlockedRowClick = false;
}


/*
 * @return true, if popup menu is visible in that time
 */
function isMenuVisible() {
        return (getActivePopupMenu() != null && isLayerVisible(getActivePopupMenu()));  
}

/*
 * A method that close popup menu
 */
function closeMenu() {
        if (getActivePopupMenu() != null) {
                hideLayer(getActivePopupMenu());
        }
}

/*
 * Handle the event when person click the menu item of the popup menu
 */
function onClickMenuItem(srcElem, url) {
        var rowSuffix = getSelectedRowSuffix();
        var finalUrl = url;
        
        // add row suffix, if defined
        if (rowSuffix != null) {
                finalUrl = finalUrl + rowSuffix;
        }
        
        // close menu
        closeMenu();

        // go to another page
        window.location = finalUrl;
}

/*
 * Handle the event when person click the menu item of the popup menu
 */
function onClickMenuItemFormSubmit(srcElem, submitName) {
        var formName = getSelectedRowFormName();

        // close menu
        closeMenu();

        // submit the form with name formName. Emulate submission with submit name submitName
        document.forms(formName).all(submitName).click();

}

/*
 * Handle the event when person click the menu item of the popup menu
 */
function onClickMenuItemOpenWindow(srcElem, url, windowName) {
        var rowSuffix = getSelectedRowSuffix();
        var finalUrl = url;
        
        // add row suffix, if defined
        if (rowSuffix != null) {
                finalUrl = finalUrl + rowSuffix;
        }
        
        // close menu
        closeMenu();

        // open new window and go to another page
        openDialogWindow(finalUrl, windowName)
}

/*
 * @return  
 */
function getSelectedRowSuffix() {
        if (activeRow != null) {
                return activeRow.getAttribute("urlSuffix");     
        } else {
                return null;
        }
}

/*
 * @return  
 */
function getSelectedRowFormName() {
        if (activeRow != null) {
                return activeRow.getAttribute("formName");      
        } else {
                return null;
        }
}

/*
 * Activate floating context button
 */
function activateContextButton(image) {
        if (isPopupMenuEnabled()) {
                swapImage(image,'', APP_PREFIX + '/images/menu_over.gif', 1);
        }
}

/*
 * Deactivate floating context button
 */
function deactivateContextButton() {
        if (isPopupMenuEnabled()) {
                swapImgRestore();               
        }       
}