/*
$Id:
Copyright(C) 1996-2005 INCREMENT P CORP.
*/
var g_strDocRoot    = "parts/";

function CPinGroup( oMap, oItemList )
{
    var oOwner      = this;
    var nStopCounter    = 0;

    // 初期化
    var oDrawCache = new Array();
    for( var i = 0 ; i < oItemList.length ; i++ )
    {
        oDrawCache[i]       = new Object();
        oDrawCache[i].Longitude = oItemList[i].Longitude;
        oDrawCache[i].Latitude  = oItemList[i].Latitude;
        oDrawCache[i].nDist = 0;
        oDrawCache[i].oObj  = CreatePin( oItemList[i].uid );
    }

    // 初期化・イメージオブジェクトの作成
    function CreatePin( uid )
    {
        var oLayer = oMap.document.getElementById( "Layer" );
        var oImgPin = oMap.document.createElement( "img" );
        oImgPin.className = 'pin';
        oImgPin.uid     = uid;
        oImgPin.style.zIndex    = 15;
        oImgPin.style.position  = "absolute";
        oImgPin.style.width = 37;
        oImgPin.style.height = 38;
        oImgPin.style.left  = -1000;
        oImgPin.style.top   = -1000;
        oImgPin.src     = g_strDocRoot + "spot.gif";
        oLayer.appendChild( oImgPin );
        //
        this.OnMouseOver    = null;
        this.OnMouseOut     = null;
        this.OnMouseDown    = null;
        this.OnMouseUp      = null;
        oImgPin.onmouseover = function(e)
        {
            this.src = g_strDocRoot + "spot_over.gif"
            if( oOwner.OnMouseOver != null )
            {
                oOwner.OnMouseOver( uid );
            }
        }

        oImgPin.onmouseout = function(e)
        {
            this.src = g_strDocRoot + "spot.gif"
            if( oOwner.OnMouseOut != null )
            {
                oOwner.OnMouseOut( uid );
            }
        }

        oImgPin.onmousedown = function(e)
        {
            var oEvent = CMN_GetEvent( this.ownerDocument, e );
            oEvent.cancelBubble = true;

            if( oOwner.OnMouseDown != null )
            {
                oOwner.OnMouseDown( uid );
            }
        }

        oImgPin.onmouseup = function(e)
        {
            if( oOwner.OnMouseUp != null )
            {
                oOwner.OnMouseUp( uid );
            }
        }

        jQuery(oImgPin).hoverIntent({
            sensitivity: 3,
            interval: 200,
            over: function(e) {
                if( oOwner.OnMouseUp != null ) {
                    oOwner.OnMouseUp( uid );
                }

            },
            timeout: 0,
            out: function(e) {}
        });


        return  oImgPin;
    }

    // 描画
    this.Draw = function( oMap )
    {
        var nStartTick = GetTick();
        var nDrawMaxTick = 1000;

        if( this.nStopCounter == 0 )
        {
            // 10msec程度で開放すれば重たく感じない。
            nDrawMaxTick = 10;

            for( i in oDrawCache )
            {
if (oDrawCache[i].oObj) {
                oDrawCache[i].oObj.style.visibility = "hidden";
}
            }
        }
        else if( this.nStopCounter == 10 )
        {
            // センター位置から近い順に描画順を並べ替える。
            var oCenter = oMap.GetViewPoint( oMap.Longitude, oMap.Latitude );
            var nSrcX = oCenter.PixelX;
            var nSrcY = oCenter.PixelY;
            for( i in oDrawCache )
            {
if (oDrawCache[i].oObj) {
                oDrawCache[i].oObj.style.visibility = "hidden";
                var oRet = oMap.GetViewPoint( oDrawCache[i].Longitude, oDrawCache[i].Latitude );
                oDrawCache[i].nDist = Math.pow( oRet.PixelX - nSrcX, 2 ) +
                             Math.pow( oRet.PixelY - nSrcY, 2 );
}
            }
            oDrawCache.sort( function( a, b ) { return a.nDist - b.nDist; } );
        }
        else if( this.nStopCounter > 10 )
        {
            return;
        }

        for( i in oDrawCache )
        {
            if( GetTick()-nStartTick > nDrawMaxTick )
            {
                break;
            }
            var oRet = oMap.GetViewPoint( oDrawCache[i].Longitude, oDrawCache[i].Latitude );
            // 画像の基準位置（ホットスポット）は固定。
            // 画像ごとに変更するにはテーブルを作成するなどの手法がある。
            var nHotSpotX = 21;
            var nHotSpotY = 36;
if (oDrawCache[i].oObj) {
            oDrawCache[i].oObj.style.left   = oRet.PixelX - nHotSpotX;
            oDrawCache[i].oObj.style.top    = oRet.PixelY - nHotSpotY;
            oDrawCache[i].oObj.style.visibility = "visible";
}
        }
        this.nStopCounter++;
    }

    // カウンタのリセット
    this.Reset = function()
    {
        this.nStopCounter = 0;
    }


    // 破棄
    this.Destroy = function()
    {
        oDrawCache = null;
        jQuery('.pin', oMap.document).each(function() {
            this.parentNode.removeChild(this);
        });
    }

    // ミリ秒(=Tick)単位の現在時間の取得
    function GetTick()
    {
        return  new Date()/1;
    }

    return this;
}
