function addListener(element, type, listener, capture)
{
	if (capture == undefined){
		capture = false;
	}
	if (element.addEventListener){
		element.addEventListener(type, listener, capture);
	}else{
		element.attachEvent('on' + type, listener);
	}
}

function removeListener(element, type, listener, capture)
{
	if (capture == undefined){
		capture = false;
	}
	if (element.removeEventListener){
		element.removeEventListener(type, listener, capture);
	}else{
		element.detachEvent('on' + type, listener);
	}
}

function isEmpty(prop)
{
	if (prop == undefined || prop == null || prop == '') return true;
	return false;
}

//---MODAL->>

var liveAjaxRequest;

function modal(cleanBuffer, title , w, h)
{
	modalCancel();
	if (w == undefined) w = 640;
	if (h == undefined) h = 480;
	if (title == undefined) title = '&nbsp;';
	var box = s$('modalBox');
	var contentBox = s$('modalContent');
	var blocker = s$('modalBlocker');
	s$('modalTitle').innerHTML = title;
	contentBox.innerHTML = '';
	box.style.display = 'block';
	blocker.style.display = 'block';
	box.style.width = w + 'px';
	contentBox.style.maxHeight = (h - 23) + 'px';
	contentBox.innerHTML = cleanBuffer;
	if (s$('modalLoader')) s$('modalLoader').style.display = 'none';
	modalResizeBlocker();
	centerModal(box);
	//Drag.init(s$('modalTitle'), box);
	//attachFormHL();
}

function modalImage(imgSrc, title, desc, w, h)
{
	modalCancel();
	if (w == undefined) w = 350;
	if (h == undefined) h = 150;
	if (title == undefined) title = '&nbsp;';
	var box = s$('modalBox');
	var contentBox = s$('modalContent');
	var blocker = s$('modalBlocker');
	s$('modalTitle').innerHTML = title;
	contentBox.innerHTML = '';
	contentBox.style.padding = '0';
	contentBox.style.overflow = 'visible';// safari+opera hack
	contentBox.style.overflowY = 'hidden';
	box.style.display = 'block';
	blocker.style.display = 'block';
	box.style.width = w + 'px';
	contentBox.style.maxHeight = (h - 23) + 'px';
	s$('modalLoader').style.display = '';
	centerModal(box);
	modalResizeBlocker();

	var newImg = null;
	newImg = new Image();
	newImg.onerror = function(){
		contentBox.innerHTML = 'ERROR! Failed to load "' + imageSrc + '"';
		return false;
	}
	newImg.src = null;
	newImg.src = imgSrc;
	if (!newImg.complete){
		setTimeout('modalImage("' + imgSrc + '", "' + title + '", "' + desc + '", ' + w + ', ' + h + ')',100);
		return false;
	}

	box.style.width = (newImg.width + 2) + 'px';
	contentBox.innerHTML = '<img src="' + imgSrc + '" border="0" alt="' + title + '" title="' + title + '" />';
	if (desc != undefined && desc != '' && desc != 'undefined'){
		contentBox.innerHTML += '<p style="margin-bottom: 0; padding-bottom: 0;">' + desc + '</p>';
		contentBox.style.maxHeight = (newImg.height + 30) + 'px';
	}else{
		contentBox.style.maxHeight = newImg.height + 'px';
	}
	s$('modalLoader').style.display = 'none';
	centerModal(box);
	//Drag.init(s$('modalTitle'), box);
	//attachFormHL();
}

function modalAjax(url, w, h, title)
{
	modalCancel();
	if (w == undefined) w = 640;
	if (h == undefined) h = 480;
	if (title == undefined) title = '&nbsp;';
	var box = s$('modalBox');
	var contentBox = s$('modalContent');
	var blocker = s$('modalBlocker');
	s$('modalTitle').innerHTML = title;
	box.style.display = 'block';
	blocker.style.display = 'block';
	box.style.width = w + 'px';
	contentBox.style.maxHeight = (h - 23) + 'px';
	centerModal(box);
	modalResizeBlocker();

	liveAjaxRequest = new Ajax.Updater('modalContent', url, {
		method: 'get',
		asynchronous: true,
		evalScripts: true,
		onSuccess: function(request, json){
			Element.hide('modalLoader');
			liveAjaxRequest = undefined;
		},
		onComplete: function(){
			centerModal(s$('modalBox'));
			//Drag.init(s$('modalTitle'), s$('modalBox'));
			//attachFormHL();
		}
	});
}

function centerModal(elm)
{
	if (elm == undefined || elm.id == undefined) elm = s$('modalBox');
	if (elm.style == undefined) elm.style = '';
	var w = elm.offsetWidth;
	var h = elm.offsetHeight;
	if (!window.innerHeight){
		var b = document.documentElement ? document.documentElement : document.body;
		elm.style.left = (Math.round(b.clientWidth / 2) - Math.round(w / 2)) + 'px';
		elm.style.top = (Math.round(b.clientHeight / 2) - Math.round(h / 2)) + 'px';
	}else{
		elm.style.left = (Math.round(window.innerWidth / 2) - Math.round(w / 2)) + 'px';
		elm.style.top = (Math.round(window.innerHeight / 2) - Math.round(h / 2)) + 'px';
	}
}

function modalCancel()
{
	if (isEmpty(s$('modalContent').style)) s$('modalContent').style = null;
	if (isEmpty(s$('modalBlocker').style)) s$('modalBlocker').style = null;
	if (!isEmpty(liveAjaxRequest)){
		liveAjaxRequest.abort();
	}
	var box = s$('modalBox');
	var blocker = s$('modalBlocker');
	s$('modalContent').innerHTML = '&nbsp;';
	s$('modalContent').style.padding = '10px';
	s$('modalContent').style.overflowY = 'scroll';
	s$('modalLoader').style.display = '';
	centerModal(box);
	box.style.display = 'none';
	blocker.style.display = 'none';
}

function modalResizeBlocker()
{
	if (s$('modalBlocker')){
		var blocker = s$('modalBlocker');
		if (blocker.style.display == 'block'){
			var innerH;
			var theHeight;
			var b = document.body;
			if (!window.innerWidth){
				//IE
				innerH = b.clientHeight;
				theHeight = b.scrollHeight;
				if (isEmpty(blocker.style.filter)) blocker.style.filter = 'alpha(opacity=80)';
			}else{
				//w3c
				if (document.documentElement){
					b = document.documentElement;
				}
				theHeight = b.scrollHeight;
				innerH = window.innerHeight;
			}
			if (theHeight < innerH){
				theHeight = innerH;
			}
			blocker.style.height = theHeight + 'px';
		}
	}
}

//<<-MODAL---

//---GET ELEMENT(s)->>
// jQuery moronic '$' function substitution

function s$(id, elem)
{
	if (elem == undefined){
		elem = document;
	}
	return elem.getElementById(id);
}

function s$$(tagname, elem)
{
	if (elem == undefined){
		elem = document;
	}
	return elem.getElementsByTagName(tagname);
}

function s$$$(e, elem)
{
	if (!e || !e.length || e.length == 0)
	return [];
	var store = [elem || document.body];
	var store_t = [];
	e = e.split(" ");
	for (var i = 0; i < e.length; i++) {
		var r = {
			id:  "",
			tag: "*",
			clas:[]
		};
		var c, s, t = e[i];
		while (t.length > 0){
			s = t.search(/.[#\.]/) + 1 || t.length;
			c = t.substr(0, s);

			if (c.substr(0, 1) == "#"){
				r.id = c.substr(1);
			}else if (c.substr(0, 1) == "."){
				r.clas.push(c.substr(1));
			}else{
				r.tag = c;
			}

			t = t.substr(s);
		}
		while (store.length > 0){
			var curr = [], temp = store.shift().getElementsByTagName(r.tag);
			for (var j = 0; j < temp.length; j++){
				curr.push(temp[j]);
			}
			while (curr.length > 0){
				var ok = true, ce = curr.shift();
				if (r.id && ce.id != r.id){
					ok = false;
				}
				for (j = 0; j < r.clas.length; j++){
					if ((" " + ce.className + " ").indexOf(" " + r.clas[j] + " ") == -1){
						ok = false;
						break;
					}
				}
				if (ok){
					store_t.push(ce);
				}
			}
		}
		store = store_t;
		store_t = [];
	}
	return store;
}

//<<-GET ELEMENT(s)---
