/**
 * Vertical slide animation
 * 2010-12-10
 
 * @author Wim La Haye
 * @copyright Techtwo Webdevelopment 2010 - 2011
 * @version 0.0.1
 */
;(function($){
    
    // Vertical slide animation  
    $.fn.techtwoCaroussel.animations.slideVerticalViewPort = function(options, caroussel)
    {
        
        var animation = this,
            caroussel = caroussel;
            
        animation.settings  = $.extend({}, $.fn.techtwoCaroussel.animations.slideVerticalViewPort.defaults, options);
            
        animation.initialize = function() 
        {
            
            animation.idx = 'idx' + Math.ceil(Math.random()*1000000);
            
            if (!animation.settings.panelWidth) {
                alert('Please define a panel width');
            }

            // Create animation wrapper
            animation.wrapper = $('<div id="' + animation.idx  +'"/>')
                .addClass('techtwo-slide-vertical-wrapper')
                .css({
                    overflow: 'hidden',
                    margin: animation.settings.wrapperMargin
                })
                .scrollLeft(0);

            // Wrap caroussel in animation wrapper
            caroussel.wrapper
                .css({width: animation.calculateTotalWidth()})
                .wrap(animation.wrapper);
                
            animation.wrapper = $('#' + animation.idx);
            
            // Position panels
            caroussel.panels
                .css({
                    width: animation.settings.panelWidth,
                    'float': 'left' 
                });

            
            // Calculate panels per viewPort
            animation.settings.panelsPerViewPort = animation.wrapper.width() / animation.settings.panelWidth;

            // Overwrite default methods to calculate next/prev panels
            caroussel.getPreviousPanel = animation.getPreviousPanel;
            caroussel.getNextPanel     = animation.getNextPanel;

        };

        animation.calculateTotalWidth = function()
        {
            return (animation.settings.panelWidth * caroussel.panels.length) +
                   (animation.settings.panelMarginHorizontal  * caroussel.panels.length);
        }

        
        animation.gotoPanel = function (index)
        {
            animation.wrapper
                .animate({'scrollLeft': index * animation.settings.panelWidth}, caroussel.settings.duration);
        }
        
        animation.getPreviousPanel = function()
        {    	
            return (0 > (caroussel.activePanel - animation.settings.panelsPerViewPort) 
            			? (caroussel.panels.length - animation.settings.panelsPerViewPort) 
    					: caroussel.activePanel - animation.settings.panelsPerViewPort);
        };

        animation.getNextPanel = function ()
        {

            return (caroussel.panels.length <= (caroussel.activePanel + animation.settings.panelsPerViewPort) ? 0 : caroussel.activePanel + animation.settings.panelsPerViewPort);
        };
        
        animation.initialize();
        
        return animation;
              
    };
        
 
    // Vertical slide settings
    $.fn.techtwoCaroussel.animations.slideVerticalViewPort.defaults = {
        direction: 'ltr',
        panelWidth: false, // either ltr or rtl
        panelMarginHorizontal: 0,
        wrapperMargin: 0
    };
    
})(jQuery);
    
