﻿function rcInfoWindow(e)
{
    this.left = 0;
    this.top = 0;
    this.content = "";
    this.id = "locInfoWindow";
    this.width = 209;
    this.height = -1;

    var intMinFromTop = 25;
    var intRightPadding = 30;
    //var intPointerOffset = 20;

    var intIWTop = -1;
    var intIWLeft = -1;

    var POINTER_HEIGHT = 40;
    var POINTER_OFFSET_FROM_TOP = 20;
    var OFFSET_LEFT = 10; //75;         // how far to the right or left of the marker the info window should appear
    var OFFSET_TOP = 10; // 50;         // how far above or below the marker the info window should appear

    /*this.containerId = "";*/

    this.show = function () {
        // check to see if an existing window is open; if so, close it;
        var oWindow = document.getElementById(this.id);
        if (oWindow != null) {
            oWindow.parentNode.removeChild(oWindow);
        }
//        alert('show window');
        var iw = document.createElement('div');

        iw.id = this.id;
        iw.className = "infoWindowShell";

        //var oEvt = new MouseEvent(e);
        /*this.top = oEvt.top;
        this.left = oEvt.left;*/

        //alert("iwcontent.style.width = " + iwcontent.style.innerWidth);
        var intIWLeft = this.getIWLeft();
        var intIWTop = this.getIWTop();
        iw.style.top = intIWTop + "px";
        iw.style.left = intIWLeft + "px";
        iw.style.position = "absolute";


        var iwcontent = document.createElement("div");
        iwcontent.innerHTML = this.content;

        iw.style.visibility = "visible";
        iw.style.display = "block";

        //iw.innerHTML = this.content;

        var iwclose = document.createElement("div");
        iwclose.className = "rcInfoWindowClose";
        iwclose.innerHTML = "Close";

        if (this.height >= 0)
            iw.style.height = this.height + "px";

        if (this.width >= 0)
            iw.style.width = this.width + "px";

        iwclose.onmouseover = function (event) {
            var oEvt = new MouseEvent(event);
            oEvt.target.className = "rcInfoWindowCloseHover";
        }

        iwclose.onmouseout = function (event) {
            var oEvt = new MouseEvent(event);
            oEvt.target.className = "rcInfoWindowClose";
        }

        iwclose.onclick = function (event) {
            var oEvt = new MouseEvent(event);
            var oIw = oEvt.target.parentNode;
            oIw.parentNode.removeChild(oIw);
            oIw = null
            //this = null;
        }

        // define pointer
        //iw.appendChild(this.getPointer(intIWLeft, intIWTop));

        //document.body.appendChild(this.getPointer(intIWLeft, intIWTop));
        //iw.appendChild(this.getPointer(intIWLeft, intIWTop));
        iw.appendChild(iwclose);
        iw.appendChild(iwcontent);

        document.body.appendChild(iw);


    }

    this.getLeft = function ()
    {
        return this.left; // + "px";

        toString = function ()
        {
            return this.left + "px";
        }
    }


    this.close = function ()
    {
        var iw = document.getElementById(this.id);
        document.body.removeChild(iw);
    }


    /*    this.getTop = function ()
    {
    return this.top;
    }


    this.getHeight = function ()
    {
    return this.height + "px";
    }

    this.getWidth = function ()
    {
    return this.width + "px";
    }*/

    //
    // Positions the infowindow's top so it's offset from where the
    // mouse was clicked but ensures that the top is not off the screen
    //
    this.getIWTop = function ()
    {
        var top = this.top - OFFSET_TOP;
        var scrollTop = getScrollTop();
        if (top < intMinFromTop || (top - intMinFromTop) < scrollTop)
        {
            //top = intMinFromTop;
            top = scrollTop + intMinFromTop;
        }

        intIWTop = top;

        return top;

        //return top + "px";
    }

    //
    // Positions the infowindow to the right of the marker, offset enough to show an arrow;
    // If necessary, positions the infowindow to the left of the marker
    //
    this.getIWLeft = function ()
    {
        var left = this.left + OFFSET_LEFT;

        //alert(contentWidth);

        //var width = (contentWidth != null && contentWidth > 0) ? contentWidth : this.width;


        if ((left + this.width) > getDocumentWidth())
        {
            left = this.left - OFFSET_LEFT - this.width;
        }

        return left; // +"px";*/
    }

    function getDocumentWidth()
    {
        var width;
        if (window.innerWidth)
        {
            //return window.innerWidth;
            width = window.innerWidth;
        }
        else
        {
            width = document.body.clientWidth;
        }
        width -= intRightPadding;
        return width;
    }

    function getDocumentHeight()
    {
        if (window.innerHeight)
        {
            return window.innerHeight;
        }
        else
        {
            return document.body.clientHeight
        }
    }

    function getScrollTop()
    {
        if (document.body.scrollTop)
        {
            return document.body.scrollTop;
        }
        else
        {
            return document.documentElement.scrollTop;
        }
    }

    this.getPointer = function (infoWindowLeft, infoWindowTop)
    {
        var blnArrowLeft = true;

        var oPtr = document.createElement("div");
        oPtr.style.position = "fixed";
        oPtr.style.top = this.top - (POINTER_HEIGHT / 2) + "px";
        oPtr.style.left = (infoWindowLeft < this.left) ? infoWindowLeft + this.width : this.left;

        if (infoWindowLeft < this.left)
        {
            blnArrowLeft = false;
        }

        var strUrl = location.href;
        if (strUrl.indexOf("?") >= 0)
        {
            strUrl += "&";
        }
        else
        {
            strUrl += "?";
        }

        strUrl += "graphics=1&type=";

        strUrl += (blnArrowLeft) ? "1" : "2";

        strUrl += "&width=" + OFFSET_LEFT + "&height=" + POINTER_HEIGHT;

        oPtr.innerHTML = "<img src='" + strUrl + "' />";

        return oPtr;

    }

    //
    // sets the x and y coordinates of the window
    //
    this.setPosition = function(x, y)
    {
        this.left = x;
        this.top = y;
        
        var myIw = "#" + this.id;
        $(myIw).css("left", this.getIWLeft());
        $(myIw).css("top", this.getIWTop());
    }

//    this.getPointer = function (infoWindowLeft, infoWindowTop)
//    {
//        var oPtr = document.createElement("div");
//        oPtr.style.position = "absolute";
//        //alert(this.left);
//        oPtr.style.left = this.left + "px";
//        oPtr.style.top = this.top - (POINTER_HEIGHT / 2) + "px";


//        var intPt2Left = (infoWindowLeft < this.left) ? infoWindowLeft + this.width : this.left + offsetLeft;
//        var intPt2Top = infoWindowTop + POINTER_OFFSET_FROM_TOP;

//        var strUrl = location.href;
//        if (strUrl.indexOf("?") >= 0)
//        {
//            strUrl = strUrl + "&";
//        }
//        else
//        {
//            strUrl = strUrl + "?";
//        }

//        //strUrl = strUrl + "graphics=1&type=1&pt1Left=" + this.left + "&pt1Top=" + this.top + "&pt2Left=" + intPt2Left + "&pt2Top=" + intPt2Top;
//        strUrl = strUrl + "graphics=1&type=";


//        oPtr.innerHTML = "<img src='" + strUrl + "' />";

//        return oPtr;

//    }
}


