var YoutubeObj;
var youtubeVars = new YoutubeVars();
var Youtube = new YoutubeC();

var youtubeMarker = new Array();
function YoutubeC() { }
YoutubeC.prototype.update = function () {
      youtubeAddress  = "/apis/getyoutube.php";
      new Ajax.Request(youtubeAddress+"?mode=tree&bbox="+youtubeVars.southWest.lng() + "," + youtubeVars.southWest.lat() +
      "," + youtubeVars.northEast.lng() + "," + youtubeVars.northEast.lat(), {
      method: 'get',
      onSuccess: function(transport) { YoutubeObj = transport.responseText.evalJSON();
      if (YoutubeObj.total>0){
        YoutubeC.prototype.hide();
         youtubeVars.markers = [];
        for (id in YoutubeObj.photos) {
          YoutubeC.prototype.generateMarkers(YoutubeObj.photos[id])
        }
      }

      }});

    }

YoutubeC.prototype.generateMarkers = function (el) {


        var point = new GLatLng(el.coordinates.latitude, el.coordinates.longitude);
        icon = new GIcon();
        icon.image = el.square;
        icon.iconSize = new GSize(40, 40);
        icon.shadowSize = new GSize(22, 22);
        icon.iconAnchor = new GPoint(9, 9);
        icon.infoWindowAnchor = new GPoint(9, 0);
        youtubeMarker = new GMarker(point, icon);
        el.html = "<div class='youtube_marker custom_marker'><div class='logo'><img src='http://l.yimg.com/g/images/youtube_logo_gamma.gif.v35314.14' alt=''></div><p><a href='http://youtube.com/photos/"+el.owner+"/"+el.id+"/' title='"+ el.title
          +"'><img src='"+el.thumbnail+"' alt=''></a></p><p>Owner: <a href='http://youtube.com/"+el.owner+"/' title = '"+el.owner+"'>"+
          el.owner + "</a> ["+el.title+"]</p></div>";
        map.addOverlay(youtubeMarker);
        GEvent.addListener(youtubeMarker, "click", function() {
        this.openInfoWindowHtml( el.html );
      });
      youtubeVars.markers.push(youtubeMarker);

}
YoutubeC.prototype.show = function () {
      var center = map.getCenter();
      var bounds = map.getBounds();
      youtubeVars.northEast = bounds.getNorthEast();
      youtubeVars.southWest = bounds.getSouthWest();

      youtubeVars.lat = center.lat();
      youtubeVars.lon = center.lng();
      this.update();
    }

YoutubeC.prototype.hide = function () {
      youtubeVars.markers.each(function(el){map.removeOverlay(el)})
    }

function YoutubeVars() {
  this.accuracy=4;
  this.lat;
  this.lon;
  this.perpage;
  this.page;
  this.markers = new Array();
}