﻿window.addEvent('load', function() {
    if ($defined($('emailsignup-address')))
        new OverText($('emailsignup-address'));
});

window.addEvent('domready', function() {
    
    $$('.navigation li a').each(function(el) {
        var fx = new Fx.Tween(el, { duration: 200, link: 'cancel' });
        el.store('prop', el.getParents('ul')[0].id.contains('splash') ? 'padding-right' : 'padding-left');
        el.addEvents({
            'mouseenter': function() { fx.start(el.retrieve('prop'), 3); },
            'mouseleave': function() { fx.start(el.retrieve('prop'), 0); }
        });
    });


    $$('img.rollover', 'img.button', 'input.button').each(function(el) {
        el.addEvents({
            'mouseover': function() {
                this.src = this.src.replace('-0.', '-1.');
            },
            'mouseout': function() {
                if (!this.hasClass('selected'))
                    this.src = this.src.replace('-1.', '-0.');
            }
        });
    });

    if ($defined($('submit-button'))) {
        $('submit-button').addEvent('click', function() {
            return validateForm();
        });
    }

    $$('div.timedpopout').each(function(el) {
        var fadeFunction = function() {
            $(el).fade('out').get('tween').chain(
                function() {
                    (el).morph({ height: 0 }).get('morph').chain(
                        function() { $(el).destroy(); }
                    );
                }
            );
        }
        fadeFunction.delay(4000);
    });

    $$('input.required', 'textarea.required').each(function(el) {
        el.addEvent('blur', function() { highliteField(this); });
    });


    SqueezeBox.assign($$('a[rel=video]'), {
        size: { x: 660, y: 380 },
        ajaxOptions: {
            method: 'get'
        }
    });  


    SqueezeBox.assign($$('a[rel=ajax]'), {
        size: { x: 380, y: 420 },
        ajaxOptions: {
            method: 'get'
        }
    });

    SqueezeBox.assign($$('a[rel=image-zoom]'));
});



function validateForm() {
    var bValid = true;

    $$('input.required', 'textarea.required').each(function(el) {
        if (el.value.length == 0)
            bValid = false;

        highliteField(el);
    });


    if (!bValid)
        alert('Please make sure you fill in all required fields.');
        
    return bValid;
}

function highliteField(el) {
    if (el.value.length == 0) {
        $(el).addClass('input-error');
    } else {
        $(el).removeClass('input-error');
    }    
}



