function PanoramioLayer() {
  var me = this;
  me.ids = {};
  me.foto= {};
  me.conta=1;
  me.scaricate=0;
  
  var icon = new GIcon();
  icon.image = "http://www.panoramio.com/img/panoramio-marker.png"; 
  icon.shadow = "";  
  icon.iconSize = new GSize(24, 24); 
  icon.shadowSize = new GSize(22, 22); 
  icon.iconAnchor = new GPoint(9, 9);  
  icon.infoWindowAnchor = new GPoint(9, 0); 

  me.markerIcon = icon;
  me.enabled = false;
 
   
}

PanoramioLayer.prototype.enable = function() {
  this.enabled = true;
  GEvent.trigger(map, "moveend");
}

PanoramioLayer.prototype.Search = function(option,search) {
  if (this.conta > this.scaricate) 
   {//self.status ='vedo sul server!'; 
    this.load(option,search);}
    
    else ; //self.status ='tutte scaricate!' + this.conta + ' sc:' + this.scaricate ;
    
}

PanoramioLayer.prototype.NFoto = function() {
       this.load({from:1, to:0},'');
 }


PanoramioLayer.prototype.disable = function() {
  this.enabled = false;
  for (var i in this.foto)   
  { 
     if (this.ids[i]=='map') { map.removeOverlay(this.foto[i]) 
     this.ids[i]='local';
     }
  }
  
}

PanoramioLayer.prototype.getEnabled = function() {
  return this.enabled;
}

PanoramioLayer.prototype.load = function(userOptions,search) {
  var options = {
    order: "popularity",
    set: "full",
    from: "0",
    to: "50",
    minx: "-180",
    miny: "-90",
    maxx: "180",
    maxy: "90",
    userold: "1958317",
    user: "2126287",
    size: "small"
  };

   for (optionName in userOptions) {
    if (userOptions.hasOwnProperty(optionName)) {
      options[optionName] = userOptions[optionName];
     }
  }
 
  var url = "http://www.panoramio.com/map/get_panoramas.php?";
  var uniqueID = "";
 
  for (optionName in options) {
    if (options.hasOwnProperty(optionName)) {
      var optionVal = "" + options[optionName] + "";
      url += optionName + "=" + optionVal + "&";
      uniqueID += optionVal.replace(/[^\w]+/g,"");
    }
   
  }
  
  ChiamateP++;
  
  document.getElementById("imgloadp").style.visibility="visible";
  var callbackName = "PanoramioLayerCallback.loader" + uniqueID; //ask dion
  eval(callbackName + " = function(json) { var pa = new PanoramioLayerCallback(json, search,options.from);}");
 
  var script = document.createElement('script');
  script.setAttribute('src', url + 'callback=' + callbackName);
  script.setAttribute('id', 'jsonScript');
  script.setAttribute('type', 'text/javascript');
  document.documentElement.firstChild.appendChild(script);
}

PanoramioLayer.prototype.noLoad = function(userOptions,search) {
  var options = {
    order: "popularity",
    set: "full",
    from: "0",
    to: "50",
    minx: "-180",
    miny: "-90",
    maxx: "180",
    maxy: "90",
    userold: "1958317",
    user:"2126287",
    size: "small"
  };
 

 
   for (optionName in userOptions) {
    if (userOptions.hasOwnProperty(optionName)) {
      options[optionName] = userOptions[optionName];
     }
  }
 
  var noperazioni=50;
  noperazioni=Math.min(noperazioni,parseInt(options.to) - parseInt(options.from));
 // alert(noperazioni);
  for (var i in this.ids ) {
    var photo = this.foto[i];
   // alert(noperazioni + photo.photo_title);
   // alert('lng ' + photo.getLatLng().lng() + ' > minx: ' + options.minx);
    if ((photo.getLatLng().lng() > options.minx)  &&  (photo.getLatLng().lng() < options.maxx)  && (photo.getLatLng().lat() > options.miny) &&  (photo.getLatLng().lat() < options.maxy)  &&  (this.ids[i] == 'local') ) {
      noperazioni--;
      this.ids[i] = 'map';
      map.addOverlay(photo);
     // alert('accesa');
      if (noperazioni <= 0) break;
    }
  } 
 
}


function PanoramioLayerCallback(json, search,from) {
  var added=0;
  var batch = [];
  
  if (PPL.conta == 1) { 
   PPL.conta = json.count;
 }
  
 if(search == '') 
 {
   for (var i = 0; i < json.photos.length; i++) {
    var photo = json.photos[i];
    
    if (!PPL.ids[photo.photo_id]) {
      var marker = this.createMarker(photo, PPL.markerIcon);
      PPL.foto[photo.photo_id]= marker;
      PPL.scaricate++;
      PPL.ids[photo.photo_id] = 'map';
      map.addOverlay(marker);
    }
    else 
     if (PPL.ids[photo.photo_id]=='local' ) {
      map.addOverlay(PPL.foto[photo.photo_id]);
      PPL.ids[photo.photo_id]= 'map';
      }
    }
    
 }
 else // load for search
 {
 // alert('search con from='+ from);
  //document.getElementById("status").innerHTML='search:= '+ from;
  for (var i = 0; i < json.photos.length; i++)
   {
    var photo = json.photos[i];
    
    if (!PPL.ids[photo.photo_id]) {
      var marker = this.createMarker(photo, PPL.markerIcon);
      PPL.foto[photo.photo_id]= marker;
      PPL.scaricate++;
      // if(photo.photo_title.indexOf(search) >=0 )
      if (MatchTitolo(photo.photo_title,search) )
      {
       PPL.ids[photo.photo_id] = "map";
       map.addOverlay(marker);	     
       }
	 else PPL.ids[photo.photo_id] = "local";
	      
    }
    else // gia caricata 
     if (MatchTitolo(photo.photo_title,search ) )
    // if (photo.photo_title.indexOf(search) >=0 )
     {
      if (PPL.ids[photo.photo_id]=='local' ) // da visualizzare 
       {
          map.addOverlay(PPL.foto[photo.photo_id]);
          PPL.ids[photo.photo_id]= 'map';
       }    
     }
    }
 // controllare se rilanciare modificando from e to   
 
 
 
  if (PPL.scaricate < PPL.conta) 
  {
    var fr = parseInt(from) + 50;
    var to= 50 + fr;
    PPL.Search({from:fr, to:to},search);
  }
  UpdateCercaF();
 
 }
 document.getElementById("imgloadp").style.visibility="hidden";   

}

PanoramioLayerCallback.prototype.formImgUrl = function(photoId, imgType) {
  return 'http://www.panoramio.com/photos/' + imgType + '/' + photoId + '.jpg';
}
 
PanoramioLayerCallback.prototype.formPageUrl = function(photoId) {
  return 'http://www.panoramio.com/photo/' + photoId;
}

PanoramioLayerCallback.prototype.createMarker = function(photo, baseIcon) {
  var me = this;
  var markerIcon = new GIcon(baseIcon);
  markerIcon.image = this.formImgUrl(photo.photo_id, "mini_square");
    
  var marker = new GMarker(new GLatLng(photo.latitude, photo.longitude), {icon: markerIcon, title: photo.photo_title});
  marker.photo_title=photo.photo_title;
  if (photo.photo_title.length > 33) {
    photo.photo_title = photo.photo_title.substring(0, 33) + "&#8230;";
  }
 
  var html = "<div id='infowin' style='height:330px; width:240px;'>" +
            "<p><a href='http://www.panoramio.com/' target='_blank'>" + 
             "<img src='http://www.panoramio.com/img/logo-small.gif' border='0' width='119px' height='25px' alt='Panoramio logo' /><\/a></p>" +
             "<a id='photo_infowin' target='_blank' href='" + photo.photo_url + "'>" +                
             "<img border='0' width='" + photo.width + "' height='" + photo.height + "' src='" + photo.photo_file_url + "'/><\/a>" +
             "<div style='overflow: hidden; width: 240px;'>" +
             "<p><a target='_blank' class='photo_title' href='" + photo.photo_url +
             "'><strong>" + photo.photo_title + "<\/strong><\/a></p>" +
             "<p>Posted by <a target='_blank' href='" + photo.owner_url + "'>" +
             photo.owner_name + "<\/a></p><\/div>" +
             "<\/div>";

  marker.html = html;

  GEvent.addListener(marker, "click", function() {
    map.openInfoWindow(marker.getLatLng(), marker.html, {noCloseOnClick: true});
  });
 
  return marker;
}

function MatchTitolo(t,s) {
 if (s.indexOf("*") >=0 ) return true;
 var ut=t.toUpperCase();
 var us=s.toUpperCase();
 var aM=[];
 var aM=us.split(" ");
 var m=0
 for (var i=0; i<aM.length; i++) if (ut.indexOf(aM[i]) < 0 ) break; 
 else m++;
 return (m == aM.length) 
 }
 
