﻿/*
* ------------------------------------------------------------------------------------------------
* @author 	Leandro Mancini leandro@neotix.com
* ------------------------------------------------------------------------------------------------
*/

var section = {
    init: function () {
        trace('section : init');

        section.structure();
        section.banner.init();
        section.highlight.init();
    },

    structure: function () {
        $('#home .ventures ul li').last().css({ 'margin': '0' });
    },

    banner: {
        time: null,
        current: null,

        init: function () {
            section.banner.mount();
            section.banner.current = 0;
        },

        mount: function () {
            $('#home .banner').append($('<div/>').addClass('pagination').append($('<div/>').addClass('inner').append('<ol/>')));

            $.each($('#home .banner ul li'), function (i, item) {
                var _src = $(item).find('img').attr('src');
                $(item).css({ 'background-image': 'url(' + _src + ')' });

                $(item).find('dl').wrap($('<div/>').addClass('inner'));

                $('#home .banner .pagination ol').append($('<li/>').attr('rel', i).bind('click', section.banner.click));
            });

            $('#home .banner .pagination ol li').eq(0).trigger('click');
        },

        timeout: function () {
            section.banner.time = setInterval(function () {
                $('#home .banner .pagination ol li').eq(section.banner.current).trigger('click');

                section.banner.current++;

                if (section.banner.current == $('#home .banner .pagination ol li').length) section.banner.current = 0;

            }, 12000);
        },

        click: function (e) {
            var _this = $(e.currentTarget);
            var _rel = _this.attr('rel');

            section.banner.current = _rel;

            if (!_this.hasClass('on')) {
                $('#home .banner .pagination ol li').removeClass('on');
                _this.addClass('on');

                $('#home .banner ul li').fadeOut();
                $('#home .banner ul li').eq(section.banner.current).delay(500).fadeIn();

                clearInterval(section.banner.time);
                section.banner.timeout();
            }
        }
    },

    highlight: {
        current: null,
        currentLength: null,

        init: function () {
            trace('highlight : init');

            section.highlight.current = 0;
            section.highlight.currentLength = $('#home .highlight ul li').length;

            section.highlight.mount();
        },

        mount: function () {
            $('#home .highlight .inner')
                .append($('<a href="javascript:void(0);"/>').addClass('bt-prev').bind('click', section.highlight.clickPrev))
                .append($('<a href="javascript:void(0);"/>').addClass('bt-next').bind('click', section.highlight.clickNext));

            $('#home .highlight ul li').eq(0).show();
        },

        clickPrev: function (e) {
            if (section.highlight.current <= 0) section.highlight.current = section.highlight.currentLength;

            section.highlight.current--;
            section.highlight.animate();
        },

        clickNext: function (e) {
            section.highlight.current++;

            if (section.highlight.current == section.highlight.currentLength) section.highlight.current = 0;

            section.highlight.animate();
        },

        animate: function () {
            var _duration = 500;

            trace(section.highlight.current)

            if (navigator.appName == 'Microsoft Internet Explorer') {
                $('#home .highlight ul li').hide();
                $('#home .highlight ul li').eq(section.highlight.current).show();
            } else {
                $('#home .highlight ul li').find('img').animate({ bottom: 45, opacity: 0 }, { duration: _duration, queue: false, easing: 'easeOutExpo', complete: function () {
                    $(this).hide();
                    $(this).parent().parent().hide();
                    $('#home .highlight ul li').eq(section.highlight.current).show().find('img').show().animate({ bottom: 0, opacity: 1 }, { duration: _duration, queue: false, easing: 'easeOutExpo' });
                }
                });

                $('#home .highlight ul li').find('dl').animate({ right: 50, opacity: 0 }, { duration: _duration, queue: false, easing: 'easeOutExpo', complete: function () {
                    $(this).hide();
                    $(this).parent().parent().hide();
                    $('#home .highlight ul li').eq(section.highlight.current).show().find('dl').show().animate({ right: 100, opacity: 1 }, { duration: _duration, queue: false, easing: 'easeOutExpo' });
                }
                });
            }

        }
    }
};

$(document).ready(section.init);
