$(document).ready(function(){
	
		$.fn.KeskustaCarousel = function(opts) {
		var defaults = {
			items : false,
			itemsArea : false,
			next : false,
			prev : false,
			auto : false,
			speed : 500,
			loop : true,
			beforeScroll : false,
			afterScroll : false,
			indicators : false,
			timerObject : false
		}; 
		var options = $.extend(defaults, opts);
	
	//---------------------------------------------------
	
		this.each(function(){
			if(!options.items || !options.itemsArea){
				return false;
			}
			if($(options.items).length < 2){
				$(options.items).css('visibility', 'visible');
				return false;
			}
			
			var carousel = $(this);
			options.carousel = $(this);
			carousel.find(options.itemsArea).prepend('<div class="IW_CarouselItemsRoller" style="position:relative;width:100%;overflow:hidden;"><table border="0" cellpadding="0" cellspacing="0"><tr></tr></table></div>');
			var roller = carousel.find(options.itemsArea).find('.IW_CarouselItemsRoller');
			
			carousel.find(options.items).each(function(e){
				roller.find('tr').append('<td index="'+(e+1)+'"></td>');
				roller.find('td:last').append($(this));
			});
			
			carousel.find(options.next).addClass('IW_CarouselNavButton');
			carousel.find(options.prev).addClass('IW_CarouselNavButton');
			
			if(options.loop){
				var adj = roller.find('td:last').clone();
				roller.find('tr').prepend(adj);
				roller.find('td:eq(1)').addClass('current');
				roller.animate({scrollLeft: roller.width()}, 0);
			}else{
				roller.find('td:eq(0)').addClass('current');
			}
			
			
			if(options.auto !== false){
				carousel.addClass('AutoScrolledCarousel');
			}
			
			roller.find(options.items).css('visibility', 'visible');
			roller.find('td:first').addClass('first');
			roller.find('td:last').addClass('last');
						
			setIndicators();
		});
	
	//---------------------------------------------------
		
		function setIndicators(){
			if(options.indicators){
				for (i=1; i<$(options.items).length; i++){
					$(options.indicators).append('<a href="#'+i+'">'+i+'</a>');
				}
			}
			$(options.indicators).find('a:first').addClass('active');
		}
	
	//---------------------------------------------------
		
		this.find(options.next).click(function(){
			initScroll($(this), 'fw');
		});
	
	//---------------------------------------------------
	
		this.find(options.prev).click(function(){
			initScroll($(this), 'bw');
		});
	
		//---------------------------------------------------
	
		this.find(options.indicators).find('a').click(function(event){
			event.preventDefault();
			var roller = $(this).parents(options.carousel).find('.IW_CarouselItemsRoller');
			var eq = parseInt($(this).attr('href').replace('#', ''));
			var left = roller.width() * eq;
			
			$(this).parents(options.indicators).find('a.active').removeClass('active');
			$(this).addClass('active');
			
			roller.find('td.current').removeClass('current');
			roller.find('td:eq('+eq+')').addClass('current');
			
			if(options.beforeScroll){	
				eval(options.beforeScroll+'();');
			}
			
			roller.animate({scrollLeft: left}, 500, function(){
				if(options.afterScroll){	
					eval(options.afterScroll+'();');
				}
			});
		});
	
	//---------------------------------------------------
	
		this.hover(function(){
			$(this).removeClass('AutoScrolledCarousel');
		}, function(){
			$(this).addClass('AutoScrolledCarousel');
		});
	
	//---------------------------------------------------
		
		$('.AutoScrolledCarousel').each(function(){
			AutoScroll($(this));
		});
	
	//---------------------------------------------------
	
		function AutoScroll(carousel){
			var btn = carousel.find(options.next);
			options.timerObject = setTimeout(function(){
				if($('.AutoScrolledCarousel').length){	
					initScroll(btn, 'fw');
				}
				AutoScroll(carousel);
			}, options.auto);
		}
		
	//---------------------------------------------------
		
		function initScroll(btn, dir){
			if(!btn.is('.busy')){
				var roller = btn.parents(options.carousel).find('.IW_CarouselItemsRoller');
				
				if(dir == 'fw'){
					if(roller.find('td.current').is('td.last')){
						roller.animate({scrollLeft: 0}, 0, function(){
							roller.find('td.current').removeClass('current');
							roller.find('td:first').addClass('current');
						});
					}
					
					roller.find('td.current').removeClass('current').next('td').addClass('current');
					var left = roller.scrollLeft() + roller.width();
				}else{
					
					if(roller.find('td.current').is('td.first')){
						roller.animate({scrollLeft: roller.find('table:first').width()}, 0, function(){
							roller.find('td.current').removeClass('current');
							roller.find('td:last').addClass('current');
						});
					}
					
					var left = roller.scrollLeft() - roller.width();
					roller.find('td.current').removeClass('current').prev('td').addClass('current');
				}
				
				if(options.indicators){	
					$(options.indicators).find('a.active').removeClass('active');
					$(options.indicators).find('a:eq('+(roller.find('td.current').attr('index')-1)+')').addClass('active');
				}
				
				if(options.beforeScroll){	
					eval(options.beforeScroll+'();');
				}
				$('.IW_CarouselNavButton').addClass('busy');
				roller.animate({scrollLeft: left}, 500, function(){
					$('.IW_CarouselNavButton').removeClass('busy');
					if(options.afterScroll){	
						eval(options.afterScroll+'();');
					}
				});
			}
		}
	
	};

function StartFlashObject(){
	CreateFlashObject($('.FlashRoller .current .FlashContent'));
}

//---------------------------------------------------------------

	$('.FlashRoller').KeskustaCarousel( {
		items : '.CarouselItem',
		itemsArea : '.CarouselItems',
		loop : true,
		next : '.next',
		prev : '.prev',
		auto : 20000, 
		beforeScroll : 'StartFlashObject',
		indicators: '.CarouselItemIndicator'
	});

//---------------------------------------------------------------
	if($('.FlashRoller .current').length){
		CreateFlashObject($('.FlashRoller .current .FlashContent'));
	}else{
		CreateFlashObject($('.FlashRoller .FlashContent:first'));
	}
	
	function CreateFlashObject(area){
		var fileID = area.find('.fileID').val();
		var w = area.parents('.FlashRollerArea').find('.width').val();
		var h = area.parents('.FlashRollerArea').find('.height').val();
		area.find('div:first').empty().css('background', '#fff').flash({
			src: '/loader.aspx?id='+fileID,
			width: w,
			height: h,
			wmode:"transparent"
		}).fadeIn(300);
	}

});
