var lcId = new Date().getTime();
var popUpWin=0;

/**
*	Microsoft Map Functions V2.0 MapControl 
*   Note: This should be upgraded to a more current version eventually.
*/

var mapcontID = 'myMap';
var oMapMgr = null;
var pinID = 1;
var mapHidden = false;
var mapBlocked = 'block';
var mapLat;
var mapLong;
var hideCount = 0;
var BlindEffectLocked = false;
var currentEffect = null;
var BlindSpeed = .5;


function LoadStaticMap( imgURL )
{
	mapLat = "1";
	mapLong = "1";
	document.getElementById(mapcontID).style.visibility='hidden';
	document.getElementById('ch').style.visibility='hidden';
	document.getElementById('ch').innerHTML = '<img src="'+ imgURL +'" />';
    hideMap();
}

function LoadFlashVEMap(strLat,strLong)
{
	//return;  //return if you want to disable map instantiation and loading
	mapLat = strLat;
	mapLong = strLong;
	
	if( isMapNotValid() )
	{
		$(mapcontID).style.display = 'none';
		$('ch').style.visible = 'hidden';
		return;
	}
	
	try
	{
	    var opt = {};
		opt.latitude = parseFloat(mapLat);
		opt.longitude = parseFloat(mapLong);
		opt.zoomlevel = 12;
		opt.showDashboard = false;
		opt.showScaleBar = false;
		opt.onMapInit = _onMapInitEnd;
		opt.dashboardSize = Msn.VE.DashboardSize.Tiny;
		
	    oMapMgr = new LNJS.MapManager(mapcontID,opt);
	}
	catch(e)
	{
		//alert(e.message);		
	}

}

function initMap()
{
	blockMap('none');
	hideMap(); //defaults map to 'hidden'	
}

function plotPin(oMapMgr)
{
	oMapMgr.deleteAllShapes();

	try
	{
        var oLatLon = new VELatLong(mapLat, mapLong);
        var oPin = new VEShape(VEShapeType.Pushpin, oLatLon); 
        oPin.SetCustomIcon("<div class='pin_N'></div>");
        oMapMgr.addShape(oPin);
	}
	catch(e)
	{
		//error
	}
}

var el;
function addControl(oMapMgr) 
{            
	if( oMapMgr != null )
	{
		//return;
		el = document.createElement("div");
		el.id = "ve_nav";             
		el.style.top ="7px";  
		el.style.fontFamily = "Helvetica, sans-serif";
		el.style.fontSize = "10";
		el.style.textAlign = "center";
		el.style.fontWeight = "bold"; 
		el.style.visibility ="hidden";          
		el.style.left = "7px"; 
		el.style.filter = "alpha(opacity=70)";
		el.style.width = "200px";
		el.style.padding = "0px";
		el.style.position = "absolute";
		el.style.zIndex = 250;
		//el.style.border = "1px solid #CCC";            
		//el.style.background = "White";     
		el.innerHTML = "<div id=\"road\" class=\"vebuttonroad vebuttonroadselected\"><a href=\"javascript:SetMapStyle('r',this);\">Road</a></div><div id=\"aerial\" class=\"vebuttonaerial\"><a href=\"javascript:SetMapStyle('a',this);\">Aerial</a></div><div id=\"birdseye\" class=\"vebuttonbirdseye\"><a href=\"javascript:SetMapStyle('o',this);\">Bird Eye</a></div>";  
		$('ch').appendChild(el);
	}
	
}

function SetMapStyle( strStyle, anch )
{
    oMapMgr.setMapStyle( strStyle );
    //alert(this);
    
    switch( strStyle )
    {
    
        case 'r':
            //selected
            $('road').className = 'vebuttonroad vebuttonroadselected';
            
            //regular
            $('aerial').className = 'vebuttonaerial';
            $('birdseye').className = 'vebuttonbirdseye';
        break;
        
        case 'a':
            //selected
            $('aerial').className = 'vebuttonaerial vebuttonaerialselected';
            
            //regular
            $('road').className = 'vebuttonroad';
            $('birdseye').className = 'vebuttonbirdseye';
        break;
        
        case 'o':
            //selected
            $('birdseye').className = 'vebuttonbirdseye vebuttonbirdseyeselected';
            
            
            //regular
            $('aerial').className = 'vebuttonaerial';
            $('road').className = 'vebuttonroad';
        break;
    }    
}


function isMapNotValid()
{
	return (mapLat=="0" && mapLong=="0");
}

function blockMap( status )
{
	
	if(mapBlocked != status)
	{
		$(mapcontID).style.display = status;
		mapBlocked = status;	
	}
}	

function hideMap()
{
	if( isMapNotValid() )
		return;

    try
    {
        if(!mapHidden)
	    {
		    if( hideCount < 1 )
		    {
		        LNJS.Effect.BlindUp('ch',{duration:0.010});
		    }
		    else
		    {
		        if(!BlindEffectLocked)
		        {
		            BlindEffectLocked = true;   
		            currentEffect = new LNJS.Effect.BlindUp('ch',{duration:BlindSpeed,
		                afterFinish:function(effect){ 
		                    BlindEffectLocked = false;
		                    if( !mapHidden ) LNJS.Effect.BlindDown('ch',{duration:BlindSpeed});
		                }});
		        }
		    }
		    hideCount++;    
		    mapHidden = true;	
	    }
	}catch(e){
	    //In case the map object isn't ready yet silently fail.
	}
}

function showMap()
{	
	if( isMapNotValid() )
		return;

	if( mapHidden)
	{	
		if(el != null) el.style.visibility = "visible";
		document.getElementById('ch').style.visibility = 'visible';
		
		blockMap('block');
		if(!BlindEffectLocked)
		{
		    BlindEffectLocked = true;
		    currentEffect = new LNJS.Effect.BlindDown('ch',{duration:BlindSpeed,
		        afterFinish:function(effect){ 
		            BlindEffectLocked = false;
		            if( mapHidden ) LNJS.Effect.BlindUp('ch',{duration:BlindSpeed});
		        }});
		}    
		_onChangeView(); //we manually call this to turn off BirdsEye button if it's unavailable!
		mapHidden = false;
	}
}

function _onChangeView()
{
    showOrHideButtonIfBirdsEyeAvailable(oMapMgr);
}

function _onMapInitEnd(oMapMgr)
{   
    addControl(oMapMgr);  //adds custom dashboard to change views

    //adding new boolean property
    showOrHideButtonIfBirdsEyeAvailable(oMapMgr);
    
	oMapMgr.attachEvent("onchangeview", _onChangeView);
    
    initMap();
    
    plotPin(oMapMgr); //plots the lat,long pin
}

function showOrHideButtonIfBirdsEyeAvailable(oMapMgr)
{
  if( oMapMgr == null ) return;
  
  if(oMapMgr.isBirdseyeAvailable())
  {
    $('birdseye').style.display = 'block';
  }else{
    //hide button
    $('birdseye').style.display = 'none';
  }
}
	
