/*******************************************************************************************************************************

imageRig.js
created: 12/08/01
author: PON
Last updated: 04/10/02

Description: image rollover object stuff

specific desriptions and comments gathered here in order to facilitate removal for production

Objects:
	imageGroups - collection of image objects into groups (allows one image to control others). Each group is an array of images
				- the first image in a group (item [0] in the array) is reserved to hold any default image
				
	imageObj    - base object that fires up an image as an object with the following properties and methods

utility functions:
	checkOffsets(elmnt)	- cycles through an element's parents and finds the sum of their ofsets.
							used to screen location of an image, primarily in IE4

imageObj passed arguments:
	img 		- the HTML image object being created as an imageObj
	group 		- a group identifier. May be a string or number. OPTIONAL
	
imageObj properties:
	.obj		- reference back to the original HTML <img> object
	.name		- identifier for the object, mirrors .obj.name
	.defaultSrc - the original source of an image, mirrors .obj.src
	.group 		- a reference to the group the image belongs to in the imageGroups object. OPTIONAL
	.state 		- keeps track of the state of the object in question. 0 = off, 1 = on, 2 = active
	.type		- holds image type (gif or jpg usually)*
	.path		- path to the image on the file server (everything prior to the file name)*
	.base		- image name with the state identifier ("_off", "_on" or "_act") stripped off
	.off		- string holding the off image naming convention for the site. Usually either "_off." or ".", DEFAULT is "_off."
	.offImg.src - creates an off image object and preloads is
	.onImg.src	- creates an on image and preloads it
	.actImg.src	- creates an active image and preloads it OPTIONAL
	.pageX 		- the x-coordinate of an image on the page (BETA... test before using)
	.pageY 		- the y-coordinate of an image on the page (BETA... test before using)
	
imageObj base methods:
	.initObj		- initialized an imageObj by setting additional references and properties
	.findImage		- gets x and y coordinates of the image and sets to .pageX and .pageY
	.setOn 			- switches an image to the on state
	.setOff 		- switches an image to the off state
	.setSrc 		- sets the source of an image to an arbitrary URI (passed as an argument) and re-initializes
	.toggle			- changes the state of an image between on/off based on what it currently is

	.toggleGroup	- sets all the images in group to off except for the one selected
	.toggleGroupDefOn- similar to .toggleGroup, however it leaves the image set as the default on
	.setGroup 		- activates the current image, sets it as the default and sets all other images of the group off
	.resetGroup		- resets the images in a group to off except for the default, which it sets on
	
imageObj optional methods:
	The following methods all involve tracking an "active" image state
	If not required for a particular site, they may be removed as they are self contained.
	
	.setActive 			- sets the selected image to it's active state
	.toggleGroupActive	- similar to .toggleGroupDefOn witht he addition of active state
	.setGroupActive		- sets selected image to be the active member of a group and sets it to the default
	.resetGroupActive 	- ll images turned off except for default, which is turned on (default state)
	
	
Comments (add updates / changes here):
- code assumes the followingnaming convenions for any image using this object:
	"_off" as the off image REQUIRED
	"_on" for an on-state REQUIRED
	"_act" for an active state image (distinct from on-state) OPTIONAL
		- separate methods for dealing with active images are included 
		  and can be removed completely if unused for a particular site
- changed grouping code
- added a bunch of substrings to the image source 
	so it could, potentially, be maipulated more 
	by other scripts (if need be)
- added (then removed) an optional delay for resetGroup
- bunch of extra url slicing done, 
  could probably be removed but needed in order to work with WebCollage
  (WebCollage is a piece-of-poop syndication software that writes one site into another through JavaScript document.writes)
- the setSrc method allows for assigning an arbitrary passed reference to an image and reinitializes it [care of Rudy]
*********************************************************************************************************************************/


	
function checkOffsets(elmnt){
	elmnt.obj.pageX += elmnt.offsetTop;
	elmnt.obj.pageY += elmnt.offsetLeft;
	if (elmnt.parentElement){
		tempElmnt = elmnt.parentElement;
		checkOffsets(tempElmnt);
	}
}
		
imageGroups = new Object();

function imageObj(img,group){
	img.onload = '';
	this.obj = img;
	this.width = img.width;
	this.height = img.height;
	this.name = img.name;
	this.defaultSrc = img.src;
	if (group != 'undefined'){
		if (typeof imageGroups[group] == "undefined"){
			imageGroups[group] = new Array();
			imageGroups[group][0] = '';
		}
		imageGroups[group][imageGroups[group].length] = this;
		this.group = imageGroups[group];
		if ((this.defaultSrc.indexOf("_on") > -1) || (this.defaultSrc.indexOf("_act") > -1)) imageGroups[group][0] = this;
	}
	this.initObj(img);
	(img.src.indexOf("_act") > -1) ? this.state = 2 : (img.src.indexOf("_on") > -1) ? this.state = 1 : this.state = 0;
}

imageObj.prototype.initObj = function(img) {
	this.type = img.src.substring((img.src.lastIndexOf(".") + 1), img.src.length);
	this.path = this.defaultSrc.substring(0,this.defaultSrc.lastIndexOf("/")+1);  //in case absolute paths are needed (syndication)
	//this.domainPath = this.defaultSrc.substring(7,this.defaultSrc.lastIndexOf("/")+1);
	//this.path = this.domainPath.substring(this.domainPath.indexOf("/"),this.domainPath.length);
	this.off = "_off."; //off convention for site
	this.base = img.src.substring((this.path.lastIndexOf("/")+1),img.src.lastIndexOf("_"));
	this.onImg = new Image();
	this.onImg.src = this.path + this.base + "_on." + this.type;
	this.offImg = new Image();
	this.offImg.src = this.path + this.base + this.off + this.type;
	this.actImg = new Image();
	this.actImg.src = this.path + this.base + "_act." + this.type;
	this.findImage();
}

imageObj.prototype.findImage = function(){
	this.pageX = (document.layers) ? this.obj.x : (document.all && !document.getElementById) ? 0 : this.obj.offsetLeft;
	this.pageY = (document.layers) ? this.obj.y : (document.all && !document.getElementById) ? 0 : this.obj.offsetTop;
   	if(document.all && !document.getElementById) checkOffsets(imgObj);
	}
	
imageObj.prototype.setOn = function(){
	this.obj.src = this.onImg.src;
	this.state = 1;
}

imageObj.prototype.setOff = function(){
	this.obj.src = this.offImg.src;
	this.state = 0;
}

imageObj.prototype.setSrc = function(url) {
	this.obj.src = url;
	this.defaultSrc = url;
	this.initObj(this.obj);
}

imageObj.prototype.toggle = function(){
	 (this.state == 0) ? this.setOn() : this.setOff();
}

imageObj.prototype.toggleGroup = function(){
	for(i=1;i<this.group.length;i++){
		(this.name != this.group[i].name) ?	this.group[i].setOff() : this.group[i].setOn();
	}
}

imageObj.prototype.toggleGroupDefOn = function(){
	for(i=1;i<this.group.length;i++){
		((this.name != this.group[i].name)&&(this.group[i].name != this.group[0].name)) ? this.group[i].setOff() : this.group[i].setOn();
	}
}

imageObj.prototype.setGroup = function(){
	for(i=1;i<this.group.length;i++){
		if (this.name != this.group[i].name){
			this.group[i].setOff();
		}else{
			this.group[i].setOn();
			this.group[0] = this.group[i];
		}
	}
}

imageObj.prototype.resetGroup = function(){
	for(i=1;i<this.group.length;i++){if(this.group[i].name != this.group[0].name) this.group[i].setOff();}
	if(typeof this.group[0] == 'object') this.group[0].setOn();
}

/********************
   OPTIONAL METHODS
********************/

imageObj.prototype.setActive = function(){
	this.obj.src = this.actImg.src;
	this.state = 2;
}

imageObj.prototype.toggleGroupActive = function(){
	for(i=1;i<this.group.length;i++){
		((this.name == this.group[i].name) && (this.state != 2)) ? this.group[i].setOn() : (this.group[i].state == 2) ? this.group[i].setActive() : this.group[i].setOff();
	}
}

imageObj.prototype.setGroupActive = function(){
	for(i=1;i<this.group.length;i++){
		if (this.name != this.group[i].name){
			this.group[i].setOff();
		}else{
			this.group[i].setActive();
			this.group[0] = this.group[i];
		}
	}
}

imageObj.prototype.resetGroupActive = function(){
	for(i=1;i<this.group.length;i++){if(this.group[i].state != 2)this.group[i].setOff();}
	if(typeof this.group[0] == 'object') this.group[0].setActive();
}