// $Revision: 1.2 $

// Netscape Navigator 4.x and earlier -will not- dynamically resize images
//  Also note that quotation marks around all attributes are -REQUIRED- for
//  some browsers to provide dynamic access to the element.

function wsPhoto() {
	var args = wsPhoto.arguments; var len = args.length;
	
	 // object attributes - ok to set directly	
	this.name      = len > 0 ? args[0] : ''; 
	this.abs_width = len > 1 ? args[1] : 0;  
	this.abs_height= len > 2 ? args[2] : 0;
	this.min_width = len > 3 ? args[3] : 0;  
	this.min_height= len > 4 ? args[4] : 0;  
	this.ofs_width = len > 5 ? args[5] : 0;
	this.ofs_height= len > 6 ? args[6] : 0;
	
	 // object state
	this._init      = 0;
	this._good      = 1;
	this.elem_photo = 0;	
	this._d         = typeof(debug)!="undefined" && ( typeof(debug_which)=="undefined" || debug_which.indexOf("wsPhoto")!=-1 );
}

wsPhoto.prototype.Init = function () {
	if ( ! this.name ) { return; }
	if ( this._d ) { debug += "[wsPhoto] Init() method called. name='"+this.name+"'\n"; }
	this.GrabWindowInfo();
	
	var doc = document.all ? "document.all" : "document";

	 // images come without I.D. tags from COPhoto.pm
	if ( document.images ) {
	  this.elem_photo = document.images[this.name];
	}
	if ( typeof(this.elem_photo)=="undefined" ) {
		if (this._d) { debug += "[wsPhoto] Searching for photo '"+this.name+"'\n"; }
		var a = document.images;
		for(var i=0; i<a.length; i++) {
			if (this._d) { debug += "[wsPhoto] Looking at image '" + a[i].name + "': [" + a[i].src + "].\n"; }
			if (a[i].name==this.name) {
				this.elem_photo = a[i];
				if (this._d) { debug += "[wsPhoto] -- Found it at image "+i+"!\n"; }
				break;
			}
		}
	}
	
	if ( typeof(this.elem_photo)!="undefined" && this.elem_photo.width && this.elem_photo.height ) {
		if ( ! this.abs_width )  { this.abs_width = this.elem_photo.width; }
		if ( ! this.abs_height ) { this.abs_height = this.elem_photo.height; }
		this._init = 1;
	} else {	
		 // ****REMOVE ME****
		//alert("This is no good! ("+this.name+")\n["+this.elem_photo+"]\n["+eval("document."+this.name)+"]\n");
		if (this._d) { debug += "[wsPhoto] Element not found '"+this.name+"' -- '"+this.elem_photo+"'\n";  }
		this._good = this._init = 0;
	}
}

wsPhoto.prototype.SetAbsSize = function () {
	this.abs_width = arguments[0];
	this.abs_height = arguments[1];
}

wsPhoto.prototype.GrabWindowInfo = function () {
	if      ( window.innerHeight )         { this.win_width=window.innerWidth; this.win_height=window.innerHeight; }
	else if ( document.body.clientHeight ) { this.win_width=document.body.clientWidth; this.win_height=document.body.clientHeight; }
	else    { return; }
	if ( this._d ) { debug += "[wsPhoto] Window width="+this.win_width+", height="+this.win_height+"\n"; }
}

wsPhoto.prototype.Resize = function ws_P_ResizePhoto() {
	if ( ! this._init ) { this.Init(); }
	if ( ! this._good ) { return; }

	 // Optimize photo for current screen if no arguments are given
	if ( arguments.length == 0 )
	{
		this.GrabWindowInfo();

    	var winWidth  = this.win_width  + this.ofs_width; 
    	var winHeight = this.win_height + this.ofs_height;
		if ( winWidth  < this.min_width )  { winWidth  = this.min_width; }
		if ( winWidth  > this.abs_width )  { winWidth  = this.abs_width; }
		if ( winHeight < this.min_height ) { winHeight = this.min_height; }
	
		var ratio = winWidth / this.elem_photo.width;
		if ( winHeight / this.elem_photo.height < ratio )
			ratio = winHeight / this.elem_photo.height;	
    	var newWidth  = this.elem_photo.width  * ratio;
    	var newHeight = this.elem_photo.height * ratio;

		var w = this.elem_photo.width;
		var h = this.elem_photo.height;
    	this.elem_photo.width  = newWidth;
    	this.elem_photo.height = newHeight;
    	
    	if ( document.frame1 ) { document.frame1.width  = newWidth; }
    	if ( document.frame3 ) { document.frame3.height = newHeight; }
    	if ( document.frame4 ) { document.frame4.height = newHeight; }
    	if ( document.frame6 ) { document.frame6.width  = newWidth; }    
    } else {
    	if ( arguments.length > 0 ) { 
    		var w = arguments[0] > this.min_width ? arguments[0] : this.min_width;
    		this.elem_photo.width  = w; 
    	}
    	if ( arguments.length > 1 ) { 
    		var h = arguments[1] > this.min_height ? arguments[1] : this.min_height;
    		this.elem_photo.height = h;
    	}
    }
}

wsPhoto.prototype.Attribute = function () {
	if ( ! this._good ) { return; }
	if ( ! this._init ) { this.Init(); }
	
	var args = arguments; var len = args.length;
	
	var attr = len > 0 ? args[0] : '';
	if ( ! attr ) { return ""; }
	
	var value;
	if ( len > 1 ) { value = args[1]; }

	if ( typeof(value)=="function" ) {
		eval("this.elem_photo."+attr+" = "+value);
	}
	else if ( typeof(value)!="undefined" ) {
		eval("this.elem_photo."+attr+" = '"+value+"'");
	}
	
	return eval("this.elem_photo."+attr);
}
