// Vypis debug info do konzoly (ak mam extensions FireBug)
//
// vid: http://www.joehewitt.com/software/firebug/faq.php (jemne modifikonvane)
function debug() {
    if (document.createEvent) {
        printfire = {}
        printfire.args = arguments;

        var event = document.createEvent("Events");
        event.initEvent("printfire", false, true);

        dispatchEvent(event);
    }
}

// Najde vsetky formularove polikca s danym menom
Form.getFieldsByName = function(name) {
    // Takto to nefunguje, ked je name typu "foo[bar]":
    // return $$("form *[name=\"" + name + "\"]");

    return $$("input, textarea, select, button").select(function(element) {
        return element.name == name;
    });
}

// Hodnota skupiny radiobuttonov (cize hodnota toho, ktory je selectnuty).
// Parameter moze byt lubovolny radiobuton zo skupiny.
Form.Element.Serializers.radioGroup = function(element) {
    element = $(element);

    var checked = $A(element.form.elements).find(function(item) {
        return item.name == element.name && item.checked;
    });

    return [element.name, checked.value];
}

// HACK / PATCH aby sa callback zavolal aj pre checkboxy/radiobuttony
Abstract.EventObserver.prototype.onElementEvent = function() {
    var param = this.getValue();
    var value = param;

    switch (this.element.type.toLowerCase()) {
        case "radio":
            this.lastValue = !value;
            break;
        case "checkbox":
            value = this.element.checked;
            break;
    }

    if (this.lastValue != value) {
        this.callback(this.element, param);
        this.lastValue = value;
    }
}

var Busy = {
    start: function() {
        document.getElementsByTagName("BODY")[0].style.cursor = "wait";
    },

    stop: function() {
        document.getElementsByTagName("BODY")[0].style.cursor = "auto";
    }
}

var StatusMessage = {
    show: function(text) {
        var message = $("status_message");
        message.innerHTML = text;
        message.addClassName("active");

        // new Effect.Highlight(message);
    },

    hide: function() {
        $("status_message").removeClassName("active");
    }
}

var URL = {
    // Pridaj k url parametre.
    append: function(url, params) {
        return url + (url.include("?") ? "&" : "?") +  params;
    }
}

// pomocne funkcie pre nastavenie vnutra textu lebo v IE to funguje inak ;)
function getText(control) {
    if (document.all) {
        return control.innerText;
    } else {
        return control.textContent;
    }
}

function setText(control, value){
    if (document.all) {
        control.innerText = value;
    } else {
        control.textContent = value;
    }
}

// nepotrebne
//document.observe("dom:loaded", function(){
//    $$('a.obmail').each(function(obmail) {
//        obmail.cleanWhitespace();
//        email = getText(obmail);
//        email = email.toString().replace(' (a) ', '@');
//        href = obmail.getAttribute('href');
//        href = href.toString().replace(' (a) ', '@');
//        href = href.toString().replace('%20(a)%20', '@');
//        obmail.setAttribute('href', href);
//        setText(obmail, email);
//    });
//});
