(function($){
	var num = function(el, props) {
		var r = 0;
		$.each(props.split(/\s+/) || [], function(i,v){
			r += parseInt($(el).css(v)) || 0;
		});
		return r;
	}
	
	
	$.fn.ticker = function(options){
		var isMethodCall = (typeof options == "string"),
			args = Array.prototype.slice.call(arguments, 1);
			
		return this.each(function() {
			var instance = $(this).data("ticker");
			if (isMethodCall && instance && $.isFunction(instance[options])) {
				instance[options].apply(instance, args);
			} else if (!instance) {
				$(this).data("ticker", new $.ticker(this, options));
			}
		});
	};
	
	$.ticker = function(element, options) {
		this.element = $(element);
		this.options = $.extend(true, {},$.ticker.defaults, options);
		this.init();
	};
	
	$.extend($.ticker.prototype, {
		init: function() {
			this.element.wrap("<div class='ticker-container'></div>");
			this.container = this.element.parent();
			this.wrapper = this.container.parent().addClass("has-js").removeClass("no-js");
			
			this.element.hide();
			this.containerW = this.wrapper.width();
			this.containerH = this.wrapper.height();
			
			this.container.css({
				width: this.containerW,
				height: this.containerH,
				position: "relative",
				overflow: "hidden"
			});
			
			this.element.css({
				display: "block",
				visibility: "hidden",
				position: "absolute",
				left: 0,
				top:0
			});
			
			this.news = $(">.news-item", this.element);
			if (this.news.length) {
				this.initDimension();
				
				this.moveTo(0, false);
				this.startTicker();
				
			}
		},
		
		initDimension: function() {
			var self = this,
				totalW = 0, w, g;
				
			w = this.containerW - num(this.element, "paddingLeft paddingRight marginLeft marginRight borderLeftWidth borderRightWidth");
			h = this.containerH - num(this.element, "paddingTop paddingBottom marginTop marginBottom borderTopWidth borderBottomWidth");
			
			this.news.each(function() {
				var el = $(this);
				el.css({
					width: w - num(el, "paddingLeft paddingRight marginLeft marginRight borderLeftWidth borderRightWidth"),
					height: h - num(el, "paddingTop paddingBottom marginTop marginBottom borderTopWidth borderBottomWidth")
				});
			});
			
			this.itemWidth = w;
			this.element.css({
				width: w * this.news.length,
				height: h,
				visibility: "visible"
			});
		},
		
		moveTo: function(idx, animate) {
			var anim = typeof animate === "undefined" ? true : false,
				index = typeof this.news[idx] === "undefined" ? 0 : idx,
				left = this.itemWidth * index;
			
			this.element.stop();
			if (anim) {
				this.element.animate({left: -left}, {queue: false, duration:1200, easing: "easeInOutExpo"});
			} else {
				this.element.css({
					left: -left
				});
			}
			
			this.currentIndex = index;
		},
		
		startTicker: function() {
			var self = this;
			this.intervalID = window.setInterval(function(){
				if (self.currentIndex >= self.news.length - 1) {
					self.element.prepend(self.news[self.news.length - 1]).css({
						left:0
					});
					self.news = $(">.news-item", self.element);
					self.currentIndex = 0;
				}
				self.moveTo(self.currentIndex+1);
			}, 5200);
		}
	});
})(jQuery);
