///////////////////////////////////////////////////////////////////////////////////////////////////
//	Summer - Common JavaScript functions.
///////////////////////////////////////////////////////////////////////////////////////////////////
//	Program Updates
//
//	07.01.2009	2.5.0	DJV		Created from existing separate functions/files.
//	06.10.2009	2.6.0	DJV		Removed functions that are now available via dlib/js.
//	25.03.2010	2.7.0	DJV		Added in the large picture overlay code (originally in showpt.php).
//
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////
// Round to 2 decimal places
///////////////////////////////////////////////////////////////////////////////////////////////////

function dec (num, dp)
{
	var n = "" + num;

	var dp1 = n.indexOf (".");
	var l = n.length;

	if (dp > 0 && dp1 == -1)
	{
		n += ".";
		dp1 = n.indexOf (".");
		l = n.length;
	}

	while (l - dp1 < dp + 1)
	{
		n += "0";
		l = n.length;
	}

	while (l - dp1 > dp + 1)
	{
		n = n.substring (0, n.length - 1);
		l = n.length;
	}

	return n;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function sdec (num, dp)
{
	if (dp == 0)
	{
		return num;
	}

	var p = Math.pow (10, dp);
	num = Math.round (num * p) / p;
	var n = "" + num;

	var dp1 = n.indexOf (".");
	var l = n.length;

	if (dp1 == -1)
	{
		n += ".";
		dp1 = n.indexOf (".");
		l = n.length;
	}

	while (l - dp1 < dp + 1)
	{
		n += "0";
		l = n.length;
	}

	while (l - dp1 > dp + 1)
	{
		n = n.substring (0, n.length - 1);
		l = n.length;
	}

	return n;
}

///////////////////////////////////////////////////////////////////////////////////////////////////

function tDec (decimals, dec_point, thousands_sep)
{
	decimals = Math.abs(decimals) + 1 ? decimals : 2;
	dec_point = dec_point || '.';
	thousands_sep = thousands_sep || ',';

	// returns matches[1] as sign, matches[2] as numbers and matches[3] as decimals

	var matches = /(-)?(\d+)(\.\d+)?/.exec((isNaN(this) ? 0 : this) + '');
	var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;

	return (matches[1] ? matches[1] : '') + (remainder ? matches[2].substr(0, remainder) +
		thousands_sep : '') + matches[2].substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) +
		(decimals ? dec_point + (+matches[3] || 0).toFixed(decimals).substr(2) : '');
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// Large picture handling for pages and product type lists
///////////////////////////////////////////////////////////////////////////////////////////////////

var	jsTitles = [],
	jsNotes = [],
	jsLinks = [],
	lpTm = null;

function showLargePic (ref, w, h)
{
	if (lpTm != null) clearTimeout (lpTm);
	var pic = document.getElementById ("largepic");
	var pics = pic.style;
	var thumb = document.getElementById ("pic" + ref);
	var xy = DLibUtilities.findPos (thumb);
	var off = DLibUtilities.scrollOffset ();
	pic.innerHTML = jsTitles [ref] + '<img src="/images/products/' + ref + '/p0_' + ref +
		'.jpg" border="0" alt="Loading larger picture..." width="' + w + '" height="' + h + '" />' +
		jsNotes [ref] + '<div id="lppopupmsg">' +
		'Please click <a href="' + jsLinks [ref] + '">here</a> for the full details on this item.</div>';
	pics.width = (w + 300) + "px";
	pics.display = "block";
	pics.position = "absolute";
	pics.left = (xy [0] + 90 - off [0]) + "px";
	var top = (xy [1] - (h / 2) + 35);
	var wh = DLibUtilities.getWindowSize ();
	pic.onmouseover = clrLargePicTimer;
	pic.onmouseout = clrLargePic;
	var boxHt = pic.clientHeight;

	if (top + boxHt + 15 > wh [1] + off [1])	// Keep image onscreen
	{
		top = wh [1] - boxHt - 15 + off [1];
	}

	if (top - off[1] < 0)
	{
		top = off [1] + 5;
	}

	pics.top = parseInt (top) + "px";
	pics.padding = "8px";
	pics.backgroundColor = "#ffc";
}

function clrLargePic (e)
{
	lpTm = setTimeout ("doClrLargePic()", 700);
}

function clrLargePicTimer (e)
{
	if (lpTm != null) clearTimeout (lpTm);
}

function doClrLargePic ()
{
	clearTimeout (lpTm);
	var pic = document.getElementById ("largepic");
	var pics = pic.style;
	pic.innerHTML = "";
	pics.display = "none";
	pics.position = "absolute";
	pics.left = "-1000px";
	pics.top = "-1000px";
}

///////////////////////////////////////////////////////////////////////////////////////////////////

var mX = -1;
var mY = -1;
document.onmousemove = mouseXY;

function mouseXY (e)
{
	var ev = e ? e : window.event;
	var off = DLibUtilities.scrollOffset ();
	mX = ev.clientX + off [0];
	mY = ev.clientY + off [1];
}

function showTooltip (txt, id)
{
	var ob = document.getElementById ("tooltip" + id);
	var xy = DLibUtilities.findPos (ob);
	var tt = document.getElementById ("popuptooltip");
	tt.style.position = "absolute";
	tt.style.top = (mY + 10) + "px";
	tt.style.left = (mX + 15) + "px";
	tt.style.display = "block";
	tt.innerHTML = txt;
}

function clearTooltip ()
{
	document.getElementById ("popuptooltip").style.display = "none";
}

///////////////////////////////////////////////////////////////////////////////////////////////////

