(function($) {
	
	$.extend($.fn, {	
		photoHarvesterEvent: function(param) {
			
			if (!param) var param = {};
						
			//Check params
			if (param.language == undefined || param.language == '') raiseError("Please specify language to use.");			
			param.ref = $(this);
			
			//Start
			$.ajax({
				type: "GET",
				url: "../resources/"+param.language+"/picturesEvento.xml",
				dataType: "xml",
				success: function(xml) 
				{
					//Parsing xml
					parseXmlFile(xml);														
				},
				error : function() { 
					//No data
					document.getElementById(param.ref.attr("id")).innerHTML = "<p>Dati attualmente non disponibili</p>";
				}				
			});
			
			function raiseError(errorMsg) 
			{
				alert("WARNING!\n"+errorMsg);
				return false;
			}					
			
			function parseXmlFile(xml)
			{				
				var htmlBuilt='';
				var innerContainer='';
				
				//Creating layer container			
				htmlBuilt += "<div class=\"items\">\n";
				
				//Parsing xml				
				$(xml).find('picture').each(function()
				{						
					var image = '';
					$(this).find('image').each(function()
					{
						image = $(this).text();
						htmlBuilt += '\t\t<img src=\'../galleryEvento/t_'+image+'\' border=\'0\' ';
					});	
					
					$(this).find('description').each(function()
					{
						htmlBuilt += 'title=\''+stripHtmlTags($(this).text())+'\'>\n';
					});						
				});		
				htmlBuilt+="</div>\n\n";					
				
				//Assign html to referenced object using a workaround for that stupid IE!								
				document.getElementById(param.ref.attr("id")).innerHTML = htmlBuilt;
						
				$(".scrollable").scrollable({
					size : 10,
					clickable: true,
					keyboard: true,
					keyboardSteps: 1
				});
				
				$(".items img").click(function() 
				{			
					$(".items img").removeClass("active");
					$(this).addClass("active");
					var url = $(this).attr("src").replace("t_", "");
					var caption = $(this).attr("title");
					var wrap = $("#image_wrap");
					var img = new Image();
					img.onload = function() {
						wrap.find("img").attr("src", url);						
						$("#image_wrap #live_caption").html("<span>"+caption+"</span>").show();						
					};
					img.src = url;
				}).filter(":first").click();							
				
								
			}
			
			//Clear the text from html tags
			function stripHtmlTags(content)
			{
				return content.replace(/(<([^>]+)>)/ig, "").replace("'","\'"); 
			}
		}
	});		
})(jQuery);