 (function($) {
	$.fn.homeNews = function(opt) {
		var options = $.extend({
			scrollDuration: 1200,
			scrollDelay: 8000,
			closeDuration: 1200,
			openDuration: 1200
		}, opt);
		return $(this).each(function() {
			var el = this;
			var closed = false;
			var scrollTimeout = 0;
			
			var cwidth = $(".homenews_news").innerWidth();
			cwidth -= parseInt($(".homenews_news").css("paddingLeft")) || 0;
			cwidth -= parseInt($(".homenews_news").css("paddingRight")) || 0;
			
			
			var tail = $(".homenews_news>ul>li:first", el).clone().addClass('homenews_helper').appendTo($(".homenews_news>ul", el));
			var items = $(".homenews_news>ul>li", el);
			var items_container = $(".homenews_news>ul",el);
			var item_width = $(items[0]).outerWidth(true);
			$(".homenews_news>ul", el).css({width: (item_width * items.length)+"px"});
			
			var current = 0;
			
			var scroll = function() {
				if (closed) return;
				
				if ($(items[current]).is(".homenews_helper")) {
					$(items_container).css({left:0});
					current = 0;
				}
				current++;
				var x = -current * item_width;
				$(items_container).stop();
				$(items_container).animate({left:x}, {queue:false, duration: options.scrollDuration, easing:"easeInOutExpo"});
			};
			
			
			
			var autoScroll = function(first) {
				if (first == undefined) first = false;
				
				if (scrollTimeout > 0) {
					window.clearTimeout(scrollTimeout);
					scrollTimeout = 0;
				}
				
				if (closed) return;
				
				scrollTimeout = window.setTimeout(function(){
					scroll();
					autoScroll();
				}, options.scrollDelay + (first ? 0 : options.scrollDuration));
			};
			
			//safari fix
			if ($.browser.safari) {
				var p = parseInt($(el).css("paddingLeft")) || 0;
				p += parseInt($(el).css("paddingRight")) || 0;
			}
			
			$("a.close", el).click(function(){
				this.blur();
				if (closed) return false;
				if (scrollTimeout > 0) {
					window.clearTimeout(scrollTimeout);
					scrollTimeout = 0;
				}
				
				$(".homenews_news", el).animate({width:0}, {queue:false, duration:options.closeDuration, easing:"easeInExpo", 
					complete: function(){
						closed = true;
						$("a.close", el).hide();
						$("a.open", el).show();
					}
				});
				return false;
			});
			
			$("a.open", el).click(function(){
				this.blur();
				if (!closed) return false;
				
				$("a.open", el).hide();
				$("a.close", el).show();
				$(".homenews_news", el).animate({width:cwidth}, {queue:false, duration:options.openDuration, easing:"easeOutExpo", 
					complete: function(){
						closed = false;
						autoScroll(true);
					}
				});
				return false;
			});
			
			autoScroll(true);
		});
	};
	
	$().ready(function(){
		$(".homenews").homeNews({
			scrollDuration: 1200,
			scrollDelay: 5000,
			closeDuration: 600,
			openDuration: 600
		});
	});
})(jQuery);
