/**
 * @project: People Choice
 * @location: Wax Interactive - www.wax.be
 * @date: 14.06.2010
 * @author: Jan De Wilde
 * @description: Core logic
 */ 

if(typeof $.PC == "undefined") {
	$.PC = Object;
}

$.PC.initialise = {
	construct		:		function() {
		// Empty
	}
}

$.PC.intro = {
	images			:	new Array(),
	timeout			:	1000,
	usedImages		:	new Array(),
	
	fadeImage		:	0,
	fadeOne			:	new Array(2,7,5,3,0,4,10,1,13,6,11,8,12,9,14),
	fadeTwo			:	new Array(4,13,5,10,11,8,1,3,0,12,9,6,2,14,7),
	fadeThree		:	new Array(7,2,8,12,4,9,3,0,10,1,13,14,5,6,11),
	
	construct		:		function() {
		$.PC.intro.images = $('#images').text().split(';');

		var ammount = $('#images').text().split(';').length-1;
		var count = 15;
		var index = 0;
		while(index < count) {
			var imgIndex = Math.floor(Math.random()*ammount)+1;
			while($.PC.intro.in_array($.PC.intro.usedImages,imgIndex)) {
				imgIndex = Math.floor(Math.random()*ammount)+1;
			}
			var element = '<div id="img'+(index+1)+'" class="image"><img src="'+$.PC.intro.images[imgIndex]+'" alt="" /></div>';
			$('#intro').append(element);
			$.PC.intro.usedImages.push(imgIndex);
			index = index+1;
		}

		setTimeout($.PC.intro.adapt,$.PC.intro.timeout);

	},
	
	in_array		:		function(arr,element) {
		var retur = false;
		for (var values in arr) {
			if (arr[values] == element) {
				retur = true;
				break;
			}  
		}
		return retur;
	},
	
	fadeOut			:		function(fadeArray) {
		if($.PC.intro.fadeImage < 15) {
			$('#img'+(fadeArray[$.PC.intro.fadeImage]+1)+' img').fadeOut("slow",function(){
				$.PC.intro.fadeImage = $.PC.intro.fadeImage +1;
				$.PC.intro.fadeOut(fadeArray);
			});
		} else {
			$('#grid').fadeOut();
		}
	},
	
	adapt			:		function() {
		var fadeArr = Math.floor(Math.random()*3);
		switch(fadeArr) {
			case 1:
				$.PC.intro.fadeOut($.PC.intro.fadeTwo);
			break;
			case 2:
				$.PC.intro.fadeOut($.PC.intro.fadeThree);
			break;
			case 0:
			default:
				$.PC.intro.fadeOut($.PC.intro.fadeOne);
			break;
		}
	}
}

$(document).ready(function(){
	$.PC.intro.construct();
});
