$(document).ready(function(){
	$('#wrapper').append('<ul id="imageGallery"></ul>');
	// Image Sources
	var images = new Array();
		images[0] = '/images/rotator/image1_1024x768.jpg';
		images[1] = '/images/rotator/image2_1024x768.jpg';
		images[2] = '/images/rotator/image3_1024x768.jpg';
		images[3] = '/images/rotator/image4_1024x768.jpg';
		images[4] = '/images/rotator/image5_1024x768.jpg';
		images[5] = '/images/rotator/image6_1024x768.jpg';
		images[6] = '/images/rotator/image7_1024x768.jpg';
		images[7] = '/images/rotator/image8_1024x768.jpg';
		images[8] = '/images/rotator/image9_1024x768.jpg';
		images[9] = '/images/rotator/image10_1024x768.jpg';
		images[10] = '/images/rotator/image11_1024x768.jpg';
		images[11] = '/images/rotator/image12_1024x768.jpg';
		images[12] = '/images/rotator/image13_1024x768.jpg';
	
	var setOne = new Array();
		
		setOne[0] = images[1-1];
		setOne[1] = images[2-1];
		setOne[2] = images[7-1];
		setOne[3] = images[8-1];
		setOne[4] = images[12-1];
		setOne[5] = images[5-1];
			
	var setTwo = new Array();
		
		setTwo[0] = images[2-1];
		setTwo[1] = images[4-1];
		setTwo[2] = images[6-1];
		setTwo[3] = images[11-1];
		setTwo[4] = images[10-1];
		setTwo[5] = images[13-1];
			
	var setThree = new Array();
		
		setThree[0] = images[5-1];
		setThree[1] = images[3-1];
		setThree[2] = images[10-1];
		setThree[3] = images[8-1];
		setThree[4] = images[9-1];
		setThree[5] = images[12-1];
			
	var setFour = new Array();
		
		setFour[0] = images[7-1];
		setFour[1] = images[8-1];
		setFour[2] = images[13-1];
		setFour[3] = images[6-1];
		setFour[4] = images[1-1];
		setFour[5] = images[3-1];
			
	var setFive = new Array();
		
		setFive[0] = images[13-1];
		setFive[1] = images[11-1];
		setFive[2] = images[10-1];
		setFive[3] = images[3-1];
		setFive[4] = images[7-1];
		setFive[5] = images[5-1];
			
	var setSix = new Array();
		
		setSix[0] = images[1-1];
		setSix[1] = images[7-1];
		setSix[2] = images[2-1];
		setSix[3] = images[8-1];
		setSix[4] = images[9-1];
		setSix[5] = images[13-1];
		
	var allSets = [setOne,setTwo,setThree,setFour,setFive,setSix];	
	
	var curSet = allSets[Math.floor( Math.random() * ( 5 ) )];
	
	//alert(curSet);
		
	/*
	* Add a shuffle function to Array object prototype
	* Usage :
	*  var tmpArray = ["a", "b", "c", "d", "e"];
	*  tmpArray.shuffle();
	*/
	Array.prototype.shuffle = function (){
		var i = this.length, j, temp;
		if ( i == 0 ) return;
		while ( --i ) {
			j = Math.floor( Math.random() * ( i + 1 ) );
			temp = this[i];
			this[i] = this[j];
			this[j] = temp;
		}
	};
	//images.shuffle();
		
	var max = 6 /*images.length;	*/
		
	LoadImage(0,max);
	// function of loading image
	// params: (int) index of image in array, (int) length of images array
	function LoadImage(index,max){
		// if current index is lower then max element (max-1)
		if(index<max){
			// create the LI, add loading class
			var list = $('<li></li>').attr('class','ani');
			// append to UL
			$('#imageGallery').append(list);
			// current LI
			var curr = $('#imageGallery li:eq('+index+')');
			// new image object
			var img = new Image();
			// image onload
			$(img).load(function () {
				//$(this).css('display','none'); // since .hide() failed in safari
				$(curr).append(this);
				//$(this).fadeIn('slow',function(){
						// once the current loaded, trigger the next image
						LoadImage(index+1,max);
				//});
			}).error(function () {
				// on error remove current
				$(curr).remove();
				// trigger the next image
				LoadImage(index+1,max);
			}).attr('src', curSet[index]);
		} else if(index==max){
			$('#imageGallery li:eq(0)').fadeIn('fast')			
			var animationSet = setInterval(function() {
				animation();
			}, 5000);
			loop = $('#imageGallery li').length;
		}
	}
	//alert($('#imageGallery li:eq(2)').children('img').attr('src');
	//alert(loop);
	$('img.next').mouseenter(function(){
		clearInterval(animationSet);
	}).mouseleave(function(){
		//animation();
		animationSet = setInterval(function() {
			animation();
		}, 5000);
	}).click(function(){
		animation();
	});
	
	function animation(){
		$('#imageGallery li:eq('+frame+')').fadeIn(700,function(){
			frame++;
			if(frame==loop){
				frame=0;
			}
		}).siblings('.ani').fadeOut(10);
	}
	
	frame = 0;
	$(window).resize(function() {
		//$('#pageTitle').html($('body').height()/$('body').width());
		if($('body').height()/$('body').width()>$('#imageGallery li:eq('+frame+') img').height()/$('#imageGallery li:eq('+frame+') img').width()){
			$('#imageGallery img').css({
				'width':'auto',	
				'height':'100%'
			});
		}else{
			$('#imageGallery img').css({
				'width':'100%',	
				'height':'auto'
			});
		}
	});
});
