﻿function PanoramioLayerCallback(json, panoLayer) {
  this.panoLayer = panoLayer;

  for (var i = 0; i < json.photos.length; i++) {
    var photo = json.photos[i];
    if (!panoLayer.ids[photo.photo_id]) {
	  photo.popup = panoLayer.popup;
      var marker = this.createMarker(photo, panoLayer.markerIcon);
      panoLayer.mgr.addMarker(marker, 0);
      panoLayer.ids[photo.photo_id] = "exists";
      panoLayer.mgr.addMarker(marker, panoLayer.map.getZoom());
    }
  }
}

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});

  //if (photo.photo_title.length > 33) {
  //  photo.photo_title = photo.photo_title.substring(0, 33) + "&#8230;";
  //}

	var geocoder = new GClientGeocoder();
	geocoder.getLocations(new GLatLng(photo.latitude, photo.longitude, true), function showAddress(response) {
		  if (!response || response.Status.code != 200) {
		    //alert("Status Code:" + response.Status.code);
		    var html = "";
            html += (photo.popup && photo.popup == 'large') ? '<div class="b-panoramio-popup b-panoramio-popup-big">' : '<div class="b-panoramio-popup">';
            html += '<p><a href="http://www.panoramio.com/" target="_blank">';
            html += '<img src="http://www.panoramio.com/img/logo-small.gif" alt="Panoramio"/></a></p>';
            html += '<a id="photo_' + photo.photo_id + '" target="_blank" href="' + photo.photo_url + '">';               
            html += '<img class="picture" width="' + photo.width + '" height="' + photo.height + '" src="' + photo.photo_file_url + '"/></a>';
            html += '<div class="right-section">';
            if (photo.photo_title) {
                html += '<div class="caption"><a>' + photo.photo_title + '</a></div>';
            }
            if (location && location != '') {
                html += '<div>' + location + '</div>';
            }
            html += '<div>' + photo.upload_date + '</div>';
            html += '</div>';
            html += '<div class="g-clear"></div>';
            html += '<p class="copyrights">' + localization.ru.panoramio_copyright + '</p>';
            html += '</div>';
            marker.html = html;
		  } else { 
			place = response.Placemark[0];
			var country = '';
			var city = '';
			if (place.AddressDetails.Accuracy > 0) {
				if (place.AddressDetails.Country) {
					if (place.AddressDetails.Country.CountryName) country = place.AddressDetails.Country.CountryName;
					if (place.AddressDetails.Country.AdministrativeArea) {
						if (place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea) {
							if (place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality &&
									place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName) {
								city = place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
							}
						}
					}
				}
			}
			var location = country + ((city != '') ? (", " + city) : '');
			var html = "";
			html += (photo.popup && photo.popup == 'large') ? '<div class="b-panoramio-popup b-panoramio-popup-big">' : '<div class="b-panoramio-popup">';
			html += '<p><a href="http://www.panoramio.com/" target="_blank">';
            html += '<img src="http://www.panoramio.com/img/logo-small.gif" alt="Panoramio"/></a></p>';
			html += '<a id="photo_' + photo.photo_id + '" target="_blank" href="' + photo.photo_url + '">';               
            html += '<img class="picture" width="' + photo.width + '" height="' + photo.height + '" src="' + photo.photo_file_url + '"/></a>';
			html += '<div class="right-section">';
			if (photo.photo_title) {
				html += '<div class="caption"><a>' + photo.photo_title + '</a></div>';
			}
			if (location && location != '') {
				html += '<div>' + location + '</div>';
			}
			html += '<div>' + photo.upload_date + '</div>';
			html += '</div>';
			html += '<div class="g-clear"></div>';
			html += '<p class="copyrights">' + localization.ru.panoramio_copyright + '</p>';
			html += '</div>';
			marker.html = html;
		  }
});

  GEvent.addListener(marker, "click", function() {
    me.panoLayer.map.openInfoWindow(marker.getLatLng(), marker.html, {noCloseOnClick: true});
  });
 
  return marker;
}

function PanoramioLayer(map, opt_opts) {
  var me = this;
  me.map = map;
  me.ids = {};
  me.mgr = new MarkerManager(map, {maxZoom: 19});
  me.popup = opt_opts.popup || "large";

  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;

  GEvent.addListener(map, "moveend", function() {
    if (me.enabled) {
      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      var northEast = bounds.getNorthEast();
      me.load(me, $.extend({maxy: northEast.lat(), miny: southWest.lat(), maxx: northEast.lng(), minx: southWest.lng()}, opt_opts));
    }
  });
}

PanoramioLayer.prototype.enable = function() {
  this.enabled = true;
  GEvent.trigger(map, "moveend");
}

PanoramioLayer.prototype.disable = function() {
  this.enabled = false;
  this.map.closeInfoWindow();
  this.mgr.clearMarkers();
  this.ids = {};
}

PanoramioLayer.prototype.getEnabled = function() {
  return this.enabled;
}

PanoramioLayer.prototype.load = function(panoLayer, userOptions) {
  var options = {
    order: "popularity",
    set: "public",
    from: "0",
    to: "40",
    minx: "-180",
    miny: "-90",
    maxx: "180",
    maxy: "90",
    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,"");
    }
  }
  var callbackName = "PanoramioLayerCallback.loader" + uniqueID; //ask dion
  eval(callbackName + " = function(json) { var pa = new PanoramioLayerCallback(json, panoLayer);}");
 
  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);
}
