﻿// get pin details
var pinDetails3D;     
var pinLat;
var pinLon;
var pinIDPlotted;
var targetPin;
var pinImgSource;
var pinImgPlotted;
// pin detail variables for indexed pins.... (function used CustomIconSpecification)
var pinHTML;
var pinDiscription;
var pinTitle;
var pinType;
var clickedLatLong;
var mapPopup;
var mapPopup;
var popupZIndex=200;
var color="default";
var defaultSimplePopupWidth = 190;
var defaultAnimatedPopupWidth = 350;
GetPopupBodyHeight=function()
{
    var e=document.getElementById("searchPopup_body").getElementsByTagName("table");
    var bh=0;
    if(e!=null&&e.length>0)
    {
	    for(var i=0;i<e.length;i++)
	    {
		    bh+=e[i].offsetHeight;
	    }
    }
    return bh;
	
}

// pin details for Map View
function getPinDetails(details)
{   
    var commaIndex = details.indexOf(',');
    pinDetails3D = details; 
    mlsCompanyID = details.substring(0, commaIndex);
    details = details.substring(commaIndex+1, details.length);
    commaIndex = details.indexOf(',');
    mlsNumber = details.substring(0, commaIndex);
    details = details.substring(commaIndex+1, details.length);
    commaIndex = details.indexOf(',');
    pinLat = details.substring(0, commaIndex);
    details = details.substring(commaIndex+1, details.length);
    commaIndex = details.indexOf(',');
    pinLon = details.substring(0, commaIndex);
    details = details.substring(commaIndex+1, details.length);
    commaIndex = details.indexOf(',');
    pinIDPlotted = details.substring(commaIndex+1, details.length);
}
var pinInfoArray = new Array();
function CollectPinInfo(pinID, type, latitude, longitude)
{
    this.pinID = pinID;
    this.type = type;
    this.latitude = latitude;
    this.longitude = longitude;
}
// Functions For PushPin Hover Events
var map = null;
var mlsNumber;
var mlsCompanyID;

function PinHover(x, y, title, details) {
    
    var e = window.event;
    if (e != null)
        var t = GetTarget(e);
    if (!t) {
        return;
    }
    if (t.src.indexOf('nearby') != -1)
        t.src = 'images/single-nearby-16-on.gif';
    else if(t.src.indexOf('OH-16') != -1)
		    t.src = 'Images/OH-16-on.gif';
    else if(t.src.indexOf('AreaDetails') != -1)
            t.src ='Images/AreaDetailsPin_on.gif'
    
    else if(t.src.indexOf('OH-visited-16') != -1)
            t.src ='images/OH-visited-16-on.gif'
    
    else
        t.src = 'images/single-family-16-on.gif';

    
    t.onmouseout = pinHoverOut;
    getPinDetails(details);
    t.onclick = getContentForPushpin         //(mlsNumber,mlsCompanyID);
}

function pinHoverOut(e) {
    var t = GetTarget(e);
    if (!t) {
        return;
    }
    
    if (t.src.indexOf('nearby') != -1)
        t.src = 'images/single-nearby-16.gif';
    else if(t.src.indexOf('OH-16') != -1)
		    t.src = 'Images/OH-16.gif';
    else if(t.src.indexOf('AreaDetails') != -1)
            t.src ='Images/AreaDetailsPin.gif'
    else if(t.src.indexOf('Home') != -1)
            t.src ='images/single-nearby-16.gif'
    
    else if(t.src.indexOf('OH-visited-16') != -1)
            t.src ='images/OH-visited-16.gif'
    else
        t.src = 'images/single-family-16.gif';
}

function attachInitialEvents() {
    map.AttachEvent("onclick", getClickedPos);
  //  map.AttachEvent("onchangeview", onStyleChanged);
    map.AttachEvent("ondoubleclick", onMapClick);
    map.AttachEvent("onmousewheel", attachOnstyleChanged);
    map.AttachEvent("onkeydown", onStyleChanged);
    map.AttachEvent("onmousedown", attachOnstyleChanged);

    document.getElementById('MSVE_navAction_tinyZoomBar_plus').onmouseup = attachOnstyleChanged;
    document.getElementById('MSVE_navAction_tinyZoomBar_minus').onmouseup = attachOnstyleChanged;
    document.getElementById('Compass').onmouseup = attachOnstyleChanged;
}
function attachOnstyleChanged() {
    try {
        map.AttachEvent("onchangeview", onStyleChanged);
    }
    catch (dl) {
        return false;
    }
}
function detachMapEvents() {
    map.DetachEvent("onclick", getClickedPos);
    map.DetachEvent("onchangeview", onStyleChanged);
    map.DetachEvent("ondoubleclick", onMapClick);
}
function attachMapEvents() {
    map.AttachEvent("onclick", getClickedPos);
    map.AttachEvent("ondoubleclick", onMapClick);
  //  map.AttachEvent("onchangeview", onStyleChanged);
}
function attachPinHoverOutEvent(e)
{
if(document.getElementById('PinControlDiv') != null)
    {
    pinHoverOutIndexed();
    }
}
function getClickedPos(e) {
    if (e.view)
        clickedLatLong = e.view.latlong;
    else if (e.latLong)
        clickedLatLong = e.latLong;
    else {
        var posX = e.clientX;
        var posY = e.clientY;
        var mapdiv = document.getElementById("myMap");
        var mapposX = findPosX(mapdiv);
        var mapposY = findPosY(mapdiv);
        clickedLatLong = map.vemapcontrol.PixelToLatLong(new VEPixel(posX - mapposX, posY - mapposY));
        map.AttachEvent("onchangeview", onStyleChanged);
    }

}
function onStyleChanged(e) {
    //  to insure all pushpins are replotted on the map and no images exist
    if (map.GetMapMode() == 2 && document.getElementById("PinControl") != null)
        pinHoverOut();
    detachMapEvents();
    ExecuteSingleMapSearch();
}
function onMapClick(e) {
    if (!e)
    {
    //do nothing
    }
    else{
            if (e.latLong)
                clickedLatLong = e.latLong;
            else {
                var posX = e.clientX;
                var posY = e.clientY;
                var mapdiv = document.getElementById("myMap");
                var mapposX = findPosX(mapdiv);
                var mapposY = findPosY(mapdiv);
                clickedLatLong = map.vemapcontrol.PixelToLatLong(new VEPixel(posX - mapposX, posY - mapposY));
               // map.AttachEvent("onchangeview", onStyleChanged);
            }
    }
    //  to insure all pushpins are replotted on the map and no images exist
    if (map.GetMapMode() == 2 && document.getElementById("PinControl") != null)
        pinHoverOut();
    detachMapEvents();
    ExecuteRecursiveMapSearch(clickedLatLong.latitude, clickedLatLong.longitude);
}

function GetEntityReference(e)
{
    var returnValues=new Array();
    if(!e)  
    {
	    e=window.event;
    }
    var t=GetTarget(e);
    if(!t)
    {
	    return;
    }
    var id=t.id;
    var a=id.indexOf('_');
    var b=id.lastIndexOf('_');
    returnValues[0]=id.substr(a+1,b-a-1);
    returnValues[1]=id.substr(b+1,id.length-b);
    return(returnValues);
}

function GetTarget(e)
{
	if(!e)var e=window.event;
	if(!e)return;
	var eventTarget;
	if(e.srcElement)eventTarget=e.srcElement;
	else if(e.target)eventTarget=e.target;
	if(!eventTarget)return;
	if(eventTarget.nodeType==3)eventTarget=targ.parentNode;
	return eventTarget;
	
}

// correctly handle PNG transparency in Win IE 5.5 or higher.
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function fixPNG(myImage) 
{
    if ((version >= 5.5) && (document.body.filters)) 
    {
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width 
                  + "px; height:" + myImage.height 
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	   myImage.outerHTML = strNewHTML	  
    }
}

function ShowPopup(body,entity)
{
    
    if(!mapPopup)
    {	
	    mapPopup=new VE_PanelCustom("searchPopup",5,80,190,0,color,popupZIndex,"","","");
	    var p=mapPopup;
	    p.SetFooterSize(0);
	    p.SetToolbarSize(0);		
    }
    var p=mapPopup;
    var width;
    var mapdiv = document.getElementById("myMap");
    var posX = findPosX(mapdiv);
    var posY = findPosY(mapdiv);
    var w=mapdiv.offsetWidth+posX;
    var pixel=new Object();
    var x,y;
    if(entity)
        pixel=map.LatLongToPixel(new VELatLong(entity.Latitude,entity.Longitude));
       
   else
        pixel=map.LatLongToPixel(new VELatLong(pinLat,pinLon));
             
    x = pixel.x + 8+posX;
    y = pixel.y - 11+posY;
    
    if(width==null)
    {
	    width=defaultSimplePopupWidth;
	}
    if(x>w-eval(width))
    {
        x-=eval(width)+17;
    }	
    p.SetColor(color);
    p.SetTitle("");
    p.SetBody(body);
    p.SetFooter("");
    p.SetFooterSize(20);
    p.Show();
    p.height=GetPopupBodyHeight()+p.toolbarHeight+p.footerHeight+30;
    if(p.height > 200) p.height = 200;
     
    if(y>posY+mapdiv.offsetHeight-120)
    {
        y-=p.height-20;
    }
    p.SetPosition(x,y,180,80);
    p.Resize();
    if(map.GetMapMode() == 2)
      {
         addPopupFrame3D(p);
      }

}
