
var sliderHotspotLinkEdit = null;

/****************************************************
function: hotspot_create_marker
description: utility function that creates a map marker
****************************************************/
function hotspot_create_marker(point, info) {

/*
  var rank = parseFloat(info['r']);

  if (rank < 2.0) {

      var marker = new GMarker(point, iHsp2);

  } else if (rank < 3.5) {

      var marker = new GMarker(point, iHsp1);

  } else {

      var marker = new GMarker(point, iHsp0);
  }
*/

  if ( parseInt(info['c']) == 0) {

      var marker = new GMarker(point, { 'icon' : iHspG } );

  } else {

      var marker = new GMarker(point, { 'icon' : iHsp0 } );
  }


  var tooltip = new Tooltip(marker, info['name'], 4);
  marker.tooltip = tooltip;

//  map.addOverlay(marker);
  map.addOverlay(tooltip);

  GEvent.addListener(marker,'mouseover',function(){ this.tooltip.show(); });
  GEvent.addListener(marker,'mouseout',function(){ this.tooltip.hide(); });

//  marker.tooltip = '<div class="tooltip"><nobr>'+info['name']+'</nobr></div>';

	marker.hotspot_id=info['id'];
	marker.token=info['token'];
	marker.name=info['name'];
	GEvent.addListener(marker, "click", function() { hotspot_build_info_window(marker); } );

//  GEvent.addListener(marker,"mouseover", function() { showTooltip(marker); });
//  GEvent.addListener(marker,"mouseout", function() { tooltip.style.visibility="hidden" });

	return marker;
}


// ====== This function displays the tooltip ======
// it can be called from an icon mousover or a side_bar mouseover
function showTooltip(marker) {

  tooltip.innerHTML = marker.tooltip;
  var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
  var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
  var anchor=marker.getIcon().iconAnchor;
  var width=marker.getIcon().iconSize.width;
  var height=tooltip.clientHeight;
  var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
  pos.apply(tooltip);
  tooltip.style.visibility="visible";
}



/****************************************************
function: hotspot_enable_move_mode
description: enables onclick hotspot-moving event for map
****************************************************/
function hotspot_enable_move_mode(hotspot_id){

	var myXHR = new XHR({method:'post',onSuccess:function(results){

		var return_obj = Json.evaluate(results);

		if (return_obj["success"]==true){

      hotspot_close();

    	if (undefined_hotspot) map.removeOverlay(undefined_hotspot);

      map_tool_mode = MAP_TOOL_MOVE_HOTSPOT;

    	eventListenerClick = GEvent.addListener(map, "click", function(marker, point) {

      	if (!marker) {

      	  if (map.getZoom() >= ADD_HOTSPOT_THRESHOLD) {

      			if (undefined_hotspot)
      			  map.removeOverlay(undefined_hotspot);

      			undefined_hotspot = new GMarker(point, { icon:iBlu, draggable:true } );
      			undefined_hotspot.enableDragging();
      			map.addOverlay(undefined_hotspot);

            msg = "<p>Please confirm you've located the hotspot correctly.</p>";
            msg += "<p><a href=\"javascript: hotspot_update_location(" + hotspot_id + ");\">OK</a>";
            msg += "<a href=\"javascript: reset_map_mode();\">Cancel</a></p>";

          	$('quick-message').innerHTML = msg;
          	$('quick-message').style.display = "block";
      		}
        }
    	});

      validate_tool_mode(map.getZoom());

		} else {

      ShowLoginPrompt("Please login or register if you'd like to move a hotspot.");
		}

	}}).send('/php/check_login.php',"");
}

/****************************************************
function: hotspot_update_location
description: move an existing hotspot
****************************************************/
function hotspot_update_location(hotspot_id) {

	var myXHR = new XHR({method:'post',onSuccess: function(results){

		var return_obj = eval("("+results+")");

		if (return_obj["success"]==true){

// clean up
		  $('quick-message').style.display = "none";

// move the marker

			for (var i=0;i<gmarkers.length;i++){
				if (parseInt(gmarkers[i].hotspot_id)==parseInt(hotspot_id)){
					gmarkers[i].setPoint( undefined_hotspot.getPoint() );
				}
			}

//      set_mode_hotspot();
      reset_map_mode();

		} else {

		  alert(return_obj["message"]);

		}
	}}).send('/php/controller_hotspot.php',"action=move&id="+hotspot_id+"&lat="+undefined_hotspot.getPoint().lat()+"&lng="+undefined_hotspot.getPoint().lng());
}
/****************************************************
function: hotspot_enable_add_mode
description: enables onclick hotspot-adding event for map
****************************************************/
function hotspot_enable_add_mode(){

	var myXHR = new XHR({method:'post',onSuccess:

		function(results){

			var return_obj = Json.evaluate(results);

			if (return_obj["success"]==true){

        if (map_mode != MAP_MODE_HOTSPOT) {

          set_mode_hotspot();
        }

			  map_tool_mode = MAP_TOOL_ADD_HOTSPOT;

      	eventListenerClick = GEvent.addListener(map, "click", function(marker, point) {

      		if (!marker) {

        	  if (map.getZoom() >= ADD_HOTSPOT_THRESHOLD) {

        	    if (!undefined_hotspot) {

                msg = "<p>Please confirm you've located the hotspot correctly.</p>";
                msg += "<p><a href=\"javascript: show_hotspot_form(undefined_hotspot.getPoint().lat(),undefined_hotspot.getPoint().lng());\">OK</a>";
                msg += "<a href=\"javascript: reset_map_mode();\">Cancel</a></p>";

              	$('quick-message').innerHTML = msg;
              	$('quick-message').style.display = "block";

        	    }

        			if (undefined_hotspot)
        			  map.removeOverlay(undefined_hotspot);

        			undefined_hotspot = new GMarker(point, { icon:iBlu, draggable:true } );
        			undefined_hotspot.enableDragging();
        			map.addOverlay(undefined_hotspot);

              if(eventListenerClick) {

                GEvent.removeListener(eventListenerClick);
                eventListenerClick=null;
              }

              map_tool_mode = MAP_TOOL_PAN;

        		}
      	  }

    	  });

// check the zoom level

        validate_tool_mode(map.getZoom());

			} else {

			  ShowLoginPrompt("Please login or register if you'd like to add a hotspot.");
			}
		}
	}).send('/php/check_login.php',"");
}


/****************************************************
function: validate_tool_mode
description: check the zoom level for the active tool
****************************************************/
function validate_tool_mode(ZoomLevel)
{
  var msg='';

  if( map_tool_mode == MAP_TOOL_ADD_HOTSPOT) {

    if (ZoomLevel < ADD_HOTSPOT_THRESHOLD) {

      msg = '<p>Please zoom in closer to add a hotspot. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    	if (undefined_hotspot) {
    	  map.removeOverlay(undefined_hotspot);
    	  undefined_hotspot=null;
    	}

    } else if (!undefined_hotspot) {

      msg = '<p>Click on map to add hotspot. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    }

  } else if( map_tool_mode == MAP_TOOL_ADD_SIGHTING) {

    if (ZoomLevel < ADD_HOTSPOT_THRESHOLD) {

      msg = '<p>Select a hotspot or zoom in closer to add a sightings record. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    	if (undefined_sighting) {
    	  map.removeOverlay(undefined_sighting);
    	  undefined_sighting=null;
    	}

    } else if (!undefined_sighting) {

      msg = '<p>Select a hotspot or click on map to add a sightings record. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    }

  } else if (map_tool_mode == MAP_TOOL_MOVE_HOTSPOT) {

    if (ZoomLevel < ADD_HOTSPOT_THRESHOLD) {

      msg = '<p>Please zoom in closer to move hotspot. <a href="javascript: reset_map_mode();">Cancel</a></p>';

    } else {

      msg = '<p>Click on map to move hotspot. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    }

  } else if (map_tool_mode == MAP_TOOL_ADD_PROFILE) {

    if (ZoomLevel < ADD_HOTSPOT_THRESHOLD) {

      msg = '<p>Please zoom in closer to add your profile. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    	if (undefined_profile) {
    	  map.removeOverlay(undefined_profile);
    	  undefined_profile=null;
    	}

    } else if (!undefined_profile) {

      msg = '<p>Click on map to add your profile. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    }

  } else if (map_tool_mode == MAP_TOOL_MOVE_PROFILE) {

    if (ZoomLevel < ADD_HOTSPOT_THRESHOLD) {

      msg = '<p>Please zoom in closer to move your profile. <a href="javascript: reset_map_mode();">Cancel</a></p>';

    } else {

      msg = '<p>Click on map to move profile. <a href="javascript: reset_map_mode();">Cancel</a></p>';
    }
  }

  if (msg) {

  	$('quick-message').innerHTML = msg;
  	$('quick-message').style.display = "block";
  }
}

/****************************************************
function: disable_add_mode
description: removes onclick event from map
****************************************************/
function reset_map_mode()
{
  hotspot_close();
  profile_close();
  sighting_close();

  if(eventListenerClick) {

    GEvent.removeListener(eventListenerClick);
    eventListenerClick=null;
  }

  map_tool_mode = MAP_TOOL_PAN;
}


/****************************************************
function: show_hotspot_form
description: displays the hotspot addition form
****************************************************/
function show_hotspot_form(latitude,longitude){

	$('quick-message').innerHTML = '';
	$('quick-message').style.display = 'none';
	$('progress').style.display = '';

	var myXHR = new XHR({method:'post',onSuccess:function(results){

	  validation_enabled=false;
	  $('hs_menu_delete_hotspot').style.display='none';

  	$('hotspot-popup-content').innerHTML = results;
  	$('hotspot-popup').style.display = "block";

		if(sliderHotspotLinkEdit) delete sliderHotspotLinkEdit;
    sliderHotspotLinkEdit = new Fx.Slide('hotspot_links_edit', {duration: 500});
    sliderHotspotLinkEdit.hide(); // hide the div

		$('hotspot_name').focus();

    center_hotspot();

    TB_show();

    $('progress').style.display = 'none';

  }}).send("/views/hotspot_form.php","longitude="+longitude+"&latitude="+latitude);
}

/****************************************************
function: hotspot_edit
description: Show the edit hotspot form
****************************************************/
function hotspot_edit(hotspot_id){

	var myXHR = new XHR({method:'post',onSuccess:function(response){

		var res = Json.evaluate(response);

		if (res["success"]==true){

      var myXHR = new XHR({method:'post',onSuccess:function(form){

    	  validation_enabled=false;
        $('hs_menu_delete_hotspot').style.display='';
    		$('hotspot-popup-content').innerHTML=form;

    		if(sliderHotspotLinkEdit) delete sliderHotspotLinkEdit;
        sliderHotspotLinkEdit = new Fx.Slide('hotspot_links_edit', {duration: 500});
        sliderHotspotLinkEdit.hide(); // hide the div

        hotspot_form_validate();

        TB_show();

      }}).send('/views/hotspot_form.php?hotspot_id=' + hotspot_id, '');

		} else {

      ShowLoginPrompt("Please login or register if you'd like to edit a hotspot.");
		}

	}}).send('/php/check_login.php', '');
}


/****************************************************
function: hotspot_save
description: save the hotspot data
****************************************************/
function hotspot_save(){

  validation_enabled=true;

  if (hotspot_form_validate() ) {

  	var id = '';
    var vars = '&hotspot_name=' + encodeURIComponent($('hotspot_name').value);

    if ($('hotspot_id') && $('hotspot_id').value) {
      vars += '&hotspot_id=' + $('hotspot_id').value;
      id = $('hotspot_id').value;
    }

    vars += '&latitude=' + $('latitude').value;
    vars += '&longitude=' + $('longitude').value;
    vars += '&altitude=' + $('altitude').value;
    vars += '&city=' + encodeURIComponent($('city').value);
    vars += '&state=' + encodeURIComponent($('state').value);
    vars += '&postalcode=' + encodeURIComponent($('postalcode').value);
    vars += '&county=' + encodeURIComponent($('county').value);
    vars += '&country=' + encodeURIComponent($('country').value);
    vars += '&description=' + encodeURIComponent($('hsp_description').value);
    vars += '&habitat=' + encodeURIComponent($('habitat').value);
    vars += '&target_birds=' + encodeURIComponent($('target_birds').value);
    vars += '&when_to_go=' + encodeURIComponent($('when_to_go').value);
    vars += '&amenities=' + encodeURIComponent($('amenities').value);
    vars += '&getting_there=' + encodeURIComponent($('getting_there').value);
    vars += '&tips=' + encodeURIComponent($('tips').value);
    vars += '&recommended_reading=' + encodeURIComponent($('recommended_reading').value);

    if ( $('other_type_check').checked && $('type_other').value ) {

      vars += '&other_type_check=true';
      vars += '&type_other=' + encodeURIComponent($('type_other').value);
    }

    var types = document.getElementsByName("hotspot_type_id[]");
    for (var i = 0; i < types.length; ++i) {
      if (types[i].checked)  vars += "&hotspot_type_id[]=" + types[i].value;
    }

    var myXHR = new XHR({method:'post',onSuccess:function(results){

    	var return_obj = Json.evaluate(results);

    	if (return_obj["success"]==true){

// update the map marker and menu items

        if (undefined_hotspot && return_obj["id"]) {

      		var info=Array();

      		id = parseInt(return_obj["id"]);

      		info["id"] = id;
      		info["token"] = return_obj["token"];
      		info["name"] = $('hotspot_name').value;

    		  var marker = hotspot_create_marker(undefined_hotspot.getPoint(),info);
    		  map.addOverlay(marker);
    		  gmarkers.push(marker);
    		  gmarkersIds.push(id);

          $('hs_menu_open_in_new_window').href=home + "hspsolo/" + info["token"];
          $('hs_menu_open_in_new_window2').href=home + "hspsolo/" + info["token"];
          $('hs_menu_printer_friendly_version').href=home + "hspsolo/" + info["token"] + "#print";
          $('hs_menu_change_hotspot_location').href="javascript:hotspot_enable_move_mode(" + id + ")";
          $('hs_menu_delete_hotspot').style.display='';

      	  map.removeOverlay(undefined_hotspot);
      	  undefined_hotspot=null;

        } else {

// ensure that an existing marker is 'red'

          for (var i = 0; i < gmarkers.length; ++i) {

            if (gmarkers[i].hotspot_id == id) {

              gmarkers[i].setImage('/images/mm_20_red.png');
              break;
            }
          }
        }

        hotspot_cancel_edit(id);

// update account area
        if ( $('accmnu_hotspots').className == 'sel') {

          user_area_show_hospots();
        }

    	}else{

  			alert(return_obj["message"]);
    	}

    }}).send('/php/save_hotspot.php', vars );
  }
}

/* check for valid name, description and type
----------------------------------------------------------------------------*/
var validation_enabled=false;

function hotspot_form_validate() {

  var valid = true;

  if(validation_enabled) {

    if (!$('hotspot_name').value) {
      $('validate_msg_hotspot_name').style.display='';
      $('hotspot_name').style.borderColor='red';
      valid = false;

    } else {
      $('validate_msg_hotspot_name').style.display='none';
      $('hotspot_name').style.borderColor='#BEBCBC';
    }

    if (!$('hsp_description').value) {
      $('validate_msg_hotspot_desc').style.display='';
      $('hsp_description').style.borderColor='red';
      valid = false;

    } else {

      $('validate_msg_hotspot_desc').style.display='none';
      $('hsp_description').style.borderColor='#BEBCBC';
    }

    var val_type = 0;
    var types = document.getElementsByName("hotspot_type_id[]");

    for (var i = 0; i < types.length; ++i) {

      if (types[i].id == 'other_type_check') {

        if (types[i].checked && $('type_other').value) {

          val_type += 1;
        }
      } else if (types[i].checked) val_type += 1;
    }
    if (val_type == 0) {

      $('validate_msg_hotspot_type').innerHTML='Please choose at least one type.';
      $('validate_msg_hotspot_type').style.display='';
      valid = false;

    } else if (val_type >= 4) {

      $('validate_msg_hotspot_type').innerHTML='Please choose 3 or fewer types.';
      $('validate_msg_hotspot_type').style.display='';
      valid = false;

    } else {

      $('validate_msg_hotspot_type').style.display='none';
    }
  }
	return valid;
}

/****************************************************
function: hotspot_close
description: closes hotspot dialog and empties its contents
****************************************************/
function hotspot_close(){

  parent.location.hash = "#";
  document.title = 'Aviatlas';


	$('hotspot-popup').style.display = "none";
	$('hotspot-popup-content').innerHTML="";
	if (undefined_hotspot) {
	  map.removeOverlay(undefined_hotspot);
	  undefined_hotspot="";
	}

	$('quick-message').innerHTML = "";
	$('quick-message').style.display = "none";

	TB_remove();
}


function set_photo_img_fullsize(filename, name, screenname, uploaded) {

  $('photo_img_fullsize').src = '/uploads/' + filename;

  $('photo_hotspot_title').innerHTML = name;
  $('photo_hotspot_date').innerHTML = screenname == '' ? '' : 'Uploaded by: ' + screenname + ' ' + uploaded;
}


/****************************************************
function: hotspot_build_info_window
description: gets hotspot window content
****************************************************/
function hotspot_build_info_window(marker){

  if(map_tool_mode == MAP_TOOL_ADD_SIGHTING) {

    map_tool_mode = MAP_TOOL_PAN;
    sighting_add(marker.hotspot_id, marker.name, marker.getLatLng().lat(), marker.getLatLng().lng(), true);

  } else {

    $('hs_menu_delete_hotspot').style.display='';

    linkhotspot(marker.token, marker.name);

    $('hs_menu_open_in_new_window').href=home + "hspsolo/" + marker.token;
    $('hs_menu_open_in_new_window2').href=home + "hspsolo/" + marker.token;
    $('hs_menu_printer_friendly_version').href=home + "hspsolo/" + marker.token + "#print";
    $('hs_menu_change_hotspot_location').href="javascript:hotspot_enable_move_mode(" + marker.hotspot_id + ")";
  //  $('hs_menu_delete_hotspot').href="javascript:delete_hotspot(" + marker.hotspot_id + ")";

    $('progress').style.display='';

  	var myXHR = new XHR({method:'post',onSuccess:function(results){

  		$('hotspot-popup-content').innerHTML = results;
  		$('hotspot-popup').style.display = "block";

      center_hotspot();

      TB_show();
      $('progress').style.display='none';

  	}}).send("/views/hotspot.php","hotspot_id="+marker.hotspot_id);
  }
}

/****************************************************
function: hotspot_show
description: show the hotspot info window
****************************************************/
function hotspot_show(hotspot_id){


  if ($('TB_overlay'))  $('TB_overlay').remove();
  hotspot_close();
  profile_close();
  map.closeInfoWindow();

  if (map_mode != MAP_MODE_HOTSPOT) {

    set_mode_hotspot(hotspot_show, hotspot_id);

  } else {

  // center on the hotspot
  	var myXHR = new XHR({method:'post',onSuccess:function(response){

    	var data = Json.evaluate(response);

    	if (data['success']==true) {

        var lat = parseFloat(data['lat']);
        var lng = parseFloat(data['lng']);

  // set map center which will guarantee the hotspots is on the map

        map_mode=MAP_MODE_NONE;

        map.setCenter(new GLatLng(lat, lng), 12);

        map_mode= MAP_MODE_HOTSPOT;

        update_hotspot_viewport_delta(hotspot_open_info, hotspot_id);

    	} else {

    	  alert('hotspot not found!');
    	}

    }}).send('/php/controller_hotspot.php?action=getcoordfromid', 'id=' + hotspot_id );
  }
}

function hotspot_open_info(hotspot_id) {

	for (var i = 0; i < gmarkers.length; i++ ) {

		if (gmarkers[i].hotspot_id == hotspot_id) {

  	  map.setCenter(gmarkers[i].getPoint(), 12);
  	  hotspot_build_info_window(gmarkers[i]);
  	  break;
		}
	}
}

/****************************************************
function: hotspot_cancel_edit
description: cancel edit mode and show the hotspot
****************************************************/
function hotspot_cancel_edit(hotspot_id){

	var myXHR = new XHR({method:'post',onSuccess:function(results){

    $('hs_menu_delete_hotspot').style.display='';
		$('hotspot-popup-content').innerHTML = results;

	}}).send("/views/hotspot.php","hotspot_id="+hotspot_id);

}


/****************************************************
function: hotspot_revert
description: Roll back a hotspot to an earlier revision
****************************************************/
function hotspot_revert(rev_id){

  if (confirm("Are you sure you want to revert to this revision?")) {

  	var myXHR = new XHR({method:'post',onSuccess:function(results){
  		var return_obj = eval("("+results+")");

  		if (return_obj["success"]==true){

  		  hotspot_close();

  		}else{
  			alert(return_obj["message"]);
  		}
  	}}).send('/php/controller_hotspot.php', 'action=revert&rev_id=' + rev_id);
  }
}


/* show a progress spinner during photo upload
****************************************************/
function hotspot_onstart_photo_upload() {

	$('photo_upload_progress').style.display = 'inline';
}

/* hide progress spinner and update content
****************************************************/
function hotspot_oncomplete_photo_upload(response) {

	$('photo_upload_progress').style.display = 'none';

  if (response) {

    var res = Json.evaluate(response);

    if (res['success'] == true) {

    	var myXHR = new XHR({method:'post',onSuccess:function(response) {

    		$('hp-tc-photo').innerHTML = response;

    // if this id the 1st photo for this hotspot, replace the thumbnail

    		if ($('hotspot-icon') && $('hotspot-icon').src == home + 'images/Geese59x59.jpg' ) {

    		  $('hotspot-icon').src = $('photo_img_fullsize').src;
    		}

    	}}).send("/php/get_photos.php", "hotspot_id=" + $('hotspot_id').value);


  // update account area

      if ( $('accmnu_hotspots').className == 'sel' ) {

        user_area_show_hospots();
      }

    } else {

      alert(res['message']);
    }

  }
}

/* hide progress spinner and update content
****************************************************/
function photo_show_upload_form() {


	var myXHR = new XHR({method:'post',onSuccess:function(results){

		var return_obj = eval("("+results+")");

		if (return_obj["success"]==true){


    // check if there's room to upload a new photo

      var thumbnails = document.getElementsByName('hotspot_thumbnail');

      if (thumbnails && thumbnails.length == 5) {

        alert('There is a limit of 5 photos per hotspot.  You\'ll have to delete an existing phohot before uploading a new one.');
    // show delete links

        var dels = document.getElementsByName('photo_delete_link');

        for (var i = 0; i < dels.length; ++i) {
          dels[i].className = 'hs-thumbnail-delete';
        }

      } else {

        $('div-photo-upload').style.display = 'block';
        $('hotspot-img-fullsize').style.display = 'none';
        if ($('button_upload_photo')) $('button_upload_photo').style.display = 'none';
      }

		} else {

      ShowLoginPrompt("Please login or register if you'd like to upload a photo.");
		}

	}}).send('/php/check_login.php',"");
}

function hotspot_photo_upload_cancel() {

  $('div-photo-upload').style.display = 'none';
  $('hotspot-img-fullsize').style.display = 'block';
  if ($('button_upload_photo')) $('button_upload_photo').style.display = '';
}

function photo_delete(el, file_id) {

  if (confirm("Are you sure you want to delete this photo?")) {

  	var myXHR = new XHR({method:'post',onSuccess:function(results){
  		var return_obj = eval("("+results+")");

  		if (return_obj["success"]==true){

        var dels = document.getElementsByName('photo_delete_link');

        for (var i = 0; i < dels.length; ++i) {
          dels[i].className = 'hs-thumbnail-delete-hidden';
        }

  		  var img_id = 'thumb' + file_id;

        var old_photo = $(img_id).src;

        if ( $('hotspot-icon') && $('hotspot-icon').src == old_photo) {
    		  $('hotspot-icon').src = '/images/Geese59x59.jpg';
        }


      	GDownloadUrl("/php/get_photos.php?hotspot_id=" + $('hotspot_id').value, function(data, responseCode) {

      		$('hp-tc-photo').innerHTML=data;

      // if this id the 1st photo for this hotspot, replace the thumbnail

      		if ($('hotspot-icon') && $('hotspot-icon').src == home + 'images/Geese59x59.jpg' ) {

      		  $('hotspot-icon').src = $('photo_img_fullsize').src;
      		}

      	});

  		} else {

  			alert(return_obj["message"]);
  		}
  	}}).send('/php/delete_photo.php', 'file_id=' + file_id);
  }
}


/****************************************************
function: hotspots_restore_visibility
description: sets all hotspot markers to visible
****************************************************/
function hotspots_restore_visibility() {

	for (var h = 0; h < gmarkers.length; h++) {

    gmarkers[h].show();
	}
}


function hotspot_enable_photo_upload() {

  $('button_submit_photo').disabled=false;
  $('button_submit_photo').style.backgroundColor='#669933';
}


function thumbnail_highlight(image) {

  image.style.borderWidth = "2px";
  image.style.borderColor = "#000000";
}

function thumbnail_unhighlight(image) {

  image.style.borderWidth = "1px";
  image.style.borderColor = "#CCCCCC";
}

/****************************************************
function: hotspot_rate
description: rate a hotspot location
****************************************************/
function hotspot_rate(id, rate) {

	var myXHR = new XHR({method:'post',onSuccess:function(response){

		var return_obj = Json.evaluate(response);

		if (return_obj["success"]==true){

    	var myXHR = new XHR({method:'post',onSuccess:function(response){

        var return_obj = Json.evaluate(response);

        if (return_obj["success"]==true){

    // refresh the hotspot content and map viewport

        	var myXHR = new XHR({method:'post',onSuccess:function(results){

        		$('hotspot-popup-content').innerHTML = results;

            clear_map_markers();
            update_hotspot_viewport_delta();


        	}}).send("/views/hotspot.php","hotspot_id="+id);

        }

    	}}).send('/php/controller_hotspot.php?action=rate','hotspot_id='+id+'&rate='+rate);


		} else {

      msg = "<p>Please login or register if you'd like to rate a location.</p><p><a href=\"javascript: clear_quick_message()\">OK</a></p>";
    	$('quick-message').innerHTML = msg;
    	$('quick-message').style.display = "block";

// not logged in, show the login form

      $('side-bar-content').innerHTML = "Please login or register if you'd like to rate a location.";
      set_active_tab($('tab-account'));
		}

	}}).send('/php/controller_user.php','action=checklogin');


}







/****************************************************
function: delete_hotspot
description: Removes a hotspot
****************************************************/
function hotspot_confirm_delete(){

	var myXHR = new XHR({method:'post',onSuccess:function(results){

		var return_obj = Json.evaluate(results);

		if (return_obj["success"]){

      if (confirm("Are you sure you want to permenantly delete this hotspot?")) {

        set_active_hp_tab_delete();

      }

		} else {
      ShowLoginPrompt("Please login or register if you'd like to delete a hotspot.");
		}

	}}).send('/php/check_login.php',"");
}


/****************************************************
function: hotspot_delete
description: Removes a users hotspot
****************************************************/
function hotspot_delete(hotspot_id){

  var msg = encodeURIComponent( $('hp-deleted-because').value );

	var myXHR = new XHR({method:'post',onSuccess:function(response){

		var res = Json.evaluate(response);

		if (res["success"]){

			for (var i = 0; i < gmarkers.length; i++){

				if (gmarkers[i].hotspot_id == hotspot_id){

          map.removeOverlay(gmarkers[i]);
          break;
				}
			}
  		$('hotspot-popup').style.display = "none";

    	TB_remove();

		}else{

			alert(res["message"]);
		}
	}}).send('/php/controller_hotspot.php?action=delete','hotspot_id='+hotspot_id+'&msg='+msg);
}


function hotspot_show_link_add()
{
  $('link_id').value='';
  $('link_label').value='';
  $('link_url').value='';
  $('hotspot_new_link_alert').innerHTML='';
  $('hotspot_new_link_alert').style.display='none';
  sliderHotspotLinkEdit.toggle();
}

function hotspot_show_link_edit(linkid)
{
  $('link_id').value=linkid;
  $('hotspot_new_link_alert').innerHTML='';
  $('hotspot_new_link_alert').style.display='none';


	var myXHR = new XHR({method:'post',onSuccess:function(response){

		var res = Json.evaluate(response);

		if (res["success"]) {

      var label = res['label'];
      var url = res['url'];
      $('link_label').value = label;
      $('link_url').value = url;
      sliderHotspotLinkEdit.toggle();

		} else {

			alert(res["message"]);
		}

	}}).send('/php/controller_hotspot.php?action=getlink','linkid=' + linkid);
}




function hotspot_link_delete(linkid) {

  if (confirm('Are you sure you want to delete this link?')) {

  	var myXHR = new XHR({method:'post',onSuccess:function(response){

  		var res = Json.evaluate(response);

  		if (res["success"]) {

  		  var elId = 'link' + linkid;

  		  $(elId).remove();

  		} else {

  			alert(res["message"]);
  		}

  	}}).send('/php/controller_hotspot.php?action=deletelink','linkid=' + linkid);
  }
}


function hotspot_hide_link_edit()
{
  $('hotspot_new_link_alert').style.display='none';
  sliderHotspotLinkEdit.hide(); // hide the div
}



function hotspot_save_link() {

  $('hotspot_new_link_alert').innerHTML='';

  var hotspot_id = $('hotspot_id').value;

  var linkid = $('link_id').value;
  var label = $('link_label').value;
  var url = $('link_url').value;

  if (!label) {

    $('hotspot_new_link_alert').innerHTML='Please enter a label for this link.';
    $('hotspot_new_link_alert').style.display='';
    $('link_label').focus();

  } else if (!url) {

    $('hotspot_new_link_alert').innerHTML='Please enter a url for this link.';
    $('hotspot_new_link_alert').style.display='';
    $('link_url').focus();

  } else {

    $('hotspot_new_link_alert').style.display='';
  	var myXHR = new XHR({method:'post',onSuccess:function(response){

  		var res = Json.evaluate(response);

  		if (res["success"]){

  		  hotspot_hide_link_edit();

  		  if (res["newid"]) {
          var id = res['newid'];

          url = res['url'];

          var li = new Element('li').injectInside( $('hotspot_links_list') );

          li.id = 'link' + id;
          li.innerHTML = '<a href="javascript:hotspot_link_delete('+id+')">Delete</a><a href="javascript:hotspot_show_link_edit('+id+')">Edit</a><a target="_blank" class="bluelink" href="'+url+'" >'+label+'</a></li>';
  		  }

  		} else {

  			alert(res["message"]);
  		}

  	}}).send('/php/controller_hotspot.php?action=savelink','hotspot_id='+hotspot_id+'&linkid='+linkid+'&label='+label+'&url='+url);
  }
}


function toggle_checklist(item) {

  if(item && item.parentNode) {

    var parent = item.parentNode;

    if (parent.className == 'birdlist-summary-title-open') {

      parent.className = 'birdlist-summary-title-closed';

    } else {

      parent.className = 'birdlist-summary-title-open';
    }
  }
}

