﻿// JScript File
function f_windowWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0);
}

function f_windowHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0);
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0);
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function GetWindowDimensions()
{       
    return [f_windowWidth(), f_windowHeight()];
}

function GetDocumentDimensions(){
    var myWidth = 0, myHeight = 0;
    if (document.body.scrollWidth)
    {
        myWidth = document.body.scrollWidth;
    }else{
        var w = document.documentElement.offsetWidth;
        if (window.scrollMaxX) w += window.scrollMaxX; 
        myWidth = w; 
    }
    
    if (document.body.scrollHeight)
    {
        myHeight = document.body.scrollHeight;
    }else{
        myHeight = document.documentElement.offsetHeight;
    }
    
    return [myWidth, myHeight];
}
    
function getPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (true) {
		    if (!obj.offsetParent) break;
            obj = obj.offsetParent;
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
    }else{
        curleft += obj.x;
        curtop += obj.y;
	}
	return [curleft,curtop];
}


function showHelpHint(anchor, hint){
    //offset width only useful once table is visible (otherwise is 0)
    hint.style.display = 'block';

    var itemPos = getPos(anchor);
    var pagewidth=GetWindowDimensions()[0];
    // which side do we need to align to?
    if((itemPos[0]-(hint.offsetWidth/2)-10) <= 0){
        //centering would cut off on LHS
        hint.style.left = (30)+'px';
    } else if(((hint.offsetWidth/2)+itemPos[0]+10) >= pagewidth){
        //centering would cut off on RHS
        hint.style.left = (pagewidth-hint.offsetWidth-30)+'px';
    } else {
        //centering is fine
        hint.style.left = (itemPos[0]-(hint.offsetWidth/2))+'px';
    }
   
    hint.style.top = (itemPos[1]+20)+'px';
    showIshim(hint);
    
    //The substring removes the background part of the url making it have a white background

    var imgHtml = '<img src="' + anchor.src.substring(0, anchor.src.lastIndexOf("&chf=")) + '" />';
    var CO2Value = anchor.src.substring(anchor.src.indexOf("&chd=t:") + 7, anchor.src.lastIndexOf("&chco")); 
    //get the value between &chd=t: and the next & = 3.0571
    hint.innerHTML = imgHtml.replace("40x20","200x100") + "<br/>" + CO2Value + " CO<sub>2</sub> Tonnes<br/> per km";
    
}

function hideHelpHint(hint){
    hint.style.display = 'none';
}

function showIshim(itemtoshim)
{
    if (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)
    {
        var iframe = document.getElementById('ishim');
        iframe.style.display = 'block';
        iframe.style.width = itemtoshim.offsetWidth;
        iframe.style.height = itemtoshim.offsetHeight;
        iframe.style.left = itemtoshim.offsetLeft;
        iframe.style.top = itemtoshim.offsetTop;
        iframe.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=50)';
    }
}


