﻿var EndTip = 0;
var OverDetails = false;
var HideMapTipsComplete = false;
var MouseDragging = false;

var MapWidth = $jq("#property-map").width();
var MapHeight = $jq("#property-map").height();
var TipDetailsWidth = $jq("#tipDetails").width();
var TipDetailsHeight = $jq("#tipDetails").height();

//this will hold the extent of all of our map tips
var MapTipArray = new Array();

function addTipAddress(objectID, extent, labelNum, html) {
    //console.log(html);
    var tipMarker = $jq("#tip").clone().attr("id", "tip_" + objectID);
    var tipDetails = $jq("#tipDetails").clone().attr("id", "tipDetails_" + objectID);
    
    var pixPnt = null;
    
    EndTip += 1;
    MapTipArray[EndTip] = extent;
    MapTipArray[EndTip][4] = objectID;
    setMapTipPosition(EndTip, tipMarker);
    
    //TODO: try doing just tipMarker.html(labelNum)
    //$jq("#" + tipMarker[0].id + " span").html(labelNum);

    setTipDetailPosition(tipDetails, XTipPix, YTipPix);

    tipDetails.html("");
    tipDetails.append(html);
    //tipDetails.append(address + "<br/><span><a href='javascript:;' onclick='zoomin(\"" + extent[0] + "\",\""+ extent[1] + "\",\"" + extent[2] + "\",\"" + extent[3] + "\")'>Zoom To</a> | </span>");
    tipDetails.appendTo("#property-results");
    tipDetails.overDetails = false;

    tipMarker.mouseenter(function() {
        if(!MouseDragging) 
            tipDetails.fadeIn("slow");
    });

    tipMarker.mouseleave(function() {
        setTimeout(function() {
            if (!tipDetails.overDetails) tipDetails.fadeOut("fast")
        }, 1000);

    });

    tipDetails.mouseenter(function() {
        this.overDetails = true;
    });

    tipDetails.mouseleave(function() {
        this.overDetails = false;
        tipDetails.fadeOut("fast");
    });
}

function addTip(objectID, extent, labelNum, altKey, propertyAddress, queryString, html) {
    var tipMarker = $jq("#tip").clone().attr("id", "tip_" + objectID);
    var tipDetails = $jq("#tipDetails").clone().attr("id", "tipDetails_" + objectID);
    
    var pixPnt = null;
    
    EndTip += 1;
    
    //add tip to array
    //0-3 are extent, 4 is the label number
    MapTipArray[EndTip] = extent;
    MapTipArray[EndTip][4] = objectID;
    
    setMapTipPosition(EndTip, tipMarker); 
    
    //add marker number
    //$jq("#" + tipMarker[0].id+ " span").html(labelNum);


    setTipDetailPosition(tipDetails, XTipPix, YTipPix);
    
    //tipDetails.css("left", XTipPix);
    //tipDetails.css("top", YTipPix - 80);
    tipDetails.html("");
    tipDetails.append(html);
    tipDetails.appendTo("#property-results");
    tipDetails.data('overDetails', false);

    tipMarker.click(function() {
        window.location = "parcel-details.aspx?" + queryString;
    });

    tipMarker.mouseenter(function() {
        // clear any timeout for fading out
        clearTimeout(tipMarker.data('fadeout'));
        if (!MouseDragging)
            tipDetails.fadeIn("fast");
    });

    tipMarker.mouseleave(function() {
        tipMarker.data('fadeout',
            setTimeout(function() {
                if (!tipDetails.data('overDetails')) tipDetails.fadeOut("fast")
            }, 400)
        );
    });

    tipDetails.mouseenter(function() {
        $jq(this).data('overDetails', true);
    });

    tipDetails.mouseleave(function() {
        $jq(this).data('overDetails', false);
        tipDetails.fadeOut("fast");
    });

}


//Update the position of the map tips and details
//Called when the map extent is changed. Called by WebMapApp.js
function updateMapTips() {
    var label = $jq("#JSLabel");
    var j,tp;
    
    //if there isn't a tip, do not do anything. 
    if (EndTip == 0)
        return;

    for (j = 1; j <= EndTip; j++) 
    {
        //get the tip div
        if(MapTipArray[j]) {
            tp = $jq("#tip_" + MapTipArray[j][4]);
            tpDetail = $jq("#tipDetails_" + MapTipArray[j][4]);
        }

        if (tp) {
            
            //set xy position
            setMapTipPosition(j, tp);
            setTipDetailPosition(tpDetail, XTipPix, YTipPix);
        }

    }

    HideMapTipsComplete = false;
}

function setMapTipPosition(i, tipMarker) {
    var pixPnt = null;
    //used to hide map tip when it is too close to the map boundaries. 
    //will need to be adjusted if map size changes
    var XMin = 5;
    var XMax = MapWidth - 5;
    var YMin = 0;
    var YMax = MapHeight + 4;
    
    if (MapTipArray[i]) {
        XMinTip = MapTipArray[i][0];
        YMinTip = MapTipArray[i][1];
        XMaxTip = MapTipArray[i][2];
        YMaxTip = MapTipArray[i][3];
        XTip = (XMinTip + XMaxTip) / 2;
        YTip = (YMinTip + YMaxTip) / 2;
        var mapPnt = new ESRI.ADF.Geometries.Point(XTip, YTip);
        pixPnt = map.toScreenPoint(mapPnt);
        XTipPix = pixPnt.offsetX;
        YTipPix = pixPnt.offsetY;


        //if maptip doesn't fall within map boundaries, remove it from map.
        if (XTipPix < XMin || XTipPix > XMax)
            XTipPix = -9000;

        if (YTipPix < YMin || YTipPix > YMax)
            YTipPix = -9000;

        tipMarker.css("left", XTipPix -3);
        tipMarker.css("top", YTipPix -24);

        tipMarker.appendTo("#property-results");
        tipMarker.show();    
        
    }

}

///Hide map tips when the user is panning/zooming
function hideMapTips() {

    if (!HideMapTipsComplete) {
        var arLen = MapTipArray.length;
        for (var i = 1, len = arLen; i < len; ++i) {
            tp = $jq("#tip_" + MapTipArray[i][4]);
            tp.hide();
        }
        HideMapTipsComplete = true;
    }

}


//Remove all map tips from the map permanetely
function removeMapTips() {
    //remove anything that begins with tip
    $jq("div[id^=tip]").remove();
}

//set the position of the tipDetail
function setTipDetailPosition(tipDetail, xTipPix, yTipPix) {

    //adjust the left or right so the tip stays within the map. 
    if (xTipPix > ((MapWidth - TipDetailsWidth))) {
        //reset left or you will have problems because both left and right will be set. 
        tipDetail.css("left", "");
        tipDetail.css("right", (MapWidth - xTipPix));
    }
    else {
        tipDetail.css("right", "");
        tipDetail.css("left", xTipPix);
    }

    //for now, it is ok if it goes above map
    tipDetail.css("top", yTipPix - (TipDetailsHeight + 20));
}


function fixTips() {
    $jq(".innerDetails").ifixpng();
}