﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="../Wcf/SDProxy.svc" />
/// <reference path="Lib/jquery-1.2.6-vsdoc.js" />
/// <reference path="common.js" />
/// <reference path="eventmanager-0.0.js" />

//
// carousel-0.0.js
//
// Version 0.0
// Written by CC
//

var carouselMgr = {
    divId: null,
    btnDivId: null,
    cWidth: 0,
    cItemWidth: 0,
    cItemCount: 0,
    cDelay: 0,
    autoMove: true,
    style: 'scroll',
    currIndex: 0,
    enablePages: false,
    setup: function(carouselWidth, itemWidth, itemCount, delay, style, enablePages, carouselListId, carouselButtonListId) {
        this.divId = carouselListId;
        this.cItemCount = itemCount;
        this.cItemWidth = itemWidth;
        this.cDelay = delay;
        this.autoMove = true;
        this.cWidth = carouselWidth;
        this.enablePages = enablePages;
        this.btnDivId = carouselButtonListId;
        this.style = style;
        $('#' + this.divId).css('width', (itemCount * itemWidth) + 'px');
        $('#' + this.btnDivId).css('width', (this.getMaxPage() * 22) + 'px');
        if (this.enablePages) {
            this.initButtons();
            this.movePage();
        } else {
            this.move();
        }
    },
    initButtons: function() {
        var ul = $('#' + this.btnDivId);
        var pC = this.getMaxPage();
        ul.html('');
        for (var i = 0; i < pC; i++) {
            var html = '';
            html += '<li><span class="btnPageSel"><a id="carouselPageSel' + i + '" href="#" onclick="carouselMgr.selectPage(' + i + '); return false;"';
            if (this.currIndex == i) html += ' class="selected"'
            html += '><span>' + (i + 1) + '</span></a></span></li>';
            ul.append(html);
        }
    },
    getMaxPage: function() {
        var iPP = parseInt(this.cWidth / this.cItemWidth);
        return Math.ceil(this.cItemCount / iPP);
    },
    selectPage: function(pageNo) {
        this.autoMove = false;
        this.showPage(pageNo);
    },
    updateButtons: function(pageNo) {
        $('#' + this.btnDivId + ' li a').removeClass('selected');
        $('#carouselPageSel' + pageNo).addClass('selected');
    },
    movePage: function() {
        if (carouselMgr.cDelay != null && carouselMgr.cDelay != undefined) {
            setTimeout(function() {
                if (carouselMgr.autoMove) {
                    carouselMgr.calculateMovePage();
                    carouselMgr.movePage();
                }
            }, carouselMgr.cDelay);
        }
    },
    calculateMovePage: function() {
        var oldLeft = carouselMgr.cWidth * carouselMgr.currIndex;
        var maxLeft = (carouselMgr.cItemCount * carouselMgr.cItemWidth) - carouselMgr.cWidth;
        carouselMgr.currIndex++;
        if (oldLeft >= maxLeft) carouselMgr.currIndex = 0;
        carouselMgr.showPage(carouselMgr.currIndex);
    },
    showPage: function(pageNo) {
        var newLeft = carouselMgr.cWidth * pageNo;
        var maxLeft = (carouselMgr.cItemCount * carouselMgr.cItemWidth) - carouselMgr.cWidth;
        if (newLeft > maxLeft) newLeft = maxLeft;
        this.updateButtons(pageNo);
        if (this.style == 'fade') {
            $('#' + carouselMgr.divId).fadeOut('fast').animate({ left: '-' + newLeft + 'px' }, 1).fadeIn('fast');
        } else {
            $('#' + carouselMgr.divId).animate({ left: '-' + newLeft + 'px' }, 100);
        }
    },
    calculateMove: function() {
        carouselMgr.currIndex++;
        var maxLeft = (carouselMgr.cItemCount * carouselMgr.cItemWidth) - carouselMgr.cWidth;
        var newLeft = carouselMgr.cItemWidth * carouselMgr.currIndex;
        if (newLeft > maxLeft) carouselMgr.currIndex = 0;
        $('#' + carouselMgr.divId).animate({ left: '-' + (carouselMgr.cItemWidth * carouselMgr.currIndex) + 'px' }, 1000);
    },
    move: function() {
        if (carouselMgr.cDelay != null && carouselMgr.cDelay != undefined) {
            setTimeout(function() {
                carouselMgr.calculateMove();
                carouselMgr.move();
            }, carouselMgr.cDelay);
        }
    }
}