﻿
var Page = {
    Setup: function () {
        new Dropdown();
        var s = $('search');
        s.addEvent('focus', function () {
            if (s.get('value') == 'Search')
                s.set('value', '');
        });
        s.addEvent('blur', function () {
            if (s.get('value') == '')
                s.set('value', 'Search');
        });
    }
}

document.addEvent('domready', function () {
    Page.Setup();
});

var Slideshow = {
    setup: function () {
        $$('.images').each(function (x) {
            var curSlide = -1;
            var slideDuration = 5000;
            var slidePages = x.getElements('a');
            if (slidePages.length == 0)
                slidePages = x.getElements('img');

            slidePages.set('tween', { duration: '1000', onComplete: function (y) { if (y.get('opacity') == 0) y.addClass('hidden'); } });
            slidePages.addClass('hidden').setOpacity(0);

            var showSlide = function (i) {
                if (i != curSlide) {
                    var src;
                    if (slidePages[i].getElement('img') != null)
                        src = slidePages[i].getElement('img').get('src');
                    else
                        src = slidePages[i].get('src');
                    Asset.image(src, {
                        onLoad: function (a) {
                            if (slidePages[curSlide] != null)
                                slidePages[curSlide].fade(0);
                            curSlide = i;
                            slidePages[curSlide].setOpacity(0).removeClass('hidden').fade(1);
                        }
                    });


                }
            }
            var nextSlide = function () {
                var next = curSlide + 1;
                if (next >= slidePages.length)
                    next = 0;

                showSlide(next);
            }

            var i = 0;


            nextSlide();
            var period = nextSlide.periodical(slideDuration);

        });


    }
}

var Dropdown = new Class({
    initialize: function () {
        this.Dropdowns = $$('.dropdown a');
        var y = this.OpenMenu.bind(this);
        var x = this.StartClose.bind(this);
        this.Dropdowns.each(function (e) {
            e.addEvent('mouseover', y.pass(e));
            e.addEvent('mouseout', x.pass(e));
        })
    },
    OpenMenu: function (e) {
        $clear(this.timeout);

        var parent = e.getParent().getParent();
        parent.getElements('a').each(function (k) {

            if (k.hasClass('open') && k != e) {
                k.removeClass('open');
                k.getParent().getElements('ul').each(function (i) {
                    i.setStyle('display', 'none');
                });
            }
        }
                );

        if (!e.hasClass('open')) {
            e.addClass('open');


            if (e.getParent().getChildren().length > 1) {

                var children = e.getParent().getChildren()[1];
                children.setOpacity(0);
                children.setStyles({ 'display': 'block' });
                children.fade(1);

                
                var left, top;
                //console.log(e.getPosition().x);

                left = 0; // e.getPosition(parent).x + e.getSize().x + 1;
                top = e.getPosition(parent).y + e.getSize().y;
                children.setStyle('width',(e.getParent().getSize().x - 22) + 'px');


                children.setStyle('top', top + 'px');
                children.setStyle('left', left + 'px');

            }
        }
    },
    StartClose: function (e) {
        this.timeout = this.CloseMenu.delay(500);
    },
    CloseMenu: function () {
        $clear(this.timeout);

        $$('.dropdown ul').each(function (i) {
            i.fade(0);
            i.getParent().getElements('a').removeClass('open');
        });

    }
});
