/*
	rotatory pracuju symetricky (ak ich je viac na stranke) kvoli jednoduchosti odchytavania eventov
*/
$(document).ready(function() {
	$(".paging").show();
	$(".paging a:first").addClass("active");		
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel div").size();
	var imageReelWidth = imageWidth * imageSum;
	var stop = false;
	$(".image_reel").css({'width' : imageReelWidth});
	
	rotateId = function(id){	
		var triggerID = $active.attr("rel") - 1;
		var image_reelPosition = triggerID * imageWidth;

		$("#"+id+" .paging a").removeClass('active');
		$active.addClass('active');
		
		//Slider Animation
		$("#"+id+" .image_reel").animate({ 
			left: -image_reelPosition
		}, 300 );
		
	}; 
	
	rotateSwitchId = function(id){		
		play = setInterval(function(){						// vytvorit rotator a nechat bezat do konca sveta (stranky) !!! nevytvaraj viac rotatorov pre jeden objekt, lebo budu pracovat sucasne, vyuzivaj flag stop
			if(!stop){
				$active = $('#'+id+' .paging a.active').next();
				if ( $active.length === 0) {$active = $('#'+id+' .paging a:first');}
				rotateId(id);
			}
		}, 5000);
	};
	
	//inicializacia rotatorov
	$('.main_view').each(function(index) {
		rotateSwitchId("bid"+index);
	});
	
	
	$(".main_view").hover(function() {
		stop = true;
	}, function() {
		var idElement = ($(this).attr("id"));
		stop = false;
		$('.main_view').each(function(index) {
			clearInterval(play);
			rotateSwitchId("bid"+index);
		});
//		rotateSwitchId(idElement);
	});	
	

	//On mouseover
	$(".paging a").live('mouseover',function(e) {	
		var idElement = ($(this).parent().parent().attr("id"));
		clearInterval(play);
		$active = $(this);
		rotateId(idElement);
		return false; //Prevent browser jump
	});	
	
});

