/**
 ** snapshotsFunctions.js
 **
 ** Written by Owen O Doherty 27/07/09
 ** Purpose - Provide JS functions for snapshots
 **
 **
**/



var map;
var geocoder;
var address;
var center_point;


//Initializes the Map when the edit page loads.
function start_map() {      
      var loc = document.getElementById('imagelocation').value;
      var country = document.getElementById('imagecountry').value;

     if ( (loc=='') && (country=='') )
    {   
      document.getElementById('map_canvas').style.display = 'none';
    }
   else
   {
      map = new GMap2(document.getElementById("map_canvas"));
      var address = loc+' '+country;
var mapControl = new GMapTypeControl();
map.addControl(mapControl);
map.addControl(new GLargeMapControl());
      geocoder = new GClientGeocoder();
      geocoder.getLocations(address, addAddressToMap);
   }
} //END start_map


//When the user updates the map
function REmapping() {
      address= document.getElementById('imagelocation').value+' '+document.getElementById('imagecountry').value;
      map = new GMap2(document.getElementById("map_canvas"));
      geocoder = new GClientGeocoder();
      geocoder.getLocations(address, addAddressToMap);
var mapControl = new GMapTypeControl();
map.addControl(mapControl);
map.addControl(new GLargeMapControl());
      
}



    // addAddressToMap() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
    function addAddressToMap(response) {
      
      if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that Location.\nPlease try again, and click Refresh Map");
        //If we cannot find this address, hide the map
         document.getElementById('map_canvas').style.display = 'none';
      } else { 
         document.getElementById('map_canvas').style.display = 'block'; 

        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]); 

        map.setCenter(point, 13); 
        marker = new GMarker(point);

        map.addOverlay(marker);

        marker.openInfoWindowHtml(place.address + '<br>' +
          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode); 
       map.checkResize();
        map.setCenter(point, 13); 

      }
    }



//Initializes the mini Map when the view_image page loads.
function start_mini_map() {      
      map = new GMap2(document.getElementById("map_canvas"));
      address= document.getElementById('imagelocation').value+' '+document.getElementById('imagecountry').value;
          geocoder = new GClientGeocoder();
          geocoder.getLocations(address, addAddressToMap2);
} //END start_map



    // addAddressToMap2() is called when the geocoder returns an
    // answer.  It adds a marker to the map with an open info window
    // showing the nicely formatted version of the address and the country code.
   //This is for when a user is just browsing maps, and we want to hide the mao without displaying an alert
    function addAddressToMap2(response) {
      
      if (!response || response.Status.code != 200) {
        //alert("Sorry, we were unable to geocode that address");
        //If we cannot find this address, hide the map
         document.getElementById('map_canvas').style.display = 'none';
      } else {
         document.getElementById('map_canvas').style.display = 'block';
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
        map.setCenter(point, 13);
        marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml('<div style="width:170px;">'+place.address + '<br>' +
          '<b>Country code:</b> ' + place.AddressDetails.Country.CountryNameCode+'</div>');
        map.setCenter(point, 13);
var mapControl = new GMapTypeControl();
map.addControl(mapControl);
map.addControl(new GLargeMapControl());
      }
    }



//JQUERY FUNCTIONS BELOW THIS LINE


/** Make the image switch neatly 
$(function() { 
      $(".photostrip_img")
                 .click(function(event) 
                  {   
                      event.preventDefault();
                      var src = $(this).attr("src");
                      src = src.replace('tn_', '');
                              $("#main_img").fadeOut(1000, function() { 
                              $("#main_img").attr("src", src);
                              $("#main_img").load( function () {$("#main_img").fadeIn(200); });     
                              });                             
                      var albumid = $("#albumid").val();
                      var user = $("#dwuserid").val();
                      var imageid = src.substring(src.lastIndexOf('/')+1, src.lastIndexOf('.'));
                         $("#edit_photo_link").attr("href", 'edit_image.php?dwuserid='+user+'&imageid='+imageid+'&albumid='+albumid);
           
                   });
   //with the image swapped, now change the edit image tag
  

 return false;
}); 

**/


/**
 **acvivates the create album filed if checked
**/

function toggleAlbumCreate(inst)
{
  //alert(''+inst);
  var chk=document.getElementById('AlbumCreate'+inst).checked;  

  if (chk==true)
  {  
     document.getElementById('newalbumname'+inst).disabled ='';
  }
  else
  {
     document.getElementById('newalbumname'+inst).disabled ='disabled';
  }
}

