var Goaze = window.Goaze || {};

Goaze.Panoramio = function(container){
	
	this.containerName = container;
	this._container = null;
	this.baseUrl = Goaze.config.baseUrl + 'PanoramioProxy.pl';
	this.order = 'upload_date';
	this.set = 'public';
	this.size = 'small';
	this.latitude = 12.482324;
	this.longitude = 41.895466;
	this.distance = 100; // distance from x/y point in km??
	// 1 degree is approx 111 km for latitude and varies from 0 to 110 for longitude
	this.from = 0;
	this.count = 10;
	
	this.pictures;
	
	this.init();
}

Goaze.Panoramio.prototype.init = function(){
}

Goaze.Panoramio.prototype.container = function(){
	
	if(! this._container){
		this._container = new YAHOO.widget.Module(this.containerName);
	}
	return this._container;
}

Goaze.Panoramio.prototype.createUrl = function(){
	
	var result = 'http://' + location.hostname + Goaze.config.basePath + "PanoramioProxy.pl?" +
		"order=" + this.order + "&" +
		"set=" + this.set + "&" +
		"size=" + this.size + "&" +
		"miny=" + (this.latitude - this.distance / 100)+ "&" +
		"maxy=" + (this.latitude + this.distance / 100) + "&" +
		"minx=" + (this.longitude - this.distance / 100) + "&" +
		"maxx=" + (this.longitude + this.distance / 100) + "&" +
		"from=" + this.from + "&" +
		"to=" + (this.from + this.count * 5);
	
	return result;
}

Goaze.Panoramio.prototype.getPictures = function(){
	
	var me = this;
	var callback = {
		success: function(o){ me.processPictures(o); },
		failure: function(o){  },
		scope: me
	};
	
	var request = YAHOO.util.Connect.asyncRequest('GET', this.createUrl(), callback);
}

Goaze.Panoramio.prototype.isLandscape = function(photo){
	
	return (photo.width > photo.height) && (photo.width / photo.height < 1.6);
}

Goaze.Panoramio.prototype.processPictures = function(request){
	
	try {
		this.pictures = YAHOO.lang.JSON.parse(request.responseText);
		var dom = YAHOO.util.Dom;

		var count = 0;		
		for(var i=0; i<this.pictures.photos.length && count < this.count; i++){
			var photo = this.pictures.photos[i];
			
			if(this.isLandscape(photo)){
				count++;
				
				var imgDiv = document.createElement('div');
				dom.addClass(imgDiv, 'panoramio container');
				
				var imgAnchor = document.createElement('a');
				imgAnchor.href = photo.photo_url;
				imgAnchor.target = "_blank";
				imgAnchor.alt = "image";
				
				var img = document.createElement('img');
				img.src = photo.photo_file_url;
				YAHOO.util.Dom.addClass(img, 'panoramio');
				img.width = photo.width;
				img.height = photo.height;
				imgAnchor.appendChild(img);
				
				imgDiv.appendChild(imgAnchor);
				imgDiv.appendChild(document.createElement('br'));
				
				// imgDiv.appendChild(document.createElement('p')).appendChild(document.createTextNode(photo.photo_title));
				// owner name & link
				var ownerAnchor = document.createElement('a');
				ownerAnchor.href = photo.owner_url;
				ownerAnchor.target = "_blank";
				var owner = photo.owner_name.substring(0, 40);
				if(owner.length == 40) owner += "...";
				ownerAnchor.appendChild(document.createTextNode("foto van: " + owner));
				dom.addClass(ownerAnchor, 'owner');
				imgDiv.appendChild(ownerAnchor);
				
				this.container().body.appendChild(imgDiv);
			}
		}
		this.container().setHeader("<p>Fotos van <a href='http://www.panoramio.com'>panoramio.com</a>. Het auteursrecht op de fotos aangeboden door Panoramio ligt bij de maker.</p>")
	}
	catch(e){
		alert("Error parsing Panoramio JSON data");
	}
}