var Noodle = {
	numDisplayed:4,
	timeout:10000,
	animationSpeed:250,
	advertCodeSkeleton:'<div id="reklama-doporucujeme" class="nahled-clanku <%advertClass%>"><a href="<%url%>"><img src="<%img%>" alt="<%title%>" style="width:45px;height:45px;" /><em><%title%></em></a></div>',
	advertClass:'advert',
	advertTitle:'span.title',
	advertUrl:'span.url',
	advertImg:'span.img',
	advertBoxId:'#advert-doporucujeme',
	moveToRightDiv:'div.posunout-do-prava',
	moveToLeftDiv:'div.posunout-do-leva',
	breakingNewsClass:'breaking-news',
	initialize:function(elm){
		this.elm = jQuery(elm);
		this.items = jQuery(this.elm).find('div');
		this.elm.parents().find(this.moveToLeftDiv).bind('click', function(){Noodle.showNext();}).bind('mouseenter', function(){Noodle.mouseenter(this);}).bind('mouseleave', function(){Noodle.mouseleave(this);});
		this.elm.parents().find(this.moveToRightDiv).bind('click', function(){Noodle.showPrew();}).bind('mouseenter', function(){Noodle.mouseenter(this);}).bind('mouseleave', function(){Noodle.mouseleave(this);});
		this.elm.bind('mouseenter', function(){Noodle.mouseenter(this);}).bind('mouseleave', function(){Noodle.mouseleave(this);});
		this.timerStart();
		this.insertAd = new Boolean((jQuery(this.advertBoxId).length) && (jQuery(this.advertBoxId).find(this.advertTitle).text().length) && (jQuery(this.advertBoxId).find(this.advertUrl).text().length) && (jQuery(this.advertBoxId).find(this.advertImg).text().length));
		this.advertIndex = 1;
		if(this.insertAd == true) {
			this.advertInitialize(this.advertIndex);
		}
	},
	advertInitialize:function(index){
		var advTitle = jQuery(this.advertBoxId).find(this.advertTitle).text();
		var advUrl = jQuery(this.advertBoxId).find(this.advertUrl).text();
		var advImg = jQuery(this.advertBoxId).find(this.advertImg).text();
		this.advertCode = this.advertCodeSkeleton.replace(/<%advertClass%>/g, this.advertClass).replace(/<%title%>/g, advTitle).replace(/<%url%>/g, advUrl).replace(/<%img%>/g, advImg);
		jQuery(this.elm.find('div:gt(' + (this.numDisplayed + 1) + ').' + this.advertClass)).remove();
		if(index == this.numDisplayed) {
			jQuery(this.elm.find('div:last')).after(this.advertCode);
		} else if((index == 1) &&(jQuery(this.elm.find('div:first.' + this.advertClass)).length == 0)) {
			jQuery(this.elm.find('div:nth-child(' + index + ')')).after(this.advertCode).css('display', 'none').animate({'width':'toggle'}, this.animationSpeed, 'linear', function(){if(Noodle.elm.length > 0){jQuery(Noodle.elm).find('div:hidden').css('display', 'block');}}).siblings('div:nth-child(' + (this.numDisplayed + index) + ')').after(jQuery(this.advertCode).css('display', 'none'));
		}
	},
	showNext:function(){
		this.elm.parents().find(this.moveToLeftDiv).unbind('click');
		this.elm.parents().find(this.moveToRightDiv).unbind('click');
		this.advertIndex = ((++this.advertIndex) > this.numDisplayed) ? (1) : (this.advertIndex);
		if(this.insertAd == true) {
			jQuery(this.elm.find('div:gt(' + (this.numDisplayed + 1) + ').' + this.advertClass)).remove();
			if(this.advertIndex == this.numDisplayed) {
				jQuery(this.elm.find('div:last')).after(this.advertCode);
			} else if((this.advertIndex == 1) &&(jQuery(this.elm.find('div:first.' + this.advertClass)).length == 0)) {
				jQuery(this.elm.find('div:nth-child(' + this.advertIndex + ')')).after(this.advertCode);
				jQuery(this.elm.find('div:nth-child(' + (this.numDisplayed + this.advertIndex) + ')')).after(this.advertCode);
			}
		}
		jQuery(this.elm.find('div:last')).css('display', 'none').prependTo(this.elm).animate({'width':'toggle'}, this.animationSpeed, 'linear', function(){Noodle.moveToFirst();});
	},
	showPrew:function(){
		this.advertIndex = ((--this.advertIndex) < 1) ? (this.numDisplayed) : (this.advertIndex);
		if(this.insertAd == true) {
			jQuery(this.elm.find('div:gt(' + (this.numDisplayed + 1) + ').' + this.advertClass)).remove();
			if(this.advertIndex == 1) {
				jQuery(this.elm.find('div:nth-child(' + (this.numDisplayed + this.advertIndex + 1) + ')')).after(this.advertCode);
			}
		}
		this.elm.parents().find(this.moveToLeftDiv).unbind('click');
		this.elm.parents().find(this.moveToRightDiv).unbind('click');
		jQuery(this.elm.find('div:first')).animate({'width':'toggle'}, this.animationSpeed, 'linear', function(){Noodle.moveToEnd();});
	},
	moveToFirst:function(){
		this.elm.parents().find(this.moveToLeftDiv).bind('click', function(){Noodle.showNext();})
		this.elm.parents().find(this.moveToRightDiv).bind('click', function(){Noodle.showPrew();});
		
	},
	moveToEnd:function(){
		jQuery(this.elm.find('div:first')).appendTo(this.elm);
		jQuery(this.elm.find('div:hidden')).css('display', 'block');
		this.elm.parents().find(this.moveToLeftDiv).bind('click', function(){Noodle.showNext();})
		this.elm.parents().find(this.moveToRightDiv).bind('click', function(){Noodle.showPrew();});
	},
	mouseenter:function(evt){
		this.timerStop();
	},
	mouseleave:function(evt){
		this.timerStart();
	},
	timerDo:function(){
		this.showNext();
		this.timerStart();
	},
	timerStart:function(){
		this.timer = setTimeout('Noodle.timerDo()', this.timeout);
	},
	timerStop:function(){
		clearTimeout(this.timer);
	}
};
