﻿window.addEvent('load', function() {

    if ($('page-middle').getSize().y > $('nav').getSize().y)
        $('nav').setStyle('height', $('page-middle').getSize().y - 12);
});

window.addEvent('domready', function() {
    $$('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.');
            }
        });
    });

    $$('li a.expand').each(function(el) {
        el.addEvent('click', function(e) {
            var subcat = $('sub-' + this.getProperty('rel'));
            subcat.setStyle('display', subcat.getStyle('display') == 'block' ? 'none' : 'block');
        });
    });

    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); });
    });

    $$('img.product-img-thumb').addEvent('error', function() {
        this.src = '/content/image/product/thumb/no-image.gif';
    });
    $$('img.product-img-standard').addEvent('error', function() {
        this.src = '/content/image/product/no-image.gif';
    });


    SqueezeBox.parsers.swf = function(preset) {
        return (preset || this.url.test(/\.swf/)) ? this.url : false;
    };

    SqueezeBox.handlers.swf = function(url) {
        var size = this.options.size;
        return new Swiff(url, {
            id: 'sbox-swf',
            width: size.x,
            height: size.y
        });
    };

    SqueezeBox.assign($$('a[rel=video]'), {
        size: { x: 660, y: 380 },
        ajaxOptions: {
            method: 'get'
        }
    });


    SqueezeBox.assign($$('a[rel=ajax]'), {
        size: { x: 550, y: 420 },
        ajaxOptions: {
            method: 'get'
        }
    });

    SqueezeBox.assign($$('a[rel=image-zoom]'));

    SqueezeBox.assign($$('a.video, a.boxed'), {
        parse: 'rel'
    });

    new OverText($('keyword'));

});



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');
    }    
}



