﻿// -- general scripts --
function getEl(id) {
    return document.getElementById(id);
}

function addCSSClass(cssClass, obj) {
    if ((obj == null) || (typeof (obj) == 'undefined'))
        return false;

    if ((' ' + obj.className + ' ').indexOf(' ' + cssClass + ' ') == -1)
        obj.className += ' ' + cssClass;
}

function removeCSSClass(cssClass, obj) {
    if ((obj == null) || (typeof (obj) == 'undefined'))
        return false;

    if ((' ' + obj.className + ' ').indexOf(' ' + cssClass + ' ') != -1)
        obj.className = (' ' + obj.className + ' ').replace(new RegExp(' ' + cssClass + ' ', "g"), '');
}

function setCSSClass(cssClass, obj) {
    if ((obj == null) || (typeof (obj) == 'undefined'))
        return false;

    obj.className = cssClass;
}

function hasCSSClass(cssClass, obj) {
    if ((obj == null) || (typeof (obj) == 'undefined'))
        return false;

    return ((' ' + obj.className + ' ').indexOf(' ' + cssClass + ' ') >= 0);
}

function removeClassInContainer(cssClass, tagName, containerObj) {
    var tagList = containerObj.getElementsByTagName(tagName);
    for (var i = 0; i < tagList.length; i++)
        if (tagList[i].className.indexOf(cssClass) != -1)
        removeCSSClass(cssClass, tagList[i]);
}

function checkBrowser(browser) {
    return (navigator.userAgent.indexOf(browser) != -1);
}

function setBackgroundImage(obj, url) {
    if ((obj == null) || (typeof (obj) == 'undefined'))
        return false;

    obj.style.backgroundImage = 'url(' + url + ')';
}

function getBackgroundPositionX(obj) {
    if (checkBrowser('MSIE')) return getStyle(obj, 'background-position-x');
    else return getStyle(obj, 'background-position').split(' ')[0];
}

function getBackgroundPositionY(obj) {
    if (checkBrowser('MSIE')) return getStyle(obj, 'background-position-y');
    else return getStyle(obj, 'background-position').split(' ')[1];
}

function getStyle(oElm, strCssRule) { /* TODO: review; rewrite */
    var strValue = "";
    if (document.defaultView && document.defaultView.getComputedStyle) {
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if (oElm.currentStyle) {
        strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

function getXPos(obj) {
    leftGes = obj.offsetLeft;
    while (obj.offsetParent && hasPosition(obj)) {
        obj = obj.offsetParent;
        leftGes = leftGes + obj.offsetLeft;
    }
    return parseInt(leftGes);
}

function getYPos(obj) {
    topGes = obj.offsetTop;
    while (obj.offsetParent && hasPosition(obj)) {
        obj = obj.offsetParent;
        topGes = topGes + obj.offsetTop;
    }
    return parseInt(topGes);
}

function hasPosition(obj) {
    return ((getStyle(obj.offsetParent, 'position') != 'relative') && (getStyle(obj.offsetParent, 'position') != 'absolute')) ? true : false;
}
// -- // general scripts --

