
shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};


function makePictureURL(photo, size)
{
	YUI().use( "console", function(Y) {
	});
	var postfix = "_" + size;
	if ( size == '' )
	{
		postfix = '';
	}
	return 'http://farm' + photo.farm + ".static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + postfix + ".jpg";
}

function requestPhotographs(div, place)
{
	YUI().use("io-base","node", "console", 'json-parse', function(Y) {
		var lat = place.place.centroid.latitude;
		var lon = place.place.centroid.longitude;
		var woeId = place.place.woeId;
		var name = place.place.name;
		var txt = place.reference.text;
	
		var method = Y.get('#smethod').getAttrs().value;
		
		var uri = "json.php?method=getPhotographs&lat=" + lat + "&lon=" + lon + "&woeId=" + woeId + "&txt=" + txt + "&smethod=" + method + "&placeName=" + name;
		
		var divId = 'carousel_' + woeId;
		var container = div.create("<div id='" + divId + "'><img src='ajax-loader.gif'></div>");
	
		function complete(id, o, args)
		{
			var id = id;
			var data = o.responseText;
			var div = args[0];
			var place = args[1];
			var container = args[2];
			
			var photos = Y.JSON.parse(data);
			
			Y.log("Photos: ")
			Y.log(photos);
			
			var photographs = photos.photo;
			
			if ( photographs.constructor != Array )
			{
				Y.log("Not array (only one photograph?) for " + txt);
				photographs = [photographs];
			}
			
			Y.log("photographs variable");
			Y.log(photographs);
			
			if ( null == photographs || photographs.length == 0 )
			{
				container.set('innerHTML', 'Nothing found');
			}
			else
			{

			
				var divId = 'carousel_' + woeId;
				photographs = shuffle(photographs);
				var pics = Y.get(document.getElementById('pics'));
				var num = pics.get('value');
				container.set('innerHTML', '');
				
				if ( num > photographs.length )
				{
					num = photographs.length;
				}
				
				for ( var i = 0; i < num; ++i )
				{
					var photo = photographs[i];
					var li = container.create("<a href='" + makePictureURL(photo, '') 
						+ "' rel=lightbox[" + woeId +  "]><img src='" + makePictureURL(photo, 's') + "'></a>&nbsp;");
					container.appendChild(li);
					
					if ( i % 10 == 0 && i > 3 )
					{
						container.appendChild(container.create("<br>"));
					}
				}
			
				div.appendChild(container);
			}
			
			Y.get('#waiting').setStyle('display', 'none');
			div.get('parentNode').setStyle('display', 'block');
			
		}
	
		Y.on('io:complete', complete, this, [div, place, container]);
	
		var request = Y.io(uri);
	});
}

function requestPlaces()
{
		YUI().use("io-base","node", "console", function(Y) {
			var article = Y.get(document.getElementById('article'));

			var text = article.getAttrs().value;
			
			if ( text.length > 5000 )
			{
				alert("Text is over 5K, it'll be truncated around 5000 characters");
				text = text.substr(0, 5000);
			}
			
			while ( text.indexOf('%') != -1)
			{
				text = text.replace('%', ' ');
			}
			Y.log(text);
			var uri = "json.php?method=getPlaces&article=" + text;

			// Define a function to handle the response data.
			function complete(id, o, args) {

			  	var id = id; // Transaction ID.
			  	var data = o.responseText; // Response data.
			  	var args = args[1]; // 'ipsum
				
				var places = eval(data);
				
				Y.log(data);
				Y.log(places);
				
				
				
				var pNode = Y.get(document.getElementById('places'));
				
				pNode.set('innerHTML', '');
				
				pNode.setStyle('display', 'none');
				Y.get('#waiting').setStyle('display', 'block');
				
				if ( null == places || 0 == places.length )
				{
					var div = pNode.create("<div class='location'></div>", document);
					div.addClass("location");
					div.set('innerHTML', "No locations found.");
					
					pNode.appendChild(div);
					
					Y.get('#waiting').setStyle('display', 'none');
					div.get('parentNode').setStyle('display', 'block');
					
					
					return;
				}
				
				
				Y.log(places[0]);
				

				for ( var i = 0; i < places.length; ++i )
				{
					var p = places[i];
					
					Y.log(p);

					var div = pNode.create("<div class='location'></div>", document);
					div.addClass("location");
					div.set('innerHTML', "<div class='placeName'>" + p.place.name + "</div>");
					
					pNode.appendChild(div);
					
					requestPhotographs(div, p);
				}
			};

			// Subscribe to event "io:complete", and pass an array
			// as an argument to the event handler "complete", since
	        // "complete" is global.   At this point in the transaction 
	        // lifecycle, success or failure is not yet known.
			Y.on('io:complete', complete, this, ['lorem', 'ipsum']);

			// Make an HTTP request to 'get.php'.
			// NOTE: This transaction does not use a configuration object.
			var request = Y.io(uri);
			}
		);
}

