/**
 * To Do:
 *
 * Allow only one of the same image to be shown.
 * Make transition animated.
 * Add image preloader.
 *  
 */ 

//Intiates the image swap
startSwap = function(placeholder, imageList,hrefList, period) {
	var imageListLength   = imageList.length; //Number of items in array
	var placeholderLength = $('.'+placeholder).length; //Number of placeholders
	
	//Sets period if not set
	if(period < 1000) {
		period = 1000;
	}
	
	//Runs change image if images provided
	else if(imageListLength > 0) {
		setInterval(function(){changeImage(placeholder, placeholderLength, imageList,hrefList, imageListLength)}, period);
	}
}

//Changes random image
changeImage = function(placeholder, placeholderLength, imageList, hrefList, imageListLength) {
	var placeholderPositon = randomNumber(placeholderLength); //Random Position
	var imagePositon       = randomNumber(imageListLength); //Random Image
	
	//Create array of current images
	//$('.'+placeholder).each('', function(i, val) {
    //	alert(val);
    //});

	//Changes image to random

    $('.'+placeholder).fadeOut(2000,function(){
		$('.'+placeholder+':eq('+placeholderPositon+')').attr('src', imageList[imagePositon]);
		$('#'+placeholder+':eq('+placeholderPositon+')').attr('href',hrefList[imagePositon]);
	});
		
   	$('.'+placeholder).fadeIn(1000);
    
	// $('.'+placeholder+':eq('+placeholderPositon+')').attr('src', imageList[imagePositon]);
	//$('#'+placeholder+':eq('+placeholderPositon+')').attr('href',hrefList[imagePositon]);
   

	//$('.'+placeholder+':eq('+placeholderPositon+')').attr('src', imageList[imagePositon]);
	//$('#'+placeholder+':eq('+placeholderPositon+')').attr('href',hrefList[imagePositon]);
    
}

//Creates a random number
randomNumber = function(maximum){
	return Math.floor(maximum * (Math.random() % 1));
}
