/**
 * getStyle: get style properties
 **
 * Params:
 *		Element:			x			: element witch propertie i want
 *		String:				styleProp	: name of propertie
 **/
function getStyle(x,styleProp) {
	if (x.currentStyle)
		var y = x.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	return y;
}


/**
 * switch_hide: switch visibility of block
 **
 * Params:
 *		String:			id			: element id
 **/
function switch_hide(id) {
	var el = document.getElementById(id);
	if (getStyle(el, 'display') == 'none') {
		el.style.display = 'block';
	} else {
		el.style.display = 'none';
	}
	return false;
}

/**
 * show_hidde: show hidden block
 **
 * Params:
 *		String:			id			: element id
 **/
function show_hidde(id) {
	document.getElementById(id).style.display = 'block';
	return false;
}

/**
 * hidde_show: hidde block
 **
 * Params:
 *		String:			id			: element id
 **/
function hidde_show(id) {
	document.getElementById(id).style.display = 'none';
	return false;
}

/**
 * getPageScrollY: get vertical scroll position
 **/
function getPageScrollY() {
	if (window.pageYOffset) {
		return window.pageYOffset;
	} else if (document.body && document.body.scrollTop) {
		return document.body.scrollTop;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	}
	return 0;
}

/**
 * getPageScrollX: get horizontal scroll position
 **/
function getPageScrollX() {
	if (window.pageXOffset) {
		return window.pageXOffset;
	} else if (document.body && document.body.scrollLeft) {
		return document.body.scrollLeft;
	} else if (document.documentElement && document.documentElement.scrollLeft) {
		return document.documentElement.scrollLeft;
	}
	return 0;
}

/**
 * showToolTip: show tool tip
 **
 * Params:
 *		String:			e			: element by witch will be tool tip show
 *		String:			element		: element id of tooltip
 *		String:			text		: text of tooltip
 **/
function showToolTip(e, element, text) {
	if (text.length > 0) {
		var el = $(element);
		el.innerHTML = text;
		var oleft = otop = 0;
		if (e.getBoundingClientRect) {
			// IE
			var rect = e.getBoundingClientRect();
			oleft = rect.left + getPageScrollX() - 2;
			otop = rect.top + getPageScrollY() - 2;
		} else {
			oleft = e.offsetLeft;
			otop = e.offsetTop;
		}
		var tp = $(e).positionedOffset();
		el.setStyle({left:(tp.left + 20) + "px",top:(tp.top + 20) + "px",display:'block'});
	}
}

/**
 * hideToolTip: hidde tool tip
 **
 * Params:
 *		String:			element		: element id of tooltip
 **/
function hideToolTip(element) {
	$(element).hide();
}

/**
 * onElementReady: call callback function, when element render finish
 **
 * Params:
 *		String:			element		: element id
 *		Function:		callback	: callback function
 **/
function onElementReady(element,callback) {
		callback();return;
	if( element && (element.nextSibling || element.textContent) ) {	callback(); }
	else { setTimeout( onElementReady.bind(element,callback), 1 ); }
}

function isNumberKey(evt,limit) {
	if (evt && evt.keyCode) return true;
	if (evt && evt.charCode == 99 && evt.ctrlKey) return true;
	if (evt && evt.charCode == 118 && evt.ctrlKey) return true;
	if (evt && (evt.charCode == 46 || (evt.charCode>=48 && evt.charCode<=57))) {
		var val = evt.target.value;
		if (evt.charCode == 46 && val.indexOf('.')>0) return false;
		val = val + String.fromCharCode(evt.charCode);
		return limit?(val<=limit):!isNaN(val);
	}
	return false;
}

function isIntegerKey(evt,limit) {
	if (evt && evt.charCode == 99 && evt.ctrlKey) return true;
	if (evt && evt.charCode == 118 && evt.ctrlKey) return true;
	if (evt && evt.keyCode) return true;
	if (evt && evt.charCode>=48 && evt.charCode<=57) {
		var val = evt.target.value + String.fromCharCode(evt.charCode);
		return limit?(val<=limit):!isNaN(val);
	}
	return false;
}

function isNumber(elm, limit) {
	elm.value = elm.value.replace(/ /g, '');
	if (isNaN(elm.value) || (limit && elm.value>limit)) elm.value='';
}

function insertArray(arr, pos, item) {
	for (i=arr.length; i>pos; i--) {
		arr[i]=arr[i-1];
	}
	arr[pos]=item;
	return arr;
}

function findOption(sel, opt, from, to, reverse) {
	if (typeof(from) == 'undefined') from = 0;
	if (typeof(to) == 'undefined') to = sel.options.length
	if (!reverse) {
		for (var i=from; i<to; i++) {
			if (sel.options[i].value == opt) return i;
		}
	} else {
		for (var i=from; i>=to; i--) {
			if (sel.options[i].value == opt) return i;
		}
	}
	return -1;	
}

function moveItemsBetweenSelect(from, to) {
	from = $(from);
	to = $(to);
	var i = from.selectedIndex;
	var len = from.options.length;
	while (i < len && from.options[i]) {
		if (from.options[i].selected) {
			if (from.options[i].hasClassName('hform-option-group')) {
				var val = from.options[i].value;
				var io = findOption(to, val);
				val = val.replace('opt_', '');
				var j = -1;
				if (io == -1) {
					to.options[to.options.length] = new Element('option', { value: from.options[i].value, 'class': from.options[i].className }).update(from.options[i].text);
					j = to.options.length;
				} else j = io+1;
				if (j > -1) {
					from.options[i] = null;
					len--;
					while (i<len && from.options[i] && from.options[i].hasClassName(val)) {
						if (j != to.options.length)
							for (var a=to.options.length; a>j; a--) to.options[a] = new Element('option', { value: to.options[a-1].value, 'class': to.options[a-1].className }).update(to.options[a-1].text);
						to.options[j++] = new Element('option', { value: from.options[i].value, 'class': from.options[i].className }).update(from.options[i].text);
						from.options[i] = null;
						len--;
					}
				} else i++;
			} else {
				var op = from.options[i].classNames().grep(/^(in)|(out)_\d+$/);
				if (op.size()>0) {
					var val = op.min();
					var o = findOption(from, 'opt_'+val, i-1, 0, true);
					var j = -1;
					if (o > -1) {
						var io = findOption(to, 'opt_'+val);
						if (io == -1) {
							to.options[to.options.length] = new Element('option', { value: from.options[o].value, 'class': from.options[o].className }).update(from.options[o].text);
							j = to.options.length;
						} else j = io+1;
						if (j > -1) {
							var bol = (o == i-1);
							while(i<len && from.options[i]  && from.options[i].hasClassName(val)) {
								if (from.options[i].selected) {
									if (j != to.options.length)
										for (var a=to.options.length; a>j; a--) to.options[a] = new Element('option', { value: to.options[a-1].value, 'class': to.options[a-1].className }).update(to.options[a-1].text);
									to.options[j++] = new Element('option', { value: from.options[i].value, 'class': from.options[i].className }).update(from.options[i].text);
									from.options[i] = null;
									len--;
								} else {
									i++;
									bol = false;
								}
							}
							if (bol) {
								from.options[o] = null;
								len--;
							}
						} else i++;
					} else i++;
				} else i++;
			}
		} else i++;
	}
}


function moveItemsBetweenSelectSimple(from, to) {
	from = $(from);
	to = $(to);
	var i = 0;
	var len = from.options.length;
	while (i < len) {
		if (from.options[i].selected) {
			to.options[to.options.length] = new Element('option', { value: from.options[i].value, 'class': from.options[i].className }).update(from.options[i].text);
			from.options[i] = null;
			len--;
		} else i++;
	}

}

function setSubItem(e, n, mods, default_last) {
	var val = '';
	var num = 0;
	if (mods[0][0] == '' && (mods[0][1][0][0] == 0 || mods[0][1][0][0] == '')) num = 1;
	for (i=0; i < n.options.length; i++) {
		if (n.options[i].selected) {
			val = n.options[i].text;
			break;
		}
	}
	e.innerHTML = '';
	if (num) e.options[e.options.length] = new Option('', mods[0][1][0][0]);
	for(i = num; i < mods.length; i++) {
		if (mods[i][0] == val) {
			for(j = 0; j < mods[i][1].length; j++) {
				e.options[e.options.length] = new Option(mods[i][1][j][0], mods[i][1][j][1]);
			}
			if (default_last) e.options[e.options.length-1].selected=true;
			break;
		}
	}
}

