OWNT.editRow = function(a) {
	var q = a.search.substr(1), key = q.match(/key=(.+)(&.*)?/)[1], Form = OWNT.hForm();
	Form.addInput('key',key);
	Form.addInput('task','edit');
	Form.submit();
	Form.destroy();
	return false;
}

OWNT.deleteRow = function(){
	return confirm("Are you sure you want to delete this row?");
}

OWNT.newRow = function(){		
	var Form = OWNT.hForm();
	Form.addInput('task','new');
	Form.submit();
	Form.destroy();
	return false;
}

OWNT.setState = function(a, tr){
	if (a.search) {
		var q = a.search.substr(1), state = q.match(/state=(-?\d)/)[1];
		if (!tr) tr = a.parentNode.parentNode;
		OWNT.AjaxPost(q,function(resp){
			if (resp=='OK') tr.className = tr.className.replace(/state-?\d/g,'state'+state);
			else alert(resp);
		});
	}
	return false;
}

OWNT.hForm = function() {
	var editWindow = open('about:blank','editWindow','status,scrollbars,resizable,height=400,width=600');
	editWindow.focus();
	var Form = document.body.appendChild(document.createElement('form'));
	Form.destroy = function(){
		this.parentNode.removeChild(this);
	}
	Form.style.display = 'none';
	Form.addInput = function(name,value) {	//this.innerHTML += "<input type='hidden' name='"+name+"' value=\""+value.replace(/"/g,'\\"')+"\" />";}
		var i = this.appendChild(document.createElement('input'));
		i.name = name;
		i.value = value;//.replace(/"/g,"''");
	}
	Form.addInput('vid',document.forms.db_view.vid.value);
	Form.setAttribute('method','post');
	Form.target = 'editWindow';
	Form.action = 'index2.php?option=com_ownt&no_html=1';
	return Form;	
}

OWNT.popTab = function(vid, id, tr){
	if (OWNT.Admm) return;
	var c = getXY(tr);
	if (typeof(OWNT.tab)=='undefined') {
		OWNT.tab = document.createElement('div');
		OWNT.a = OWNT.tab.appendChild(document.createElement('a'));
		OWNT.tab.className = 'controltab';
		OWNT.tab.onmouseover = OWNT.tab.onmousemove = function(){clearTimeout(OWNT.ttt)};
	}		
	document.body.appendChild(OWNT.tab);
	OWNT.tab.style.left = c.l;
	OWNT.tab.style.top = c.t; 
	OWNT.tab.style.height = c.h;
	OWNT.a.href = 'index2.php?option=com_ownt&no_html=1&task=edit&vid='+vid+'&key='+id;
	OWNT.a.onclick=function(e){return OWNT.admm(c,tr,this,vid,id)};
	clearTimeout(OWNT.ttt);

	tr.onmouseout = OWNT.tab.onmouseout = function(){OWNT.ttt = setTimeout(function(){
			if (OWNT.tab.parentNode) document.body.removeChild(OWNT.tab);			
	},500)}

	function getXY(element) {
		var x = y = 0;
		for(var e = element; e; e = e.offsetParent) y += e.offsetTop;
		for(var e = element; e; e = e.offsetParent) x += e.offsetLeft;
		for(e = element.parentNode; e && e != document.body; e = e.parentNode) if (e.scrollTop) y -= e.scrollTop;
		for(e = element.parentNode; e && e != document.body; e = e.parentNode) if (e.scrollLeft) x -= e.scrollLeft;
		var s = element.cells[0];
		return {
			l:x-26+'px', 
			t:y-(typeof(navigator.product)!='undefined' && navigator.product =='Gecko')+'px', //silly hack :( but no other way to catch 1px difference
			h:element.offsetHeight-1+'px'
		};
	}
}

OWNT.admm = function(c,tr,a,vid,id) {
	tr.oldClassName = tr.className;
	tr.className += " ow_hl";
	OWNT.tr = tr;
	document.body.removeChild(OWNT.tab);
	if (!OWNT.Admm) {
		OWNT.Admm = document.createElement("ul");
		add(a.href, "edit", "Edit",'editRow');
		add('index2.php?option=com_ownt&no_html=1&task=delete&vid='+vid+'&key='+id,"new", "Add new",'newRow');
		var state = tr.className.match(/\bstate(-?\d)\b/);
		if (state) {
			state = state[1];
			if (state!=1) add('index2.php?option=com_ownt&no_html=1&task=pub&vid='+vid+'&key='+id+'&state=1', 'publish', 'Publish', 'setState');
			if (state!=0) add('index2.php?option=com_ownt&no_html=1&task=pub&vid='+vid+'&key='+id+'&state=0', 'unpublish', 'Unpublish', 'setState');
			if (state!=-1) add('index2.php?option=com_ownt&no_html=1&task=pub&vid='+vid+'&key='+id+'&state=-1', 'archive', 'Archive', 'setState');
			if (state!=-2) add('index2.php?option=com_ownt&no_html=1&task=pub&vid='+vid+'&key='+id+'&state=-2', 'trash', 'Trash', 'setState');
		} //else 
		add('index2.php?option=com_ownt&no_html=1&task=delete&vid='+vid+'&key='+id,"del", "Delete", 'deleteRow');

		OWNT.Admm.id = "ntadmm";
		OWNT.Admm.style.top = c.t;
		OWNT.Admm.style.left = c.l;
		document.body.appendChild(OWNT.Admm);
		OWNT.Admm.die = function(){
			OWNT.Admm.parentNode.removeChild(OWNT.Admm); 
			OWNT.Admm = null;
			tr.className = tr.oldClassName;			
		}
		OWNT.Admm.onclick = OWNT.Admm.die;
		OWNT.Admm.onmouseout = function(){
			OWNT.Admm.fade = setTimeout(this.die,1000);
			OWNT.Admm.onmousemove = function(){clearTimeout(OWNT.Admm.fade)};
		}
//		setTimeout(function(){
//			document.onclick = function(e){
//				OWNT.Admm.parentNode.removeChild(OWNT.Admm);
//				OWNT.Admm = null;	
//				document.onclick = null;
//			}
//		},10);
	}
	return false;
	
	function add(href, cls, txt, fn){		
		var callbak = OWNT[fn];
		var li = OWNT.Admm.appendChild(document.createElement("li"));
		var a = li.appendChild(document.createElement("a"));
		li.className = cls;
		a.href = href;
		a.onclick = function(){return callbak(a, tr)};
		a.appendChild(document.createTextNode(txt));
	}
}
