/*
Detect which version of ie is running
*/

var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
var ieversion = "0";

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
 ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
}

/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

var fadeState=0;
var trailimage=[, 100, 100] //image path, plus width and height
var newImg1 = new Image();
var offsetfrommouse=[15,15]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration=0 //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 264; // 2/3 of 400 pixels
var timeoutHandle = 0;
var timeinHandle = 0;
var setOpacityInHandle = 0;
var setOpacityOutHandle = 0;
if (displayduration>0) {
	setTimeout("hidetrail()", displayduration*1000);
}
var currentimageheight = 300;
var currentimagewidth = 264;
if (document.getElementById || document.all) {
		document.write('<div id="trailimageid">');
		document.write('</div>');
}


function gettrailobj() {
	if (document.getElementById)
	return document.getElementById("trailimageid").style
	else if (document.all)
	return document.all.trailimagid.style
}

function gettrailobjnostyle() {
	if (document.getElementById)
	return document.getElementById("trailimageid")
	else if (document.all)
	return document.all.trailimagid
}

function truebody() {
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(imagename,description,width,height) {
	if (ieversion<7) { 
		setOpacity(0); 
	}
	clearTimeout(timeoutHandle);
	clearTimeout(timeinHandle);
	if (height < 400 & width < 400 & height != 0 & width != 0) {
		currentimageheight = height/1;
		currentimagewidth = width/1;
	} else {
		if (height > 0) {
			currentimageheight = Math.round(height*0.66);
		} else {
			currentimageheight = 264;
		}
		if (width > 0) {
			currentimagewidth = Math.round(width*0.66);
		} else {
			currentimagewidth = 500;
		}
	}
	document.onmousemove=followmouse;
	cameraHTML = '';
	newHTML = '<div style="padding: 5px; background-color: #FFF; border: 1px solid #888; width:' + (currentimagewidth+20) + 'px; ">';
	newHTML = newHTML + '<h2 style="text-align:center; color:#999999; font-size:14px; ">' + description + '</h2>';
	newHTML = newHTML + '<div align="center" style="padding: 8px 2px 2px 2px;">';
	newHTML = newHTML + '<img src="' + imagename + '"';
	if ( height > 0 && width > 0 ) {
		newHTML = newHTML + ' height="' + currentimageheight + '" width="' + currentimagewidth + '"';
	} else {
		newHTML = newHTML + ' height="200"';
	}
	newHTML = newHTML + ' border="0"/></div>';
	newHTML = newHTML + '</div>';

	gettrailobjnostyle().innerHTML = newHTML;
	
	if (ieversion<7) { 
		gettrailobj().display="inline";
	}

	if (fadeState==0) {
		timeinHandle=setTimeout("fadeIn()",750);
	} else {
		if (ieversion<7) { 
			setOpacity(1);
		}
	}

}

function hidetrail() {
	clearTimeout(timeoutHandle);
	clearTimeout(timeinHandle);
	if (ieversion<7) {
 		clearTimeout(setOpacityInHandle);	
 		clearTimeout(setOpacityOutHandle);	
	}
	if (fadeState==1) {
		timeoutHandle=setTimeout("fadeOut()", 1000);
	}
}

/* set the opacity of the element (between 0.0 and 1.0) */
function setOpacity(level) {
	gettrailobj().opacity = level;
	gettrailobj().MozOpacity = level;
	gettrailobj().KhtmlOpacity = level;
	gettrailobj().filter = "alpha(opacity=" + (level * 100) + ");";
}


var duration = 1000;  /* 1000 millisecond fade = 1 sec */
var steps = 30;       /* number of opacity intervals   */
var delay = 0;     /* 5 sec delay before fading out */

function fadeIn(){
	if (ieversion<7) {
  		for (i = 0; i <= 1; i += (1 / steps)) {
    		setOpacityOutHandle=setTimeout("setOpacity(" + i + ")", i * duration);
  		}
	} else {
		gettrailobj().display="inline";
	}
   fadeState=1;
}

function fadeOut() {
	if (ieversion<7) {
  		for (i = 0; i <= 1; i += (1 / steps)) {
    		setOpacityInHandle=setTimeout("setOpacity(" + (1 - i) + ")", i * duration);
  		}
	} else {
		gettrailobj().display="none";
	}
  	fadeState=0;
}

function helpx() {
		alert(truebody().scrollLeft+truebody().clientWidth);
		return truebody().scrollLeft+truebody().clientWidth-200;
}

function helpy() {
		alert(pageXOffset+window.innerWidth-15);
		return pageXOffset+window.innerWidth-15;
}


function followmouse(e) {
	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]
	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-15
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	if (typeof e != "undefined") {
		if (docwidth - e.pageX < (currentimagewidth + 100)) {
			xcoord = e.pageX - xcoord - currentimagewidth - 30;
		//	alert('e.pageX='+e.pageX+' xcoord='+xcoord+' docwidth='+docwidth);
		} else {
			xcoord += e.pageX;
		}
		if (docheight - e.pageY < (currentimageheight + 100)) {
			if ( document.body ){
				scrollTop = Math.max(truebody().scrollTop, document.body.scrollTop);
			} else {
				scrollTop = truebody().scrollTop;
			}
			ycoord += e.pageY - Math.max(0,(100 + currentimageheight + e.pageY - docheight - scrollTop));
		} else {
			ycoord += e.pageY;
		}
	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX <  (currentimagewidth + 100)) {
			xcoord = event.clientX + truebody().scrollLeft - xcoord -  currentimagewidth - 30;
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}
	if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
}

