
SKEL.Image = {
	imageCache: { },
	slot: 0,
	imageUrls: Array(),
	onLoadFunc: 0,
	onCompleteFunc: 0,

	preload: function(url,onLoadFunc,onLoadSuperFunc,args){
		//var image = new Image();
		//safari 2 only supports the following line, not new Image()
		var image = document.createElement('img');
		if(onLoadFunc){
			image.onload = onLoadFunc;
		}

		if(onLoadSuperFunc){
			image.skel_image_onload_callback = onLoadSuperFunc;
		}

		if(args){
			image.args = args;
		}
		//image.onerror = onErrorFunc;
		//image.onabort = onAbortFunc;
		image.src = url;
		SKEL.Image.imageCache[url] = image;
		return image;
	},

	preloadNext: function(obj){
		if(obj){
			if(SKEL.Image.onLoadFunc){
				SKEL.Image.onLoadFunc(obj);
			}
		}

		var url = SKEL.Image.imageUrls[SKEL.Image.slot++];
		if(url){
			//console.log('preloadNext',url);
			setTimeout(function(){
				SKEL.Image.preload(url,SKEL.Image.preloadNext);
			},50); //this timeout assists with IE to make it not have a script issue

		}
	},

	//preloads in order
	preloadArray: function(imageUrls,onLoadFunc,onCompleteFunc){
		SKEL.Image.imageUrls = imageUrls;
		SKEL.Image.onLoadFunc = onLoadFunc;
		SKEL.Image.onCompleteFunc = onCompleteFunc;
		//setTimeout(SKEL.Image.preloadNext,1000); //this timeout fixes an issue with IE
		SKEL.Image.preloadNext();

		/*for(key in imageUrls){

			var nextUrl = imageUrls[key+1];
			if(nextUrl){*/

		//		SKEL.Image.preload(url,SKEL.Image.preloadCallback,SKEL.Image.preload,nextUrl);
			//}
		//}
	},

	//this handles our callback handling with arguments
	preloadCallback: function(event){
		var obj = event.currentTarget;
		if(obj.skel_image_onload_callback){

		}
	},

	scale: function(image,newWidth,newHeight,lockRatio){
		if(lockRatio == '')
			lockRatio = true;

		var ratio = 0;

		//console.log('arg ['+newWidth+']['+newHeight+']');
		//console.log('pre ['+image.width+']['+image.height+']['+ratio+']');

		if(image.width > newWidth || image.height > newHeight){
			if(image.width >= image.height){
				ratio = newWidth / image.width;
				image.width = newWidth;
				image.height = Math.round(image.height*ratio);
			} else if (image.height > image.width) {
				ratio = newHeight / image.height;
				image.height = newHeight;
				image.width = Math.round(image.width*ratio);
			}
		}

		//console.log('post ['+image.width+']['+image.height+']['+ratio+']');
	}
}
