﻿function jFooter(initialHeight, expandedHeight, rotateSpeed) {
    // private variables
    var winWidth = $(window).width();
    var winHeight = $(window).height();
    var pos;
    var startTop;
    var newTop;
    var tmrShow;
    var tmrDelay;
    var cke;
    var expanded = false;
    
    // set public properties
    var iniHeight = this.initialHeight = initialHeight;
    var expHeight = this.expandedHeight = expandedHeight;
    var newHeight = this.height = initialHeight;

    //  run
    initializePosition();
    insertMarkup();
    assignClickHandlers();
    assignResizeHandler();
    
    // add new item
    this.addItem = function(newItem) {
        $('#jFooter').find('#jFooterCarousel').append(newItem);
    }
    
    // assign click handlers
    function assignClickHandlers() {
        // assign click handler to hide notification
        $('#jFooter .controls').click(function(e) {
            if (expanded) {
                Close();
            } else {
                Open();
            }

        });

    }

    function assignResizeHandler() {
        $(window).resize(function() {
            // get new sizes
            winWidth = $(window).width();
            winHeight = $(window).height();
            $('#jFooter').width(winWidth);
            if (expanded) {
                $('#jFooter').css("top", (winHeight - expandedHeight) + 'px');
            } else {
                $('#jFooter').css("top", (winHeight - initialHeight) + 'px');
            }

        });
    }
    
    var Open = this.Open = function(cback) {
        newTop = winHeight - expandedHeight;
        newHeight = this.height = expandedHeight;
        $('#jFooter .controls').css("background", "url(/images/icons/jFooterDown.png) no-repeat");
        $('#jFooter').animate({
            top: newTop,
            height: newHeight
        }, '800', 'easeInBack', cback);
        expanded = this.expanded = true;
    }

    var Close = this.Close = function(cback) {
        newTop = winHeight - initialHeight;
        newHeight = this.height = initialHeight;
        $('#jFooter .controls').css("background", "url(/images/icons/jFooterUp.png) no-repeat");
        $('#jFooter').animate({
            top: newTop,
            height: newHeight
        }, '800', 'easeInBack', cback);
        expanded = this.expanded = false;
    }

    this.Bounce = function() {
        // animate opening, then animate closing
        Open(function() {
            Close();
        });
    }
    
    // set the intitial position of the notification
    function initializePosition() {
        startTop = (winHeight - newHeight) + 'px';
    }

    // create a DOM object and insert into document
    function insertMarkup() {
        this.jf = '<div id="jFooter" style="top:' + startTop + ';height:' + newHeight + 'px;width:' + winWidth + 'px;">'
                + '<div class="controls"></div>'
                + '<div id="scrollContainer">'
                + '<ul id="jFooterCarousel">'
                + '</ul>'
                + '</div>'
                + '<div style="clear:both;"></div>';
        $('body').append(this.jf);
    }

    function mycarousel_initCallback(carousel) {
        // Disable autoscrolling if the user clicks the prev or next button.
        carousel.buttonNext.bind('click', function() {
            carousel.startAuto(0);
        });

        carousel.buttonPrev.bind('click', function() {
            carousel.startAuto(0);
        });

        // Pause autoscrolling if the user moves with the cursor over the clip.
        carousel.clip.hover(function() {
            carousel.stopAuto();
        }, function() {
            carousel.startAuto();
        });
    };

    this.setupRotator = function() {
        $('#jFooterCarousel').jcarousel({
            auto: rotateSpeed,
            wrap: 'last',
            initCallback: mycarousel_initCallback
        });
    }
}
