/*
* ProductHandler
* 
* @brief: misc product related functions
*/
function ProductHandler( imageName ) {
	this.imageName = imageName;
	this.trailW = 1;
	this.trailH = 1;
}

/*
* ProductHandler.initTrail
* 
* @brief: write pic's hidden div to document
* @args:	-
* @return:	-
*
*/
ProductHandler.prototype.initTrail = function() {
	if( document.getElementById || document.all ) {
//		document.write( '<div id="trailimageid" style="position:absolute;visibility:hidden;left:0px;top:-1000px;width:1px;height:1px;border:1px solid #888888;background:#DDDDDD;z-index:1000;"><img id="ttimg" src="site/img/spacer.gif" /></div>' );
		document.write( '<div id="trailimageid" style="position:absolute;visibility:hidden;left:0px;top:-1000px;width:1px;height:1px;border:1px solid #888888;background:#DDDDDD;z-index:1000;"><img id="ttimg" class="spacerGif" alt="" /></div>' );
	}
}

/*
* ProductHandler.getTrailObj
* 
* @brief: gets pic div's dom
* @args:	-
* @return:	-
*
*/
ProductHandler.prototype.getTrailObj = function() {
	if( document.getElementById ) {
		return document.getElementById( "trailimageid" ).style;
	} else if( document.all ) {
		return document.all.trailimageid.style;
	}
}

/*
* ProductHandler.trueBody
* 
* @brief: determine root dom node depends on browsertype
* @args:	-
* @return:	obj of root dom
*
*/
ProductHandler.prototype.trueBody = function() {
	return ( !window.opera && document.compatMode && document.compatMode != "BackCompat" ) ? document.documentElement : document.body;
}

/*
* ProductHandler.hideTrail
* 
* @brief: hide pic's div
* @args:	-
* @return:	-
*
*/
ProductHandler.prototype.hideTrail = function() {
	document.onmousemove = "";
//	document.getElementById( 'ttimg' ).src = 'site/img/spacer.gif';
	document.getElementById( 'ttimg' ).className = 'spacerGif';
	this.getTrailObj().visibility = "hidden";
	this.getTrailObj().left = -1000;
	this.getTrailObj().top = 0;
}

/*
* ProductHandler.showTrail
* 
* @brief: show pic's div
* @args:	width:Int - width of the div
*			height:Int - height of the div
*			file:String - file name with path
* @return:	-
*
*/
ProductHandler.prototype.showTrail = function( width , height , file ) {
	if( navigator.userAgent.toLowerCase().indexOf( 'opera' ) == -1 ) {
		this.trailW = width;
		this.trailH = height;

		this.getTrailObj().visibility = "visible";
		this.getTrailObj().width = this.trailW + "px";
		this.getTrailObj().height = this.trailH + "px";

		document.getElementById( 'ttimg' ).src = file;
		document.getElementById( 'ttimg' ).width = width;
		document.getElementById( 'ttimg' ).height = height;
		document.onmousemove = this.followMouse;
	}
}

/*
* ProductHandler.followMouse
* 
* @brief: event handler, override the original one
* @args:	e:Event
* @return:	-
*
*/
ProductHandler.prototype.followMouse = function( e ) {
	if( navigator.userAgent.toLowerCase().indexOf( 'opera' ) == -1 ) {
		var xcoord = 20;
		var ycoord = 20;

		if( typeof e != "undefined" ) {
			xcoord += e.pageX;
			ycoord += e.pageY;
		} else if( typeof window.event != "undefined" ) {
			xcoord += ws.wsProduct.trueBody().scrollLeft + event.clientX;
			ycoord += ws.wsProduct.trueBody().scrollTop + event.clientY;
		}

		var docwidth = document.all ? ws.wsProduct.trueBody().scrollLeft + ws.wsProduct.trueBody().clientWidth : pageXOffset + window.innerWidth - 15;
		var docheight = document.all ? Math.max( ws.wsProduct.trueBody().scrollHeight , ws.wsProduct.trueBody().clientHeight ) : Math.max( document.body.offsetHeight , window.innerHeight );

		if( xcoord + ws.wsProduct.trailW + 3 > docwidth ) {
			xcoord = xcoord - ws.wsProduct.trailW - ( 20 * 2 );
		}
		
		if( ycoord - ws.wsProduct.trueBody().scrollTop + ws.wsProduct.trailH > ws.wsProduct.trueBody().clientHeight ) {
			ycoord = ycoord - ws.wsProduct.trailH - 20;
		}
		
		ws.wsProduct.getTrailObj().left = xcoord + "px";
		ws.wsProduct.getTrailObj().top = ycoord + "px";
	}
}
