/*
Simple Image Trail script- By JavaScriptKit.com
Visit http://www.javascriptkit.com for this script and more
This notice must stay intact
Heavily edited and updated for DOM by Rafe Harwood - www.rafe.me.uk
*/

var maxWidth=220; // maximum image width.
var htmlStr='';

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 = 20; // maximum image height.

if (document.getElementById || document.all){
  var trail = document.createElement('div');
  trail.setAttribute('id', 'trailimageid');
  document.getElementById("outerContainer").appendChild(trail)
}

function addSeparatorsNF(nStr, inD, outD, sep)
{
  nStr += '';
  var dpos = nStr.indexOf(inD);
  var nStrEnd = '';
  if (dpos != -1) {
    nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
    nStr = nStr.substring(0, dpos);
  }
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(nStr)) {
    nStr = nStr.replace(rgx, '$1' + sep + '$2');
  }
  return nStr + nStrEnd;
}

function gettrailobj(){
  if (document.getElementById)
    return document.getElementById("trailimageid").style
  else if (document.all)
    return document.all.trailimagid.style
  else
    return '';
}

function gettrailobjnostyle(){
  if (document.getElementById)
    return document.getElementById("trailimageid")
  else if (document.all)
    return document.all.trailimagid
  else
    return '';
}

function truebody(){
  return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function showtrail(quote,room,existinginstallation,proposedinstallation,height,width,hide){

  if(hide){document.getElementById(hide).style.visibility = "hidden";}

  if (height > 0){currentimageheight = height;}
  var trailImageId = document.getElementById("trailimageid");
  if (width > 0){
    maxWidth = width;
    trailImageId.style.width = (width - 19) + "px";
  }else{
    maxWidth = 720;
    trailImageId.style.width = "700px";
  }

  document.onmousemove=followmouse;

  htmlStr = '<div class="details"><p class="date">' + quote + '</p>';

/*  if(room.indexOf("[") < 0){*/
    htmlStr = htmlStr + '<table>';
    htmlStr = htmlStr + '<tr><td style="width:12.5%"></td><th style="width:12.5%">Fittings</th><th style="width:12.5%">Load per fitting (W)</th><th style="width:12.5%">No. of fittings</th><th style="width:12.5%">Connected load (kW)</th><th style="width:12.5%">kWh p.a.</th><th style="width:12.5%">Maintenance cost</th><th style="width:12.5%">Running cost p.a.</th></tr>';
    htmlStr = htmlStr + '<tr><th>Existing</th><td>' + existinginstallation[0] + '</td><td>' + existinginstallation[1] + '</td><td>' + existinginstallation[2] + '</td><td>' + (existinginstallation[3] * 1).toFixed(3) + '</td><td>' + (existinginstallation[4] * 1).toFixed(3) + '</td><td>' + addSeparatorsNF(existinginstallation[5], '.', '.', ',') + '</td><td>' + addSeparatorsNF(existinginstallation[6], '.', '.', ',') + '</td></tr>';
    htmlStr = htmlStr + '<tr><th>Proposed</th><td>' + proposedinstallation[0] + '</td><td>' + proposedinstallation[1] + '</td><td>' + proposedinstallation[2] + '</td><td>' + (proposedinstallation[3] * 1).toFixed(3) + '</td><td>' + (proposedinstallation[4] * 1).toFixed(3) + '</td><td>' + addSeparatorsNF(proposedinstallation[5], '.', '.', ',') + '</td><td>' + addSeparatorsNF(proposedinstallation[6], '.', '.', ',') + '</td></tr>';
    htmlStr = htmlStr + '<tr class="totals"><th>Savings</th><td></td><td>' + (existinginstallation[1] - proposedinstallation[1]) + '</td><td>' + (existinginstallation[2] - proposedinstallation[2]) + '</td><td>' + (existinginstallation[3] - proposedinstallation[3]).toFixed(3) + '</td><td>' + (existinginstallation[4] - proposedinstallation[4]).toFixed(3) + '</td><td>' + addSeparatorsNF(existinginstallation[5] - proposedinstallation[5], '.', '.', ',') + '</td><td>' + addSeparatorsNF(existinginstallation[6] - proposedinstallation[6], '.', '.', ',') + '</td></tr>';
    htmlStr = htmlStr + '</table>';

//    htmlStr = htmlStr + '<table><caption>Proposed Installation</caption><tr><th>Proposed fittings</th><th>Proposed connected load per fitting (W)</th><th>No. of fittings</th><th>Total proposed connected load (kW)</th><th>Total proposed kWh p.a.</th><th>Proposed maintenance cost (Lamps / Labour)</th><th>Total running cost R p.a. proposed fittings</th></tr><tr>';
//    for (x in proposedinstallation) htmlStr = htmlStr + '<td>' + proposedinstallation[x] + '</td>';
//    htmlStr = htmlStr + '</tr></table>';
/*
  }else{
    htmlStr = htmlStr + deftitle.replace(/]/gi, ">").replace(/\[/gi, "<");
  }
*/

/*
//  if(room.indexOf("[") < 0){
//    htmlStr = htmlStr + '<dl><dt>' + room + '</dt>';
//    for (x in existinginstallation)
//    {
//      htmlStr = htmlStr + '<dd>' + existinginstallation[x] + '</dd>';
//    }
//    htmlStr = htmlStr + '</dl>';
//  }else{
//    htmlStr = htmlStr + deftitle.replace(/]/gi, ">").replace(/\[/gi, "<");
//  }
*/

  htmlStr = htmlStr + '</div>';

  gettrailobjnostyle().innerHTML = htmlStr.replace(/]/gi, ">").replace(/\[/gi, "<");
  gettrailobj().display="inline";
}

function hidetrail(hide){
  gettrailobj().innerHTML = " ";
  gettrailobj().display="none"
  document.onmousemove=""
  gettrailobj().left="-500px"
  if(hide){document.getElementById(hide).style.visibility = "visible";}
}

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 < maxWidth){
      xcoord = e.pageX - xcoord - (maxWidth - 30); // Move to the left side of the cursor
    } else {
      xcoord += e.pageX;
    }
    if (docheight - e.pageY < (currentimageheight)){
      ycoord += e.pageY - Math.max(0,(currentimageheight + e.pageY - docheight - truebody().scrollTop));
    } else {
      ycoord += e.pageY;
    }

  } else if (typeof window.event != "undefined"){
    if (docwidth - event.clientX < maxWidth){
      xcoord = event.clientX + truebody().scrollLeft - xcoord - (maxWidth - 30); // Move to the left side of the cursor
    } else {
      xcoord += truebody().scrollLeft+event.clientX
    }
    if (docheight - event.clientY < (currentimageheight)){
      ycoord += event.clientY + truebody().scrollTop - Math.max(0,(currentimageheight + event.clientY - docheight));
    } else {
      ycoord += truebody().scrollTop + event.clientY;
    }
  }

  if(ycoord < 0) { ycoord = ycoord*-1; }
  gettrailobj().left=xcoord+"px"
  gettrailobj().top=ycoord+"px"

}
