﻿var veMap = null;
var map = null;
var showPoints = false;
var points=[];
var currentContact = null;

// Defines a Map Property
function mapProp(lat,lon, z, view, fix, mod, swi, typ){ 
    this.latitude = lat;
    this.longitude = lon;
    this.zoom = z;
    this.viewType = view;
    this.fixed = fix;
    this.mode = GetVEMapMode(mod);
    this.showSwitch = swi;
    this.type = typ; // Map Type
}

// Defines a Shape Property
function pointProp(id,tit,lat,lon,ico,showInfo,info,imgOn,imgOff)
{
    this.contactID = id;
    this.title = tit;
    this.latitude = lat;
    this.longitude = lon;
    this.icon = ico;
    this.showInfoBox = showInfo;
    this.infoBox = info;
    this.imageOn = imgOn;
    this.imageOff = imgOff;
}

function GetVEMap(){
veMap = new VEMap('myMap');

RenderMap();

if(showPoints == true)
    ShowLocations();
}   

function RenderMap()
{
    if(map == null)
    {
        veMap.LoadMap();
    }
    // If Properties Exists
    else
    {
        veMap.LoadMap(new VELatLong(map.latitude, map.longitude), map.zoom , map.viewType , map.fixed, map.mode, map.showSwitch);    
        // Map Type 0 - contactos
        // Map Type 1 - mapa ficha
        // Map Type 2 - itinerários
        // Map Type 3 - mapas peças e imóveis
        if(map.type == 0)
        {
            veMap.AttachEvent("onclick", shapeContacto_onclick); 
        }
        if(map.type == 1)
        {
        	//hide div
        	var mapaAreaDiv = document.getElementById('mapaArea');
        	if(mapaAreaDiv != null)
        		mapaAreaDiv.style.display = 'none';
        }
    } 
}

function GetVEMapMode(mode)
{
    if(mode == false) return VEMapMode.Mode3D;
        return VEMapMode.Mode2D;
}

function ShowLocations()
{
    for(i=0;i<points.length;i++)
    {
        AddPoint(points[i]);
    }
}

function AddPoint(point)
{         
    var shape = new VEShape(VEShapeType.Pushpin, new VELatLong(point.latitude, point.longitude));
    //Set the icon
    shape.SetCustomIcon(point.icon);
    shape.contactoID = point.contactID;  
    shape.imageOn = point.imageOn;
    shape.imageOff = point.imageOff; 
               
    //Set the info box        
    if(point.showInfoBox == true)
    {
        veMap.ClearInfoBoxStyles();    
        //shape.SetTitle(point.title);   
        shape.SetDescription(point.infoBox);  
    }    
    //Add the shape the the map    
    veMap.AddShape(shape);     
}

function UnloadVEMap()
{
    if(map != null)
    {
       map.Dispose();
    }
    veMap = null;
    showPoints = false;
    points=[];
    currentContact = null;
}

function shapeContacto_onclick(e){
   if (e.elementID != null){
    var shape = veMap.GetShapeByID(e.elementID);
    toggle_visibilityContacts(shape.contactoID, shape.imageOn, shape.imageOff);
}
}

// Change visible contact description
function toggle_visibilityContacts(id, imgOn, imgOff) {
    var contacto = document.getElementById('contacto' + id);
    var contactoMapa = document.getElementById('contactoMapa' + id);
    if(contacto.style.display == 'block')
    {
        contacto.style.display = 'none';
        contactoMapa.src = '/Style%20Library/images/' + imgOff;
        currentContact = null;
    }
    else
    {
        if(currentContact != null)
        {
            document.getElementById('contacto' + currentContact).style.display = 'none';
            document.getElementById('contactoMapa' + currentContact).src = '/Style%20Library/images/' + imgOff;
        }
        contacto.style.display = 'block';
        contactoMapa.src = '/Style%20Library/images/' + imgOn;
        currentContact = id;
    }
}