/*
 * Common functions.
 * History:
 * 2010-09-30: function copyAttributes is added as part of #317 'Change of booking process' implementation.
 * 2010-11-19: functions isIE, isIE6, isFF, pxToInt and intToPx are added as part of #399 'Offer on demand' implementation.
 * 2011-01-10: map is added, pxToInt and toPx are made prototype functions.
 * 2011-02-24: function isSafari is added as part of #572 'Offer on demand does not work' implementation.
 * 2011-03-22: Function fireEvent is added as part of #523 'New court result list'.
 * 2011-05-03: Function swapClasses is added as part #648 'Result list: Collapseable categories'.
 * 2011-05-24: Function isOpera() is added as part #676 'Results list: Spacing and color improvements'.
 *
 * @author Artyom Dmitriev, Softage LLC.
 */

function openPopup (url, attributes) {
	popup = window.open(url, "fenster1", attributes);
	popup.focus();
}

function setValueForInput(elementId, value) {
    $$('input').each(function(item) {
        var id = item.identify();
        if (id.indexOf(elementId) > 0) {
            item.setValue(value);
        }
    });
}

/**
 * Copies specified attributes from one element to another.
 * @param elementFrom the element whose attributes should be copied
 * @param elementTo the element to copy the attributes to
 * @param attrs an array containing the names of the attributes
 */
function copyAttributes(elementFrom, elementTo, attrs) {
    attrs.each(function(attrName) {
        elementTo.setAttribute(attrName, elementFrom.getAttribute(attrName));
    });
}

/**
 * Determines if the current browser is Internet Explorer.
 */
function isIE() {
    return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

/**
 * Determines if the current browser is Internet Explorer 6.
 */
function isIE6() {
    return /\bMSIE 6/.test(navigator.userAgent) && !window.opera;
}

/**
 * Determines if the current browser is Mozilla Firefox.
 */
function isFF() {
    return /firefox/i.test(navigator.userAgent);
}

/**
 * Determines if the current browser is Safari.
 */
function isSafari() {
    return /safari/i.test(navigator.userAgent);
}

/**
 * Determines if the current browser is Opera.
 */
function isOpera() {
    return /opera/i.test(navigator.userAgent);
}

/**
 * Fires event evt on component obj
 * @param obj the component to fire the event on
 * @param evt the event
 */
function fireEvent(obj, evt) {
    if (document.createEvent) {
        var evObj = document.createEvent('MouseEvents');
        evObj.initEvent(evt, true, false);
        obj.dispatchEvent(evObj);
    } else if (document.createEventObject) {
        obj.fireEvent('on' + evt);
    }
}

/**
 * If the given element has class class1, it will be changed
 * to class2; otherwise if the given element has class class2,
 * it will be changed to class1.
 * @param element the DOM element to apply class the swap to
 * @param class1 the style class to swap with class2
 * @param class2 the style class to swap with class1
 */
function swapClasses(element, class1, class2) {
    var classToRemove;
    var classToAdd;
    if (element.hasClassName(class1)) {
        classToRemove = class1;
        classToAdd = class2;
    } else if (element.hasClassName(class2)) {
        classToRemove = class2;
        classToAdd = class1;
    }

    element.removeClassName(classToRemove).addClassName(classToAdd);
}

String.prototype.pxToInt = function() {
	if (this.match(/^-?[0-9]+px$/i)) {
        return parseInt(this.substring(0, this.length - 2), 10);
	}

    throw {
        name: 'IllegalStateException',
        message: 'pxToInt cannot be applied to ' + this
    };
};

Number.prototype.toPx = function() {
    return this + 'px';
};

/**
 * A map emulation.
 */
map = function(){
    var size = 0;
    return {
        put: function(key, value) {
            if (typeof this[key] === 'undefined') {
                size++;
            }
            this[key] = value;
        },
        remove: function(key) {
            if (typeof this[key] !== 'undefined') {
                size--;
            }
            delete this[key];
        },
        isEmpty: function() {
            return size === 0;
        },
		getSize: function() {
			return size;
		}
    };
};
