var MEEDir = "http://www.msonline.it/MyEasyEditor/";

var MEEMode = "db" // possible values are 'db' if you use a db based site, or "file" if you have not a db on your site.

var MEEId = '';

function MEESetId(id){
	MEEId = id;
}

function MEEEffect(id, hide){
	this.action = false;
	if(hide == null){
		this.hi = true;
	}else{
		this.hi = hide;
	}
	this.i = 1;
	this.w = 0;
	this.z = 0;
	this.move = false;
	this.element = document.getElementById(id);
	this.element.style.visibility = 'visible';
	this.element.style.display = 'block';
	this.element.style.overflow = 'hidden';
	this.he = this.element.offsetHeight;
	if(!this.hi){
		this.element.style.height = "0px";
	}

	this.dec = function(){
		var h = this.element.offsetHeight;
		if((h - this.i) < 0){
			this.element.style.height = "0px"
			this.i = 1;
			this.hi = false;
			window.clearInterval(this.w);
			this.action = false;
		}else{
			this.i++;
			h = h - this.i;
			this.element.style.height = h+"px";
		}
	};
	
	this.inc = function(){
		var h = this.element.offsetHeight;
		if((h + this.i) > this.he){
			this.element.style.height = this.he+"px"
			this.i = 1;
			this.hi = true;
			window.clearInterval(this.w);
			this.action = false;
		}else{
			this.i++;
			h = h + this.i;
			this.element.style.height = h+"px";
		}
	};
	
	this.MEEHider = function(){
		this.action = true;
		var me = this;		// questa riga serve per un problema di "scope" per IE
		if(this.hi){
			this.w = window.setInterval(function(){ me.dec(); }, 10);
		}else{
			this.w = window.setInterval(function(){ me.inc(); }, 10);
		}
	};
	this.setMove = function(bval){
		this.move = bval;
	};
	this.start = function(){
		var elem_start = this.element;	
		var difx = (elem_start.style.left.replace(/px/g,'')) - cx;
		var dify = (elem_start.style.top.replace(/px/g,'')) - cy;
		if(this.move){
			window.clearInterval(this.z);
			this.move = false;
			document.onselectstart = function() {return true;} // ie
			document.onmousedown = function() {return true;} // mozilla
		}else{
			this.move = true;
			document.onselectstart = function() {return false;} // ie
			document.onmousedown = function() {return false;} // mozilla
			this.z = window.setInterval(function foo(){elem_start.style.left = (cx+difx)+"px"; elem_start.style.top = (cy+dify)+"px";}, 30);
		}
	};
	document.onmousemove = MEEmouseMove;
}
function MEEmouseMove(e){
	// compatibilità crossbrowser
	if (e == undefined) var e = window.event;
	cx = (e.clientX != undefined) ? e.clientX : e.client.x;
	cy = (e.clientY != undefined) ? e.clientY : e.client.y;
	return false;
}


function MEEgetElementsByClass(aClassName){
	var all = document.getElementsByTagName('*');
	var selected = new Array();
	var len = all.length
	var i = 0;
	while(i < len){
		if(all[i].className.indexOf(aClassName) != -1){
			selected.push(all[i]);
		}
		i++
	}
	return selected;
}

function MEERequest(url, obj){
	if(window.XMLHttpRequest){	// per IE7+, Firefox, Chrome, Opera, Safari
		var req = new XMLHttpRequest();
	}else{	// per IE6, IE5
		var req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	url = MEEDir+url;
	req.open("get",url,true);
	req.onreadystatechange=function(){
		if(req.readyState == 4){
		if(req.status == 200){
			obj.innerHTML = req.responseText;
		}else{
			alert('errore');
		}
	}
	}
	req.send();
}

function MEEEditableInit(){
	var editable = MEEgetElementsByClass('editable');
	var len = editable.length;
	for(var i = 0; i < len; i++){
		editable[i].onclick = function(){ MEESetId(this.id);};
		editable[i].contentEditable = 'true';
	}
}

function MyEasyEditorInit(){
	var body = document.getElementsByTagName("body")[0];
	var head = document.getElementsByTagName("head")[0];
	MEEStyle = document.createElement("link");
	MEEStyle.type = "text/css";
	MEEStyle.rel = "stylesheet";
	MEEStyle.href = MEEDir+"src/style.css";
	head.appendChild(MEEStyle);
	/* var MEEStyle = document.createElement("style");
	MEEStyle.type = "text/css";
	head.appendChild(MEEStyle);
	MEERequest("src/style.css", MEEStyle); */
	var MEEMain = document.createElement("div"); 
	MEEMain.id = "MyEasyEditor_main";
	MEEMain.className = "MEE_main";
	MEEMain.style.top = "10px";
	MEEMain.style.left = "10px";
	body.appendChild(MEEMain);
	MEERequest("src/main.php?MEEDir="+MEEDir, MEEMain);
	var MEEFunc = document.createElement("script");
	MEEFunc.type = "text/javascript";
	head.appendChild(MEEFunc);
	MEERequest("src/MyEasyEditorFunctions.js", MEEFunc);
	var MEELink = document.createElement("a");
	MEELink.accessKey = "z";
	MEELink.href = "javascript: MEEMakeVisible();";
	body.appendChild(MEELink);
	MEEEditableInit();
	MEE = new MEEEffect('MyEasyEditor_main', true, true);

}
