 /**
  * Random Image Rotation Script
  * Auther: Brenton Bull
  * Company: Fraynework Multimedia (http://www.fraynework.com.au)
  */
var imageSet = 1;
var totalImages = 29
function rotateImage()
{
	switch(imageSet)
	{
		case 1:
			_rotateImage(1);
			break;
		case 2:
			_rotateImage(2);
			break;
		case 3:
			_rotateImage(3);
			break;
		case 4:
			_rotateImage(4);
			break;
	}
	nextSet = $.randomBetween(1,4);
	do
	{
		nextSet = $.randomBetween(1,4);
	} while(nextSet == imageSet)
	debug(nextSet);
	imageSet = nextSet;
 }
 
 function _rotateImage(cell)
 {
	//debug('[1]: [2]: [3]: [4]:');
	//debug(cell);
	//debug("[CurrentImage] ("+$('.cell' + cell).attr('id')+") Classes: "+$('.cell' + cell).attr('class'));
	$currentImage = $('.cell' + cell);
	//$.random(totalImages)
	//debug("Next Image: "+$nextImage);
	//$imageNo = $.random(totalImages);
	//debug('#image'+$.strPad($imageNo,3));
	//$nextImage = $('#image'+$.strPad($imageNo,3));
	
	do {
		
		$imageNo = $.randomBetween(1,totalImages);
		$nextImage = $('#image'+$.strPad($imageNo,3));
		//debug($imageNo + " -- " + $nextImage.attr('id'));
	} while($currentImage == $nextImage || $nextImage.hasClass('cell1') || $nextImage.hasClass('cell2') || $nextImage.hasClass('cell3') || $nextImage.hasClass('cell4'))
	
	//debug("[Next Image] ("+$nextImage.attr('id')+")");
	/*
	$currentImage.fadeOut(1000, function(){
		$currentImage.removeClass('cell'+cell);
		$nextImage.fadeIn(1000).addClass('cell'+cell);
	});*/
	
	$currentImage.fadeOut(1500, function() { $currentImage.removeClass('cell'+cell); });
	$nextImage.fadeIn(2000).addClass('cell'+cell);
	/*
	$nextImage.fadeIn(function(){
		
	});
	*/
 }
 
jQuery.extend({
	random: function(X) {
	    return Math.floor(X * (Math.random() % 1));
	},
	randomBetween: function(MinV, MaxV) {
	  return MinV + jQuery.random(MaxV - MinV + 1);
	},
	strPad: function(i,l,s) {
		var o = i.toString();
		if (!s) { s = '0'; }
		while (o.length < l) 
		{
			o = s + o;
		}
		return o;

	}
});

function debug(message)
{
	return false;
	$('#debug').val(message+"\n"+$('#debug').val());
}
  
  $(document).ready(function(){
      debug('Script Started');
	  setInterval('rotateImage()', 3000);
});


