﻿/**
* @Date 10 - 01 - 2011
* @author Wesley van der Meer, Working Spirit ICT
* @Description Banner animation.
*/

window.Isential = (function ($) {

    /**
    * http://alternateidea.com/blog/articles/2007/7/18/javascript-scope-and-binding
    */
    Function.prototype.bind = function (scope) {
        var method = this;
        return function () {
            return method.apply(scope, arguments);
        };
    };

    var Isential = {
        init: function () {
            this.Banner.init();
        }
    };
    Isential.Banner = {
        init: function () {
            $this = (this);
            /**
            * HTML Wrapper
            */
            $this.wrapper = $('#brandBoxContainer');
            /**
            * Root of banner
            */
            $this.root = $("#brandBox");
            $this.items = $(".slide", this.root);
            $this.count = this.items.length - 1;
            $this.pagingReel = $('#bannerPager', this.wrapper);
            $this.aPaging = new Array();
            $this.index = 0;
            $this.descriptionWrapper = $("div#description > div.text", this.wrapper);
            $this.initiated = 0;
            sHtml = "";
            for (i = 0; i <= this.count; i++) {
                if (i === 0) {
                    sHtml += '<div class="bannerPaging activeSlide" id="slide_' + i + '"></div>';
                } else {
                    sHtml += '<div class="bannerPaging"  id="slide_' + i + '"></div>';
                }
            }
            $($this.pagingReel).append(sHtml);
            $('.slide').click(function () {
                Isential.Banner.resume();
            });

            $this.aPaging = $($this.pagingReel).find('bannerPaging');
            $this.descriptionWrapper.text($($this.items[0]).attr('alt'));
            $this.start();
        },
        start: function () {
            /**
            * Set interval result
            * and bind (this = reference to Isential.Banner) to next function (or the this operator will not provide any member property).
            * Look at http://alternateidea.com/blog/articles/2007/7/18/javascript-scope-and-binding for examples
            */
            $this.timer = setInterval($this.next.bind($this), $this.settings.delay);
            $this.aPagers = $('.bannerPaging', $this.pagingReel);

            $('.bannerPaging').click(function () {
                iIndex = this.id.replace('slide_', '');
                $this.index = iIndex;
                Isential.Banner.setSlideActiveByClick(iIndex);
            });
        },
        stop: function () {

            clearInterval($this.timer);
        },
        resume: function () {
            $this.start();
        },
        slideOut: function (p_iIndex) {
            $($this.items[p_iIndex]).hide();
            $($this.items[p_iIndex]).removeClass('active').addClass('inactive');
        },
        slideIn: function (p_iIndex) {
            $($this.items[p_iIndex]).fadeIn('slow');
            $($this.items[p_iIndex]).removeClass('inactive').addClass('active');
            $this.descriptionWrapper.text($(this.items[p_iIndex]).attr('alt'));
        },
        setPagerActiveByIndex: function (p_iIndex) {
            if ($this.aPagers.length > 0) {
                $($this.aPagers[p_iIndex]).addClass('activeSlide');
            }
        },
        setPagerInActiveByIndex: function (p_iIndex) {
            if ($this.aPagers.length > 0) {
                $($this.aPagers[p_iIndex]).removeClass('activeSlide');
            }
        },
        setSlideActiveByClick: function (p_iIndex) {
            //this.stop();

            prevIndex = p_iIndex - 1;
            nextIndex = p_iIndex + 1;

            // console.info("setSlideActiveByClick()::106:: OF PREV-INDEX: " + $this.index);
            $('.bannerPaging').removeClass("activeSlide");
            $('.slide').hide();
            $this.setPagerActiveByIndex(p_iIndex);
            $this.slideOut(prevIndex);
            $this.slideIn(p_iIndex);
        },
        next: function () {
            $this.initiated = 1;
            /**
            * fade out the old slide.
            */
            $this.setPagerInActiveByIndex($this.index);
            $this.slideOut($this.index);
            /**
            * Reset index
            */

            if ($this.index >= $this.items.length - 1) {
                $this.index = 0;
            } else {
                $this.index++;
            }
            //console.info("NEXT()::129::DUMP OF INDEX: " + $this.index);
            /**
            * fade in the new slide
            */
            $this.setPagerActiveByIndex($this.index);
            $this.slideIn($this.index);

        },
        settings: {
            delay: 10000
        }
    };
    $(function () {
        Isential.init();
    });
} (jQuery))
