var __eventListeners = [];

/* * * * * * * * * * * * * * * * */

function addEvent(instance, eventName, listener) {
    var listenerFn = listener;
    if (instance.addEventListener) {
        instance.addEventListener(eventName, listenerFn, false);
    } else if (instance.attachEvent) {
        listenerFn = function() {
            listener(window.event);
        }
        instance.attachEvent("on" + eventName, listenerFn);
    } else {
        throw new Error("Event registration not supported");
    }
    var event = {
        instance: instance,
        name: eventName,
        listener: listenerFn
    };
    __eventListeners.push(event);
    return event;
}

/* * * * * * * * * * * * * * * * */

function removeEvent(event) {
    var instance = event.instance;
    if (instance.removeEventListener) {
        instance.removeEventListener(event.name, event.listener, false);
    } else if (instance.detachEvent) {
        instance.detachEvent("on" + event.name, event.listener);
    }
    for (var i = 0; i < __eventListeners.length; i++) {
        if (__eventListeners[i] == event) {
            __eventListeners.splice(i, 1);
            break;
        }
    }
}

/* * * * * * * * * * * * * * * * */

function unregisterAllEvents() {
    while (__eventListeners.length > 0) {
        removeEventListener(__eventListeners[0]);
    }
}
/* * * * * * * * * * * * * * * * */
function getOffsetLeft(el) {
	var left = el.offsetLeft;
	if (el.offsetParent) left += getOffsetLeft(el.offsetParent);
	return left;
}
/* * * * * * * * * * * * * * * * */
function getOffsetTop(el) {
	var top = el.offsetTop;
	if (el.offsetParent) top += getOffsetTop(el.offsetParent);
	return top;
}
/* * * * * * * * * * * * * * * * */
function urlencode(ch) {
	ch = ch.replace(/[ ]/g,"%20")
	return escape(ch)
}
/* * * * * * * * * * * * * * * * */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function cPostInfo (post_id,date,moder_id,moder,reasons) {
	this.post_id = post_id;
	this.moder_id = moder_id;
	this.moder = moder;
	this.reasons = reasons;
	this.date = date;

	this.show = function (container) {
		var tbody,tr,td,el;
		tbody = document.createElement('tbody');

		this.showModer(tbody);
		this.showDate(tbody);
		this.showReasons(tbody);
		return tbody;
	}
	this.showModer = function (container) {
		var tr,td,el;
		tr = document.createElement('tr');
		this.createTitle('Поставил:',tr);

		td = document.createElement('td');
		el = document.createElement('a');
		el.href = 'profile.php?mode=viewprofile&u='+this.moder_id;
		el.appendChild(document.createTextNode(this.moder));
		td.appendChild(el);
		tr.appendChild(td);

		container.appendChild(tr);
	}
	this.showDate = function (container) {
		var tr,td,el;
		tr = document.createElement('tr');
		this.createTitle('Дата:',tr);
		td = document.createElement('td');
		td.appendChild(document.createTextNode(this.date));
		tr.appendChild(td);

		container.appendChild(tr);
	}
	this.showReasons = function (container) {
		var tr,td,i,ul,li;
		tr = document.createElement('tr');
		this.createTitle('Причины:',tr);

		td = document.createElement('td');
		ul = document.createElement('ul');
		for (i = 0; i < this.reasons.length ; i++) {
			li = document.createElement('li');
			li.appendChild(document.createTextNode(this.reasons[i]));
			ul.appendChild(li);
		}
		td.appendChild(ul);
		tr.appendChild(td);

		container.appendChild(tr);
	}
	this.createTitle = function(title,tr) {
		var td = document.createElement('td');
		td.className = 'ti';
		td.appendChild(document.createTextNode(title));
		tr.appendChild(td);
	}

}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function cWarningInfo () {
	this.posts = new Object();
	this.container = null;
	this.initialized = false;
	this.srcObj = null;
	this.current = 0;

	this.init = function () {
		this.container = document.createElement('table');
		this.container.style.left = '0px';
		this.container.style.top = '0px';
		this.container.style.visibility = 'hidden';
		this.container.id = 'warningtab';
		this.container.cellSpacing = '2px';
		this.container.appendChild(document.createElement('tbody'));

		document.body.appendChild(this.container);
		this.initialized = true;
	}
	this.getInfo = function(post,srcObj) {
		this.srcObj = srcObj;

		if (this.current == post) {
			return this.hideTable();
		}
		this.current = post;

		if (this.posts[post] != undefined) {
			this.show(this.posts[post]);
		} else {
			this.showWaiting();
			var url = 'jsinfo.php';
			var pars = 'mode=warninginfo&p=' + post;
			new Ajax.Request(
				url,
				{
					method: 'post',
					parameters: pars,
					onComplete: warningResponse
				});
		}
	}
	this.show = function (post) {
		if (!this.initialized) this.init();
		var tbody = post.show();

		if (this.container.tBodies && this.container.tBodies[0]) {
			this.container.replaceChild(tbody,this.container.tBodies[0]);
		} else {
			this.container.appendChild(tbody);
		}
		this.showTable();

	}
	this.showWaiting = function () {
		if (!this.initialized) this.init();

		var tbody,tr,td,img;
		tbody = document.createElement('tbody');
		tr = document.createElement('tr');
		td = document.createElement('td');
		td.className = 'waiting';
		td.appendChild(wait_img);
		tr.appendChild(td);
		tbody.appendChild(tr);

		if (this.container.tBodies && this.container.tBodies[0]) {
			this.container.replaceChild(tbody,this.container.tBodies[0]);
		} else {
			this.container.appendChild(tbody);
		}

		this.showTable();
	}
	this.errorMessage = function (message) {
		if (!this.initialized) this.init();

		var tbody,tr,td;
		tbody = document.createElement('tbody');
		tr = document.createElement('tr');
		td = document.createElement('td');
		td.className = 'error';
		td.appendChild(document.createTextNode(message));
		tr.appendChild(td);
		tbody.appendChild(tr);

		if (this.container.tBodies && this.container.tBodies[0]) {
			this.container.replaceChild(tbody,this.container.tBodies[0]);
		} else {
			this.container.appendChild(tbody);
		}

		this.showTable();

	}
	this.showTable = function () {
		if (this.srcObj == null) return;
		var l,t;

		l = getOffsetLeft(this.srcObj);
  		t = getOffsetTop(this.srcObj);
  		this.container.style.left = (l - 325) + 'px';
  		this.container.style.top = (t + 20) + 'px';
  		this.container.style.visibility = 'visible';
	}
	this.hideTable = function () {
		this.container.style.visibility = 'hidden';
		this.current = 0;
	}


}
/* * * * * * * * * * * * * * * * */
var warning = new cWarningInfo();

function warningResponse (t) {
	try {

		var response = eval('(' + t.responseText + ')');
    	if (response.post_id > 0) {
    		var postid = response.post_id;
    		warning.posts[postid] = new cPostInfo(response.post_id,response.date,response.moder_id,response.moder,response.reasons);
    		warning.show(warning.posts[postid]);
    	} else {
    		warning.errorMessage('Ошибка при получении данных')
    	}


  	} catch (e) {
		warning.errorMessage('Ошибка при получении данных');
  	}
}

/* * * * * * * * * * * * * * * * */
var selected_text = '';

function getSel()
{
	var txt = '';
	if (window.getSelection) {
		txt = window.getSelection();
		txt = txt.toString();
	} else if (document.getSelection) {
		txt = document.getSelection();
	} else if (document.selection) {
		txt = document.selection.createRange().text;
	}

	selected_text = txt;
	return txt;

}