/**
 * @author Mirosław Bogacz, 2010-10-14
 * @version 0.0.1
 */
(function($){
	$.fn.marquee = function(Setup) {
		
		Default = {
			time: 30,
			speed: 1
		}
		
		$.extend(Default, Setup);
		
		return this.each(function ( index , value ) {
			
			function Marquee() {
				var Marquee, chapter, taht;
			
				Marquee = {
					marquee: $(value).get(0),
					chapter: $(value).find('div').get(),
					chapterInner: [],
					chapterCount: 0,
					intervalId: 0,
					intervalTime: Default.time,
					intervalSpeed: Default.speed,
					
					init : function() {
						debug('Marquee->init');
						
						$(that.marquee).css({
							'overflow': 'hidden'
						});
						
						$(that.chapter).each(function ( index , value ) {
							that.chapterInner.push($(value).html());
							$(value).remove();
						});
						
						that.chapter = [];
						
						that.timer(true);
					},
					
					timer : function(status) {
						debug('Marquee->timer', status);
						
						that.intervalId = setInterval(that.move, that.intervalTime);
					},
					
					move : function() {
						
						
						if (that.chapter.length == 0) {
							//debug(that.chapter.length);
							that.chapter.push(document.createElement('div'));
							that.chapter[0].innerHTML = that.chapterInner[0];
							$(that.marquee).append(that.chapter[0]);
						
							$(that.chapter[0]).css({
								'position': 'absolute',
								'left': $(that.marquee).width() + 10,
								'white-space': 'nowrap'
							});	
							
							that.chapterCount += 1;
						}
						
						$.each(that.chapter, function ( index , value ) {
							var current;
							
							current = $(that.chapter[index]);
							
							if (!$(that.chapter[index + 1]).is('span')) {
								if ((that.chapter.length - 1) > -1 && $(that.chapter[that.chapter.length - 1]).position().left + $(that.chapter[that.chapter.length - 1]).width() < $(that.marquee).width()) {
									
									
									that.chapter.push(document.createElement('div'));
									that.chapter[that.chapter.length - 1].innerHTML = that.chapterInner[that.chapterCount];
									$(that.marquee).append(that.chapter[that.chapter.length - 1]);
									
									$(that.chapter[that.chapter.length - 1]).css({
										'position': 'absolute',
										'left': $(that.marquee).width() + 10,
										'white-space': 'nowrap'
									});	
									
									//debug(that.chapter.length);
									
									if (that.chapterCount + 1 < that.chapterInner.length) {
										that.chapterCount += 1;
									} else {
										that.chapterCount = 0;
									}
									
								}
							}
							
							if ($(current).is('div')) {
							
								if (true) {
									$(current).css({
										'left': $(current).position().left - that.intervalSpeed
									});	
								}
								
								if ($(current).position().left + $(current).width() < 0) {
									$(current).remove();
									that.chapter.splice(index, 1);
								}
							
							}
							
						});
						
					}
				
				};
				
				that = Marquee;
				that.init();
				
			}
			
			start = new Date().getTime();
			new Marquee();
			end = new Date().getTime();
			
			debug(end - start + ' ms');
		});
		
	};
})(jQuery);

function debug() {
	if(false) {
		console.log(arguments);
	}
}
