// JavaScript Document

$(function() {
	var INTERVAL;
	var NR_total = 32;
	var NR_start = 5;
	var lista_pre = []
	var lista_post = [];
	init();
	

	
	function slideSwitch(_m) {
		var $active = $('#slideshow img.active');
		if ( $active.length == 0 ) $active = $('#slideshow img:last');
	
		// use this to pull the images in the order they appear in the markup
		var $next =  $active.next().length ? $active.next() : $('#slideshow img:first');
	
		// uncomment the 3 lines below to pull the images in random order
		
		 var $sibs  = $active.siblings();
		 var rndNum = Math.floor(Math.random() * $sibs.length );
		 var $next  = $( $sibs[ rndNum ] );
	
		clearTimeout(INTERVAL);
		var m = (_m == 1)?4000:0;
		INTERVAL = setTimeout( slideSwitch, 6000 + m );	
		$active.addClass('last-active');
		$next.css({opacity: 0.0})
			.addClass('active')
			.clearQueue()
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
					
			});	
		if (_m == 1) genereaza(lista_post);
		if (!BLOC_SCHIMB_REVIEW) show_review();
		}

	function randomSort(a,b) {
		// Get a random number between 0 and 10
		var temp = parseInt( Math.random()*NR_total );
		// Get 1 or 0, whether temp is odd or even
		var isOddOrEven = temp%2;
		// Get +1 or -1, whether temp greater or smaller than 5
		var isPosOrNeg = temp>1 ? 1 : -1;
		// Return -1, 0, or +1
		return( isOddOrEven*isPosOrNeg );
		}

	function genereaza(L){
		//console.log(L);
		for (j=1;j<=L.length-1;j++){
			$('<img src="img/slideshow/background' + L[j] + '.jpg" alt="Slideshow" />').appendTo('#slideshow');
			}
		}
		

	function init(){
		var lista = [];

		for (i=1;i<=NR_total;i++){lista.push(i);}
		lista.sort(randomSort);
		//console.log(lista);
		lista_pre = lista.slice(0,NR_start);
		lista_post = lista.slice(NR_start,NR_total);
		
		genereaza(lista_pre);
		slideSwitch(1);

		$("#bt_slideshow").click(function(e){
			e.preventDefault();
			clearTimeout(INTERVAL);
			INTERVAL = setTimeout( "slideSwitch", 5000 );
			slideSwitch();
			});
					
		}

	});
