
AIM = {

	frame : function(c) {
 
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);
 
		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}
 
		return n;
	},
 
	form : function(f, name) {
		f.setAttribute('target', name);
		f.setAttribute('enctype', 'multipart/form-data');
	},
 
	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
 
	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		if (d.location.href == "about:blank") {
			return;
		}
 
		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}
 
}
function startCallback() {
	// make something useful before submit (onStart)
	return true;
}

function completeCallback(response) {
	// make something useful after (onComplete)
	//document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
	//document.getElementById('r').innerHTML = response;
	if(response != ""){
	
		 /**
		 * Gives focus to an element and fires the onFocus event handler.
		 */
		document.getElementById('main').focus();
	
		tempora = response.split(".");
		
		/**
		 * Executes a command (According to JAVA2S refercence: Executes outside the regular methods. )
		 */
		switch (tempora[tempora.length-1]){
			// supported image
			case 'jpg': case 'JPG': case 'bmp': case 'BMP': case 'jpeg': case 'JPEG': case 'png': case 'PNG': case 'gif': case 'GIF':
				//document.execCommand('insertImage',false,response);
				//objEdit.document.body.innerHTML = text2insert + objEdit.document.body.innerHTML;
				
        if ( document.getElementById('r2').checked )
          style="style=\"float: left;\""
				else if ( document.getElementById('r3').checked )
          style="style=\"position: absolute;\""
				
				id=Math.floor(Math.random()*10000);
				
				var text2insert = "<img id=\"id_"+id+"\" "+style+"  src=\""+response+"\" />";
				document.execCommand('inserthtml',false,text2insert);
        break;
			default:
				//name = prompt("Zadejte prosim popisek souboru", "Soubor");
				var text2insert = "<a href=\""+response+"\">link</a>";
				//objEdit.document.body.innerHTML = text2insert + objEdit.document.body.innerHTML;
				document.execCommand('inserthtml',false,text2insert);
				
				
				//objEdit.document.write("<a href=\""+response+"\">"+name+"</a>");
				
				//var a = document.createElement('a'); 
				//a.innerHTML = name;
				//a.setAttribute('href', response);
				//objEdit.document.appendChild(a); 
				
				//var start = document.getElementById(textarea_object).selectionStart;
				//var end = document.getElementById(textarea_object).selectionEnd;
				
				//document.getElementById(textarea_object).value = document.getElementById(textarea_object).value.substr(0, start) + text2insert + document.getElementById(textarea_object).value.substr(end);
				//objEdit.document.execCommand('insertparagraph',false,text2insert);
				//nastaveni kurzoru na spravné misto / vybrani pozadovaneho textu
				//if (typeof selectionStart != 'undefined' && typeof selectionEnd != 'undefined') {
				//	textarea.selectionStart = start + selectionStart;
			//		textarea.selectionEnd = start + selectionEnd;
			//	}
				
				
				
				break;
		}
		
		/**
		 * Gives focus to an element and fires the onFocus event handler.
		 */
		 //objEdit.focus();
	 }
}
/*

		AIM = {
	 
			frame : function(c) {
		 
				var n = 'f' + Math.floor(Math.random() * 99999);
				var d = document.createElement('DIV');
				d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
				document.body.appendChild(d);
		 
				var i = document.getElementById(n);
				if (c && typeof(c.onComplete) == 'function') {
					i.onComplete = c.onComplete;
				}
		 
				return n;
			},
		 
			form : function(f, name) {
				f.setAttribute('target', name);
				f.setAttribute('enctype', 'multipart/form-data');
			},
		 
			submit : function(f, c) {
				AIM.form(f, AIM.frame(c));
				if (c && typeof(c.onStart) == 'function') {
					return c.onStart();
				} else {
					return true;
				}
			},
		 
			loaded : function(id) {
				var i = document.getElementById(id);
				if (i.contentDocument) {
					var d = i.contentDocument;
				} else if (i.contentWindow) {
					var d = i.contentWindow.document;
				} else {
					var d = window.frames[id].document;
				}
				if (d.location.href == "about:blank") {
					return;
				}
		 
				if (typeof(i.onComplete) == 'function') {
					i.onComplete(d.body.innerHTML);
				}
			}
		 
		}
		function startCallback() {
			// make something useful before submit (onStart)
			return true;
		}

		function completeCallback(response) {
			// make something useful after (onComplete)
			document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
			document.getElementById('r').innerHTML = response;
		}
*/
