Object.extend(Prototype.Browser, {
    IE6:     Prototype.Browser.IE && (typeof window.XMLHttpRequest == "undefined"),
    IE7:     Prototype.Browser.IE && (typeof window.XMLHttpRequest == "object")
});


function toggleContact(event) {
    Event.stop(event);
    var trigger = this;
    var target  = $('contact');
    new Effect.toggle(target, 'slide', {
        afterFinish: function() {
            trigger.toggleClassName('active');
            if (trigger.hasClassName('active')) {
                target.down('input').focus();
            } else {
                trigger.blur();
            }
        }
    });
}

function checkFormFields() {
    var fields  = $$('#contact input[type=text], #contact textarea');
    var myForm  = $('contact').down('form');
    fields.invoke('observe', 'blur', checkEmpty);
    myForm.observe('submit', validateForm.bindAsEventListener(this, fields, myForm));
}

function checkEmpty(event) {
    var label = this.previous('label');

    if (this.value != '' && !label.hasClassName('hint')) return;
    if (this.value == '' && !label.hasClassName('hint')) {
        var tippEl = new Element('span').update('Das ist ein Pflichtfeld...');
        label.addClassName('hint').insert(tippEl);
    } else if (this.value != '' && label.hasClassName('hint')) {
        label.removeClassName('hint');
        label.down('span').remove();
    }
}

function validateForm(e, fields, myForm) {
    Event.stop(e);
    var errors = [];

    fields.each(function(field) {
        // Spam field must stay empty
        if (field.hasClassName('required')) {return;}
        var label = field.previous('label');
        if (!label.hasClassName('hint')) {
            if (field.value == '') {
                var tippEl = new Element('span').update('Das ist ein Pflichtfeld...');
                label.addClassName('hint').insert(tippEl);
                errors.push(field);
            }
        } else {
            errors.push(field);
        }
    });
    // No errors, then submit
    if (errors.length == 0) {
        myForm.submit();
    }
}

function fadeNotification() {
    var notify = $('contact').down('.notify');
    new Effect.Fade(notify, {duration:2});
    new Effect.BlindUp(notify, {
        duration:2,
        afterFinish: function() {
            notify.remove();
        }
    });
}

function IE_warning() {
    var msg = new Element('div', {id: 'ie-warning'});
    Element.insert($('wrapper'), msg);
    new Ajax.Updater($('ie-warning'), 'ie_warning.html', {
        method: 'get',
        onComplete: hideThis.delay(3)
    });
}
function hideThis() {
    var target  = $('ie-warning');
    var trigger = $('hideThis');

    trigger.observe('click', function() {
        new Effect.Fade(target, {
            duration:2,
            afterFinish: function() {
                target.remove();
            }
        });
    });
}

function init() {
    if (!$('contact').hasClassName('stay')) {
        $('contact').hide();
    }
    if ($$('.notify').length >= 1) {
        fadeNotification.delay(8);
    }
    $('contact-trigger').observe('click', toggleContact);
    checkFormFields();
    if (Prototype.Browser.IE) {
        IE_warning();
    }
}

document.observe('dom:loaded', init);
