/*
 * This file contains javascript helper functions
 */


// hide content within a <div></div>
function hideDiv(divName) {
   if (document.getElementById) { // DOM3 = IE5, NS6
      document.getElementById(divName).style.visibility = 'hidden';
   } else {
      if (document.layers) { // Netscape 4
         document.divName.visibility = 'hidden';
      } else { // IE 4
         document.all.divName.style.visibility = 'hidden';
      }
   }
}

// show content within a <div></div>
function showDiv(divName) {
   if (document.getElementById) { // DOM3 = IE5, NS6
      document.getElementById(divName).style.visibility = 'visible';
   } else {
      if (document.layers) { // Netscape 4
         document.divName.visibility = 'visible';
      } else { // IE 4
         document.all.divName.style.visibility = 'visible';
      }
   }
}

// show the first div and hide the second div
function show1Hide1Div(div1,div2) {
   showDiv(div1);
   hideDiv(div2);
}

// build an ajax object
function ajaxObject(url) {                                           // This is the object constructor
   var that=this;                                                    // A workaround for some javascript idiosyncrocies
   this.updating = false;                                            // Set to true if this object is already working on a request
   this.callback = function () {}                                    // A post-processing call -- a stub you overwrite.

   this.update = function(passData) {                                // Initiates the server call.
      if (that.updating==true) { return false; }                     // Abort if we're already processing a call.
      that.updating=true;                                            // Set the updating flag.
      var AJAX = null;                                               // Initialize the AJAX variable.
      if (window.XMLHttpRequest) {                                   // Are we working with mozilla?
         AJAX=new XMLHttpRequest();                                  //  Yes -- this is mozilla.
      } else {                                                       // Not Mozilla, must be IE
         AJAX=new ActiveXObject("Microsoft.XMLHTTP");                //  Wheee, ActiveX, how do we format c: again?
      }                                                              // End setup Ajax.
      if (AJAX==null) {                                              // If we couldn't initialize Ajax...
         return false;                                                // Return false (WARNING - SAME AS ALREADY PROCESSING!)
      } else {
         AJAX.onreadystatechange = function() {                      // When the browser has the request info..
            if (AJAX.readyState==4) {                                //   see if the complete flag is set.
               that.updating=false;                                  //   Set the updating flag to false so we can do a new request
               that.callback(AJAX.responseText,AJAX.status);         //   Pass respons and status to callback.
               delete AJAX;                                          //   delete the AJAX object since it's done.
            }                                                        // End Ajax readystate check.
         }                                                           // End create post-process fucntion block.
         var timestamp = new Date();                                 // Get a new date (this will make the url unique)
         var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1);   // Append date to url (so the browser doesn't cache the call)
         AJAX.open("GET", uri, true);                                // Open the url this object was set-up with.
         AJAX.send(null);                                            // Send the request.
         return true;                                                // Everything went a-ok.
      }                                                              // End Ajax setup aok if/else block                 
   }
      
   // This area set up on constructor calls.
   var urlCall = url;                                                // Remember the url associated with this object.
}             

function phoneClick(id, resultDivName, clickDivName, sponsored, rootDir) {
   var obj = new ajaxObject(rootDir + '/includes/phoneClick.php');
   obj.callback = function (responseText, responseStatus) { 
      displayPhoneResult(responseText, responseStatus, resultDivName);  
   }                                     
   obj.update('id=' + id + '&sp=' + sponsored); 
   
   // now hide the clicker
   hideDiv(clickDivName);
}

function displayPhoneResult(responseText, responseStatus, divName) {
   if (responseStatus==200) {
      // show the result
      document.getElementById(divName).innerHTML=responseText;
   } else {
      divName.innerHTML='Failed to get the feed you requested';
   }
}

function mapClick(id, sponsored, rootDir) {
   var obj = new ajaxObject(rootDir + '/includes/mapClick.php');
   obj.callback = function (responseText, responseStatus) {
      mapClickHandler(responseText, responseStatus);  
   }                                                    
   obj.update('id=' + id + '&sp=' + sponsored);
}

function mapClickHandler(responseText, responseStatus) {
   // nothing to do
}

function gmapLoad(divName, centerLat, centerLon) {
    if (GBrowserIsCompatible()) {
       if(document.getElementById(divName) != undefined ) {
          var map = new GMap2(document.getElementById(divName));
          map.addControl(new GLargeMapControl());
          map.addControl(new GMapTypeControl());
                          
          // center the map
          map.setCenter(new GLatLng(centerLat, centerLon), 10);
       }
    }
    
    return map;
 }
 
 function gmapAddMarker(map, lat, lon, title, text, id, sponsored, iconFile, rootDir) {
    var point = new GLatLng(lat, lon);
    var theIcon = new GIcon(G_DEFAULT_ICON);
    theIcon.image = iconFile;
    var markerOptions = { icon:theIcon, title:title };
    var marker = new GMarker(point,markerOptions);
    GEvent.addListener(marker, "click", function() {
       marker.openInfoWindowHtml(text);
       // track the click
       mapClick(id, sponsored, rootDir);
    });
    
    map.addOverlay(marker);
 }

