﻿window.addEvent('domready', function() { Page.setup(); });

var Page = {
    Animating: false,
    setup: function() {

        Page.DropDown = new Dropdown();


        var left = 0;
        $$('.tab_page').each(function(e) {
            e.setStyle('left', left + 'px');
            left += e.getSize().x + 1;
        });

        if ($$('.tab_content').length > 0) {
            /*Page.Scroll = new Fx.Scroll($$('.tab_content')[0], {
            duration: 1000, transition: Fx.Transitions.Sine.easeOut, onStart: function() { Page.Animating = true; }, onComplete: function() { Page.Animating = false; }
            });*/
        }



        $$('.tab_nav a').addEvent('click', function(e) {
            $clear(tmr);
            if (!Page.Animating) {
                var el = e.target;
                var current = 0;
                var id = 0;
                $$('.tab_nav a').each(function(f) {
                    if (f == el) {
                        current = id;
                        f.addClass('tab_selected');
                    }
                    else {
                        f.removeClass('tab_selected');
                    }
                    id++;
                });
                var id = 0;
                $$('.tab_content .tab_page').each(function(x) {
                    if (id == current) {
                        $$('.tab_content_inner').tween('left', -(816 * current));
                      //  Page.Scroll.toElement(x);
                    }
                    id++;
                });

            }
            e.stop();
        });

        var current = 0;
        var tmr;
        var fn = function(id) {
            current = current + 1;
            if (current > 5)
                current = 0;
            var id = 0;
            $$('.tab_nav a').each(function(x) {
                if (id == current)
                    x.addClass('tab_selected');
                else
                    x.removeClass('tab_selected');
                id++;
            });
            id = 0;
            $$('.tab_content .tab_page').each(function(x) {
            if (id == current) {
                $$('.tab_content_inner').tween('left', -(816 * current));
                   // Page.Scroll.toElement(x);
                }
                id++;
            });

            tmr = fn.delay(10000);
        };
        tmr = fn.delay(10000);



        $$('.tab_images img,.images img').set('tween', { duration: 2000 });

        $$('.tab_images,.images').each(function(x) {
            var images = x.getElements('img');
            images.setOpacity(0);
            x.setStyle('visibility', 'visible');
            var img = x.getElements('img');
            if (img.length > 0) {
                images[0].fade(1);
                images.currentImage = 0;
                if (x.getElements('img').length > 1) {
                    Page.nextImage.periodical(8000, images);
                }
            }
        });

        if ($('email') != null) {

            $('email').addEvent('focus', function() {
                if ($('email').value == 'Email Address')
                    $('email').value = '';
            });
            $('email').addEvent('blur', function() {
                if ($('email').value == '')
                    $('email').value = 'Email Address';
            });


            $('email_form').addEvent('submit', function(e) {
                e.stop();
                var el = new Element(e.target);
                var req = new Request({
                    'url': el.get('action'),
                    onComplete: Page.EmailDone,
                    onFailure: Page.EmailFailed
                });
                req.send($('email_form').toQueryString());
                $('email_form').fade(0.5);

            });
        }
    },
    EmailDone: function(r) {
        if (r == "OK") {
            $('email_intro').set('html', '<p>Thank you for subscribing to our email newsletter. We will be in touch shortly.</p>');
            $('email_form').fade(0);
            $('emailinfo').set('tween', { onComplete: Page.HideEmail });
            $('emailinfo').fade.delay(3000, $('emailinfo'), 0);
        }
        else {
            $('email_intro').set('html', '<p>' + r + '<br /></p>');
            $('email_form').fade(1);
        }
    },
    HideEmail: function() {

        var myEffect = new Fx.Morph('emailinfo', { duration: 'long', transition: Fx.Transitions.Sine.easeOut, onComplete: function() { } });

        myEffect.start({
            'height': 0,
            'margin-bottom': 0
        });
    },
    EmailFailed: function(r) {
        alert(r.responseText);
    },
    nextImage: function() {
        this[this.currentImage].fade(0);
        this.currentImage++;
        if (this.currentImage >= this.length)
            this.currentImage = 0;
        this[this.currentImage].fade(1);
    }
}   
