var imgArr = new Array();
var imgArr1 = new Array();
var first = true;


(function($){

 $.fn.shuffle = function() {
 return this.each(function(){
 var items = $(this).children().clone(true);
 return (items.length) ? $(this).html($.shuffle(items)) : this;
 });
 }

 $.shuffle = function(arr) {
 for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
 return arr;
 }

})(jQuery);


jQuery.preloadImages = function() {
	var a = (typeof arguments[0] == 'object')? arguments[0] : arguments;
	for(var i = a.length -1; i > 0; i--) {
		jQuery("<img>").attr("src", a[i]);
	}
}

/*
$.preloadImages(horizontal);
$.preloadImages(vertical);
*/
var iL = 0;

function getHorizontalImage()
{
	
	if(imgArr.length == 0)
	{
		$("#preloader img").each(function() { imgArr.push($(this).attr("src")); imgArr1.push($(this).attr("src"));});
		//imgArr = imgArr1;
	}
	
	imgArr = $.shuffle(imgArr);
	return imgArr.pop();
}

function getVerticalImage()
{

	if(imgArr.length == 0)
	{
		$("#preloader img").each(function() { imgArr.push($(this).attr("src")); imgArr1.push($(this).attr("src"));});
		//imgArr = imgArr1;
	}
	
	imgArr = $.shuffle(imgArr);
	return imgArr.pop();
}

var image1 = document.getElementById("image1");
var image2 = document.getElementById("image2");

var aSpeed = 200;




$(document).ready(
function()
{
	$("#preloader img").each(function() { imgArr.push($(this).attr("src")); imgArr1.push($(this).attr("src"));});
	iL = imgArr.length;
  image1 = document.getElementById("image1");
  image2 = document.getElementById("image2");

  loadfirst();
}
);

function loadfirst()
{
	var img = getHorizontalImage();
	image1.src = img;	
	setTimeout(bottom_start,1500);
}


function bottom_start()
{

	$("#imageDiv1").animate({"top": "-=131px"},aSpeed);
	 setTimeout(bottom_end,2000);
}

function bottom_end()
{
	
	$("#imageDiv1").animate({"top": "-=131px"},aSpeed);
	 setTimeout(Image2Change,10);
}

function Image2Change()
{
	var img = getHorizontalImage();
	image2.src = img;	
	setTimeout(right_start,990);
}

function right_start()
{
	 $("#imageDiv2").animate({"right": "+=239px"},aSpeed);
	 setTimeout(right_end,2000);
}

function right_end()
{
	 $("#imageDiv2").animate({"right": "+=239px"},aSpeed);
	 setTimeout(Image1Change,10);
}

function Image1Change()
{
	var img = getVerticalImage();
	image1.src = img;
	setTimeout(top_start,990);
}

function top_start()
{
	
	
	$("#imageDiv1").animate({"top": "+=131px"},aSpeed);
	
	setTimeout(top_end,2000);
}

function top_end()
{
	
	$("#imageDiv1").animate({"top": "+=131px"},aSpeed);
	setTimeout(Image3Change,10);
}

function Image3Change()
{
	var img = getHorizontalImage();
	image2.src = img;	
	setTimeout(left_start,990);
}

function left_start()
{
	 
	 $("#imageDiv2").animate({"right": "-=239px"},aSpeed);
	 setTimeout(left_end,2000);
}

function left_end()
{
	 $("#imageDiv2").animate({"right": "-=239px"},aSpeed);
	 setTimeout(Image4Change,10);
}

function Image4Change()
{
	var img = getVerticalImage();
	image1.src = img;
	setTimeout(bottom_start,990);
}


