var debug = false;

var bildArray = new Array(); 
var current = new Array();
var itemListsInt = 0;
var singleFading = false;

var fadingTime = 400;
var waitngTime = 2000;
var loopTimer = 1000;

if (itemLists == null) {
	var itemLists = new Array();
}

function randomInt(minVal,maxVal,floatVal) {
  var randVal = minVal+(Math.random()*(maxVal-minVal));
  return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
}


function waitAfterSwitchingPics() {
	if (debug) {
		fireunit.ok(true, "started waitAfterSwitchingPics()");
	}
	$(this).oneTime(waitngTime, "show", function() {
		$(document).everyTime(loopTimer, function() {
			switchPic();
		});
	});
}

function switchPic() {
	if (debug) {
		fireunit.ok(true, "started switchPic()");
	}
	var img = new Image();
	if(itemList.length == 0) {
		//itemList = new Array();
		//jQuery.extend(true, itemList, itemListCopy);
		singleFading = false;
		if(itemListsInt < itemLists.length-1) {
			itemListsInt++;
		} else {
			itemListsInt = 0;
		}
		itemList = new Array();
		jQuery.extend(true, itemList, itemLists[itemListsInt]);
		if(itemList.length == 3) {
			singleFading = true;
		}
		$(document).stopTime();
		waitAfterSwitchingPics();
	} else {
		if( itemList.length >= 2 && singleFading == false) {
			var i = randomInt(0,(itemList.length-1));
			var item = itemList.splice(i,1);
			switchPicByItem(item[0].pos, item[0].image);
	
			var i = randomInt(0,(itemList.length-1));
			var item = itemList.splice(i,1);
			switchPicByItem(item[0].pos, item[0].image);
		} else {
			var i = randomInt(0,(itemList.length-1));
			var item = itemList.splice(i,1);
			switchPicByItem(item[0].pos, item[0].image);
		}
	}
}


function switchPicByItem(pos, url) {
	if (current[pos] == null) {
		current[pos] = $(pos + ' img').attr('src');
	}
	if (debug) {
		fireunit.ok(true, "started switchPicByItem(" + pos + ", " + url + "). replace: " + current[pos]);
	}
	//alert(current);
	$(pos).crossSlide({
		  sleep: 1,
		  fade: 0.5,
		  loop: 1
		}, [
		  { src: current[pos] },
		  { src: 'fileadmin/templates/images/background-images/' + url }
		]);
	current[pos] = 'fileadmin/templates/images/background-images/' + url;

	/*
	var img = new Image();
	$(pos).fadeTo(fadingTime, 0, function() {
		$(img).load(
			function () {
				$(pos).replaceWith(this);
				$(this).hide();
				$(pos).attr('title', 'Mosaik');
				$(pos).attr('alt', 'Mosaik');
				$(pos).attr('border', '0');
				$(this).fadeIn(fadingTime);
			}
		).error(function () {
			// notify the user that the image could not be loaded
		}).attr('src', 'images/background-images/' + url);
	});
	*/
}


$.fn.exists = function() { return this.length>0; };


function init() {
	if (debug) {
		fireunit.ok(true, "method init() started.");
	}
	/*
	//check the existence of the imgage boxes
	if ( $('#box_1 img').exists() ) {
		bildArray.push( $('#box_1 img') );
	}
	if ( $('#box_2 img').exists() ) {
		bildArray.push( $('#box_2 img') );
	}
	if ( $('#box_3 img').exists() ) {
		bildArray.push( $('#box_3 img') );
	}
	if ( $('#box_4 img').exists() ) {
		bildArray.push( $('#box_4 img') );
	}
	if ( $('#box_5 img').exists() ) {
		bildArray.push( $('#box_5 img') );
	}
	if ( $('#box_6 img').exists() ) {
		bildArray.push( $('#box_6 img') );
	}
	if ( $('#box_7 img').exists() ) {
		bildArray.push( $('#box_7 img') );
	}
	if ( $('#box_8 img').exists() ) {
		bildArray.push( $('#box_8 img') );
	}
	if ( $('#box_9 img').exists() ) {
		bildArray.push( $('#box_9 img') );
	}
	//clone bildArray
	bildArrayCopy = new Array();
	jQuery.extend(true, bildArrayCopy, bildArray);

	//deep copy the original array
	itemListCopy = new Array();
	jQuery.extend(true, itemListCopy, itemList);
	 */

	//deep copy the original array
	itemList = new Array();
	jQuery.extend(true, itemList, itemLists[itemListsInt]);
	if(itemList.length == 3) {
		singleFading = true;
	}
	if (debug) {
		fireunit.ok(itemList.length == 3, "itemList.length == 3");
	}

	$(document).everyTime(loopTimer, function() {
		if (debug) {
			fireunit.ok(true, "initialized loop for switchPic()");
		}
		switchPic();
	});
}


$(document).ready(
	function(){
		/*
		$('#box_2').crossSlide({
			  sleep: 1,
			  fade: 0.5,
			  loop: 1
			}, [
			  { src: 'images/background-images/startseite/1-weltmeyer/bild2.jpg' },
			  { src: 'images/background-images/startseite/2-triviant/bild2.jpg'   }
			]);
		*/
		if(itemLists.length != 0) {
			if (debug) {
				fireunit.ok(true, "document ready.");
			}
			init();
		}
	}
);
