var qs = new Querystring();
//var nodeId = qs.get("nid");

google.load("maps", "2.x");
var map = null;
var geocoder = null;

var address_array = new Array();
var names_array = new Array();
var thumbs_array = new Array();
var place_array = new Array();
var msg_array = new Array();

if(window.XMLHttpRequest){ // Mozilla, Safari, ...
var xmlHttp = new XMLHttpRequest();
} 
else if(window.ActiveXObject){ // IE
    var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

if(window.XMLHttpRequest){ // Mozilla, Safari, ...
var xmlHttp1 = new XMLHttpRequest();
} 
else if(window.ActiveXObject){ // IE
    var xmlHttp1 = new ActiveXObject("Microsoft.XMLHTTP");
}

if(window.XMLHttpRequest){ // Mozilla, Safari, ...
var xmlHttp2 = new XMLHttpRequest();
} 
else if(window.ActiveXObject){ // IE
    var xmlHttp2 = new ActiveXObject("Microsoft.XMLHTTP");
}

if(window.XMLHttpRequest){ // Mozilla, Safari, ...
var xmlHttp3 = new XMLHttpRequest();
} 
else if(window.ActiveXObject){ // IE
    var xmlHttp3 = new ActiveXObject("Microsoft.XMLHTTP");
}

/*xmlHttp.open('post','process/processShow.inc.php?mode=fetch_nodemembers_address',true);
xmlHttp.onreadystatechange = function() { fnResponse(xmlHttp); };
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
qryString="nid=" + nodeId;
xmlHttp.send(qryString);

xmlHttp1.open('post','process/processShow.inc.php?mode=fetch_nodemembers_names',true);
xmlHttp1.onreadystatechange = function() { fnResponse1(xmlHttp1); };
xmlHttp1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp1.send(qryString);

xmlHttp2.open('post','process/processShow.inc.php?mode=fetch_nodemembers_thumbs',true);
xmlHttp2.onreadystatechange = function() { fnResponse2(xmlHttp2); };
xmlHttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp2.send(qryString);*/

xmlHttp3.open('post',HTTP_SERVER+'process/processShow.inc.php?mode=fetch_nodemembers_ids',true);
xmlHttp3.onreadystatechange = function() { fnResponse3(xmlHttp3); };
xmlHttp3.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
qryString="nid=" + nodeId;
xmlHttp3.send(qryString);

function fnResponse(xmlHttp){   
    if(xmlHttp.readyState == 4){ 
        if(xmlHttp.status == 200){
            var response=xmlHttp.responseText;           
            address_array = JSON.decode(response);          
        }
        else{
            alert(arrMsg["ajaxError"]);
        }
    }
}

function fnResponse1(xmlHttp1){   
    if(xmlHttp1.readyState == 4){ 
        if(xmlHttp1.status == 200){
            var response=xmlHttp1.responseText;   
            names_array = JSON.decode(response);
        }
        else{
            alert(arrMsg["ajaxError"]);
        }
    }
}

function fnResponse2(xmlHttp2){   
    if(xmlHttp2.readyState == 4){ 
        if(xmlHttp2.status == 200){
            var response=xmlHttp2.responseText;   
            thumbs_array = JSON.decode(response);    
        }
        else{
            alert(arrMsg["ajaxError"]);
        }
    }
}

function fnResponse3(xmlHttp3){   
    if(xmlHttp3.readyState == 4){ 
        if(xmlHttp3.status == 200){
            var response=xmlHttp3.responseText;   
            if(response != '')
            {
                var user_array = JSON.decode(response); 
                
                xmlHttp.open('post',HTTP_SERVER+'process/processShow.inc.php?mode=fetch_members_address',true);
                xmlHttp.onreadystatechange = function() { fnResponse(xmlHttp); };
                xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                qryString="ids=" + user_array;
                xmlHttp.send(qryString);   

                xmlHttp1.open('post',HTTP_SERVER+'process/processShow.inc.php?mode=fetch_members_names',true);
                xmlHttp1.onreadystatechange = function() { fnResponse1(xmlHttp1); };
                xmlHttp1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlHttp1.send(qryString);   
                
                xmlHttp2.open('post',HTTP_SERVER+'process/processShow.inc.php?mode=fetch_members_thumbs',true);
                xmlHttp2.onreadystatechange = function() { fnResponse2(xmlHttp2); };
                xmlHttp2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlHttp2.send(qryString);
            }
        }
        else{
            alert(arrMsg["ajaxError"]);
        }
    }
}

function initialize()
{
    var counter = 0;
    while(address_array.length == 0 && counter < 30)
    {  
        counter++;
    }

    if(address_array.length > 0)
    {    
        map = new google.maps.Map2(document.getElementById("map"));
        map.setCenter(new google.maps.LatLng(34,0), 2);
        map.addControl(new google.maps.LargeMapControl());
        map.addControl(new google.maps.MapTypeControl());
        map.setMapType(G_NORMAL_MAP); 
        geocoder = new GClientGeocoder();
        
        for (var i = 0 ; i < address_array.length; i++)
        {
            var name = names_array[i];
            var thumb = thumbs_array[i];
            var address = address_array[i];              
            
            var temp = findInArray(address_array, address_array[i], i)
            if(temp != "")
            {
                place_array.push(address_array[i]);                
                temp = new String(temp);
                var temp_arr = temp.split(",");
                var msg = name + ', ' + address + '<br><img src="' + thumb + '" alt="'+name+'" title="'+name+'"/><br><br>'; 
                for(var j = 0; j < temp_arr.length; j++)
                {
                    var k = temp_arr[j];
                    var name = names_array[k];
                    var thumb = thumbs_array[k];
                    var address = address_array[k]; 
                    
                    msg = msg + name + ', ' + address + '<br><img src="' + thumb + '" alt="'+name+'" title="'+name+'"/><br><br>'; 
                }
                
                msg_array.push(msg);
            }
            else
            {            
                onefriend = name + ', ' + address + '<br><img src="' + thumb + '" alt="'+name+'" title="'+name+'"/>';
                showAddress(address,onefriend);  
            }
        }
        
        if(place_array.length > 0)
        {
            for (var i = 0 ; i < place_array.length; i++) 
                showAddress(place_array[i],msg_array[i]);    
        }
    }
}
function showAddress(address,msg) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            } else {
                var blueIcon = new google.maps.Icon(G_DEFAULT_ICON);
                blueIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
                markerOptions = { icon:blueIcon };
                var marker = new google.maps.Marker(point,markerOptions);
                var mark = createMarker(point,msg);
                map.addOverlay(mark);
                google.maps.Event.trigger(mark, "click");
           }
        }
     );
   }
}

  function createMarker(point, msg) {
      var marker = new google.maps.Marker(point);
      google.maps.Event.addListener(marker,"click", function() {
        var html = "<div style='width: 210px; padding-right: 10px'>" + msg + "<\/div>";
        map.openInfoWindowHtml(point, html);
      });
      return marker;
    }

  function findInArray(arr, val, key)
  {
    var ret = "";
    for(var i = 0; i < arr.length; i++)
    {
        if(i == key)
            continue;
            
        if(arr[i] == val)
        {
            if(ret == "")   
                ret = i;
            else
                ret = ret + "," + i;   
        }        
    }
    return ret;
  }
    
google.setOnLoadCallback(initialize);
