/**

 * @author Agne Lund, Artjom Kurapov, Margus Lamp

 * @since 18.05.11 17:05

 */



var oAutoRotatorTimer;

var bAutoScrollerDirectionRight=true;



$(document).ready(function(){

	//Frontpage banner

	if($('#frontbanner').length>0){

		theRotator();

	}

	if($('#scroller').length>0){

		initScroller();







		oAutoScrollTimer = setInterval(function() {

			if(bAutoScrollerDirectionRight){

				$('#scroller .btnRight').click();

			}

			else{

				$('#scroller .btnLeft').click();

			}



			if($('#scroller .btnRight').hasClass('disabled') || $('#scroller .btnLeft').hasClass('disabled')){

				bAutoScrollerDirectionRight = !bAutoScrollerDirectionRight;

			}



		}, 3000);

	}

});

/* Rotate image */

var imagecount; 
function theRotator() {

	//Set the opacity of all images to 0

	$('#frontbanner ul.slides li').css({display: 'none'});



	imagecount = $('#frontbanner ul.slides li').length;

	var i = 1;

	$('#frontbanner ul.slides li').each(function(){

		$(this).css("z-index",i*10);

		i++;

	});



	var i = 1;

	$('#frontbanner ul.slides li .more').each(function(){

		$(this).css("z-index",(i+1)*10);

		i++;

	});



	//Get the first image and display it (gets set to full opacity)

	$('#frontbanner ul.slides li:first').css({display: 'block'});

	$('#frontbanner ul.bullets').empty()

	for(var i=0;i<imagecount;i++){

		$('#frontbanner ul.bullets').append('<li><a href="#"></a></li>');

	}



	$('#frontbanner ul.bullets li:first').addClass("active");

	$('#frontbanner ul.bullets li a').click(function(){

		$('#frontbanner ul.bullets li').removeClass("active");

		$(this).parent().addClass("active");

		var pos = $('#frontbanner ul.bullets li').index($(this).parent());

		rotate(pos);

		resetRotateTimer();

	});



	$('#frontbanner .btnLeft').click(function(){

		var current = ($('#frontbanner ul.slides li.show')?  $('#frontbanner ul.slides li.show') : $('#frontbanner ul.slides li:first'));

		if ( current.length == 0 ) current = $('#frontbanner ul.slides li:first');

		var pos = $('#frontbanner ul.slides li').index(current);

		if(pos > 0){

			rotate(pos-1)

		}

		else{

			rotate($('#frontbanner ul.slides li').length-1)

		}

		resetRotateTimer();

		return false;

	});



	$('#frontbanner .btnRight').click(function(){

		var current = ($('#frontbanner ul.slides li.show')?  $('#frontbanner ul.slides li.show') : $('#frontbanner ul.slides li:first'));

		if ( current.length == 0 ) current = $('#frontbanner ul.slides li:first');

		var pos = $('#frontbanner ul.slides li').index(current);

		if(pos<$('#frontbanner ul.slides li').length-1){

			rotate(pos+1);

		}

		else{

			rotate(0);

		}

		resetRotateTimer();

		return false;

	});

	setRotateTimer();

}



function setRotateTimer() {
	if(imagecount>1){

		oAutoRotatorTimer = setTimeout(function() {

			rotateToNext();

			resetRotateTimer();

		}, 5000);

	}
}



function killRotateTimer() {
	if(imagecount>1){

		clearTimeout(oAutoRotatorTimer);

		oAutoRotatorTimer = null;

	}

}



function resetRotateTimer() {

	if(imagecount>1){
		
		killRotateTimer();

		setRotateTimer();

	}

}



function rotateToNext() {

	var current = ($('#frontbanner ul.slides li.show') ? $('#frontbanner ul.slides li.show') : $('#frontbanner ul.slides li:first'));

	if ( current.length == 0 ) current = $('#frontbanner ul.slides li:first');

	var pos = $('#frontbanner ul.slides li').index(current);

	if(pos < $('#frontbanner ul.slides li').length-1){

		rotate(pos+1);

	}

	else {

		rotate(0);

	}

}



function rotate(pos) {

	//Get the first image

	var current = ($('#frontbanner ul.slides li.show')?  $('#frontbanner ul.slides li.show') : $('#frontbanner ul.slides li:first'));

	var currentdot = ($('#frontbanner ul.bullets li.active')?  $('#frontbanner ul.bullets li.active') : $('#frontbanner ul.bullets li:first'));



    if ( current.length == 0 ) current = $('#frontbanner ul.slides li:first');

	if ( currentdot.length == 0 ) currentdot = $('#frontbanner ul.bullets li:first');



	//Get next image, when it reaches the end, rotate it back to the first image

	if(pos>=0){

		var next = $('#frontbanner ul.slides li').eq(pos);

		var nextdot = $('#frontbanner ul.bullets li').eq(pos);

	}

	else{

		var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#frontbanner ul.slides li:first') :current.next()) : $('#frontbanner ul.slides li:first'));

		var nextdot = ((currentdot.next().length) ? ((currentdot.next().hasClass('active')) ? $('#frontbanner ul.bullets li:first') :currentdot.next()) : $('#frontbanner ul.bullets li:first'));

	}



	//Set the fade in effect for the next image, the show class has higher z-index

	next.css({display: 'none'})

	.addClass('show')

	.fadeIn('slow');



	//Hide the current image

	current.fadeOut('slow')

	.removeClass('show');



	currentdot.removeClass("active");

	nextdot.addClass("active");

}


