/*
	Essential Sales Products Ltd
	Home Page Animation Script
*/

var productRotator = {

    // how long each element takes to fade in (1,000s sec)
    fadeDuration: 2000,

    // how long to wait before the next fade (1,000s secs)
    fadeInterval: 6000,

    // how many elements to fade before starting again
    numberOfElements: 0,

    // prefix of the ids of the elements
    idPrefix: '',

    cursor: '',

    // position in the sequence when auto fading
    sequencePointer: 1,

    // number of the currently displayed element
    currentElement: 1,

    // id of the pause/play control 
    pausePlayCtrl: '',

    // is the fading paused?
    isPaused: true,

    timerPtr: null,

    descriptionPrefix: '',

    // initialise this object
    init: function(args) {

        if (args.idPrefix)
            this.idPrefix = this.tidyId(args.idPrefix);

        if (args.numberOfElements)
            this.numberOfElements = args.numberOfElements;

        if (args.fadeDuration)
            this.fadeDuration = args.fadeDuration;

        if (args.fadeInterval)
            this.fadeInterval = args.fadeInterval;

        if (args.cursor)
            this.cursor = this.tidyId(args.cursor);

        if (args.descriptionPrefix)
            this.descriptionPrefix = this.tidyId(args.descriptionPrefix);

        if (args.controlIdPrefix) {
            this.controlIdPrefix = this.tidyId(args.controlIdPrefix);
            for (var i = 1; i <= this.numberOfElements; i++) {
                $(this.controlIdPrefix + i).click(function() { productRotator.locatorClicked(this.id) });
            }
        }

        if (args.pausePlayCtrl) {
            this.pausePlayCtrl = this.tidyId(args.pausePlayCtrl);
            $(this.pausePlayCtrl).click(function() { productRotator.togglePauseFade() });
        }

        this.currentElement = 1;

    },

    // Show the element at position num
    showHideElement: function(args) {

        var elemNum = 0;
        var prefix = '';

        if (args.elementNumber)
            elemNum = args.elementNumber;
        else
            return;

        if (args.idPrefix)
            prefix = args.idPrefix;
        else
            return;

        if (elemNum <= this.numberOfElements && elemNum > 0) {

            var dur;

            if (args.fadeDuration)
                dur = args.fadeDuration;
            else
                dur = this.fadeDuration;

            if (args.show == true) {
                $(prefix + elemNum).fadeIn(dur);
            }
            else {
                $(prefix + elemNum).fadeOut(dur);
            }
        }
    },

    triggerFadeSequence: function() {
        this.timerPtr = setTimeout('productRotator.doFadeSequence()', this.fadeInterval);
    },

    doFadeSequence: function() {
        if (!this.isPaused) {
            this.sequencePointer++;
            if (this.sequencePointer > this.numberOfElements)
                this.sequencePointer = 1;
            this.goToElement({ elementNumber: this.sequencePointer });
            this.triggerFadeSequence();
        }
    },


    goToElement: function(args) {
        var elemNum;
        var dur = this.fadeDuration;
        if (args.elementNumber)
            elemNum = args.elementNumber;
        else
            return false;
        if (args.duration)
            dur = args.duration;
        // this.showDescription(elemNum);
        this.showHideElement({ elementNumber: this.currentElement, show: false, idPrefix: this.idPrefix, fadeDuration: dur });
        this.showHideElement({ elementNumber: elemNum, show: true, idPrefix: this.idPrefix, fadeDuration: dur });
        this.setCursorPosition(elemNum);
        this.updatePointers(elemNum);
        return false;
    },

    showDescription: function(elemNum) {
        this.showHideElement({ elementNumber: this.currentElement, show: false, idPrefix: this.descriptionPrefix });
        this.showHideElement({ elementNumber: elemNum, show: true, idPrefix: this.descriptionPrefix });
    },

    setCursorPosition: function(num) {
        if (this.cursor != '') {
            var selectedCtrl = $(this.controlIdPrefix + num);
            var pos = $(selectedCtrl).position();
            $(selectedCtrl).css({ backgroundPosition: 'bottom left' });
            $(this.controlIdPrefix + this.currentElement).css({ backgroundPosition: 'top left' });
            $(this.cursor).animate({ left: pos.left });
        }
    },

    locatorClicked: function(locId) {
        this.pause();
        var elemNum = locId.charAt(locId.length - 1);
        if (!isNaN(elemNum)) {
            if (elemNum > 0 && elemNum <= this.numberOfElements) {
                this.sequencePointer = elemNum;
                this.goToElement({ elementNumber: elemNum });
            }
        }
    },

    // triggered from the Pause/Play control - decides whether to call pause or play
    togglePauseFade: function() {
        if (this.isPaused)
            this.play();
        else
            this.pause();
    },

    //
    play: function() {
        this.isPaused = false;
        $(this.pausePlayCtrl).css({ backgroundPosition: 'top left' });
        this.triggerFadeSequence();
    },

    pause: function() {
        this.isPaused = true;
        $(this.pausePlayCtrl).css({ backgroundPosition: 'bottom left' });
        clearTimeout(this.timerPtr);
    },

    updatePointers: function(num) {

        this.currentElement = num;
    },

    // make sure an element id begins with a #
    tidyId: function(id) {
        if (id) {
            if (id.charAt(0) != '#')
                id = '#' + id;
            return id;
        }
    }
};