/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isObject(x)
{
    return typeof x == "object" && x !== null;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isNumber(x)
{
    return typeof x == "number";
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isArray(x)
{
    return isObject(x) && x.constructor == Array;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isBool(x)
{
    return typeof x == "boolean";
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isString(x)
{
    return typeof x == "string";
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isStringEmpty(x)
{
    return (typeof x == "string") && (x == "");
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isUndefined(x)
{
    return typeof x == "undefined";
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isFunction(x)
{
    return typeof x == "function";
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function getById(obj)
{
    if (!isObject(obj))
        if (!isObject(obj = document.getElementById(obj)))
            return false;

    return obj;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function serializeArray(aArray)
{
    var serialized = "";
    for (k in aArray)
        serialized += encodeURIComponent(aArray[k]) + "&";
    return serialized;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function unserializeArray(aString)
{
    if (!isString(aString)) return [];

    var elements = aString.split("&");

    for (k in elements)
        elements[k] = decodeURIComponent(elements[k]);

    return elements;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function toInt(num)
{
    if (isNumber(num))  return num;
    if (!isString(num)) return 0;

    var integerPattern = /^(-)?0*([0-9]*)[^0-9]*$/;
    var match = num.match(integerPattern);

    if (!match) return 0;

    return match[2] ? parseInt(match[2]) * (match[1] ? -1 : 1) : 0;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function toFloat(num)
{
    if (isNumber(num))  return num;
    if (!isString(num)) return 0;

    var integerPattern = /^(-)?0*([0-9]*)(\.|,)([0-9]*)[^0-9]*$/;
    var match = num.match(integerPattern);

    if (!match) return 0;

    return match[2] ? parseFloat(match[2]+"."+match[4]) * (match[1] ? -1 : 1) : 0.0;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function createCookie(name, value, days)
{
	if (days)
    {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
        var expires = "";

	document.cookie = name + "=" + value + expires + "; path=/";
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');

	for (var i = 0; i < ca.length; i++)
    {
		var c = ca[i];

		while (c.charAt(0) == ' ')
            c = c.substring(1, c.length);

		if (c.indexOf(nameEQ) == 0)
            return c.substring(nameEQ.length, c.length);
	}
	return null;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function eraseCookie(name)
{
	createCookie(name, "", -1);
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function getElementX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;// + (is_ie ? 12 : 0);
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function getElementY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;// + (is_ie ? 17 : 0);
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function getChildNodesList(aObj, aOutput, tag, recursive)
{
    if (!isObject(aObj)) return;
    if (!aObj.hasChildNodes()) return;

    var child = aObj.firstChild;
    tag = isString(tag) ? tag : "";

    do
    {
        if (tag == "" || (child.nodeType != 3 && child.tagName.toLowerCase() == tag))
            aOutput[aOutput.length] = child;
        if (child.hasChildNodes() && recursive == true)
            getChildNodesList(child, aOutput, tag, recursive);
        child = child.nextSibling;
    }
    while (child);
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isInArray(aArray, aNeedle)
{
    if (!isArray(aArray)) return false;
    for (key in aArray)
        if (aArray[key] === aNeedle)
            return true;
    return false;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function isMouseIn(obj)
{
    if (!isObject(obj))
        return false;

    oX = getElementX(obj);
    oY = getElementY(obj);
    oW = obj.offsetWidth;
    oH = obj.offsetHeight;
    if (mousePos.x <= (oX + oW) &&
        mousePos.x >= oX &&
        mousePos.y <= (oY + oH) &&
        mousePos.y >= oY)
    return true; else return false;
}

/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function bubbleEvent(aEvent, aBubble)
{
    try
    {
        if (!aEvent)
            if (!(aEvent = window.event)) return;

        aEvent.cancelBubble = !aBubble;

        if (aEvent.stopPropagation && !aBubble)
            aEvent.stopPropagation();
    }
    catch (e) {};
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function clearDocumentSelection()
{
    if (document.selection)
        if (document.selection.clear)
            document.selection.clear();

    if (window.getSelection)
    {
        if (window.getSelection().removeAllRanges)
            window.getSelection().removeAllRanges();
    }
    else
    {
        if (document.getSelection)
            if (document.getSelection().removeAllRanges)
                document.getSelection().removeAllRanges();
    }

    if (document.clearSelection)
        document.clearSelection();
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function centerWindow(wnd)
{
    wnd = $(wnd);

    if (!wnd) return;

    try
    {
        var x = Math.floor((getDocumentDmi().w  - wnd.offsetWidth)  / 2);
        var y = Math.floor((getDocumentDmi().h - wnd.offsetHeight) / 2);

        wnd.style.left = ((x < 0) ? 0 : x) + getDocumentScroll().x + "px";
        wnd.style.top  = ((y < 0) ? 0 : y) + getDocumentScroll().y  + "px";
    }
    catch (e) {}
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function getDocumentDmi()
{
    if (self.innerWidth)
    {
        return {
	        w : self.innerWidth,
	        h : self.innerHeight
        }
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
        return {
	        w : document.documentElement.clientWidth,
	        h : document.documentElement.clientHeight
        }
    }
    else if (document.body)
    {
        return {
	        w : document.body.clientWidth,
	        h : document.body.clientHeight
        }
    }

    return { w : 0, h : 0 }
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function getDocumentScroll()
{
    var scrOfX = 0, scrOfY = 0;

    if( typeof( window.pageYOffset ) == 'number' )
    {
        return {
            y : window.pageYOffset,
            x : window.pageXOffset
        }
    }
    else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
    {
        return {
            y : document.body.scrollTop,
            x : document.body.scrollLeft
        }
    }
    else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
    {
        return {
            y : document.documentElement.scrollTop,
            x : document.documentElement.scrollLeft
        }
    }

    return { x : 0, y : 0 }
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function encodeURLParam(param)
{
    if (param === null | isUndefined(param))
        return '';
    return encodeURIComponent(param);
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function getStyleProp(element, property)
{
	var el = $(element), value;

	if (el.currentStyle)
		var value = el.currentStyle[property];
	else if (window.getComputedStyle)
		var value = document.defaultView.getComputedStyle(el, null).getPropertyValue(property);

	return value;
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function htmlSpecialChars(aString)
{
    return (aString + "").replace('&','&amp;').replace('<', '&lt;').replace('>','&gt;');
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function hideElement(el)
{
    var obj = $(el);
    if (!obj) return;
    if (!obj.style) return
    obj.style.display = "none";
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function showElement(el, type)
{
    var obj = $(el);
    if (!obj) return;
    if (!obj.style) return
    obj.style.display = isUndefined(type) ? "block" : type;
}


var __atSign = '(malpa)', __dotSign = "(kropka)";
/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function decodeEmailLink(aElement)
{
    aElement = getById(aElement);
    try
    {
        aElement.innerHTML = aElement.innerHTML.replace(__atSign, '@').replace(__dotSign, '.');
        aElement.href = aElement.href.replace(encodeURIComponent(__atSign), '@').replace(encodeURIComponent(__dotSign), '.');
    }
    catch (e) {}
}


/*-------------------------------------------------------------------------------------*/
/**
 *
 */
function decodeEmailLinks()
{
    var links = document.body.getElementsByTagName("a");
    try
    {
        for (i in links)
            if (links[i].href && links[i].href.substring(0, 7) == "mailto:")
                decodeEmailLink(links[i]);
    }
    catch (e) {}
}
