var in_view = new Array(1,6,3,8,9,2,4,5,12,10,7,11);

$(document).ready(function()
{

	//Logos
	$("#middle-column .single-logo a img").hover(function()
	{
		 $(this).animate({opacity: 0.5}, 300);
	},
	function()
	{
		$(this).animate({opacity: 1.0}, 300);
	});			
	

		setInterval(animateLogos, 1200);

	
});



function animateLogos()
{
	//random is the image to be swapped in
	var random = getRandom();
	
	//keep getting random numbers until we find a new image
	//do not swap the image in if its already in view
	while($.inArray(random, in_view) != -1)
	{
		random = getRandom();
	}
		
	var swap_out = in_view.pop();//Remove the next image to be swapped out from our in_view array
	in_view.unshift(random); //put the new image to be displayed at front of array
	
	//find the correct image to fade out
	$("#middle-column .single-logo a img").each(function()
	{
		var src = $(this).attr("src");
		var filename = getFilename(src);
		
		if(filename == swap_out)//Found
		{
			var newSrc = src.replace(filename, random);//set new src
			var parent = $(this).parent("a").parent(".single-logo");
			parent.css("background-image", "url(" + newSrc + ")");
			
			var self = $(this);
			$(this).animate( { "opacity": 0 }, 1500, function()
			{
				self.attr("src", newSrc);
				self.css("opacity", "1");
				parent.css("background-image", "url()");
			});
			
			return false; //break out the loop
		}
	});	
}

function getRandom()
{
	return Math.floor(Math.random()*24+1);
}

function getFilename(url)
{
   if (url)
   {
      var m = url.toString().match(/.*\/(.+?)\./);
      if (m && m.length > 1)
      {
         return m[1];
      }
   }
   return "";
}



