/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
*/

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 = 270;   // maximum image size.

var fadeDelay = 2000;		// delay time of fading-in start (in msec)
var clearDelay = 6000;		// delay time of clear of div (in msec)
if (navigator.appName=='Microsoft Internet Explorer'){
  var fadeSpeedIn = 0.25;	// speed of fading-in of picture (in percents)
  var fadeDelayIn = 1;		// delay time of fading-in of picture (in msec)
  var fadeSpeedOut = -0.5;	// speed of fading-out of picture (in percents)
  var fadeDelayOut = 1;		// delay time of fading-out of picture (in msec)
} else {
  var fadeSpeedIn = 0.2;	// speed of fading-in of picture (in percents)
  var fadeDelayIn = 10;		// delay time of fading-in of picture (in msec)
  var fadeSpeedOut = -0.5;	// speed of fading-out of picture (in percents)
  var fadeDelayOut = 1;		// delay time of fading-out of picture (in msec)
}
var fade=0; var mode;

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,title,description,width,height){
        gettrailobj().display="none"
        if (height > 0)  currentimageheight = height;
        document.onmousemove=followmouse;

        newHTML = '<div style="padding: 10px; background-color: #EEE; border: 1px solid #888;">';
        newHTML = newHTML + '<h2>' + title + '</h2>';
        newHTML = newHTML + description;

        newHTML = newHTML + '<div align="center" style="padding-top: 8px;">';
        newHTML = newHTML + '<img src="' + imagename + '" style="border: 1px solid #888;"></div>';

        newHTML = newHTML + '</div>';
        gettrailobjnostyle().innerHTML = newHTML;

        if (width > 0){
                width += 24;
                gettrailobj().width = width + 'px';
        }
        mode = 'in'; fade = 0;
        setTimeout("trailFade("+fadeSpeedIn+","+fadeDelayIn+")", fadeDelay);
}

function hidetrail(){
        mode = 'out';
        setTimeout("trailFade("+fadeSpeedOut+","+fadeDelayOut+")", 0);
        setTimeout("cleartrail()",clearDelay);
}

function cleartrail(){
        if (!mode) {
            gettrailobj().innerHTML = " ";
            gettrailobj().display="none";
            document.onmousemove="";
            gettrailobj().left="-500px";
        }
}

function trailFade (fs, delay) {
        if ((fs>0 && mode=='in') || (fs<0 && mode=='out')) {
            fade=fade+fs;
            fade=(fade>1)?1:fade; fade=(fade<0)?0:fade;
            gettrailobj().filter='alpha(opacity='+parseInt(100*fade)+')';
            gettrailobj().opacity=fade;
            if (gettrailobj().display=="none") gettrailobj().display="inline";
            if (fade>0 && fade<1) setTimeout("trailFade("+fs+","+delay+")",delay);
            if (!fade) mode='';
        }
}

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 (document.all){
        //      gettrailobjnostyle().innerHTML = 'A = ' + truebody().scrollHeight + '<br>B = ' + truebody().clientHeight;
        //} else {
        //      gettrailobjnostyle().innerHTML = 'C = ' + document.body.offsetHeight + '<br>D = ' + window.innerHeight;
        //}

        if (typeof e != "undefined"){
                if (docwidth - e.pageX < 380){
                        xcoord = e.pageX - xcoord - 350; // Move to the left side of the cursor
                } else {
                        xcoord += e.pageX;
                }
                if (docheight - e.pageY < (currentimageheight + 100)){
                        ycoord += e.pageY - Math.max(0,(100 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
                } else {
                        ycoord += e.pageY;
                }

        } else if (typeof window.event != "undefined"){
                if (docwidth - event.clientX < 380){
                        xcoord = event.clientX + truebody().scrollLeft - xcoord - 350; // Move to the left side of the cursor
                } else {
                        xcoord += truebody().scrollLeft+event.clientX
                }
                if (docheight - event.clientY < (currentimageheight + 100)){
                        ycoord += event.clientY + truebody().scrollTop - Math.max(0,(100 + currentimageheight + event.clientY - docheight));
                } else {
                        ycoord += truebody().scrollTop + event.clientY;
                }
        }

        if(ycoord < 0) { ycoord = ycoord*-1; }
        gettrailobj().left=xcoord+"px"
        gettrailobj().top=ycoord+"px"
}

// next part zoom picture
var fadeDelayZ = 100;		// delay time of fading-in start (in msec)
var clearDelayZ = 500;		// delay time of clear of div (in msec)
var zoomOffset = 15;		// nonused border for zoom pictuer (in pix)
var imgX1; var imgY1;
var fadeZ=0; var modeZ;

function getMainPic(){
  if(document.getElementById) return document.getElementById("mainpic").style; else if (document.all) return document.all.mainpic.style;
}
function getMainPic_ns(){
  if(document.getElementById) return document.getElementById("mainpic"); else if (document.all) return document.all.mainpic;
}
function getZoomObj(){
  if(document.getElementById) return document.getElementById("zoomPicObj").style; else if (document.all) return document.all.zoomPicObj.style;
}
function getZoomPic(){
  if (document.getElementById) return document.getElementById("zoomPicSrc"); else if (document.all) return document.all.zoomPicSrc;
}
function getZoomView(){
  if(document.getElementById) return document.getElementById("zoomView").style; else if (document.all) return document.all.zoomView.style;
}
function truebody(){
  return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function findPos(coord) {
  var obj = getMainPic_ns();
	var cur = 0;
  if (obj.offsetParent)
    while (obj.offsetParent && obj.tagName != "BODY"){
	   	if (coord == 'x') {cur += obj.offsetLeft}
      else {cur += obj.offsetTop}
     	obj = obj.offsetParent;
    }
  else if (obj.coord) cur += obj.coord;
	return cur;
}

function showtrailZ(){
    		getZoomObj().left='0px'; getZoomObj().top='0px';
        imgX1=findPos('x'); imgY1=findPos('y');
        getZoomView().display="none";
        document.onmousemove=followmouseZ;
        modeZ = 'in'; fadeZ = 0;
        setTimeout("trailFadeZ("+fadeSpeedIn+","+fadeDelayIn+")", fadeDelayZ);
}

function hidetrailZ(){
        modeZ = 'out';
        setTimeout("trailFadeZ("+fadeSpeedOut+","+fadeDelayOut+")", 0);
        setTimeout("cleartrailZ()",clearDelayZ);
}

function cleartrailZ(){
        if (!modeZ) {
            getZoomView().display="none";
            document.onmousemove="";
        }
}

function trailFadeZ (fs, delay) {
        if ((fs>0 && modeZ=='in') || (fs<0 && modeZ=='out')) {
            fadeZ=fadeZ+fs;
            fadeZ=(fadeZ>1)?1:fadeZ; fadeZ=(fadeZ<0)?0:fadeZ;
            getZoomView().filter='alpha(opacity='+parseInt(100*fadeZ)+')';
            getZoomView().opacity=fadeZ;
            if (getZoomView().display=="none") getZoomView().display="inline";
            if (fadeZ>0 && fadeZ<1) setTimeout("trailFadeZ("+fs+","+delay+")",delay);
            if (!fadeZ) modeZ='';
        }
}

function followmouseZ(e){
        var x=0; var y=0;
        if (typeof e != "undefined"){
                x += e.pageX;
                y += e.pageY;
        } else if (typeof window.event != "undefined"){
                x += truebody().scrollLeft + event.clientX;
                y += truebody().scrollTop + event.clientY;
        }
        zoomW=parseInt(getZoomView().width.replace('px',''))-12;
        zoomH=parseInt(getZoomView().height.replace('px',''))-12;
        mainW=getMainPic_ns().width;
        mainH=getMainPic_ns().height;
        x=(x<imgX1)?0:(x>(imgX1+mainW))?(mainW):(x-imgX1);
        y=(y<imgY1)?0:(y>(imgY1+mainH))?(mainH):(y-imgY1);
    		xZoom=parseInt((x*(getZoomPic().width-zoomW-(2*zoomOffset))/getMainPic_ns().width)+zoomOffset)*-1;
    		yZoom=parseInt((y*(getZoomPic().height-zoomH-(2*zoomOffset))/getMainPic_ns().height)+zoomOffset)*-1;
    		getZoomObj().left = xZoom+'px';
    		getZoomObj().top = yZoom+'px';
}

function changeZoomSrc(suffix){
  var src = getZoomPic().src;
  var ext = src.substring(src.length-4);
  if (src.lastIndexOf('.bw' + ext) != -1 ||
      src.lastIndexOf('.bc' + ext) != -1 ||
      src.lastIndexOf('.sp' + ext) != -1 ||
      src.lastIndexOf('.sc' + ext) != -1 ||
      src.lastIndexOf('.cv' + ext) != -1)
    src = src.substring(0,(src.length)-7);
  else
    src = src.substring(0,(src.length)-4);
  src = src + suffix + ext;
  getZoomPic().src = src;
}
