/* KaisarCode Image Preloader *//*
This function allows you to detect when all the page's images are
fully loaded and ready to show, by creating a few objects, events and properties into the DOM:

New Object:
document.preloader -> basic object

Events:
document.ready -> Is triggered when the DOM is ready
document.preloader.oncompleteload -> Is triggered when the images are fully loaded
document.preloader.onload -> Is triggered every time that an image is loaded

Properties:
document.preloader.loaded -> [Integer] is a count of loaded images

Enjoy! >_o
*//* ****************************/

function kcImagePreloader(images){
	//Attach the event "ready" to the document
	document.ready=function(){};
	//Create the "preloader" Object
	document.preloader=new Object();
	//Attach the event "oncompleteload" to the images collection
	document.preloader.oncompleteload=new function(){};
	//Attach the property "loaded"
	document.preloader.loaded=0;
	//Attach the event "onload"
	document.preloader.onload=new function(){};
	//Detect when the DOM is accessible
	if(document.addEventListener) {
		document.addEventListener("DOMContentLoaded",preload, false);
	}
	else{
		document.onreadystatechange=function(){
			if(document.readyState=="complete"){
				preload();
			}
		}
	}
	//Initialize preloading
	function preload(){
		//Triggers document.onready event
		document.ready();
		//For each image in the DOM, we create a dummy image
		var img=new Object();
		for(i=0;i<images.length;i++){
			img[i]=new Image();
			img[i].onload=function(){
				//Destroy the dummy image
				img[document.preloader.loaded]=false;
				//increase image loaded count
				document.preloader.loaded++;
				//Triggers the images.loadedStateChange event 
				document.preloader.onload();
				//Triggers the onImagesLoaded event 
				if(document.preloader.loaded>=images.length){
					//when all images are loaded we trigger the document.preloader.oncompleteload
					document.preloader.oncompleteload();
				}
			}
			//We start to load the dummy image with the SRC of the corresponding real image
			img[i].src=images[i];
		}
	}
}
