var ccLicense = [

	{id:"0", description:"All rights reserved", url:"#"},
	{id:"1", description:"Attribution-NonCommercial-ShareAlike", url:"http://creativecommons.org/licenses/by-nc-sa/2.0/"}, 
	{id:"2", description:"Attribution-NonCommercial", url:"http://creativecommons.org/licenses/by-nc/2.0/"}, 
	{id:"3", description:"Attribution-NonCommercial-NoDerivs", url:"http://creativecommons.org/licenses/by-nc-nd/2.0/"}, 
	{id:"4", description:"Attribution", url:"http://creativecommons.org/licenses/by/2.0/"}, 
	{id:"5", description:"Attribution-ShareAlike", url:"http://creativecommons.org/licenses/by-sa/2.0/"}, 				
	{id:"6", description:"Attribution-NoDerivs", url:"http://creativecommons.org/licenses/by-nd/2.0/"} 
];

var Preview = Class.create({
	
	initialize: function(){

		$("photoInformation").hide();
		$("photoDescriptionBackground").hide();
		$("photoLink").hide();
		$("photoClose").hide();

		$("photoContent").observe("mouseover", function(e){
			$("photoInformation").show();
			$("photoDescriptionBackground").show();
			$("photoLink").show();
			$("photoClose").show();
			$("photoDescriptionBackground").style.height = ($("photoInformation").offsetHeight + 8) + "px";
			Event.stop(e);
		});

		Event.observe($("photoContent"), "mouseout", function(e){
			$("photoInformation").hide();
			$("photoDescriptionBackground").hide();
			$("photoLink").hide();
			$("photoClose").hide();
			Event.stop(e);
		});

		$("photoBackground").observe("click", this.hide);
		$("photoClose").observe("click", (function(e){
			Event.stop(e);	
			this.hide();
		}).bind(this));
		
		$("photoAuthor").observe("click", this.hide);
		$("photoZoom").observe("click", this.hide);
	},

	hide: function(e){
		$("photolist_container").style.overflow = "";	
		$("photo").hide();	
	},
	
	show: function(photo){
		try {
			$("photolist_container").style.overflow = "hidden";
	 		$("photoAuthor").update(unescape(photo.ownername || photo.owner.username));
	 		$("photoAuthor").href = "#user:" + photo.ownername || photo.owner.username;
	 		
	 		var license = ccLicense[photo.license];
	 		
	 		$("photoLicense").update(license.description);
	 		$("photoLicense").href = license.url;
	 		 		
	 		$("photoTitle").update(unescape(photo.title || photo.title));
	 		$("photoDate").update(photo.datetaken || photo.dates.taken);
	 		$("photoGeoposition").update("East: " + (photo.longitude || photo.location.latitude) + ", North: " + (photo.latitude || photo.location.latitude));
	 		
	 		$("photoZoom").href = "#geo:" +  (photo.latitude || photo.location.latitude) + "," +  (photo.longitude || photo.location.longitude)+ ",15/";
	 		
	 		var tags = photo.tags.split(" ");
	 		
	 		if (tags.length === 0){
	 			$("photoTags").update("This photo has no tags.");
	 		} else {
	 			$("photoTags").update("");
		 		tags.each((function(t){
		 			var tag = Element.create("a", {
		 				href:"#tag:" + t,
		 				innerHTML: unescape(t),
		 				title: "Click to see more photos with this tag."
		 			});
		 			
		 			tag.observe("click", this.hide);
		 			
		 			$("photoTags").appendChild(tag);	  
		 			$("photoTags").appendChild(document.createTextNode(" "));	
		 		}).bind(this));
	 		}
	 		
	 		$("photoLink").href = "http://www.flickr.com/photo.gne?id=" + photo.id;
	 		$("photoLink").title = "http://www.flickr.com/photo.gne?id=" + photo.id;
	 		
			Element.show("photo");
			$("photoImage").update("");
			
			var img = Element.create("img", {src:"http://static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + ".jpg"});
			$("photoImage").appendChild(img);
			$("photoContent").hide();						
		
			var interval = setInterval(function(){
				if (img.width > 1 || img.complete){
					$("photoLoading").hide();
					$("photoContent").show();
	
					var win = getWindowDimensions();
					
					$("photoContent").style.left = Math.round((win.width - img.offsetWidth) /2) + "px";
					$("photoContent").style.top = Math.round((win.height - img.offsetHeight) /2) + "px";
					
					$("photoDescriptionBackground").style.width = (img.offsetWidth -8) + "px";				
					
					clearInterval(interval);
				}
			}, 500);
			$("photoLoading").show();				
		} catch (e) { 
			Logger.error(e);
		}
	}
});
