/*!
 * jQuery newsScroller plugin
 * Version 1.0 (29-APR-2011)
 *
 * Examples at: http://www.iicsw.com/jquery/newsScroller/
 * Copyright (c) 2011-2012 A. Mondati
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 */
 
;(function($) {

$.fn.newsScroller = function(o){
	var scr = this;
	
    o = $.extend({
        delta: 3,
		speed: 100,
		startupdelay: 1500,
		vertical: true,
		padding: 20
    }, o || {});

	var stop = false;
		
    return this.each(function() {
		var el = $(this), ul = $("ul", el), li = $("li", ul), lin = li.size(), animCss=(o.vertical)?"top":"left";

		var elh = (o.vertical)?height(el):width(el);
		//var ulh = (o.vertical)?height(ul):width(ul);
		var ulh = (o.vertical)?height(ul):(elh*lin);
		
		if(!o.vertical) {
			$("div.item", li).css('margin-right', o.padding+'px');
			li.css({overflow: "hidden", 'float': "left", width: elh+'px'});
			el.css('width', ulh+"px");
		} else {
			$("div.item", li).css('margin-bottom', o.padding+'px');
			ulh = (o.vertical)?height(ul):(elh*lin);
		}

		el.mouseover(function() {
			stop = true;
		}).mouseout(function(){
			stop = false;
		});
		
		if(ulh>elh || !o.vertical) {
			var t0 = 0;
			var t1 = ulh;
			var ulc = ul.clone();

			el.append(ulc);

			ulc.css(animCss, ulh+'px');

			$(this).oneTime(o.startupdelay, "sudly", function() {
				$(this).everyTime(o.speed, 'scrt', function(i) {
					if(!stop) {
						if(t0<=ulh*-1 || t1<=ulh*-1) {
							//$(this).stopTime("scrt");
							if(t0<t1) {
								t0 = parseInt(ulc.css(animCss))+ulh;
								ul.css(animCss, t0+'px');
							} else {
								t1 = parseInt(ul.css(animCss))+ulh;
								ulc.css(animCss, t1+'px');
							}
						}
						t0 -= o.delta;
						ul.css(animCss, t0+'px');
						t1 -= o.delta;
						ulc.css(animCss, t1+'px');
					}
				}, 0);
			});
		}
    });
}

function css(el, prop) {
    return parseInt($.css(el[0], prop)) || 0;
};

function width(el) {
    return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};

function height(el) {
	return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};

})(jQuery);

