/* DAVID ZORYCHTA & DIMITRY 
   VINOGRADOV 
   MACMEE@GMAIL@COM */

//this script will keep checking an image until it's loaded then run an inline function
function imageLoaded(id,runcode)
{
	//if image not loaded yet check again in 1/10 seconds then run inline function 
	if($(id).height() == 0)
		setTimeout(function(){ imageLoaded(id,runcode) },100);
	else
		return runcode.call(this,id);	
}

//turn a image into a group of div boxes
var rand_imgGen = 0;
function imageBoxCreate(selector,runcode,xfullsize,yfullsize)
{
	imageLoaded(selector,function(src){ 

		//build dimensions & data
		rows = 8;
		cols = 4;
		if(xfullsize===undefined) xfullsize = $(src).width();
		if(yfullsize===undefined) yfullsize = $(src).height();
		xsize = Math.floor(xfullsize / rows);
		ysize = Math.floor(yfullsize / cols);
		containername = rand_imgGen++;
		imagepath = $(src).attr('src');
		
		//insert container
		$('<div id="' + containername + '" class="' + containername + '" style="display: ' + $(src).css('display') + '; position: relative; width: ' + $(src).width() + 'px; height: ' + $(src).height() + 'px;"></div>').insertAfter(src);
	
		//hide image 
		$(src).remove();
		
		//insert blocks
		for(y = 0; y < cols; y++)
		{
			ysize_use = ysize;
			if(y==(cols-1)) ysize_use = yfullsize - (ysize*(cols-1));
		
			var xsize_use;
			for(x = 0; x < rows; x++)
			{
				xsize_use = xsize;
				if(x==(rows-1)) xsize_use = xfullsize - (xsize*(rows-1));
				$('#' + containername).append('<div id="' + containername + '_' + x + '_' + y + '" style="position: absolute; left: ' + xsize*x + 'px; top: ' + ysize*y + 'px; width: ' + xsize_use + 'px; height: ' + ysize_use + 'px; background-image: url(' + imagepath + '); background-position: ' + xsize*-x + 'px ' + ysize*-y + 'px;"></div>');
			}
		}

		runcode();
		
	});
}

function imageBoxFadeIn(selector)
{
	var delay = 0;
	$(selector).show();
	$(selector).find('div').hide();
	$(selector).find('div').each(function(){
		setTimeout('$("#' + $(this).attr('id') + '").fadeIn();',delay);
		delay = delay + 100;
	});
}

function makeSlideshow(selector,itemwidth,itemheight,duration,images)
{
	//show base image and add necessary css things
	$(selector).css('position','relative').css('width',itemwidth + 'px').css('height',itemheight + 'px');
	$(selector).append('<div id="containerslidwshowimages_0"><img id="slidwshowimages_0" src="' + images[0] + '"></div>');

	//find amount of images, make a count
	var image_amount = images.length;
	var imagesincriment = 0;
	
	//function that inserts images
	function doit(insert)
	{
		//dont go beyond how many images we have in the iterator
		if(insert >= image_amount) insert = 0;
		
		//insert image
		$(selector).append('<div style="position: absolute; top: 0; left: 0;" id="containerslidwshowimages_' + ++imagesincriment + '"><img style="display: none;" id="slidwshowimages_' + imagesincriment + '" src="' + images[insert] + '"></div>');
		
		//fade image in 
		imageBoxCreate('#slidwshowimages_' + imagesincriment,function(){
			imageBoxFadeIn($('#containerslidwshowimages_' + imagesincriment).find('div'));
		},itemwidth,itemheight);
		
		//delete old images so it doesnt load slow
		$('#containerslidwshowimages_' + (imagesincriment - 3)).remove();
		
		//repeat after 3 seconds
		setTimeout(function(){ doit(++insert) },duration);
	}
	
	//load in the first image 
	setTimeout(function(){ doit(1) },duration);
}
