$.fn.ptColScroll = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  	 
    return this.each(function () {
    
   		
        var $container = $(this),
        	$wrapper = $('> div',this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> .textContent'),
            $items = $slider.find('> .column'),
            $single = $items.filter(':first'),
            itemPadding = Number($single.css('margin-right').slice(0, -2)),         
            visible = 2,
            currentPage = 1,
            pages = $items.length;
        	//itemPadding = Number($single.css('margin-right').slice(0, -2)),
        var sliderWidth = 0;            
		var itemWidths = new Array();
		var clientIsIE = false;
		
		if ($.browser.msie) { clientIsIE = true; }
		
		$items.each(function(index) {
        	itemWidths.push($(this).width());
        	sliderWidth += $(this).width();
	 	});
		
        
        
        // 4. paging function
        function gotoPage(page) {
        	
            var left = 0; //left = singleWidth * dir * visible * n
               
            for ( var i = (page-2); i >= 0; i--) {

            	left += itemWidths[i]+itemPadding ;
            }
           
            	
            if ( page <= pages && page >= 1 ) {

            	//$('.dot').removeClass('active');
	            //$('.dot:eq('+(page-1)+')').addClass('active');
	            	            
	            if ( page == 1 ) {
	            	if (!clientIsIE)	$('.back',$container).fadeTo(0,0.5);
	            } else if ( page == pages ) {
	            	
	            	if (!clientIsIE) $('.forward',$container).fadeTo(0,0.5);
	            }else{
	            	
	            	if (!clientIsIE) $('.arrow',$container).fadeTo(0,1);
	            }
	            
	            $wrapper.filter(':not(:animated)').animate({
	                scrollLeft : left
	            }, 900, 'easeOutExpo',function () {
	                if (page == 0) {
	                    $wrapper.scrollLeft(singleWidth); //$wrapper.scrollLeft(singleWidth * visible * pages);
	                    page = pages;
	                } 
	                currentPage = page;

	            });  
            }         
            
            return false;
        }
        
        if ( pages > 2 )
        	$wrapper.after('<a class="arrow back">Previous</a><a class="arrow forward">Next</a>');
        
        if (!clientIsIE)
        	$('.back',$container).fadeTo(0,0.5);
        
        //add dots
       	/*
var dotsHtml = "";
        for ( var i=0; i < pages; i++ ) {
        	dotsHtml += '<div class="dot"></div>';
        }
        $wrapper.after('<div class="pageDots">'+dotsHtml+'</div>');
        
       	var $pageDots = $(this).find('.pageDots');
    	$pageDots.css('margin-left',(-1*($pageDots.width()/2))+'px');
    	
    	$('.dot:eq('+(currentPage-1)+')',this).addClass('active');
        
        
        $('.dot').click(function(){
        	
        	var index = $('.dot').index(this);
        	gotoPage(index + 1);
        	
        });
*/
        
        // 5. Bind to the forward and back buttons
        $('.back',this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('.forward',this).click(function () {
        
            return gotoPage(currentPage + 1);
        });
       
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
    
};
