/*
 * Copyright 2011 1fb.net Financial Services.
 *
 * This document may not be reproduced, distributed or used in any manner whatsoever without the expressed written
 * permission of 1st Financial Bank USA.
 */

/**
 * @fileOverview Define the Springboard class.
 * @author Ray Chen
 * @version 1.2
 * @since 7.00, 2011-11-23
 */

/**
 * Creates name space FFB.CS.Common.
 */
var FFB;
if (!FFB) FFB = {};
if (!FFB.CS) FFB.CS = {};
if (!FFB.CS.Common) FFB.CS.Common = {};

/**
 * Definition of the Springboard class.
 * @constructor
 * @this {Springboard} Springboard class.
 * @param {Array<Object>} parameters A list containing various parameters for the creation of a Springboard object.
 */
FFB.CS.Common.Springboard = function(parameters) {
    var DEFAULT_VALUE_DISPLAY_BOARD_COUNT = 1;
    var DEFAULT_VALUE_MOVE_PAUSE = 3000;
    var DEFAULT_VALUE_WAITING_TIME_BEFORE_AUTO_SHOW = 9000;

    var _DIRECTION_PREV = "prev";
    var _DIRECTION_NEXT = "next";

    var _MOUSE_BUTTON_LEFT = 1;

    var _parameters = parameters;

    var _containerId = null;
    var _prevControlId = null;
    var _nextControlId = null;

    var _displayBoardCount = null;
    var _movePause = null;

    var _workMode = null;
    var _waitingTimeBeforeAutoShow = null;

    var _controller = null;
    var _inAutoShow = false;
    var _allowToAutoShow = true;
    var _static = true;

    /**
     * Returns if current object is static or not (motive).
     */
    this.isStatic = function() {
        return _static;
    };

    initialize();

    /**
     * Initializes current object.
     */
    function initialize() {
        if (!processParameters()) {
            return;
        }

        createController();

        switch (_workMode) {
            case FFB.CS.Common.Springboard.WORK_MODE_NORMAL:
                bindEventHandlersInNormalMode();
                break;

            case FFB.CS.Common.Springboard.WORK_MODE_AUTO_SHOW:
                bindEventHandlersInAutoShowMode();
                processAutoShowMode();
                break;

            default:
                break;
        }

        showAllBoards();
    }

    /**
     * Processes the parameters passed in from external.
     */
    function processParameters() {
        var isNormal = true;

        if (_parameters == null
                || _parameters.containerId == null
                || _parameters.prevControlId == null
                || _parameters.nextControlId == null) {
            isNormal = false;
        }

        _containerId = _parameters.containerId;
        _prevControlId = _parameters.prevControlId;
        _nextControlId = _parameters.nextControlId;

        if (_parameters.displayBoardCount != null) {
            _displayBoardCount = _parameters.displayBoardCount;
        } else {
            _displayBoardCount = DEFAULT_VALUE_DISPLAY_BOARD_COUNT;
        }

        if (_parameters.movePause != null) {
            _movePause = _parameters.movePause;
        } else {
            _movePause = DEFAULT_VALUE_MOVE_PAUSE;
        }

        if (_parameters.workMode != null) {
            _workMode = _parameters.workMode;
        } else {
            _workMode = FFB.CS.Common.Springboard.WORK_MODE_NORMAL;
        }

        if (_parameters.waitingTimeBeforeAutoShow != null) {
            _waitingTimeBeforeAutoShow = _parameters.waitingTimeBeforeAutoShow;
        } else {
            _waitingTimeBeforeAutoShow = DEFAULT_VALUE_WAITING_TIME_BEFORE_AUTO_SHOW;
        }

        return isNormal;
    }

    /**
     * Creates the controller of current object.
     */
    function createController() {
        _controller = $j(_containerId).bxSlider({
            displaySlideQty: _displayBoardCount,
            pause: _movePause,

            autoDirection: _DIRECTION_NEXT,

            controls: false,
            autoStart: false,
            auto: true,

            onAfterSlide: execOnAfterBoardMove
        });
    }

    /**
     * Binds event handlers for current object in Normal work mode.
     * Remarks:
     * - For most event handlers, a "return false" is added in the last to prevent default behavior of target page element.
     *   But some event handlers use "return false" to allow those default behaviors for actual needs.
     */
    function bindEventHandlersInNormalMode() {
        $j(_prevControlId).click(function(event) {
            _static = false;
            moveToPreviousBoard();
            return false;
        });

        $j(_prevControlId).mousedown(function(event) {
            if (event.which != _MOUSE_BUTTON_LEFT) {
                return true;
            }
            _controller.setAutoDirection(_DIRECTION_PREV);
            startAutoShow();
            return true;
        });

        $j(_prevControlId).mouseup(function(event) {
            stopAutoShow();
            return false;
        });

        $j(_prevControlId).mouseleave(function(event) {
            stopAutoShow();
            return false;
        });

        $j(_prevControlId).bind("drag", function(event) {
            stopAutoShow();
            return false;
        });

        $j(_nextControlId).click(function(event) {
            _static = false;
            moveToNextBoard();
            return false;
        });

        $j(_nextControlId).mousedown(function(event) {
            if (event.which != _MOUSE_BUTTON_LEFT) {
                return true;
            }
            _controller.setAutoDirection(_DIRECTION_NEXT);
            startAutoShow();
            return true;
        });

        $j(_nextControlId).mouseup(function(event) {
            stopAutoShow();
            return false;
        });

        $j(_nextControlId).mouseleave(function(event) {
            stopAutoShow();
            return false;
        });

        $j(_nextControlId).bind("drag", function(event) {
            stopAutoShow();
            return false;
        });
    }

    /**
     * Binds event handlers for current object in Auto Show work mode.
     * Remarks:
     * - For most event handlers, a "return false" is added in the last to prevent default behavior of target page element.
     *   But some event handlers use "return false" to allow those default behaviors for actual needs.
     */
    function bindEventHandlersInAutoShowMode() {
        $j(_prevControlId).click(function(event) {
            _static = false;
        });

        $j(_prevControlId).mousedown(function(event) {
            moveToPreviousBoard();
            return true;
        });

        $j(_prevControlId).mouseenter(function(event) {
            _allowToAutoShow = false;
            stopAutoShow();
            return false;
        });

        $j(_prevControlId).mouseleave(function(event) {
            _allowToAutoShow = true;
            return false;
        });

        $j(_nextControlId).click(function(event) {
            _static = false;
        });

        $j(_nextControlId).mousedown(function(event) {
            moveToNextBoard();
            return true;
        });

        $j(_nextControlId).mouseenter(function(event) {
            _allowToAutoShow = false;
            stopAutoShow();
            return false;
        });

        $j(_nextControlId).mouseleave(function(event) {
            _allowToAutoShow = true;
            return false;
        });

        var allBoards = getAllBoards();

        $j(allBoards).mouseenter(function(event) {
            _allowToAutoShow = false;
            stopAutoShow();
            return false;
        });

        $j(allBoards).mouseleave(function(event) {
            _allowToAutoShow = true;
            return false;
        });
    }

    /**
     * Performs processes in Auto Show work mode.
     */
    function processAutoShowMode() {
        $j.idleTimer(_waitingTimeBeforeAutoShow);

        $j(document).bind("idle.idleTimer", function() {
            if (_allowToAutoShow && !_inAutoShow) {
                startAutoShow();
            }
        });
    }

    /**
     * Moves to previous board.
     */
    function moveToPreviousBoard() {
        _controller.goToPreviousSlide();
    }

    /**
     * Moves to next board.
     */
    function moveToNextBoard() {
        _controller.goToNextSlide();
    }

    /**
     * Starts the Auto Show.
     */
    function startAutoShow() {
        if (_inAutoShow) {
            return;
        }

        _controller.startShow();
        _inAutoShow = true;
    }

    /**
     * Stops the Auto Show.
     */
    function stopAutoShow() {
        _controller.stopShow();
        _inAutoShow = false;
    }

    /**
     * Gets all boards.
     */
    function getAllBoards() {
        return $j(_containerId).children();
    }

    /**
     * Shows all boards.
     */
    function showAllBoards() {
        var allBoards = getAllBoards();

        $j(allBoards).css("visibility", "visible");
    }

    /**
     * Does execution after every board move.
     */
    function execOnAfterBoardMove() {
        _static = true;
    }
};

FFB.CS.Common.Springboard.WORK_MODE_NORMAL = 0;
FFB.CS.Common.Springboard.WORK_MODE_AUTO_SHOW = 1;

