﻿/*
* horizontal news ticker
* Tadas Juozapaitis ( kasp3rito@gmail.com )
* http://www.jugbit.com/jquery-vticker-vertical-news-ticker/
*/
(function($){
$.fn.hTicker = function(options) {
	var defaults = {
		speed: 700,
		pause: 2000,
		showItems: 4,
		animation: '',
		mousePause: true,
		isPaused: false,
		direction: 'left',
		width: 0
	};

	var options = $.extend(defaults, options);

	moveLeft = function(obj2, width, options){

        var obj = obj2.children('ul');
        if( obj.is(':animated') ){
            // do nothing
            return;
        }else{ 
            // normal link functions
    	    var clone = obj.children('li:first').clone(true);
    		
		    if(options.width > 0)
		    {
			    width = obj.children('li:first').width();
		    }		
    		
    	    obj.animate({left: '-=' + width + 'px'}, options.speed, function() {
        	    $(this).children('li:first').remove();
        	    $(this).css('left', '0px');
            });
    		
		    if(options.animation == 'fade')
		    {
			    obj.children('li:first').fadeOut(options.speed);
			    if(options.width == 0)
			    {
			    obj.children('li:eq(' + options.showItems + ')').hide().fadeIn(options.speed).show();
			    }
		    }
    	    clone.appendTo(obj);
		}
	};
	
	moveRight = function(obj2, width, options){

		var obj = obj2.children('ul');
        if( obj.is(':animated') ){
            // do nothing
            return;
        }else{ 
            // normal link functions
    	    var clone = obj.children('li:last').clone(true);
    		
		    if(options.width > 0)
		    {
			    width = obj.children('li:first').width();
		    }
    		
		    obj.css('left', '-' + width + 'px')
			    .prepend(clone);
    			
    	    obj.animate({left: 0}, options.speed, function() {
        	    $(this).children('li:last').remove();
            });
    		
		    if(options.animation == 'fade')
		    {
			    if(options.width == 0)
			    {
				    obj.children('li:eq(' + options.showItems + ')').fadeOut(options.speed);
			    }
			    obj.children('li:first').hide().fadeIn(options.speed).show();
		    }
        }
	};
	
	return this.each(function() {
		var obj = $(this);
		var maxWidth = 0;
		
        obj.$container = obj.parent();

        obj.moveBackClass = 'simply-scroll-btn-left';
        obj.moveForwardClass = 'simply-scroll-btn-right';

        obj.$btnBack = $('.simply-scroll-back', obj.$container).addClass('simply-scroll-btn' + ' ' + obj.moveBackClass).bind("click",function(){
			moveRight(obj, maxWidth, options);
		});
        obj.$btnForward = $('.simply-scroll-forward', obj.$container).addClass('simply-scroll-btn' + ' ' + obj.moveForwardClass).bind("click",function(){
			moveLeft(obj, maxWidth, options);
		});

		obj.css({overflow: 'hidden', position: 'relative'})
			.children('ul').css({position: 'absolute', margin: 0, padding: 0})
			.children('li').css({margin: 0, padding: '0 3px'});

		if(options.width == 0)
		{
			obj.children('ul').children('li').each(function(){
				if($(this).width() > maxWidth)
				{
					maxWidth = $(this).width();
				}
			});

			obj.children('ul').children('li').each(function(){
				$(this).width(maxWidth);
			});

			obj.children('ul').width((maxWidth * (options.showItems + 1)) + 24);
			obj.width((maxWidth * (options.showItems)) + 15);
		}
		else
		{
			obj.width(options.width);
		}
		
	
/*
		
    	var interval = setInterval(function(){ 
			if(options.direction == 'left')
			{ 
				moveLeft(obj, maxWidth, options); 
			}
			else
			{ 
				moveRight(obj, maxWidth, options); 
			} 
		}, options.pause);
		
		if(options.mousePause)
		{
			obj.bind("mouseenter",function(){
				options.isPaused = true;
			}).bind("mouseleave",function(){
				options.isPaused = true;
			});
		}
		
		*/
	});
};
})(jQuery);
