	var Uploadize = {};
	Uploadize._createIFrame = {};
	
	function getElementByIdCompat(name) 
	{
		if (document.getElementById) 
		{
			return document.getElementById(name);
		} 
		else if (document.all) 
		{
			return document.all[name];
		} 
		else if (document.layers) {
			var theobj = {};
			theobj = document.layers[name];
			theobj.style = document.layers[name];
			return theobj;
		}
	}
	
	function $() 
	{
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) 
		{
			var element = arguments[i];
			if (typeof element == 'string')
				element = getElementByIdCompat(element);
			if (arguments.length == 1)
				return element;
			elements.push(element);
		}
		return elements;
	}
	
	function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
	}
	
	function removeEventSimple(obj,evt,fn) {
		if (obj.removeEventListener)
			obj.removeEventListener(evt,fn,false);
		else if (obj.detachEvent)
			obj.detachEvent('on'+evt,fn);
	}

	
	Uploadize = function (element)
	{
		//Form Layout Absolutization
		$(element).style.position = "relative";
		$(element).parentNode.style.position = "relative";
		$(element).style.width = $(element).offsetWidth + "px";
		$(element).style.height = $(element).offsetHeight + "px";
		
		var inputs = $(element).getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
		{
			inputs[i].style.left = inputs[i].offsetLeft + "px";
			inputs[i].style.top = inputs[i].offsetTop + "px";
			inputs[i].style.width = inputs[i].offsetWidth + "px";
			inputs[i].style.height = inputs[i].offsetHeight + "px";
		}
		for (var i = 0; i < inputs.length; i++)
		{
			inputs[i].style.position = "absolute";
		}		
		//Variables declaration
		Uploadize._querystr = "?";
		Uploadize._uploadizeFolder = "uploadize/";
		Uploadize.UPLOAD_FRAME_ID =  $(element).id + '_UploadFrame';
		Uploadize.INFO_TEXT_ID = $(element).id + '_InfoText';
		Uploadize.L_WRAPPER_ID = $(element).id + '_LoaderWrapper';
		Uploadize.L_LABEL_ID = $(element).id + '_LoaderLabel';
		Uploadize.L_SUCCESS_ID = $(element).id + '_SuccessLabel';
		Uploadize.L_FAIL_ID = $(element).id + '_FailLabel';
		//Constructor
		setTimeout(function()
							{
								Uploadize._createIFrame($(element));
								Uploadize._simulateSubmit($(element));
								Uploadize._attachFormAttributes();								
							},100);

		// Load CSS
		var oLink = document.createElement("link");
		oLink.href = "uploadize/media/styles.css";
		oLink.rel = "stylesheet";
		oLink.type = "text/css";
		document.body.appendChild(oLink);

	}
	
	Uploadize._createIFrame = function (element)
	{
		var inputElement = null;
		
		var inputs = element.getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
			if (inputs[i].type == "file") inputElement = inputs[i];
		
		var prospective = document.createElement('iframe');
		prospective.setAttribute('id', Uploadize.UPLOAD_FRAME_ID);
		prospective.setAttribute('name', Uploadize.UPLOAD_FRAME_ID);
		prospective.className = 'UZUploadFrame';
		//prospective.setAttribute('className','UZUploadFrame');
		prospective.setAttribute('src', this._uploadizeFolder + 'php/engine.php');
		if (inputElement.style.width) 
		{
			setTimeout(function()
			{
			var inputElementIFrame = document.getElementById(Uploadize.UPLOAD_FRAME_ID).contentWindow.document.getElementById('file');
			inputElementIFrame.style.width = prospective.style.width = inputElement.style.width;
			},500);
		}
		else 
		{
			prospective.style.width = "228px";
		}
		if (inputElement.style.height) 
		{
			prospective.style.height = inputElement.style.height;
		}
		else 
		{
			prospective.style.height = "24px";	
		}
		prospective.scrolling = "no";
		prospective.frameborder = "0";
		prospective.style.left = inputElement.offsetLeft + "px";
		prospective.style.top = inputElement.offsetTop + "px";
		element.appendChild(prospective);

		var infotext = document.createElement('input');
		infotext.setAttribute('type','text');
		infotext.setAttribute('id',Uploadize.INFO_TEXT_ID);
		infotext.className = 'UZInfoText';
		//infotext.setAttribute('className','UZInfoText');
		infotext.setAttribute('value','');
		infotext.readOnly = true;
		infotext.style.left = inputElement.offsetLeft + "px";
		infotext.style.top = inputElement.offsetTop + "px";
		element.appendChild(infotext);
		
		var lbar = document.createElement('div');
		lbar.setAttribute("id",Uploadize.L_WRAPPER_ID);
		lbar.className = 'UZLoaderWrapper';
		//lbar.setAttribute('className','UZLoaderWrapper');
		lbar.style.width = element.offsetWidth + "px";
		lbar.style.height = element.offsetHeight + "px";
		element.appendChild(lbar);

		var tbar = document.createElement('div');
		tbar.setAttribute("id",Uploadize.L_LABEL_ID);
		tbar.className = 'UZLoaderLabel';
		//tbar.setAttribute('className','UZLoaderLabel');
		tbar.style.left = (element.offsetWidth / 2) - 70 + "px";
		tbar.style.top = (element.offsetHeight / 2) - 18 + "px";
		tbar.innerHTML = "<div style='font-family: Verdana; font-size: 12px; color: #FFF; margin-left: 45px; margin-top: 10px;'>Uploading...</div>";
		element.appendChild(tbar);
		
		var successbar = document.createElement('div');
		successbar.setAttribute("id",Uploadize.L_SUCCESS_ID);
		successbar.className = 'UZSuccessLabel';
		//successbar.setAttribute('className','UZSuccessLabel');
		successbar.style.left = (element.offsetWidth / 2) - 110 + "px";
		successbar.style.top = (element.offsetHeight / 2) - 22 + "px";
		successbar.innerHTML = "<div style='font-family: Verdana; font-size: 16px; color: #FFF; margin-left: 55px; margin-top: 13px;'>File was uploaded.</div>";
		element.appendChild(successbar);

		var rejectbar = document.createElement('div');
		rejectbar.setAttribute("id",Uploadize.L_FAIL_ID);
		rejectbar.className = 'UZFailLabel';
		rejectbar.style.left = (element.offsetWidth / 2) - 110 + "px";
		rejectbar.style.top = (element.offsetHeight / 2) - 22 + "px";
		rejectbar.innerHTML = "<div style='font-family: Verdana; font-size: 16px; color: #FFF; margin-left: 55px; margin-top: 13px;'>File was rejected.</div>";
		element.appendChild(rejectbar);
		
		setTimeout(function()
		{
		var inputElementIFrame = document.getElementById(Uploadize.UPLOAD_FRAME_ID).contentWindow.document.getElementById('file');
		var inputs = element.getElementsByTagName('input')		
		var inputInfoText = null;
		for (var i = 0; i < inputs.length; i++)
		{
			if (inputs[i].id == Uploadize.INFO_TEXT_ID) 
			{
			inputInfoText = inputs[i];
			}
		}
		inputElementIFrame.onchange = function()
		{
			inputInfoText.value = inputElementIFrame.value;
		}
		}, 1000);
	}
	
	
	Uploadize._simulateSubmit = function (element)
	{
		var inputs = element.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++)
			if ((inputs[i].type == "submit") || (inputs[i].type == "button")) var origSubmit = inputs[i];
		
		document.getElementById(Uploadize.UPLOAD_FRAME_ID).onload = function()
		{
			//alert("Loaded");
		}
		setTimeout(function()
		{
			var inputs = element.getElementsByTagName("input");
			for (var i = 0; i < inputs.length; i++)
				if ((inputs[i].type == "submit") || (inputs[i].type == "button")) var origSubmit = inputs[i];
				

			var fakeSubmit = document.getElementById(Uploadize.UPLOAD_FRAME_ID).contentWindow.document.getElementById('submit');

			//alert(origSubmit.parentNode.innerHTML);
			/*
			element.onsubmit = origSubmit.onclick = function()
			{
				if (!$(Uploadize.INFO_TEXT_ID).value)
				{
					return false;
				}
				origSubmit.disabled = "disabled";
				fakeSubmit.click();
				Uploadize._startUpload(element);
				return false;
			} */
			//alert(Uploadize.INFO_TEXT_ID + ' initialized');
			addEventSimple(origSubmit,'click',function(e)
			{
				if (!e) var e = window.event;
				if (!e.target) e.target = e.srcElement;
				var target_wrapper = e.target.parentNode.id; // --> The id of the wrapper in which the event is triggered
				if (!$(target_wrapper + "_InfoText").value)
				{
					return false;
				}
				origSubmit.disabled = "disabled";
				fakeSubmit.click();
				Uploadize._startUpload(target_wrapper);
				return false;
			});
			
			addEventSimple(element,'submit',function(e)
			{
				if (!e) var e = window.event;
				if (!e.target) e.target = e.srcElement;
				var target_wrapper = e.target.parentNode.id; // --> The id of the wrapper in which the event is triggered

				if (!$(target_wrapper + "_InfoText").value)
				{
					return false;
				}
				origSubmit.disabled = "disabled";
				fakeSubmit.click();
				Uploadize._startUpload(target_wrapper);
				return false;
			});
			
			addEventSimple($(Uploadize.UPLOAD_FRAME_ID).contentWindow.document.getElementById("sample"),'submit',function(e)
			{
				if (!e) var e = window.event;
				if (!e.target) e.target = e.srcElement;
				var target_wrapper = e.target.parentNode.id; // --> The id of the wrapper in which the event is triggered
				
				//alert(target_wrapper);
				if (!$(element.id + "_InfoText").value)
				{
					return false;
				}
				origSubmit.disabled = "disabled";
				Uploadize._startUpload(target_wrapper);
				return true;
			});
			/*
			$(Uploadize.UPLOAD_FRAME_ID).contentWindow.document.getElementById("sample").onsubmit = function()
			{
				if (!$(Uploadize.INFO_TEXT_ID).value)
				{
					return false;
				}
				origSubmit.disabled = "disabled";
				Uploadize._startUpload(element);
				return true;				
			}
			*/
			
		}, 1000);
		
	}
	
	Uploadize._startUpload = function (element)
	{
		var wrapper = $(Uploadize.L_WRAPPER_ID);
		var loader = $(Uploadize.L_LABEL_ID);
		wrapper.style.display = "block";
		loader.style.display = "block";

	}
	
	Uploadize._startUpload._fadeIn = function (elem_id) 
	{
		var speed = 10;
		var timer = 0;
	
			for (var i = 60; i <= 100; i++)
			{
				setTimeout("Uploadize._startUpload._fadeInFNC(" + i + ",'" + elem_id + "')", (timer * speed));
				timer++;
			}

	}  
	
	Uploadize._startUpload._fadeInFNC = function(i, elem_id)
	{
			var object = $(elem_id).style;
			object.opacity = (i / 100);
			object.MozOpacity = (i / 100);
		    object.KhtmlOpacity = (i / 100);
		    object.filter = "alpha(opacity=" + i + ")";
	}
		
	Uploadize._endUpload = function (success)
	{
		var wrapper = $(Uploadize.L_WRAPPER_ID);
		var loader = $(Uploadize.L_LABEL_ID);
		
		loader.style.display = "none";
		// Only the wrapper stays here. The real elements are not unloaded but covered by this wrapper.
		Uploadize._startUpload._fadeIn(Uploadize.L_WRAPPER_ID);
		
		if (success)
		{
			$(Uploadize.L_SUCCESS_ID).style.display = "block";
		}
		else
		{
			$(Uploadize.L_FAIL_ID).style.display = "block";
		}
	}
	
	Uploadize._endUpload._showSuccess = function ()
	{
		var wrapper = $(Uploadize.L_WRAPPER_ID);
		var loader = $(Uploadize.L_LABEL_ID);
		
		loader.style.display = "none";
		// Only the wrapper stays here. The real elements are not unloaded but covered by this wrapper.
		Uploadize._startUpload._fadeIn(wrapper);
		//alert('Successfully uploaded!');
	}
	
	Uploadize._attachFormAttributes = function()
	{
		setTimeout(function() 
		{
			var oForm = document.getElementById(Uploadize.UPLOAD_FRAME_ID).contentWindow.document.getElementById('sample');
			oForm.setAttribute('method','post');
			oForm.setAttribute('action',Uploadize._querystr);
			oForm.setAttribute('enctype','multipart/form-data'); // IE is sentimental on enctype
		}, 1000);
	}
	
	Uploadize.prototype = 
	{
		uploadize : function() 
		{      
			
		},

		uploadTo : function(upload_to) 
		{      
			Uploadize._querystr += "upload_to=" + upload_to + "&";                        
		},

		allowExtensions : function() 
		{      
			alert(this.element);                           
		},
		
		//=====Setters and Getters=======
		
		setFileName : function(file_name) 
		{      
			Uploadize._querystr += "file_name=" + file_name + "&";                           
		}		

	}

