(function($){
	$.fn.constructAlbum = function(options) {
		var defaults = {
			mode:"new",
			elements:""
		};
		
		var options = $.extend(defaults, options);
		
		
		var CSSstr = "<style>";
		CSSstr    += "#contentPreview .row {";
		CSSstr    += "width:805px;";
		CSSstr    += "}";
		CSSstr    += "#contentPreview .cell {";
		CSSstr    += "width:195px;";
		CSSstr    += "float:left;";
		CSSstr    += "padding:0;";
		CSSstr    += "text-align:center;";
		CSSstr    += "vertical-align:text-bottom;";
		CSSstr    += "display:none;";
		CSSstr    += "}";
		CSSstr    += "#contentPreview img {";
		CSSstr    += "margin:5px 0 5px 0;";
		CSSstr    += "background-color:#fff;";
		CSSstr    += "padding:3px;";
		CSSstr    += "border:1px solid #ccc;";
		CSSstr    += "-moz-border-radius:3px;";
		CSSstr    += "-webkit-border-radius:3px;";
		CSSstr    += "}";
		CSSstr    += "</style>";
		
		this.append(CSSstr);
		
		var HTMLstr = "<div id='contentPreview'>";
		HTMLstr    += "<input id='totalOfElements' name='totalOfElements' type='hidden' name='totalOfElements' value='0' />";
		HTMLstr    += "<div class='row'></div>";
		HTMLstr    += "</div>";
		
		HTMLstr    += "<div class='clear' style='height:10px'></div>";

		HTMLstr    += "<div id='inputFiles' style='display:none;'>";
		HTMLstr    += "<input id='inputFile' type='file' size='20' />";
		HTMLstr    += "</div>";

		this.append(HTMLstr);
		
		if (options.mode=="edit") {
			var elements = options.elements.split(",");
			for (i=0; i<elements.length; i++ ) {
				if ($("#contentPreview > .row:last > .cell").length==4) {
					var HTMLstr = "<div class='clear'></div>";
					HTMLstr    += "<div class='row'></div>";
					$("#contentPreview").append(HTMLstr);
				}
				
				var HTMLstr = "<div class='cell'></div>";
				$("#contentPreview > .row:last").append(HTMLstr);
								
				var $editElementContainer = $("#contentPreview > .row:last > .cell:last");
				
				var nameOfElement = elements[i].split("/");
				nameOfElement = nameOfElement[(nameOfElement.length-1)];
				
				var HTMLstr = "<input name='oldInputFile" + Number(i) + "' type='hidden' value='" + nameOfElement + "' />";
				HTMLstr    += "<img src='lib/thumb.php?img=" + elements[i] + "&width=180'>";
				HTMLstr    += "<br />";
				//Usar jquery para passar o tamanho para um <span>
				//HTMLstr    += "Tamanho: " + "0000" + "KB";
				//HTMLstr    += "<br />";
				HTMLstr    += "<label for='oldFiles'><input type='checkbox' name='oldFiles[" + i + "]' /> Remover imagem</label>";
				
				$editElementContainer.append(HTMLstr);
				
				$editElementContainer.fadeIn(1500);
			}
			$("#contentPreview > #totalOfElements").attr("value", elements.length);
		}
		
		$("#inputFiles").fadeIn(1000);
		
		this.find("#inputFile").change(function() {
			var numberOfElement = $("#totalOfElements").attr("value");
			$("#totalOfElements").attr("value",(Number(numberOfElement)+1));
			
			if ($("#contentPreview > .row:last > .cell").length==4) {
				var HTMLstr = "<div class='clear'></div>";
				HTMLstr    += "<div class='row'></div>";
				$("#contentPreview").append(HTMLstr);
			}
			
			var HTMLstr = "<div class='cell'></div>";
			$("#contentPreview > .row:last").append(HTMLstr);
			
			$(this).clone().appendTo("#contentPreview > .row:last > .cell:last");
			$("#contentPreview > .row:last > .cell:last > #inputFile").hide().attr("id", "inputFile"+Number(numberOfElement)).attr("name", "inputFile"+Number(numberOfElement));
			
			$("#inputFiles > #inputFile").val("");
			
			var $elementContainer = $("#contentPreview > .row:last > .cell:last");
			
			var img = document.getElementById("inputFile"+Number(numberOfElement));
			var HTMLstr = "<img src='"+img.files.item(0).getAsDataURL()+"' width='180px'>";
			HTMLstr    += "<br />";
			HTMLstr    += "Tamanho: " + (img.files.item(0).fileSize/1024).toFixed(1) + "KB";
			HTMLstr    += "<br />";
			HTMLstr    += "<input type='button' value='Remover Imagem' style='font-size:9px;'>";
			
			$elementContainer.append(HTMLstr).fadeIn(1000);
			
			$elementContainer.find("input[type='button']").click(function() {
				$elementContainer.animate({opacity:0}, 500, function() {
					$elementContainer.remove();
					if ($("#contentPreview > .row").length>1) {
						for (i=0; i<=($("#contentPreview > .row").length-2); i++) {
							if ($("#contentPreview > .row:eq(" + i + ") > .cell").length<4) {
								$("#contentPreview > .row:eq(" + (i+1) + ") > .cell:eq(0)").appendTo($("#contentPreview > .row:eq(" + i + ")"));
							}
						}
						if ($("#contentPreview > .row:last > .cell").length==0) {
							$("#contentPreview > .row:last").remove();
							$("#contentPreview > .clear:last").remove();
						}
					}
				});
			});
		});
	};
})(jQuery);