function SimImg()
{
	this.ajaxURL = "/ajax/simimg.php";
	this.container = null;
	this.isOpen = false;
	this.amount = 0;
	this.amountMax = 5;
	this.numInc = 0;
	
	this.open = function()
	{
		if (!this.isOpen)
		{
			this.isOpen = true;
			this.container = $(document.createElement("div"));
			//$("#simimg_upload_iframe").show().css("width", "1000px").css("height", "100px");
			
			this.amount = 0;
			this.numInc = 0;
			
			this.container.addClass("popup");
			this.container.css({
				width: "600px",
				height: "275px",
				"margin-left": "-300px",
				"margin-top": "-140px",
				display: "none"
			});
	
			var title = $(document.createElement("div"));
			this.container.append(title);
			title.addClass("title");
			title.html("" +
				"<div style='float: left; width: 70%;'>" +
					"Upload Image" +
				"</div>" +
				"<div style='float: left; width: 30%; text-align: right;'>" +
					"<img class='png' src='http://zacisa.info/images/icons/cancel.png' onclick='SimImg.close();' />" +
				"</div>" +
				"<div class='clear'></div>");
			
			var body = $(document.createElement("div"));
			this.container.append(body);
			body.addClass("body");
			body.css({
				"margin-top": "0px"
			});
			body.html("<img src='/images/ajax-loader.gif' style='margin-left: 270px; margin-top: 110px;' />");
			
			$("body").append(this.container);
			this.container.show(400);
			this.container.draggable({ containment: "body", handle: ".title" });
			
			$.post(this.ajaxURL,
				{},
				function(data)
				{
					SimImg.container.children(".body").html("<form method='post' action='/simimg.us/api_upload.php' target='simimg_upload_iframe' enctype='multipart/form-data' onsubmit='SimImg.whileUploading();'>" +
							data +
							"<div style='padding: 4px;'>" +
								"<div style='float: left; width: 230px;'>" +
									"Image" +
								"</div>" +
								"<div style='float: left; width: 155px;'>" +
									"Title" +
								"</div>" +
								"<div style='float: left; width: 155px;'>" +
									"Tags " +
									"<span style='font-size: 10px;'>(comma delimited)</span>" +
								"</div>" +
								"<div class='clear'></div>" +
							"</div>" +
							"<div id='simimg_upload_fields'></div>" +
							"<div style='padding: 4px; text-align: center;'>" +
								"<input type='button' class='button' value='Add Another' onclick='SimImg.addAnother();' /> " +
								"<input type='submit' class='button' value='Upload Images' />" +
							"</div>" +
							"</form>");
					SimImg.addAnother();
				});
		}
	};
	
	this.close = function()
	{
		if (this.isOpen)
		{
			this.container.hide(400);
			
			setTimeout(function()
			{
				SimImg.container.remove();
				SimImg.isOpen = false;
			}, 401);
		}
	};
	
	this.addAnother = function()
	{
		if (this.amount < this.amountMax)
		{
			this.numInc++;
			this.amount++;
			
			this.container.find("#simimg_upload_fields").append("<div id='simimg_num_"+this.numInc+"' style='padding: 4px;'>" +
					"<input type='file' name='images[]' class='button' /> " +
					"<input type='text' name='title[]' style='width: 150px;' /> " +
					"<input type='text' name='tags[]' style='width: 150px;' /> " +
					"<img class='png' src='http://zacisa.info/images/icons/delete.png' onclick='SimImg.remove("+this.numInc+");' />" +
				"</div>");
		}
	};
	
	this.remove = function(num)
	{
		if (this.amount > 1)
		{
			$("#simimg_num_"+num).remove();
			this.amount--;
		}
	};
	
	this.whileUploading = function()
	{
		this.container.children(".body").children().hide();
		this.container.children(".body").append("<img src='/images/ajax-loader.gif' style='margin-left: 270px; margin-top: 110px;' />");
		return true;
	};
	
	this.submitResult = function(data)
	{
		var hash = "";
		if (data.hashes.length > 0)
		{
			for(var i=0; i<data.hashes.length; i++)
			{
				hash = data.hashes[i];
				bbcode_add('posttext', '', '[URL=http://simimg.com/view/'+hash+'][IMG]http://simimg.com/'+hash+'[/IMG][/URL]\n');
			}
		}
		
		if (data.error > 0)
		{
			alert(data.reasons);
		}
		
		this.close();
	};
};

function SimImg_Old()
{
	this.container = null;
	
	/**
	 * Opens the Upload Popup
	 */
	this.open = function()
	{
		if (!this.container)
		{
			this.container = $(document.createElement("div"));
			$("body").append(this.container);
			this.container.addClass("simimg_upload");
			this.container.hide();

			this.__setupTitle();
			this.__setupBody();
			this.container.show("800");
			hideYoutubeVideos(801);
		}
	};
	
	/**
	 * Closes the Upload Popup
	 */
	this.close = function()
	{
		if (this.container)
		{
			this.container.hide("800");
			this.container = null;
			showYoutubeVideos(801);
		}
	};
	
	/**
	 * Adds another Upload Field
	 */
	this.addMore = function()
	{
		this.container.css("height", parseInt(this.container.css("height")) + 24);
		$("#uploadDivAdd").append("<div style='padding-top: 2px;'><input type='file' name='images[]' style='width: 220px; height: auto;' /></div>");
		
		if (parseInt(this.container.css("height")) < 300)
		{
			this.container.css("margin-top", parseInt(this.container.css("margin-top")) - 12);
		}
	};
	
	this.upload = function()
	{
		this.container.children(".body").children().hide();
		this.container.children(".body").css("text-align", "center").append("<img src='/images/ajax-loader.gif' />");
		this.container.css({
			"height": "68px",
			"margin-top": "-45px"
		});
		return true;
	};
	
	this.uploadComplete = function(hash)
	{
		if (hash.length > 0)
		{
			var domains = new Array("com", "us", "me");
			
			if (hash.search(/\|/) != -1)
			{
				hash = hash.split("|");
				for(var i=0, n=hash.length; i<n; i++)
				{
					bbcode_add('posttext', '[IMG]http://simimg.'+domains[Math.floor(Math.random()*3)]+'/'+hash[i], '[/IMG]\n');
				}
			} else {
				bbcode_add('posttext', '[IMG]http://simimg.'+domains[Math.floor(Math.random()*3)]+'/'+hash, '[/IMG]\n');
			}
		} else {
			alert("Only Images are allowed to be uploaded");
		}
		
		this.close();
	};
	
	this.__setupTitle = function()
	{
		var title = $(document.createElement("div"));
		this.container.append(title);
		title.addClass("title");
		title.html("<div style='float: left; width: 60%;'>" +
				"Simple Image Upload" +
			"</div>" +
			"<div style='float: left; width: 40%; text-align: right;'>" +
				"<a onclick='SimImg.close()'>" +
					"Close" +
				"</a>" +
			"</div>" +
			"<div class='clear'></div>");
	};
	
	this.__setupBody = function()
	{
		var body = $(document.createElement("div"));
		this.container.append(body);
		body.addClass("body");
		
		var html = "<form method='post' action='/simimg.us/api_upload.php' target='simimg_upload_iframe' enctype='multipart/form-data' onsubmit='SimImg.upload()'>";
		html += "<input type='hidden' name='user_sid' value='"+$("[infoType='simimg_usersid']").html()+"' /> ";
		html += "<div id='uploadDivAdd'>";
			html += "<input type='file' name='images[]' style='width: 220px; height: auto;' /> ";
			html += "<input type='submit' class='button' value='Upload' />";
		html += "</div>";
		html += "<div style='padding-top: 4px; text-align: center;'>";
			html += "<input type='button' class='button' value='Add Another' onclick='SimImg.addMore();' />";
		html += "</div>";
		html += "</form>";
		
		body.html(html);
	};
};
var SimImg = new SimImg();