jQuery.noConflict();

//transition time, minimum time, maximum time
var transTime = 300;
var minTime = 3000;
var maxTime= 10000;

//incase we add other features with mouse over and a shuffle
var over = false;
var fast = false;
var fall = false;

//url data
var siteBase = "/"
var base = siteBase + "media/homepage/"

//picture variable and link variable
var myList = [, new Array(), new Array(), new Array()];
var myLink = [, new Array(), new Array(), new Array()];

//random global variable
var myRand = 0;


jQuery(document).ready(function()
{
	//3 arrays of images -  colors are 0

	

	getList(base + "left/", 1,0);
	getList(base + "middle/", 2,0); 
	getList(base + "right/", 3,1);
	

	
});


//executes after the last xml is loaded
function runMain(){
	
	
	//calls my function for each array list
				setTimeout(function() {
									myFunction(myList[2], 0, 0, over, fast, fall, 2);	
									}, Math.floor(Math.random()*1000));
			setTimeout(function() {
							  myFunction(myList[1], 0, 0, over, fast, fall, 1);
							},1000 +Math.floor(Math.random()*2000));
			
			setTimeout(function() {
							  myFunction(myList[3], 0, 0, over, fast, fall, 3);
							}, 1000+Math.floor(Math.random()*2000));

}

	//timered function for each placeholder
	function myFunction(myPics, prev, prev2, over, fast, fall, where){
	
		//previous
		var img1 = prev;
		//new image, make sure not the same
		var img2 = Math.floor(Math.random()*(myPics.length));
		
		//no repeating images
		while(img2 == prev2 || img2 == prev)
		{
			img2 = (img2 +1 )% myPics.length;//Math.floor(Math.random()*(myPics.length));
		}
		
		
		//call the switch function when it is time if the mouse over event is false
		if(over == false)
		{
			mySwitch (img2, where);
		}
		else
		{
			
		}
	
		//reassigns  previous image
		prev2 = prev;
		prev = img2;
		
		myRand = minTime + Math.floor(Math.random()*(maxTime-minTime)+1);
		
		//executes it again
		setTimeout(function() {
							  myFunction(myPics, prev, prev2, over, fast, fall,where);
							}, myRand);
	};
	
	
		/*********    switches the images with fade    *******/
	function mySwitch ( img1, where)
	{
		var myEle = jQuery("#homepage-"+where+" a  img").clone().attr('src',myList[where][img1]);
		jQuery("#homepage-"+where+" a img").addClass("removeMe").css("z-index",2);
		jQuery("#homepage-"+where+" a").append(myEle);
		jQuery("#homepage-"+where+" a img.removeMe").fadeOut(1200, function() { 
																				 jQuery(this).remove(); });
		jQuery("#homepage-"+where+" a").attr('href', myLink[where][img1]);
	};

	
	function getList(folder, where, runNext) {
		 var myArray = new Array();
		 var i=0;
	
		 jQuery.ajax({
					 type:"GET",
					 url: folder+"list.xml",
					 dataType: "xml",
					 success: function(xml) {
						 jQuery(xml).find('name').each(function(){
																
								myList[where][i] = folder+jQuery(this).text();

								i++;
								
						 });
						 
						 i=0;
						  jQuery(xml).find('link').each(function(){
																
								myLink[where][i] = siteBase+jQuery(this).text();

								i++;
								
						 });
						 
						if(runNext ==1)
						 	{runMain();
							}
						 
					 }
				 
	});
	 	
}

