// TLAYER.JS
//
//  - CROSS BROWSER DHTML LAYER CLASS
//
// script by NICOLE
// nicole@big.or.jp
// http://www10.big.or.jp/~nicole
//
//  - support browser
//              Windows - IE4, IE5, Netscape4, Netscape6
//              Mac     - IE4, IE5, Netscape4
//
//
// -----------------------------------------------
// :exsample.1
//
//      var mylayer = new TLayer( "mylayer"); // var name
//      mylayer.assign( "divID" ); // stylesheet id name
//
// :exsample.2
//	var mylayer = new TLayer( "mylayer", "divID" );
//
// -----------------------------------------------
// PUBLIC METHOD
//
// show();
// hide();
// showHide();
// moveTo( x, y );
// moveBy( x, y );
// slideTo( x, y );
// slideBy( x, y );
// clip( top, right, bottom, left );
// fade( true | false ); -- WinIE5.5 or later
//
// PUBLIC PROPERTY
//
// slideSpeed
// 
//
// PROTECTED METHOD
//
// getVisibility(); -- return value is true / false
// setVisibility( true | false )
// setLeft( x );
// getLeft();
// setTop( y );
// getTop();
//
//
// PRIVATE METHOD
//
// -----------------------------------------------
// VERSION HISTORY
//
// 2.0 - 2001/03/02
//                  moveTo method fixed ( remove "parseInt" );
//






////////////////////////////////////////////////////////////
//
// CONSTRUCTOR

function TLayer( name, element_id ) {
	this.name = name;
	this.element = null;
	this.style = null;
	this.doc = null;
	this.isDOM1 = false;
	this.slideSpeed = 70; // SLIDE SPEED : 10=very fastet, 50=fast, 70=normal, 140=slow 180=very slow

	this.m_slideTimerID = null;
	this.m_fadeTimerID = null;

	// BROWSER CHECK
	var b = navigator.appName;
	if( b == "Netscape") {
		this.isNS = true;
		this.isIE = false;
	} else if( b == "Microsoft Internet Explorer") {
		this.isNS = false;
		this.isIE = true;
	}

	// BROWSER VERSION CHECK
	var v = parseInt( navigator.appVersion );
	this.isNS4 = ( this.isNS && 4 == v);
	this.isNS6 = ( this.isNS && 5 == v);
	this.isIE4 = ( 0 < navigator.appVersion.indexOf('MSIE 4') );
	this.isIE5 = ( 0 < navigator.appVersion.indexOf('MSIE 5') );
	this.isIE55= ( 0 < navigator.appVersion.indexOf('MSIE 5.5'));

	// PLATFORM CHECK
	var p = navigator.userAgent.toLowerCase();
	this.platform = null;
	if( -1 < p.indexOf("win") ){
		this.platform = "win";
	} else if ( -1 < p.indexOf("mac")) {
		this.platform = "mac";
	}

	this.isWin = "win" == this.platform;
	this.isMac = "mac" == this.platform;

	if( element_id ) { this.assign( element_id ); }
}

TLayer.prototype.assign = function( id ) {
	var msg = "TLayer - ERROR - not found element id \"" + id + "\". check HTML source code.";

	// GET ELEMENT
	if( document.getElementById ) {
		// DOM Level1 ( IE5.5 or Netscape6 )
		this.element = document.getElementById(id);
		if( null == this.element ) alert( msg + " [document.getElementById]");
		this.style = this.element.style;
		this.doc = document;
		this.isDOM1 = true;
	} else if( document.all ) {
		// IE4
		this.element = document.all[id];
		if( null == this.element ) alert( msg + " [document.all]" );
		this.style = this.element.style;
		this.doc = document;
	} else if( document.layers ) {
		this.element = this.getNestedLayerElement( id, document.layers );
		if( null == this.element ) alert( msg + " [document.layers]" );
		this.style = this.element;
		this.doc = document;
	}

	this.x = this.getLeft();
	this.y = this.getTop();
}

// Netscape4 : nested layer
TLayer.prototype.getNestedLayerElement = function( id, layers ) {
	var result = null;
	if( layers[id] ) return layers[id];

	for(var i=0; i<layers.length; i++ ) {
		if( 0 < layers[i].document.layers.length ) {
			result = this.getNestedLayerElement( id, layers[i].document.layers );
			if( null != result ) return result;
		}
	}
	return null;
}

TLayer.prototype.create = function( style ) {
	var element_id = "tlayer2" + this.name;
	document.write('<span id=' + element_id + ' style="' + style + '"><\/span>');
	if( this.isIE ) {
		this.assign( element_id );
	}
}

////////////////////////////////////////////////////////////
//
// VISIBILITY

TLayer.prototype.setVisibility = function( visible ) {
	if( visible ){
		this.style.visibility = this.isIE || this.isNS6 ? "visible":"show";
	} else {
		this.style.visibility = "hidden";
	}
}

TLayer.prototype.getVisibility = function() {
	return this.style.visibility.indexOf("hid");
}

TLayer.prototype.show = function() {
	this.setVisibility( true );
}

TLayer.prototype.hide = function() {
	this.setVisibility( false );
}

TLayer.prototype.showHide = function() {
	this.setVisibility( !this.getVisibility() );
}

////////////////////////////////////////////////////////////
//
// POSITIONING

TLayer.prototype.getLeft = function() {
	return parseInt( this.isIE ? this.style.pixelLeft : this.style.left );
}

TLayer.prototype.setLeft = function( x ) {
	this.moveTo( x, null );
}

TLayer.prototype.getTop = function() {
	return parseInt( this.isIE ? this.style.pixelTop : this.style.top );
}

TLayer.prototype.setTop = function( y ) {
	this.moveTo( null, y );
}

TLayer.prototype.getWidth = function() {
	// stylesheet de width wo sitei sinai to kinou sinai
	return parseInt( this.isIE ? this.style.pixelWidth : this.style.right );
}

TLayer.prototype.getHeight = function() {
	// stylesheet de height wo sitei sinai to kinou sinai
	return parseInt( this.isIE ? this.style.pixelHeight : this.style.height );
}

////////////////////////////////////////////////////////////
//
// MOVE

TLayer.prototype.moveTo = function( x, y ) {
	this.x = x != null ? x : this.x;
	this.y = y != null ? y : this.y;

	if( null == this.style ) return;

	if( this.isIE ) {
		this.style.pixelLeft = this.x;
		this.style.pixelTop = this.y;
	} else {
		this.style.left = this.x;
		this.style.top = this.y;
	}
}

TLayer.prototype.moveBy = function( x, y ) {
	this.moveTo( this.x + x, this.y + y );
}

////////////////////////////////////////////////////////////
//
// SLIDE

TLayer.prototype.slideInterval = function() {
	this.m_slideStep++;
	var ag = ( this.m_slideStep + this.slideSpeed ) * Math.PI / this.slideSpeed;
	var ar = Math.sin( ag );
	var x = ar * ( Math.cos( ag ) * this.m_slideRadiusX + this.m_slideRadiusX );
	var y = ar * ( Math.cos( ag ) * this.m_slideRadiusY + this.m_slideRadiusY );

	x = x * this.m_slideDirectionX + this.m_slideStartX;
	y = y * this.m_slideDirectionY + this.m_slideStartY;

	this.moveTo( x, y );

	if( (this.slideSpeed / 2 ) < this.m_slideStep ) {
		clearInterval( this.m_slideTimerID );
		this.m_slideTimerID = null;
		this.moveTo( this.m_slideDestX, this.m_slideDestY );
	}
}

TLayer.prototype.slideTo = function( x, y, speed ) {
	if( this.x == x && this.y == y ) return;

	if( speed ) { this.slideSpeed = speed; }

	this.m_slideStartX = this.x;
	this.m_slideStartY = this.y;
	this.m_slideDestX = x;
	this.m_slideDestY = y;
	this.m_slideRadiusX = Math.abs( this.m_slideStartX - x );
	this.m_slideRadiusY = Math.abs( this.m_slideStartY - y );
	this.m_slideStep = 0;
	this.m_slideDirectionX = ( x < this.m_slideStartX ) ? 1 : -1;
	this.m_slideDirectionY = ( y < this.m_slideStartY ) ? 1 : -1;

	if( null == this.m_slideTimerID ) {
		this.m_slideTimerID = setInterval( this.name + ".slideInterval();", 10 );
	}
}

TLayer.prototype.slideBy = function( x, y, speed ) {
	this.slideTo( this.x + x, this.y + y, speed );
}

////////////////////////////////////////////////////////////
//
// CLIP

TLayer.prototype.clip = function( top, right, bottom, left ) {
	if(!top) top = 0;
	if(!right) right = 9999; // 
	if(!bottom) bottom = 9999; //
	if(!left) left = 0;

//	alert( top + "," + right + "," + bottom + "," + left );

	if( this.isDOM1 ) {
		this.style.clip = "rect(" + top + " " + right + " " + bottom + " " + left + ")";
	} else if( this.isNS ) {
		this.style.clip.top = top;
		this.style.clip.right = right;
		this.style.clip.bottom = bottom;
		this.style.clip.left = left;
	}
}

////////////////////////////////////////////////////////////
//
// FADE ( WINDWOS IE5.5 or later )

TLayer.prototype.fade = function( visible, interval, step ) {
	if( this.getVisibility() == visible ) { return; }

	if( this.isWin && this.isIE55 ) {
		this.m_fadeDirection = visible ? 1 : -1;
		this.m_fadeOpacity = visible ? 0 : 100;
		this.fadeInterval(); // pre call
		this.m_fadeStep = step ? step : 3;
		this.show();

		if( null == this.m_fadeTimerID ) {
			this.m_fadeTimerID = setInterval( this.name + ".fadeInterval();", interval ? interval : 10 );
		}

	} else {
		this.setVisibility( visible );
	}
}

TLayer.prototype.fadeInterval = function() {
	this.m_fadeOpacity += this.m_fadeDirection * this.m_fadeStep;
	var s = "alpha(opacity=" + parseInt(this.m_fadeOpacity) + ")";
	this.style.filter = s;

	if( this.m_fadeOpacity < 0 || 100 < this.m_fadeOpacity ) {
		clearInterval( this.m_fadeTimerID );
		this.m_fadeTimerID = null;
		this.style.filter = "";
		this.setVisibility( 1 == this.m_fadeDirection );
	}
}


TLayer.prototype.setHTML = function( html ) {
	this.element.innerHTML = html;

	if( this.isNS4 ) {
		this.doc.open();
		this.doc.write( html );
		this.doc.close();
	}
}
