//$Revision: 1.1 $
//  wsRollover.js -requires- that wsPhoto.js is included first from
//  the parent HTML block, i.e., 
//   <script type="text/javascript" src="/js/wsPhoto-1.0.0.js"></script>
//   <script type="text/javascript" src="/js/WsRollover-1.0.0.js"></script>
// ---------------------------------------------------------------------

// CONSIDER making wsRollover.js independent from wsPhoto.js, if
//  we don't need the features, and it creates overhead

var ws_RO_collection = new Array();

function wsRollover() {
	var args = wsRollover.arguments; var len = args.length;
	
	this._d = typeof(debug)!="undefined" && ( typeof(debug_which)=="undefined" || debug_which.indexOf("wsRollover")!=-1 );
	
	this.name         = len > 0 ? args[0] : '';
	this.link_name    = len > 1 ? args[1] : '';
	this.width        = len > 2 ? args[2] : 0;
	this.height       = len > 3 ? args[3] : 0;
	this.src_inactive = len > 4 ? args[4] : '';
	this.src_rollover = len > 5 ? args[5] : '';
	this.src_active   = len > 6 ? args[6] : '';
	
	 // object state
	this._init        = 0;
	this._is_active   = 0;
	this._good        = 1;
	this._class_id    = ws_RO_ClassAddButton(this);	
	this.obj_photo    = new wsPhoto( this.name, this.width, this.height );
}

wsRollover.prototype.Init = function () {
	if ( this._init > 0 ) { return; }

	this.obj_photo.Init();
	if ( ! this.src_inactive ) {
		this.src_inactive = this.obj_photo.Attribute("src");
	}

	this.img_active   = new Image(); this.img_active.src = this.src_active;
	this.img_rollover = new Image(); this.img_rollover.src = this.src_rollover;
		
	if ( this.link_name ) {
		if ( document.getElementById ) {
			this._elem_link = document.getElementById(this.link_name);
		}
		else if ( eval("document."+this.link_name) ) {
			this._elem_link = eval("document."+this.link_name);
		}
	}
	
	 // if all else fails, revert to the old-fashioned way!
	if ( typeof(this._elem_link)=="undefined" ) {
		var a = document.anchors; 
		if (this._d) { debug += "[wsRollover] Examining all "+a.length+" anchor tags on the page!\n"; }
		for (var i=0; i<a.length; i++) {
			if (this._d) { debug += "[wsRollover] -- Anchor "+i+": name='"+a[i].name+"', id='"+a[i].id+"'\n"; }
			if ( a[i].name==this.link_name ) {
				this._elem_link = a[i];
				if ( this._d ) { debug += "[wsRollover] Got the right link!\n"; }
				break;
			}
		}
	}

	var d = this._d;
	var w = (this.link_name && this._elem_link ) ? '-anchor tag-' : '-image tag-';

	if ( this._d ) { debug += "[wsRollover] Element link for '" + this.link_name + "': " + this._elem_link + "\n"; }
		
	var button = this;
	
	if ( this.link_name && this._elem_link ) {
		var e = this._elem_link;
		e.onclick     = function () { if (d) { debug+= "[wsRollover] "+w+" onclick()--returning false.\n"; } return false; }
		if (d) { debug+= "[wsRollover] Set anchor tag onclick() event = '"+this._elem_link.onclick+"'\n"; }
	} 
	var e = this.obj_photo.elem_photo;

	if ( this._d ) { debug += "[wsRollover] '" + this.link_name + "' element = '" + e + "'\n"; }

	if ( e )
	{	
		e.onmouseover = function () { if (d) { debug += "[wsRollover] "+w+" onmouseover()\n"; } button.Rollover(); }
		e.onmouseout  = function () { if (d) { debug += "[wsRollover] "+w+" onmouseout()\n"; } button.Rollout(); }
		e.onclick     = function () { if (d) { debug += "[wsRollover] "+w+" onclick()\n"; } button.Toggle(); }
		this._good = 1;
	}
	else { this._good = 0; }
	
	this._init = 1;	
}

wsRollover.prototype.Activate = function () {
	this.Init();	
	if ( ! this._good ) { return; }
	this._is_active = 1;
	this.obj_photo.Attribute("src", this.src_active);
	if ( this._d ) { debug += "[wsRollover] '" + this.name + "' activated!\n"; }
}

wsRollover.prototype.Deactivate = function () {
	this.Init();
	if ( ! this._good ) { return; }
	this._is_active = 0;
	this.obj_photo.Attribute("src", this.src_inactive);
	if ( this._d ) { debug += "[wsRollover] '" + this.name + "' DEactivated!\n"; }
}

wsRollover.prototype.Rollover = function () {
	this.Init();
	if ( ! this._is_active ) { this.obj_photo.Attribute("src", this.src_rollover); }
}

wsRollover.prototype.Rollout = function () {
	this.Init();
	if ( this._is_active ) { this.Activate(); }
	else                   { this.Deactivate(); }
}

wsRollover.prototype.Toggle = function () {
	this.Init();
	if ( this._d ) { debug += "[wsRollover] '" + this.name + "' toggled.\n"; }

	if ( this._is_active ) { this.Deactivate(); }
	else                   { this.Activate(); }
}

function ws_RO_ClassAddButton(obj)  { var id = ws_RO_collection.length; ws_RO_collection[id] = obj; return id; }