﻿var rotator = {
    container: {},
    slides: {},
    menuitems: {},
    curridx: 0,
    total: 0,
    timer: null,
    timerlength: 10000,
    init: function() {
        rotator.container = $('#home-banners');
        rotator.slides = rotator.container.children('a');
        rotator.total = rotator.slides.length;

        if (rotator.total > 1) {
            var m = '<ul><li class="dir"><a class="prev" href="javascript:void(0)">previous</a></li>';
            $('#home-banners').children('a').each(function(i) {
                m += '<li><a href="javascript:void(0)">' + (i + 1) + '</a></li>';
            });
            m += '<li class="dir"><a class="next" href="javascript:void(0)">next</a></li></ul>';

            rotator.container.append(m);
            rotator.menuitems = rotator.container.children('ul').children('li:not(.dir)');

            rotator.menuitems.each(function(i) {
                var $this = $(this);
                $this.children('a').bind('click', function() {
                    rotator.curridx = $this.index() - 1;
                    rotator.gotoslide();
                });
            });
            rotator.container.find('a.prev').bind('click', function() { if (!$(this).parent().hasClass('inactive')) rotator.decrement() });
            rotator.container.find('a.next').bind('click', function() { if (!$(this).parent().hasClass('inactive')) rotator.increment() });

            rotator.gotoslide();
            rotator.starttimer();
        } else {
            rotator.setslide(rotator.curridx);
        }
    },
    starttimer: function() {
        clearTimeout(rotator.timer);
        rotator.timer = setTimeout(rotator.increment, rotator.timerlength);
    },
    increment: function() {
        rotator.curridx++;

        if (rotator.curridx >= rotator.total)
            rotator.curridx = 0;

        rotator.gotoslide();
    },
    decrement: function() {
        rotator.curridx--;
        if (rotator.curridx < 0)
            rotator.curridx = 0;

        rotator.gotoslide();
    },
    gotoslide: function() {
        rotator.setmenu(rotator.curridx);
        rotator.setslide(rotator.curridx);

        rotator.starttimer();
    },
    setmenu: function(idx) {
        rotator.menuitems.removeClass('curr');
        rotator.menuitems.eq(idx).addClass('curr');

        rotator.container.children('ul').children('li.dir').removeClass('inactive');
        if (idx == 0)
            rotator.container.children('ul').children('li.dir:first').addClass('inactive');
        if (idx == (rotator.total - 1))
            rotator.container.children('ul').children('li.dir:last').addClass('inactive');
    },
    setslide: function(idx) {
        rotator.slides.not(':eq(' + idx + ')').css('z-index',1).stop(true).animate({ opacity: 0 }, 'normal');
        rotator.slides.eq(idx).css('z-index',10).animate({ opacity: 1 }, 'normal');
    }
};

$(document).ready(rotator.init);
