// ---------------------------------------------------------------
// Class Rollover
// (C) Copyright by Publicform GmbH (www.publicform.de)
//
// Version: 1.0
// Date: 2004-08-06
// Author: Andreas Doelling (doelling@publicform.de)
//
// This class provides the functionality to build rollover effects, 
// e.g. menus.
// The Rollover instance contains an array of items which 
// represent the single images and their different states.
// In addition, functions can be specified which are called at 
// mouseover, mouseout and click.
// ---------------------------------------------------------------

function Rollover() {
	// -----------------------------------------------------
	// Properties
	// -----------------------------------------------------
	this.items = new Array();
	this.preloadInt;
	this.preloadCounter = 0;
	this.startId;
	
	
	// -----------------------------------------------------
	// Method init
	// Starts image preload.
	// -----------------------------------------------------
	this.init = function() {
		if(this != arguments.callee.scope){
	      if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
		  } else {
		  		return  arguments.callee.scope.init();
		  }
	    }
		this.preloadInt = window.setInterval(this.preloadImages, 100);
	}
	this.init.scope = this;
	
	
	// -----------------------------------------------------
	// Method addItem
	// Adds item to the slideshow.
	// Required parameters: id, targetImgId, imgPathNormal, 
	// imgPathOver, 
	// Optional parameters: imgPathActive, functionOver, 
	// functionOut, functionClick
	// -----------------------------------------------------
	this.addItem = function() {
		var param = this.addItem.arguments;
		var item = this.items[param[0]] = new Array();
		item["imgId"] 		= param[1];
		item["imgOut"] 		= new Image();
		item["imgOver"] 	= new Image();
		if(typeof param[4] == "string")   item["imgClick"] 		= new Image();
		if(typeof param[5] == "function") item["functionOver"] 	= param[5];
		if(typeof param[6] == "function") item["functionOut"] 	= param[6];
		if(typeof param[7] == "function") item["functionClick"] = param[7];
		item["imgOut"].src	= param[2];
		item["imgOver"].src = param[3];
		if(typeof item["imgClick"] == "object")   item["imgClick"].src = param[4];
	}
	
	
	// -----------------------------------------------------
	// Method setFunction
	// Assigns a function to the Rollover object's item specified
	// by the given id, which is called in case of an event of 
	// the given type.
	// -----------------------------------------------------
	this.setFunction = function(itemId, eventType, func) {
		if(typeof this.items[itemId] == "object" && typeof func == "function") {
			switch(eventType) {
				case "mouseover":	this.items[itemId]["functionOver"] = func; break;
				case "mouseout":	this.items[itemId]["functionOut"] = func; break;
				case "click":		this.items[itemId]["functionClick"] = func; break;
			}
		}
	}
	
	
	// -----------------------------------------------------
	// Method preloadImages
	// Preloads the images.
	// -----------------------------------------------------
	this.preloadImages = function() {
		if(this != arguments.callee.scope){
	      if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
		  } else {
		  		return  arguments.callee.scope.preloadImages();
		  }
	    }
		var loaded = true;
		for(el in this.items) {
			if(!this.items[el]["imgOut"].complete) {loaded = false;break;}
			if(!this.items[el]["imgOver"].complete) {loaded = false;break;}
			if(typeof this.items[el]["imgClick"] == "object" && !this.items[el]["imgClick"].complete) {loaded = false;break;}
		}
		if(loaded == true) {
			window.clearInterval(this.preloadInt);
		}
	}
	this.preloadImages.scope = this;
	
	
	// -----------------------------------------------------
	// Method mouseover
	// Handles mouseover events propagated by a Controller object.
	// -----------------------------------------------------
	this.mouseover = function(evt) {
		if(this != arguments.callee.scope){
			if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
			} else {
		  		return  arguments.callee.scope.mouseover(evt);
			}
	    }
		var srcElement = (evt.srcElement)? evt.srcElement : evt.target;
		if(this.isResponsible(srcElement)) {
			document.getElementById(this.items[srcElement.id]["imgId"]).src = this.items[srcElement.id]["imgOver"].src;
			if(typeof this.items[srcElement.id]["functionOver"] == "function") {
				this.items[srcElement.id]["functionOver"]();
			}
		}
	}
	this.mouseover.scope = this;
	
	
	// -----------------------------------------------------
	// Method mouseout
	// Handles mouseout events propagated by a Controller object.
	// -----------------------------------------------------
	this.mouseout = function(evt) {
		if(this != arguments.callee.scope){
			if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
			} else {
		  		return  arguments.callee.scope.mouseout(evt);
			}
	    }
		var srcElement = (evt.srcElement)? evt.srcElement : evt.target;
		if(this.isResponsible(srcElement)) {
			document.getElementById(this.items[srcElement.id]["imgId"]).src = this.items[srcElement.id]["imgOut"].src;
			if(typeof this.items[srcElement.id]["functionOut"] == "function") {
				this.items[srcElement.id]["functionOut"]();
			}
		}
	}
	this.mouseout.scope = this;
	
	
	// -----------------------------------------------------
	// Method click
	// Handles click events propagated by a Controller object.
	// -----------------------------------------------------
	this.click = function(evt) {
		if(this != arguments.callee.scope){
			if(typeof arguments.callee.apply == "function") {
			  	return arguments.callee.apply(arguments.callee.scope, arguments);
			} else {
		  		return  arguments.callee.scope.click(evt);
			}
	    }
		var srcElement = (evt.srcElement)? evt.srcElement : evt.target;
		if(this.isResponsible(srcElement)) {
			if(typeof this.items[srcElement.id]["imgClick"] == "object"){
				document.getElementById(this.items[srcElement.id]["imgId"]).src = this.items[srcElement.id]["imgClick"].src;
			}
			if(typeof this.items[srcElement.id]["functionClick"] == "function") {
				this.items[srcElement.id]["functionClick"]();
			}
			return false
		} else {
			return true;
		}
	}
	this.click.scope = this;
	
	
	// -----------------------------------------------------
	// Method keypress
	// Handles keypress events propagated by a Controller object.
	// -----------------------------------------------------
	this.keypress = this.click;
	this.keypress.scope = this;
	
	
	// -----------------------------------------------------
	// Method isResponsible
	// Looks if the event source object has the same id as 
	// one of the Rollover instance's items. If so, returns
	// true.
	// -----------------------------------------------------
	this.isResponsible = function(srcElement) {
		return (typeof this.items[srcElement.id] == "object");
	}
}






