
// GMap.js is a Javascript
// that contains all the functions to display a map
// and to display the latest events

var maxPinDisplay;
var maxDisplayTime;
var cacheDuration;

var primaryMap;
var myOldGMarker = new Array();
var eventCount = 0;
var arraySize;
var displayTime = 0;
var currentZoom = 0;
var dCaseCtrl = 0;

var x = 0;
var y = 0;

function JTVMarker()
{
    this.marker = null;
    this.typeID = 0;
}

function OrderOfCreation(marker,b) {
        return 1;
      }

function LoadMap()
 {
     if (GBrowserIsCompatible())
      {
            primaryMap = new GMap2(document.getElementById("map"));
            primaryMap.setCenter(new GLatLng(39.4378568950242,-95.546875), 4);
            currentZoom = 4;
            primaryMap.setMapType(G_HYBRID_MAP);
            primaryMap.addControl(new GMapTypeControl());                                 
            primaryMap.disableDragging();
            primaryMap.disableDoubleClickZoom();                        
            //primaryMap.disableInfoWindow();
            primaryMap.savePosition();                      
            primaryMap.addControl(new AreaZoomControl());
            primaryMap.addControl(new newJottsControl());
            primaryMap.addControl(new ADRControl());
            primaryMap.addControl(new MapControl());
            document.getElementById("adr").style.visibility = "hidden";
            document.getElementById("eE").style.visibility = "hidden";
            document.getElementById("eE").innerHTML = "O";
            document.getElementById("adr").innerHTML = "0";            
            
            document.getElementById("pNJ").innerHTML = '<img src="Images/anim_JTV.gif" alt="Loading...">';            
            
            GEvent.addListener(primaryMap,"maptypechanged", function(){
                                                            if (primaryMap.getCurrentMapType().getName() == "Map")
                                                            {                                                                
                                                                if(document.getElementById("eE").style.visibility == "hidden" && previousButton == "NorthAmerica")                                                                    
                                                                    document.getElementById("eE").style.visibility = "visible";
                                                                if(document.getElementById("adr").style.visibility == "hidden" && previousButton == "Seattle")
                                                                    document.getElementById("adr").style.visibility = "visible";                                                                                                                            
                                                            }
                                                            else
                                                            {
                                                                if(document.getElementById("eE").style.visibility == "visible")
                                                                    document.getElementById("eE").style.visibility = "hidden";
                                                                if(document.getElementById("adr").style.visibility == "visible")
                                                                    document.getElementById("adr").style.visibility = "hidden";
                                                            }
                                                            });                                                                                                                
      }
     else
      {
            //document.write('Error!</br>Your browser is not compatible!</br>We recommend : <a href="http://www.mozilla.com">Mozilla Firefox</a>');            
      }
 }    
              
function LoadApp(str)
 {                
     if (str == "")
     {
        document.getElementById("pNJ").style.visibility = "visible";
        document.getElementById("pNJ").innerHTML = eventCount + ' new events';
        var timeOut4 = setTimeout(function(){
                                RetrieveEvents()
                                }, 60000);
     }
     else
     {                   
        var JTVEvents = CreateEvents(str);
        eventCount = JTVEvents.length;
        document.getElementById("pNJ").style.visibility = "hidden";
        document.getElementById("pNJ").innerHTML = eventCount + ' new events';
        Main(JTVEvents);
     }              
 }

function Main(JTVEvents)
 {    
           x = 0;
           y = 0;
                      
           primaryMap.clearOverlays();
           myOldGMarker = new Array();
                
           arraySize = JTVEvents.length;
           SetDisplayTime();
           var timeoutId = setTimeout(function(){
               DisplayMarker(JTVEvents);
               }, displayTime);                           
 }

function SetDisplayTime()
 {
    if (arraySize != 0)
        displayTime = Math.round(cacheDuration/arraySize);
    
    if (displayTime > maxDisplayTime)
    {
        displayTime = maxDisplayTime;
    }    
 }
 
function SetJottIcon(activityType)
 {
      var jottIcon = new GIcon();
      
      switch (activityType)
      {
        case 0:
            jottIcon.image = "Images/newJotter.png";
            jottIcon.iconSize = new GSize(18, 23);
            jottIcon.iconAnchor = new GPoint(9, 23);
            break;
        case 1:
            jottIcon.image = "Images/jottToSelf.png";
            jottIcon.iconSize = new GSize(18, 33);
            jottIcon.iconAnchor = new GPoint(9, 33);
            break;
        case 2:
            jottIcon.image = "Images/jottSentFrom.png";
            jottIcon.iconSize = new GSize(18, 33);
            jottIcon.iconAnchor = new GPoint(9, 33);
            break;
        case 3:
            jottIcon.image = "Images/jottReceived.png";
            jottIcon.iconSize = new GSize(18, 23);
            jottIcon.iconAnchor = new GPoint(9, 23);
            break;
        case 5:
            jottIcon.image = "Images/jottVoiceMail.png";
            jottIcon.iconSize = new GSize(18, 23);
            jottIcon.iconAnchor = new GPoint(9, 23);
            break;
        case 6:
            jottIcon.image = "Images/jottToLink.png";
            jottIcon.iconSize = new GSize(18, 23);
            jottIcon.iconAnchor = new GPoint(9, 23);
            break;
        default:
            jottIcon.image = "Images/jottToSelf.png";
            jottIcon.iconSize = new GSize(18, 23);
            jottIcon.iconAnchor = new GPoint(9, 23);
            break;
      }
     
   
      jottIcon.shadow = null;
      jottIcon.shadowSize = null;
      jottIcon.infoWindowAnchor = null;
      jottIcon.printImage = jottIcon.image;
      jottIcon.mozPrintImage = jottIcon.image;
      jottIcon.printShadow = jottIcon.shadow;
      jottIcon.transparent = jottIcon.image;
      jottIcon.imageMap = null;
      jottIcon.maxHeight = 0;
      jottIcon.dragCrossImage = null;
      jottIcon.dragCrossSize = null;
      jottIcon.dragCrossAnchor =null;
      return jottIcon;
 }
       
 
function DisplayMarker(Events)
 {       
    if (x == (maxPinDisplay - 1))
    {
        x = 0;
    }
    
    if (myOldGMarker[x])
    {
        if (!myOldGMarker[x].marker.isHidden())
            myOldGMarker[x].marker.hide();      
    }
    
    myOldGMarker[x] = new JTVMarker();
    
    switch(GetDisplayCase(Events))
    {                    
        case 0:
            var myGPoint = primaryMap.fromLatLngToDivPixel(new GLatLng(Events[y].Latitude, Events[y].Longitude));
                        
            var angle = dCaseCtrl * 45;
            var circle_radius = 3 //px
            myGPoint.x += Math.round(Math.sin(angle)*circle_radius);
            myGPoint.y += Math.round(Math.cos(angle)*circle_radius);
            dCaseCtrl++;
                                    
            myOldGMarker[x].marker = new GMarker(
                                    primaryMap.fromDivPixelToLatLng(myGPoint),
                                    {
                                     icon:SetJottIcon(parseFloat(Events[y].ActivityType)),
                                     dragCrossMove:false,
                                     title:null, 
                                     clickable:false, 
                                     draggable:false, 
                                     bouncy:false, 
                                     bounceGravity:0, 
                                     autoPan:false, 
                                     zIndexProcess:OrderOfCreation}
                                     );
            myOldGMarker[x].typeID = currentZoom;                                  
            break;        
        default:
            dCaseCtrl = 0;            
            myOldGMarker[x].marker = new GMarker(
                                    new GLatLng(Events[y].Latitude, Events[y].Longitude),
                                    {
                                     icon:SetJottIcon(parseFloat(Events[y].ActivityType)),
                                     dragCrossMove:false,
                                     title:null, 
                                     clickable:false, 
                                     draggable:false, 
                                     bouncy:false, 
                                     bounceGravity:0, 
                                     autoPan:false, 
                                     zIndexProcess:OrderOfCreation}
                                    );
            break;      
    }
    
    if (myOldGMarker[x].marker)
        primaryMap.addOverlay(myOldGMarker[x].marker);
        
    eventCount--;
    document.getElementById("pNJ").innerHTML = eventCount + ' new events';
    x++;
    y++;
        
    if (y < arraySize)
    {
        var timeoutId = setTimeout(function(){
                DisplayMarker(Events);
                }, displayTime);
    }
    else
    {        
        var animTimeout1 = setTimeout(function(){            
                                document.getElementById("pNJ").style.border = "2px inset orange";
                                var animTimeout2 = setTimeout(function(){
                                                    document.getElementById("pNJ").style.border = "1px outset black";
                                                    }, 250); 
                                document.getElementById("pNJ").innerHTML = '<img src="Images/anim_JTV.gif" alt="Loading...">';                               
                                RetrieveEvents();
                                }, 1000);
    }
 }

function GetDisplayCase(Events)
{
    for (a = 0 ; a < y ; a ++)
    {   
        if (Events[a] && Events[a].Latitude == Events[y].Latitude && Events[a].Longitude == Events[y].Longitude)
        return 0;
    }    
    return 1;    
}

