////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	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).
//	01.09.2011	2.10.0	DJV		Mods for new image handling.
//	22.10.2011	2.10.1	DJV		Mods for image path changes. $id mods.
//	07.12.2011	3.0.0	DJV		V3 mods for showLargePic.
//	23.01.2012	3.0.1	DJV		Fixed error in doClrLargePic when no large picture on screen.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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 = [],
	jsSubtitles = [],
	jsNotes = [],
	jsLinks = [],
	lpTm = null,
	v3site = false;

function showLargePic (ref, w, h, v3)
{
	if (lpTm != null) clearTimeout (lpTm);
	
	if (v3)
	{
		v3site = true;
		var pic = $id ("searchpreview");
		var pics = pic.style;
		var thumb = $id ("pic" + ref);
		var xy = DLibUtilities.findPos (thumb);
		var off = DLibUtilities.scrollOffset ();

		pic.innerHTML = 
			'<iframe src="javascript:false;"></iframe>' +
			'<div id="searchpreviewimage">' +
				href (img ('/images/products/' + ref.charAt (0).toUpperCase () + '/' + ref +
					'/p1m_' + ref + '.jpg', 0, jsTitles [ref] + ' - click for full details', "", 'width="220"'), jsLinks [ref], "", 
					'title="More information about - ' + jsTitles [ref] + '"') + br () +
				href ('Full product information &#9658;', jsLinks [ref], "", 'class="productpreview productpreviewlarge"') +
			'</div>' +
			'<div id="searchpreviewcontent">' +
				'<h2>' + jsTitles [ref] + '</h2>' + 
				'<h3>' + jsSubtitles [ref] + '</h3>' +				
				jsNotes [ref] +
			'</div>';

		pics.display = "block";
		pics.position = "absolute";

		pics.left = (70 - off [0]) + "px";

		var top = ((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 18px 8px 8px";
		pics.backgroundColor = "white";
	}
	else
	{
		var pic = $id ("largepic");
		var pics = pic.style;
		var thumb = $id ("pic" + ref);
		var xy = DLibUtilities.findPos (thumb);
		var off = DLibUtilities.scrollOffset ();
		pic.innerHTML = jsTitles [ref] + '<img src="/images/products/' + ref.charAt (0).toUpperCase () + '/' + ref +
			'/p1m_' + 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 = $id ((v3site ? "searchpreview" : "largepic"));
	
	if (pic)
	{
		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 = $id ("tooltip" + id);
	var xy = DLibUtilities.findPos (ob);
	var tt = $id ("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 ()
{
	$id ("popuptooltip").style.display = "none";
}

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


