$(document).ready(function(){
	if($('#news-div').length>0){
		var fHeight=0,
				w=$('#news-div').width(),
				max=$('.novinka').length-1,
				position=Math.round(Math.random()*max);
	
		function animate_news(newposition){
			$('.controls .active').removeClass('active');
	
			$('#novinky').animate({'margin-left':(newposition*(w+20)*-1)+'px'});
			position = newposition;
			
			$('.controls li:not(.prev):not(.next)').eq(position).addClass('active');
		}
	
		//prepare
		$('#news-div')
			.css({'overflow':'hidden'});
		$('.novinka')
			.wrapAll('<div id="novinky" />')
			.css({'float':'left', 'width': w, 'margin-right':'20px'})
			.each(function(){ if($(this).height()>fHeight) {fHeight = $(this).height(); }});
		$('#novinky')
			.css({
				'width':(max+1)*(w+20)+'px',
				'margin-left':(position*(w+20)*-1)+'px',
				'height':fHeight+'px'
			})
			.append('<span style="clear:both;display:block;height:0 !important;line-height:0 !important">&nbsp;</span>');
		
		//add controls
		$('#news-div')
			.append('<div class="controls-outer"><ul class="controls"><li class="prev">&laquo;</li><li class="next">&raquo;</li></ul></div>');
		for(var i=0;i<=max;i++){ $('.controls .prev').after('<li>&#149;</li>'); }
		$('.controls li:not(.prev):not(.next)').eq(position).addClass('active');
	
		//bind controls
		$('.controls .prev').bind('click',function(){ animate_news((position>0)?position-1:max); });
		$('.controls .next').bind('click',function(){ animate_news((position<max)?position+1:0); });
		$('.controls li:not(.prev):not(.next)').bind('click',function(){ 
			animate_news($(this).index('#news-div li:not(.prev):not(.next)'));
		});
	}
});


