/**
 * My JS API
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
var api =
{
	_init : function ()
	{
		var t = this, ua = navigator.userAgent, i, nl, n, base;
		
		// Browser checks
		t.isOpera = (window.opera && opera.buildNumber) || false;
		t.isWebKit = /WebKit/.test(ua);
		t.isOldWebKit = t.isWebKit && !window.getSelection().getRangeAt;
		t.isIE = !t.isWebKit && !t.isOpera && (/MSIE/gi).test(ua) && (/Explorer/gi).test(navigator.appName);
		t.isIE6 = t.isIE && /MSIE [56]/.test(ua);
		t.isGecko = !t.isWebKit && /Gecko/.test(ua);
		t.isMac = ua.indexOf('Mac') != -1;
		t.onOpenParams = window.opener ? window.opener.api.WinManager.params : {};
		
		// If base element found, add that infront of baseURL
		nl = document.getElementsByTagName('base');
		for (i=0; i < nl.length; i++) {
			if (nl[i].href) {
				base = nl[i].href;
			}
		}
		
		function getBase(n)
		{
			if (n.src && /js\/javascript\.js/.test(n.src)) {
				t.baseURL = n.src.substring(0, (n.src.lastIndexOf('/') - 3));

				// If path to script is relative and a base href was found add that one infront
				if (base && t.baseURL.indexOf('://') == -1) {
					t.baseURL = base + t.baseURL;
				}
				return t.baseURL;
			}
			return null;
		};
		
		// Check document
		nl = document.getElementsByTagName('script');
		for (i=0; i<nl.length; i++) {
			if (getBase(nl[i]))
				return;
		}

		// Check head
		n = document.getElementsByTagName('head')[0];
		if (n) {
			nl = n.getElementsByTagName('script');
			for (i=0; i<nl.length; i++) {
				if (getBase(nl[i]))
					return;
			}
		}
	},
	
	getViewPort : function(w)
	{
		var d, b;

		w = !w ? window : w;
		d = w.document;
		b = this.boxModel ? d.documentElement : d.body;

		// Returns viewport size excluding scrollbars
		return {
			x : w.pageXOffset || b.scrollLeft,
			y : w.pageYOffset || b.scrollTop,
			w : w.innerWidth || b.clientWidth,
			h : w.innerHeight || b.clientHeight
		};
	},
	
	e : function (id)
	{
		var e = {};
		if (typeof id == 'string') {
			e = document.getElementById(id);
		}
		return e;
	},
	
	t : function (name, target)
	{
		var list = [];
		if (typeof name == 'string') {
			if (typeof target == 'undefined' || target == null) {
				list = document.getElementsByTagName(name);
			} else {
				target = target || {};
				if (typeof target.childNodes == 'object') {
					for (var i = 0; i < target.childNodes.length; i++) {
						if (target.childNodes[i].nodeName.toLowerCase() == name.toLowerCase()) {
							list[list.length] = target.childNodes[i];
						}
					}
				}
			}
		}
		return list;
	},
	
	c : function (cl)
	{
		var retnode = [];
		var myclass = new RegExp('\\b'+cl+'\\b');
		var elem = this.t('*');
		var c = 0;
		for (var i = 0; i < elem.length; i++) {
			var classes = elem[i].className;
			if (myclass.test(classes)) {
				retnode[c++] = elem[i];
			}
		}
		return retnode;
	},
	
	each : function(o, cb, s)
	{
		var n, l;
		if (!o) return 0;
		s = s || o;
		if (typeof(o.length) != 'undefined') {
			// Indexed arrays, needed for Safari
			for (n=0, l = o.length; n<l; n++) {
				if (cb.call(s, o[n], n, o) === false) return 0;
			}
		} else {
			// Hashtables
			for (n in o) {
				if (o.hasOwnProperty(n)) {
					if (cb.call(s, o[n], n, o) === false) return 0;
				}
			}
		}
		return 1;
	},
	
	removeAttribute : function(e, attr)
	{
		if (typeof e != 'object' || typeof attr != 'string') return;
		if (this.isIE6 || this.isIE) {
			e.removeAttribute(attr, 0);
		} else {
			e.removeAttribute(attr);
		}
	},
	
	setAttribute : function(e, attr, value)
	{
		if (typeof e != 'object' || typeof attr != 'string' || typeof value == 'undefined') return;
		if (this.isIE6 || this.isIE) {
			e.setAttribute(attr, value, 0);
		} else {
			e.setAttribute(attr, value);
		}
	}
}

// Required for GZip AJAX loading
window.api = api;

// Initialize the API
api._init();

/**
 * Window Manager
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
api.WinManager = 
{
	params : {},
	opnwin : {},
	urlprm : {},
	
	Open : function (s, p, u)
	{
		var f = '', vp = api.getViewPort();
		s = s || {};
		p = p || {};
		u = u || {};
		
		// Opera uses windows inside the Opera window
		sw = api.isOpera ? vp.w : screen.width;
		sh = api.isOpera ? vp.h : screen.height;
		
		s.name = s.name || 'mc_' + new Date().getTime();
		s.width = parseInt(s.width || 320);
		s.height = parseInt(s.height || 240);
		s.resizable = true;
		s.left = s.left || parseInt(sw / 2.0) - (s.width / 2.0);
		s.top = s.top || parseInt(sh / 2.0) - (s.height / 2.0);
		
		// Build features string
		api.each(s, function(v, k) {
			if (typeof v == 'boolean') {
				v = v ? 'yes' : 'no';
			}
			if (!/^(name|url)$/.test(k)) {
				f += (f ? ',' : '') + k + '=' + v;
			}
		});
		
		// set params
		this.params = p;
		this.SplitUrlParams(u);
		
		// construct url
		var ext = (s.url || s.file).split('?');
		var url = (ext[0] || '') + '?';
		var pxt = '';
		if (typeof ext[1] != 'undefined') {
			pxt += ext[1];
		}
		api.each(this.urlprm, function(v, k) {
			if (typeof v == 'string' && v != '') {
				if (pxt != '') pxt += '&';
				pxt += k + '=' + v;
			}
		});
		url += pxt;
		
		try {
			this.opnwin = window.open(url, s.name, f);
		} catch (ex) { }
	},
	
	SplitUrlParams : function(p)
	{
		p = p || {};
		var v, k, uv, uk;
		api.each(p, function(v, k) {
			api.each(api.WinManager.urlprm, function(uv, uk) {
				if (uk != k || typeof v != 'string' || v == '') {
					v = uv;
					k = uk;
				}
			});
			api.WinManager.urlprm[k] = v;
		});
	},
	
	SetUrlParams : function (u)
	{
		this.SplitUrlParams((u || {}));
	}
}

/**
 * Object for counting
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
api.Counter =
{
	value : [],
	
	Reset : function (name)
	{
		this._create();
		this.value[name] = 0;
	},
	
	NewValue : function (name)
	{
		this._create();
		return this.value[name]++;
	},
	
	_create : function (name)
	{
		if (typeof this.value[name] == 'undefined') {
			this.value[name] = 0;
		}
	}
}

/**
 * Get elements by class name
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
api.Select = 
{
	Create : function (e)
	{
		if (typeof e != 'object' || e.tagName != 'SELECT') return;
		this.ShowClose(e);
		var d = '<div class="selectbox">';
		d += '<a href="#" class="select-front" onclick="return !api.Select.Open(this)">' + e.options[e.selectedIndex].text + '</a>';
		d += '<ul onmouseout="api.Select.Close(this)" onmousemove="api.Select.Show(this)" class="select-options" style="display:none">';
		for (var i = 0; i < e.options.length; i++)
		{
			d += '<li onclick="return !api.Select.Mark(this, api.e(\'' + e.id + '\'))" class="' + (e.options[i].selected ? ' selected' : '') + '" id="' + e.options[i].value + '">' + e.options[i].text + '</li>';
		}
		d += '</ul></div>';
		e.parentNode.innerHTML += d;
	},
	
	Open : function (e)
	{
		var uls = api.t('UL', e.parentNode);
		var ul = (uls.length > 0) ? uls[0] : null;
		if (typeof e == 'object' && e.tagName == 'A' && typeof ul == 'object' && ul.tagName == 'UL') {
			this.ShowClose(ul);
			if (!api.isIE) {
				ul.style.left = e.offsetLeft + 'px';
			}
		}
		// close all js select boxes
		api.each(api.c('select-options'), function (v, k) {
			if (v != ul) v.style.display = 'none';
		});
		return true;
	},
	
	Mark : function (e, a)
	{
		var as = api.t('A', e.parentNode.parentNode);
		var ah = (as.length > 0) ? as[0] : null;
		if (typeof ah == 'object' && ah.tagName == 'A') {
			ah.innerHTML = e.innerHTML;
			this.ShowClose(e.parentNode);
			api.each(e.parentNode.childNodes, function (v, k) {
				if (v.getAttribute('id') == e.getAttribute('id')) {
					v.className = 'selected';
					a.selectedIndex = k;
				} else {
					v.className = '';
				}
			});
		}
		return true;
	},
	
	ShowClose : function (e)
	{
		if (typeof e != 'object') return;
		e.style.display = (e.style.display == 'none') ? '' : 'none';
	},
	
	Close : function (e)
	{
		if (typeof e != 'object') return;
		e.style.display = 'none';
	},
	
	Show : function (e)
	{
		if (typeof e != 'object') return;
		e.style.display = '';
	}
}

/**
 * Get elements by class name
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
document.getElementsByClassName = function(cl)
{
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	var c = 0;
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) {
			retnode[c++] = elem[i];
		}
	}
	return retnode;
};

/**
 * Get element by tag id
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function $(e, target)
{
	if (typeof e == 'string') {
		if (typeof target != 'object') target = document;
		e = target.getElementById(e);
	}
	return e;
};

/**
 * Get elements by tag name
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function $$(e, target)
{
	if (typeof e == 'string') {
		if (typeof target != 'object') target = document;
		e = target.getElementsByTagName(e);
	}
	return e;
};

/**
 * Get elements by class name
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function $$$(e)
{
	if (typeof e == 'string') {
		e = document.getElementsByClassName(e);
	}
	return e;
};

/**
 * Check checkbox by class name
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function check(is)
{
	$$$(is).each(function(e){
		e.checked = (e.checked) ? false : true;
	});
	return true;
};

/**
 * Show or hide element
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function show(e)
{
	var el = api.e(e);
	if (el.style.display == 'block') {
		el.style.display = 'none';
	} else {
		el.style.display = 'block';
	}
	return true;
};

/**
 * Enable or disable elements
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function enable(elements)
{
	for (var i = 0; i < elements.length; i++) {
		if ($(elements[i]).disabled) {
			$(elements[i]).disabled = false;
		} else {
			$(elements[i]).disabled = true;
		}
	}
};

/**
 * Enable or disable elements
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function metaEnabled(elements, checking)
{
	enable(elements);
	for (var i = 0; i < checking.length; i++) {
		if ($('new_meta_data').checked) {
			$(checking[i]).disabled = $('new_meta_data').disabled;
		}
	}
};

/**
 * Object ArticleParam
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function ArticleParam()
{
	this.AddRow = function ()
	{
		$('article_params_container').innerHTML += $('article_param_row').innerHTML;
		var elements = $$$('nodisplay');
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].tagName != 'TR') continue;
			if (elements[i].getAttribute('className') != undefined) {
				elements[i].removeAttribute('className');
				continue;
			}
			if (elements[i].getAttribute('class') != undefined) {
				elements[i].removeAttribute('class');
				continue;
			}
		}
		return true;
	}
};

/**
 * Object Tabs creating clickable tab elements
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function Tabs ()
{
	this.Init = function ()
	{
		var e = $$('fieldset');
		var t = $('tc-content');
		var txt = '<div id="js_tabs_nav" class="top_navigation"><div class="tn_left"></div><ul>';
		for (var i = 0; i < e.length; i++) {
			if (!/js_tab/i.test(e[i].getAttribute('class'))) continue;
			var l = $$('legend', e[i]);
			var lt = '';
			if (typeof l[0] == 'object') {
				l[0].style.display = 'none';
				lt = l[0].textContent;
			} else {
				lt = 'N/A';
			}
			txt += '<li title="' + lt + '"><a href="#"' + (i == 0 ? ' class="active"' : '') + ' onclick="return !Tabs.ShowTab(' + i + ')">' + lt + '</a></li>';
			if (i > 0) {
				e[i].style.display = 'none';
			}
		}
		txt += '</ul><div class="tn_right"></div></div>';
		t.innerHTML = txt + t.innerHTML;
		return true;
	}
	
	this.ShowTab = function(id)
	{
		var e = $$('fieldset');
		var l = $$('a', $('js_tabs_nav'));
		for (var i = 0; i < e.length; i++) {
			e[i].style.display = (i == id) ? '' : 'none';
			l[i].className = (i == id) ? 'active' : '';
		}
		return true;
	}
};

/**
 * Object Tree working with recursive tree
 *
 * @author Miroslav Osko <nois@audionet.sk>
 */
function Tree ()
{
	// expand tree
	this.expand = function (node)
	{
		var e = $$('li');
		for (var i = 0; i < e.length; i++) {
			if (e[i].className == node) {
				var action = (e[i].style.display == 'none') ? 'open' : 'close';
				if (action == 'open') {
					e[i].removeAttribute('style');
				} else {
					e[i].style.display = 'none';
				}
			}
		}
		return this._expandlink(node);
	}
	
	//expand level link
	this._expandlink = function (node)
	{
		var e = $$('a');
		for (var i = 0; i < e.length; i++) {
			if (e[i].className.indexOf(node) != -1) {
				var parts = e[i].className.split(" ", 1);
				if (parts[0] != node) continue;
				e[i].className = node + ' oplink ' + ((e[i].className.indexOf('open') != -1) ? 'close' : 'open');
				break;
			}
		}
		return true;
	}
	
	// expand tree node from bottom level to top level
	this.expandNode = function (node)
	{
		var e = $$('li');
		for (var i = 0; i < e.length; i++) {
			if (e[i].className == node) {
				this._parent(e[i].parentNode);
				break;
			}
		}
	}
	
	// recursive function for expand tree level
	this._parent = function (element)
	{
		switch (element.tagName) {
			case "UL":
				var childs = element.childNodes;
				for (var i = 0; i < childs.length; i++) {
					if (childs[i].tagName == "LI") {
						if (childs[i].style.display == 'none') {
							this.expand(childs[i].className);
						}
						break;
					}
				}
			case "LI":
				this._parent(element.parentNode);
				break;
		}
	}
};
var Tree = new Tree();
var ArticleParam = new ArticleParam();
var Tabs = new Tabs();