//==========//
// Name: 		Image Handler Library v1.0
// Copyright:	Cozai Open Source License (http://www.cozai.com/opensource/license.html)
// Author: 		Andy Freeborough (andy.freeborough@cozai.com)
// Date: 		27 Jun 2001
// Description:	
//==========//


//----------//
// To do:
//----------//
// 	*	Automatic layer conversion.
//		Do a check in swapImage to see if the layers have already been converted.  If not, then do so.
//		Need to have an over-ride too.  Just a automaticConversion = 0/1 or something.
//
//	*	Specify image objects anywhere in the dom.
//		Have direct specification, so that images in other frames/windows/layers/etc can be specifically
//		swapped. Could do a conversion for these too, mapping them to object.imageName or something.
//
//	*	Netscape 6 image caching.
//		Image caching to work under Netscape 6. Might need a hidden frame, which sucks.
//----------//


// Register library with the Library Manager.
libraryManager.register("Image Handler",1,0,"","ImageHandler");

// Create a global instance of the Image Handler as it is a static library.
imageHandler = new ImageHandler();

function ImageHandler() {
	
	// Properties
	this.imageCache = new Array();
	this.allCacheCalls = 0;
	this.layersConverted = 0;
	this.tempImage = new Image();

	// Library properties
	this.libraryName = "Image Handler";
	this.majorVersion = 1;
	this.minorVersion = 0;
	
	// Public methods
	this.swap = ImageHandler_swap;
	this.convertLayers = ImageHandler_convertLayers;
	this.cache = ImageHandler_cache;

	// Upgrade this library if available.
	libraryManager.upgrade( this );	
	
}

function ImageHandler_Upgrade( objectRef ) {
	// Do nothing.
}

function ImageHandler_swap( names ) {
	
	var i;
	var imageList;
	var imagePair;

	imageList = names.split(",");

	for (i = 0 ; i < imageList.length ; i++) {
		imagePair = imageList[i].split("=");
		if (imagePair.length == 2) {
			if (document.images[imagePair[0]]) {
				document.images[imagePair[0]].src = imagePair[1];
			}
		}
	}	
}

function ImageHandler_convertLayers( doc ) {
	
	var i;
	var n;
	var imageName;
	
	this.layersConverted--;
	
	if (doc.layers) {
		
		for (i = 0 ; i < doc.layers.length ; i++) {
			for (n = 0 ; n < doc.layers[i].document.images.length ; n++) {
				document.images[doc.layers[i].document.images[n].name] = doc.layers[i].document.images[n];
			}
			this.convertLayers(doc.layers[i].document);
		}
		
	}
	
	this.layersConverted++;
	if (this.layersConverted == 0) {
		this.layersConverted = 1;
	}
	
}

function ImageHandler_cache( name ) {
	
	var current = this.imageCache.length;
	this.imageCache[current] = new Image();
	this.imageCache[current].src = name;
	
}