/***************************************************************************
****************************************************************************

	Name: swzImageLoad
	Requirements : prototype.js

	x = new swzImageLoad({
		src : 'this source file of the image you want to load',
		onCreate:function(){what to do when the image is requested},
		onSuccess:function(){what to do once the image is loaded},
		onFailure:function(){what to do if the image loading fails},
		onComplete:function(){notice that the request is completed}
	})

****************************************************************************
***************************************************************************/



	var swzImageLoad = Class.create({
	
		initialize:function(options){
			
			this.name = 'swzImageLoad';

			//create the options!
			this.options = {
				src			: '',
				onCreate	: '',
				onSuccess	: '',
				onFailure	: '',
				onComplete	: ''
			}
			Object.extend(this.options,options || '');
					
			//call the loader based on if it's an array or one file!
			if(Object.isArray(this.options.src)){
				this.loadImages(options);
			} else {
				this.loadImage(options);
			}
		},
		
		//loadImage()
		//use to load only one image
		loadImage:function(){
			if(this.options.onCreate != ''){
				this.options.onCreate(this.options.src);
			}
			
			//create a new image object
			this.image 			= new Image();
			
			//create a nice small object to use!
			img = this.image;
			
			//set what happens when the image loads!
			img.onload = this.success.bindAsEventListener(this);
			img.onabort = this.failure.bindAsEventListener(this);
			img.onerror = this.failure.bindAsEventListener(this);
			
			//load the image
			img.src = this.options.src;
			
		},
		
		//loadImages()
		//use to load all the images
		loadImages:function(){
			
			arr = this.options.src;
			obj = this;
			arrSuccess = new Array();
			arrFailure = new Array();
			
			arr.each(function(item){
				new swzImageLoad({
					src : item,
					onSuccess:function(transport){
						arrSuccess[arrSuccess.length] = item;
					},
					onFailure:function(transport){
						arrFailure[arrFailure.length] = item;
					},
					onComplete:function(transport){

						//check we've loaded all the images!
						if(arrSuccess.length + arrFailure.length == arr.length){
							
							//create the transport
							transport = {
								success : arrSuccess,
								failure : arrFailure								
							}
														
							//pass back success or failure!
							if(arrSuccess.length == arr.length){
								obj.success(transport);	
							} else {
								obj.failure(transport);									
							}
							
							
						}
						
						
					}
				});
			});
			
		},
				
		//success()
		//execute onSuccess if set
		success:function(transport){

			if(Object.isUndefined(transport.success)){
				transport = {
					success : this.options.src,	
					failure : ''
				}
			}
			
			//check for the onSuccess function
			if(this.options.onSuccess != ''){
				this.options.onSuccess(transport);
			}
			
			//call the complete function
			this.complete();
		},
		
		//failure()
		//execute onFailure if set
		failure:function(transport){	
		
			if(Object.isUndefined(transport.failure)){
				transport = {
					success : '',	
					failure : this.options.src
				}
			}
			
			//check for the onSuccess function
			if(this.options.onFailure != ''){
				this.options.onFailure(transport);
			}
			
			//call the complete function
			this.complete();
		},
		
		//complete()
		//execute onComplete if set
		complete:function(){
			if(this.options.onComplete != ''){
				this.options.onComplete(this.options.src);
			}
		}
	})
