// SimpleScroll by Mike Smotherman
// Does simple automatic horizontal scrolling
// v1.0, Aug 4 2010

(function($){
	$.fn.simplescroll2 = function(options){
		options = $.extend({
			speed: 600,
			delay: 2000,
			items: '.scrollitem2'
		}, options);
		
		return this.each(function(){
			var that = $(this),
				items = that.children(options.items),
				itemwidth = items.eq(0).outerWidth(true),
				partheight = items.eq(0).innerHeight(),
				itemheight = items.eq(0).outerHeight(true),
				blocks = items.length,
				current = 0,
				max = Math.max(0, blocks - Math.floor(that.width() / itemwidth)),
				h1=false, h2=false
			;
			
			var hoverbox = $('<div id="sliderhoverbox" />').appendTo(document.body).css({
				position: 'absolute',
				top: 0,
				left: 0,
				zIndex: 50
			});
			
						
			items.wrapAll('<div class="itemslider2" />');
			
			var slider = that.children('.itemslider2');
			
			that.css({
				position: 'relative',
				"overflow": 'hidden'
			});
			
			items.css({
				float: 'left',
				position: 'relative'
			}).bind('mouseenter', function(){
				clearInterval(ss.interval);
				ss.interval=null;
				var $that = $(this);
				$that.addClass('active');
				hoverbox.empty().append($(this).children('.scrollhidden').clone().css({display:'block'})).css({
					top: $that.offset().top+partheight,
					left: $that.offset().left,
					display: 'block'
				});
				h1 = true;
			}).bind('mouseleave', function(){
				h1 = false;
				$(this).removeClass('active');
				
			});
			
			hoverbox.bind('mouseenter', function(){
				h2=true;
			}).bind('mouseleave', function(){
				h2=false;
			});
			
			items.children('scrollhidden').css({
			});
			
			slider.css({
				position: 'relative',
				top: 0,
				left: 0,
				width: blocks*itemwidth,
				height: itemheight
			});
			
			var anim = function(){
				current++;
				
				if(current > max){current=0;};
				
				slider.animate({
					left: -(current*itemwidth)
				}, options.speed)
			};
			
			var ss = {};
			ss.time = options.speed + options.delay;
			
			ss.interval = setInterval(function(){
				anim();
			}, ss.time);
			
			ss.checker = setInterval(function(){
				if(h1===false && h2===false && !ss.interval){
					ss.interval = setInterval(function(){
						anim();
					}, ss.time);
					hoverbox.empty().hide();
				};
			}, 100);
			
			
		}); // end each
	};
	$.fn.simplescroll = function(options){
		options = $.extend({
			speed: 600,
			delay: 2000,
			items: '.scrollitem'
		}, options);
		
		return this.each(function(){
			var that = $(this),
				items = that.children(options.items),
				itemwidth = items.eq(0).outerWidth(true),
				partheight = items.eq(0).innerHeight(),
				itemheight = items.eq(0).outerHeight(true),
				blocks = items.length,
				current = 0,
				max = Math.max(0, blocks - Math.floor(that.width() / itemwidth)),
				h1=false, h2=false
			;
			
			var hoverbox = $('<div id="sliderhoverbox" />').appendTo(document.body).css({
				position: 'absolute',
				top: 0,
				left: 0,
				zIndex: 50
			});
			
						
			items.wrapAll('<div class="itemslider" />');
			
			var slider = that.children('.itemslider');
			
			that.css({
				position: 'relative',
				"overflow": 'hidden'
			});
			
			items.css({
				float: 'left',
				position: 'relative'
			}).bind('mouseenter', function(){
				clearInterval(ss.interval);
				ss.interval=null;
				var $that = $(this);
				$that.addClass('active');
				hoverbox.empty().append($(this).children('.scrollhidden').clone().css({display:'block'})).css({
					top: $that.offset().top+partheight,
					left: $that.offset().left,
					display: 'block'
				});
				h1 = true;
			}).bind('mouseleave', function(){
				h1 = false;
				$(this).removeClass('active');
				
			});
			
			hoverbox.bind('mouseenter', function(){
				h2=true;
			}).bind('mouseleave', function(){
				h2=false;
			});
			
			items.children('scrollhidden').css({
			});
			
			slider.css({
				position: 'relative',
				top: 0,
				left: 0,
				width: blocks*itemwidth,
				height: itemheight
			});
			
			var anim = function(){
				current++;
				
				if(current > max){current=0;};
				
				slider.animate({
					left: -(current*itemwidth)
				}, options.speed)
			};
			
			var ss = {};
			ss.time = options.speed + options.delay;
			
			ss.interval = setInterval(function(){
				anim();
			}, ss.time);
			
			ss.checker = setInterval(function(){
				if(h1===false && h2===false && !ss.interval){
					ss.interval = setInterval(function(){
						anim();
					}, ss.time);
					hoverbox.empty().hide();
				};
			}, 100);
			
			
		}); // end each
	};
})(jQuery);
