var section = {
    init: function () {
        section.setColor('#c4002f', 1);

        structure.mainNavOverColor = '#FFFFFF';
        structure.mainNavOutColor = '#858585';

        $('.content').wrap($('<div/>').addClass('bg-white'));

        section.highlights.init();
        section.courses.init();

        var url = '/static/swf/teaser.swf?settings=/service/getbanner.aspx';
        var params = { allowScriptAccess: 'always', wmode: 'transparent', base: '/upload/arquivo' };
        var atts = { id: 'banner' };
        var vars = { settings: '/service/getbanner.aspx' };

        swfobject.embedSWF(url, "banner-holder", "700", "460", "10", null, vars, params, atts);
    },

    setColor: function () {
        var _color = arguments[0] == undefined ? '#ffffff' : arguments[0];
        var _time = arguments[1] == undefined ? 500 : arguments[1];
        $('body').animate({ backgroundColor: _color }, { duration: _time, easing: 'easeOutExpo' });
    },

    highlights: {
        current: null,
        currentWidth: null,
        currentLength: null,

        init: function () {
            trace('section : highlights : init');

            section.highlights.current = 0;
            section.highlights.currentWidth = 305;
            section.highlights.currentLength = $('.highlights ul li').length;

            section.highlights.structure();
            section.highlights.mountPagination();
            section.highlights.slideHighlights();
            section.highlights.rollovers.init();
            //section.highlights.timeline();
        },

        structure: function () {
            $('.content .highlights').append($('<div/>').addClass('pagination').append($('<div/>').addClass('inner').append('<ol/>')));

            //$('.highlights ul li').last().addClass('bgN');

            $('.highlights ul').wrap($('<div/>').addClass('mask'));
        },

        mountPagination: function () {
            trace(section.highlights.currentLength);

            if (section.highlights.currentLength > 3) {
                $.each($('.highlights ul li'), function (i, item) {
                    //$('.pagination ol').append($('<li/>').attr('rel', i).bind('click', section.highlights.clickPagination));
                });

                var _width = $('.pagination ol').width();
                $('.pagination .inner').width(_width);
            }
        },

        slideHighlights: function () {

            $.each($('.highlights ul li'), function (i, item) {
                var l = 300 * i;
                $(item).css('left', l.round());
            });

            var timeout;

            var intervalo = setInterval(function () {
                $('.highlights ul').append($('.highlights ul li:eq(0)').clone().css('left', 300 * $('.highlights ul li').length));
                $('.highlights ul li').animate({ left: '-=300px' }, { duration: 1500, easing: 'easeOutElastic', complete: function () {
                    section.highlights.rollovers.init();
                } });

                timeout = setTimeout(function () {
                    $('.highlights ul li:eq(0)').remove();
                }, 2000);
            }, 5000);
        },

        time: null,

        timeline: function () {
            clearInterval(section.highlights.time);
            section.highlights.time = setInterval(function () {
                section.highlights.current++;
                $('.pagination ol li').eq(section.highlights.current).trigger('click');
                if (section.highlights.current >= section.highlights.currentLength) {
                    $('.pagination ol li').eq(0).trigger('click')
                }
            }, 8000);

            trace(section.highlights.current)
        },

        clickPagination: function (e) {
            var _this = $(e.currentTarget);
            var _rel = _this.attr('rel');

            $('.pagination ol li').removeClass('on');
            _this.addClass('on');

            section.highlights.current = _rel;

            section.highlights.animate();

            clearInterval(section.highlights.time);
            section.highlights.timeline();
        },

        animate: function () {
            var _animate = section.highlights.current * section.highlights.currentWidth;

            $('.highlights ul').animate({ marginLeft: -_animate }, { duration: 500, queue: false, easing: 'easeOutQuint' });
        },

        rollovers: {
            init: function () {
                trace('section : highlights : rollovers : init');

                //$('.highlights ul li').append('<span/>')

                $('.highlights ul li.test').bind('mouseenter', section.highlights.rollovers.test.mEnter).bind('mouseleave', section.highlights.rollovers.test.mLeave);
                $('.highlights ul li.method').bind('mouseenter', section.highlights.rollovers.method.mEnter).bind('mouseleave', section.highlights.rollovers.method.mLeave);
                $('.highlights ul li.units').bind('mouseenter', section.highlights.rollovers.units.mEnter).bind('mouseleave', section.highlights.rollovers.units.mLeave);
                $('.highlights ul li.calendar').bind('mouseenter', section.highlights.rollovers.calendar.mEnter).bind('mouseleave', section.highlights.rollovers.calendar.mLeave);
            },

            test: {
                mEnter: function (e) {
                    var _this = $(e.currentTarget);
                    _this.find('div').find('img.fL').animate({ left: 0 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .delay(500).next('img.fR').animate({ right: 0, opacity: 1 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .next('img.shadow').animate({ width: 100, left: 0 }, { duration: 500, queue: false, easing: 'easeOutQuint' });
                },

                mLeave: function (e) {
                    var _this = $(e.currentTarget);
                    _this.find('div').find('img.fL').animate({ left: 30 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .delay(500).next('img.fR').animate({ right: 10, opacity: 0 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .next('img.shadow').animate({ width: 50, left: 25 }, { duration: 500, queue: false, easing: 'easeOutQuint' });
                }
            },

            calendar: {
                mEnter: function (e) {
                    var _this = $(e.currentTarget);
                    if (navigator.appName == 'Microsoft Internet Explorer') {
                        _this.find('.mark-calendar').show();
                    } else {
                        _this.find('.mark-calendar').fadeIn()
                    }
                },

                mLeave: function (e) {
                    var _this = $(e.currentTarget);
                    if (navigator.appName == 'Microsoft Internet Explorer') {
                        _this.find('.mark-calendar').hide();
                    } else {
                        _this.find('.mark-calendar').fadeOut();
                    }
                }
            },

            method: {
                mEnter: function (e) {
                    var _this = $(e.currentTarget);
                    _this.find('div').animate({ marginLeft: 25 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .find('img.z4').animate({ left: 0 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .next('img.z3').animate({ left: 35 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .next('img.z2').animate({ left: 70 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .next('img.z1').animate({ left: 105 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .next('img.shadow').animate({ width: 151, left: 15 }, { duration: 500, queue: false, easing: 'easeOutQuint' });
                },

                mLeave: function (e) {
                    var _this = $(e.currentTarget);
                    _this.find('div').animate({ marginLeft: 70 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .find('img').animate({ left: 0 }, { duration: 500, queue: false, easing: 'easeOutQuint' })
                    .next('img.shadow').animate({ width: 75, left: 0 }, { duration: 500, queue: false, easing: 'easeOutQuint' });
                }
            },

            units: {
                mEnter: function (e) {
                    var _this = $(e.currentTarget);
                    _this.find('img.bullet').animate({ top: 3, opacity: 1 }, { duration: 500, queue: false, easing: 'easeInOutQuint' });
                },

                mLeave: function (e) {
                    var _this = $(e.currentTarget);
                    _this.find('img.bullet').animate({ top: 3, opacity: 0 }, { duration: 500, queue: false, easing: 'easeInOutQuint', complete: function () { $(this).css({ 'top': '-30px' }) } });
                }
            }
        }
    },

    courses: {
        init: function () {
            $('.courses ul li').addClass('button');

            section.courses.structure();

            if (!Modernizr.csstransitions) {
                section.courses.menu.init();
            }
        },

        structure: function () {
            $('.button').css({ 'border-radius': '10px' });

            $.each($('.button'), function (i, item) {
                var _href = $(item).find('a').attr('href');

                trace(_href)

                $(item).css({ 'left': (165 * i) });
                $(item).wrap($('<a/>').attr('href', _href))
            });
        },

        menu: {
            init: function () {
                $.each($('.button'), function (i, item) {
                    $(item).css({ 'left': (165 * i) })
                });

                $('.button')
				    .bind('mouseenter', section.courses.menu.mnEnter)
				    .bind('mouseleave', section.courses.menu.mnLeave);

                $('.button.on').trigger('mouseenter');
            },

            mnEnter: function (e) {
                var _this = $(e.currentTarget);

                _this.animate({
                    backgroundColor: '#383838',
                    width: 213,
                    height: 313,
                    marginTop: -50,
                    marginLeft: -30
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).find('div').animate({
                    backgroundPosition: '0 0'
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).find('h2').animate({
                    color: '#FFFFFF',
                    fontSize: 32,
                    top: 15
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).next('img').animate({
                    bottom: -7,
                    left: -40
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).next('p').animate({
                    opacity: 1
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).next('a').animate({
                    backgroundPosition: '0 -5px'
                }, { duration: 800, queue: false, easing: 'easeOutQuint' });
            },

            mnLeave: function (e) {
                var _this = $(e.currentTarget)

                _this.animate({
                    backgroundColor: '#eee',
                    width: 155,
                    height: 230,
                    marginTop: 0,
                    marginLeft: 0
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).find('div').animate({
                    backgroundPosition: '-55px 0'
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).find('h2').animate({
                    color: '#ed1f52',
                    top: 15,
                    fontSize: '25'
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).next('img').animate({
                    bottom: -7,
                    left: 5
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).next('p').animate({
                    opacity: 0
                }, { duration: 800, queue: false, easing: 'easeOutQuint' }).next('a').animate({
                    backgroundPosition: '0 131px'
                }, { duration: 800, queue: false, easing: 'easeOutQuint' });
            }

        }
    }
};

$(document).ready(section.init);

