/*Global Variable Here*/
var LOCATION_URL = window.location;
var index        = 0;
var userLink     = '';
var myArray      = new Array();
var photoString  = '';
var counter      = 0;
var timer;
var map;



function loadMap()
{      
   if (GBrowserIsCompatible()) {                          
      map = new GMap2(document.getElementById("map_canvas"));        
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      point = new GLatLng(0, 0);
      map.setCenter(point,3); 
      getUserPosition();     
      GEvent.addListener(map, "moveend", function() {showBubble();});
   }    
}


function getUserPosition()
{   
   $.getJSON("googlemap.php?cmd=get_user_location", function(data){ 
        for(var j =0; j < data.length; j++)
        {
           myArray.push(data[j]);
        } 
        if(myArray.length > 0) {
          showMap();
        }  
    });   
} 


function showMap()
{
   map.clearOverlays();
   if(index > myArray.length-1) {  
      clearTimeout(timer);
      getUserPosition();
      location.href = LOCATION_URL;
      return;
   }            
   
   
   if(myArray[index].userId!=0)
   {
      photoElement = null;
      photoElement = document.createElement("LI");
      photoElement.className = "li_user_photo_thumb";
      photoElement.style.display = 'none';
      
      if(myArray[index].userId==0) {
         userLink = '#';
      }
      else {
         userLink = '/run.php/UserProfile?user_id='+myArray[index].userId;
      }   
      
      photoElement.innerHTML = "<a title='Test' href='"+userLink+"'><img style='display:block;'  border='0' src='"+ myArray[index].profileImage+"'></a>";
      var photoTooltip  = '<span><img src=\''+ myArray[index].profileImageBig+'\'></span>';
         
      if(document.getElementById('ul_photo_container')) {
         $('#ul_photo_container').prepend(photoElement);
         $($(photoElement).find("a")).tooltip({ 
                                           track: true, 
                                           delay: 0, 
                                           showURL: false, 
                                           showBody: " - ", 
                                           fade: 250,      
                                           bodyHandler: function() { 
                                               return photoTooltip; 
                                           } 
                                       });
      }
      
      $(photoElement).show("slow", function(){actionAfterEffect();});      
  }
  else
  {
      actionAfterEffect();
  }
}



function actionAfterEffect()
{   
   myTemplate   = '<div class="container">'
                +  '<a  href='+userLink+'><img border="0" src=\''+ myArray[index].profileImage+'\' class="quizImage"></a>'
	             +    '<p class="name"><span><a border="0" href="'+userLink+'">'+ myArray[index].username+'</a> in '+ myArray[index].city+','+ myArray[index].country +'</span></p>'
	             +    '<p class="description">Has taken this quiz</p>'
	             +    '<p class="quiz"><a border="0" href="/run.php/QuizRunner?quiz_id='+myArray[index].quizId+'">'+ myArray[index].quizName.replace(/\"+/gi,"'") +'</a></p>'
	             +    '<p style="margin-left:50px;">'+getRelativeTime(myArray[index].timeDiff)+'</p>' 
                +  '</div>';
          
   point = new GLatLng(myArray[index].latitude, myArray[index].longitude);       
   map.panTo(point);
   timer = setTimeout('showMap('+index+')', 5000); 

   index++;
   counter++;
}



function showBubble()
{
   if(index == 0 ) return;   
   map.addOverlay(new GMarker(point));  
   map.openInfoWindowHtml( map.getCenter(), myTemplate);
}


function getRelativeTime(timeValue) 
{
   var delta = parseInt(timeValue, 10);

   if(delta < 60) {
      return 'a moment ago';
   } 
   else if(delta < 120) {
      return 'about a minute ago';
   }
   else if(delta >= 120 && delta < 180) {
      return '2 minutes ago';
   } 
   else if(delta < (45*60)) {
      return (parseInt(delta / 60)).toString() + ' minutes ago';
   }
   else if(delta < (90*60)) {
      return 'about an hour ago';
   } 
   else if(delta < (24*60*60)) {
      return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
   } 
   else if(delta < (48*60*60)) {
      return '1 day ago';
   }
   else {
      return (parseInt(delta / 86400)).toString() + ' days ago';
   }
}


function getUTCTime(myDate)
{   
   localOffset = myDate.getTimezoneOffset() * 60000;   
   utc = myDate.getTime() + localOffset;
   return utc;
}


