var hoverTipsPanel = null;
var resizableHoverTipsPanel = null;

/**
 * Create an EsriAction which creates a timer and does a callback every time the
 * timer completes.
 */
function HoverTipsAction(tout) {
    this.inheritsFrom(new EsriAction());
    var element, callback, bounds;
    var timer;
    var timeout = (tout) ? tout : 500;
    var self = this;
    
    this.activate = function (elem, cb) {
        element = elem;
        callback = cb;
        element.style.cursor = "pointer";
        element.onmouseout = processMouseOut;
        element.onmousemove = processMouseMove;
    };
    
    this.deactivate = function () {
        clearTimeout(timer);
        if (element != null) {
            element.onmousemove = null;
            element.style.cursor = "default";
        }
        element = bounds = callback = null;
    };
    
    function processMouseMove(e) {
        bounds = EsriUtils.getElementPageBounds(element);
        clearTimeout(timer);
        var pt = EsriUtils.getXY(e).offset(-bounds.left, -bounds.top);
        timer = setTimeout(function () {
            callback(pt);
        }, timeout);
    }
    
    function processMouseOut(e) {
        clearTimeout(timer);
    }
}//end HoverTipsAction

/**
 * Create an EsriToolItem which uses the HoverTipsAction and on callback, sends
 * a request to the server to the attributes for the map tip.
 */
function HoverTipsTool(id, toolName, tout) {
    this.inheritsFrom(new EsriMapToolItem(id, toolName, new HoverTipsAction(tout), true));

    var self = this;

    /**
     * Send request to server to retrieve attribute information for the point
     */
    this.postAction = function (point) {
        var map = self.control;
        var formId = map.formId;
        var url = EsriUtils.getServerUrl(formId);
        point = point.offset(-map.viewBounds.left, -map.viewBounds.top);
        var toc = EsriControls.tocs["tocelement"];
        var layers = "";
        var tolerance = 8;
        
        for (n = 0; n <= toc.nodes.length - 1; n++) {
            var node = toc.nodes[n];
            if (node.isChecked && !node.isDisabled && node.label != "GCRO" && node.label != "State Border" && node.label != "Parish / County border") {
                layers = layers + "," + node.label;
            }
        }
        
        var params = new Array();
        params.push("layers=" + layers);
        params.push("x=" + point.x);
        params.push("y=" + point.y);
        params.push("tolerance=" + tolerance);
        
        var responseHandler = new ResponseHandler();
        responseHandler.getResponseProcessor = function () {
            return function (xh) {
                response(xh, point, map.id);
            };
        };
        
        GCROUtils.performCommand(url, true, "hovertips", currentViewId, params, responseHandler, null);
    };
        
    /**
     * Process response from server and display attributes in tip window
     */
    function response(xh, point, mapId) {
        if (xh != null && xh.readyState == 4 && xh.status == 200) {
            // Create an instance of the hover tips window.
            var hoverTipsWindow = new HoverTipsWindow("500px", "12px");
            
            // Generate and show the hover tip.
            hoverTipsWindow.generateHoverTipsContent(xh);
        }//end if
    }
}//end HoverTipsTool

