/*
 * 
 *  updater.js
 *  Description: edits span to something else
 * 
*/
var changing=false;


function heightEl(span){

	if (document.layers){
	  h=document.layers[span].clip.height;
	} else if (document.all && !document.getElementById){
	  h=document.all[span].offsetHeight;
	} else if(document.getElementById){
	  h=document.getElementById(span).offsetHeight;
	}
return h;
}
function widthEl(span){

	if (document.layers){
	  w=document.layers[span].clip.width;
	} else if (document.all && !document.getElementById){
	  w=document.all[span].offsetWidth;
	} else if(document.getElementById){
	  w=document.getElementById(span).offsetWidth;
	}
return w;
}


function highLight(span){
            //span.parentNode.style.border = "2px solid #D1FDCD";
            //span.parentNode.style.padding = "0";
            span.style.border = "1px solid #54CE43";          
}

function noLight(span){
        //span.parentNode.style.border = "0px";
        //span.parentNode.style.padding = "2px";
        span.style.border = "0px";   


}
function fieldBlur(actual,idfld)
{
	ajaxManager('save_page','http://www.garlicia.ro/update/updater.php','tblInfo='+idfld+'&content='+actual.value.replace(/&/g,'%26'),idfld);
	//document.getElementById(idfld).innerHTML=lstChange;
	changing=false;
	
}
function editBox(actual) {
	//alert(actual.nodeName+' '+changing);
	if(!changing){
		if (actual.innerHTML=="saving...")  return ;
		width = widthEl(actual.id);
		height =heightEl(actual.id) + 2;

		if((height < 40) && (actual.innerHTML.length<100)){
			height=18;
			if(width  <=50)	width = 150;
			actual.innerHTML = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" + actual.innerHTML + "\"  onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\" />";
		}else{
			if(width < 70) width = 90;
			if(height < 50) height = 50;
			actual.innerHTML = "<textarea name=\"textarea\" id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur(this,'" + actual.id + "');\">" + actual.innerHTML + "</textarea>";
		//toggleArea(actual.id +"_field");
		}
		changing = true;

	}

		actual.firstChild.focus();
}

function editbox_init(){
        var spans = document.getElementsByTagName("span");

        for(i=0; i<spans.length; i++)
                if(spans[i].className == 'updater_class')
                {
                        spans[i].style.cursor = "pointer";
						spans[i].title = "Click to edit!";
						spans[i].onclick = function(){ editBox(this); }
                }
}



addEvent(window, "load", editbox_init);

