// Modified by Antony Shen.
// Handcrafted by Construct Internet Design, August 1997
//   under commission of Netscape Communications, Inc.
//
// Project Team:
// Project Management: Becky Price
// Quality Assurance: Cynsa Bonorris

//
//	 Tech:			Design:
//	   Sascha Becker	  David Collier
//	   Peter Molettiere 	  Josh Draper
//	   Lior Saar		  Annette Loudon
//	   James Waldrop	  Gabriella Marks
//
//
// http://www.construct.net/
//
// imgButton.js
//
// implements a layer with a button whose image changes on mouseover.
// user can define a click action later
// would be trivial to include a handler for clicks that would punt to an url

function imgButton( I ) {
	 var ImageWidth=30;
    var btnL = new Layer(ImageWidth);

    btnL.document.close();
    btnL.clip.top = 0;
    btnL.clip.left = 0;
    btnL.clip.right = 30;
    btnL.clip.bottom = 30;
    btnL.top = 0;
    btnL.left = 500;
    btnL.background.src = I;
	btnL.visibility="show";

    btnL.outImg = I;
    btnL.overImg = null;
	btnL.downImg = null;

    btnL.setClip    = H_clip ;
    btnL.setTopLeft = H_TopLeft ;
    btnL.Redraw     = imgButtonRedraw ;

    btnL.imgOver = imgOver;
    btnL.imgOut = imgOut;
    btnL.imgDown = imgDown;

    btnL.onmousedown = H_onmousedown;
    btnL.onmouseup = H_onmouseup;
    btnL.onmouseover = H_onmouseover;
    btnL.onmouseout = H_onmouseout;
    btnL.captureEvents( Event.MOUSEDOWN | Event.MOUSEUP );

    btnL.zIndex = 140;
    return btnL;
}

function H_TopLeft( t, l ) {
    this.top = t;
    this.left = l;
    // this.visibility = 'inherit';   // LS me
}

function H_clip( t, b, l, r ) {
	this.clip.top = t;
	this.clip.bottom = b;
	this.clip.left = l;
	this.clip.right = r;
}

function H_onmouseout(e) {
	this.background.src = this.outImg.src;
    if( ! waitForImage( this.background ) )
        println( "H_onmouseout: failed to read " + this.outImg.src ) ;  // LS

	this.mousedown = false;
}

function imgOver( img ) { 
    var I = new Image;
    I.src = img;
    this.overImg = I; 
}

function imgOut( img ) {
    var I = new Image;
    I.src = img;
    this.outImg = I;
}

function imgDown( img ) {
	this.downImg = img;
}

function H_onmouseover(e) {
	if( (this.overImg != null)&&(!this.mousedown) ) { this.background.src = this.overImg.src; }
}

function imgButtonRedraw()
{
    setTimeout( layerRedraw, 20, this ) ;
}

function layerRedraw( layer )
{
    layer.background.src = layer.background.src ;
    layer.visibility = 'inherit';
}

function H_onmousedown(e) {
	if( this.downImg != null ) { this.background.src = this.downImg; }
	this.mousedown = true;
}

function H_onmouseup(e) {
	if( this.overImg != null ) { this.background.src = this.overImg.src; } else { this.background.src = this.outImg.src; }
	if( this.mousedown ) {
		// check to make sure a click handler is defined
		if( this.onclick ) {
			// generate a mouseCLICK
			this.onclick(e);
		}
	}
	this.mousedown = false;
}


function waitForImage( image )    // LS
{
    var timeout = 666 * 666 ;
    for( var i = 0 ; i < timeout ; i++ )
        if( image.width > 0 )
            { return( true ) ; }
    return( false ) ;
}
