﻿var HomeMooSliderPosition = 0;
var HomeMooSliderImageDisplay = 0;

window.addEvent('domready', function () {
    ProductScroll();
    AutoSlide();

    $$('#tabs-featured').getFirst().addClass("tabs-featured-selected");

    if (Browser.Engine.trident) {
        $$('.button-b a span').setStyle('margin-top', '1px');
    }

    $("movenexthome").addEvent("click", function (e) {
        //alert('dfg');
        HomeMooMoveNext();
    });
});

function HomeMooMoveNext() {
    if (HomeMooSliderImageDisplay == 2) {
        // Slide pannel and reset image display
        //   HomeMooSliderImageDisplay = 0;

        if (HomeMooSliderPosition == 5) {

            HomeMooSliderPosition = 0;
        }
        else {
            HomeMooSliderPosition++;
            $$('.featured-display-right').setStyle('display', 'none');

        }

        $("tabs-featured").getChildren()[HomeMooSliderPosition].fireEvent("click");

    }
    else {
        // Shuffle image along
        HomeMooSliderImageDisplay++;
    }


    //    var thumbs = $$(".scroll-item")[HomeMooSliderPosition].getElements('.smallThumbMenu ul li');
    //    thumbs[HomeMooSliderImageDisplay].fireEvent("click");

}


function ProductScroll() {
    var HomeMooMorpher = new Fx.Morph($('featured-product-scroller'), { duration: 400, transition: 'expo:out' });
    var ContentWidth = 937;
    var SubMenu = $$('ul.tabs-featured li');
    SubMenu.each(function (el, i, a) {
        el.addEvents({ click: function () {
            SubMenu.each(function (el2, i2, a2) {
                el2.removeClass('tabs-featured-selected');
            });

            HomeMooMorpher.cancel().start({ 'left': -ContentWidth * i });
            el.addClass('tabs-featured-selected');
        }
        });
    });

    //fading images

    var imageFadeContainers = $$('.scroll-item');
    imageFadeContainers.each(function (element, index, array) {
        var thumbs = element.getElements('.smallThumbMenu ul li');
        //alert(thumbs);
        thumbs.each(function (e, i, a) {
            e.addEvent("mouseenter", function () {

                var partialControlContainer = getScrollItemContainer(e);
                var featuredContainer = getFeaturedRightContainer(partialControlContainer);
                featuredContainer.getChildren('img').each(function (e2, i2, a2) {
                    if (i == i2) {
                        e2.getParent().morph({ 'display': 'block' });
                        e2.morph({ 'opacity': '1' });
                    }

                    else {
                        e2.morph({ 'opacity': '0' });
                    }
                });
            });
        });
    });

    var ThumbMouseOut = $$('.featured-product');
    ThumbMouseOut.each(function (el, i, a) {
        el.addEvents({ 'mouseenter': function () {
        },
            'mouseleave': function () {
                $$('.featured-display-right').setStyle('display', 'none');
            }
        });
    });



}

function getScrollItemContainer(element) {

    while (!element.hasClass('scroll-item') && element != document.body) {
        element = element.getParent();

    }
    return element;
}

function getFeaturedRightContainer(parent) {
    return parent.getFirst('.featured-display-right');
}


function AutoSlide() {

    var periodicalID;
    var begin = function () {
        periodicalID = (function () {
            $('movenexthome').fireEvent('click');
        }).periodical(5000);
    }
    begin();

    if ($('HomeMooScreen')) {
        $('HomeMooScreen').addEvents({
            mouseenter: function () {
                $clear(periodicalID);
            },
            mouseleave: begin
        });
    }
}

