// make sure popwin initilizes (see onlaod.js)
if (initialize) { initialize.productPreview = true }

var productPreview = new Object();

// number that triggers productPreview.write // lower number is more difficult to achieve
productPreview.nIntentSensitivity = 4;

// number of milliseconds until preview is shown
productPreview.nShowPreviewDelay = 450;



/**
* This function initializes [1] rollover images in all browsers, [2] rollover
* input buttons in all browsers, and [3] the primary/secondary navigation
* dhtml menu in IE-only. The primary/secondary navigation menu is also CSS
* driven for browsers that understand li:hover (Safari, Firefox, IE7, etc).
*/
productPreview.ini =function()
{
	// exit function if you are not a modern browser
	if (!document.getElementById) return;

	// get a collections of images
	var cDivs = document.getElementsByTagName('DIV');

	for (var i = 0; i < cDivs.length; i++)
	{
		var oDiv = cDivs[i];
		
		if ( oDiv.className == 'product' )
		{
			// find product link // attach events to it
			var oA = oDiv.getElementsByTagName('A')[0];

			oA.preview = null;
			oA.shield = null;

			oA.id = 'productPreview_ID_'+i;

			oA.onmouseover = productPreview.startShowTimers;
			oA.onmouseout  = productPreview.startHideTimers;
		}
	}
}



/**
* This function starts the timers that create DOM elements and start the preview.
*/
productPreview.startShowTimers = function()
{
	// used to pass object into timer functions
	var oA = this;

	if ( this.nodeName != 'A' )
	{
		oA = document.getElementById(this.oA_id);
	}

	// kill hidePreviewTimer
	if ( oA.hidePreviewTimer )
	{
		clearTimeout(oA.hidePreviewTimer);
		oA.hidePreviewTimer = null;
	}

	if ( !oA.preview )
	{
		productPreview.writeDecision( oA );
	}
	else if ( oA.preview.style.display == 'none' )
	{
		oA.showPreviewTimer = setTimeout( function(){ productPreview.show( oA ) } , productPreview.nShowPreviewDelay );
	}
}



/**
* This function decides whether or not to write the preview HTML. It
* captures/compares mouse position over time to determine if the user
* is "thinking" about a given product. If the sensitivity threshold is
* reached, then we write the HTML into the page.
*/
productPreview.writeDecision = function( oA )
{
	// This function gets the current mouse position
	function getMousePosition( e )
	{
		if (!e) var e = window.event;
		oA.currentMouseX = e.screenX;
		oA.currentMouseY = e.screenY;
	}

	// initialize previousMouse position
	// high number guarantees difference is greater than sensitivity threshold
	oA.previousMouseX = oA.previousMouseY = 10000;
	oA.onmousemove = getMousePosition;


	// This function compares mouse coordinates.
	function determineIntent()
	{
		// if the difference between previous and current coordinates (for both x and y) are less than the sensitivity threshold
		if ( (Math.abs(oA.previousMouseX-oA.currentMouseX)<productPreview.nIntentSensitivity) && (Math.abs(oA.previousMouseY-oA.currentMouseY)<productPreview.nIntentSensitivity) )
		{
			clearInterval(oA.determineIntentInterval);
			oA.determineIntentInterval = null;
			oA.onmousemove = null;
			
			// finally write the HTML
			productPreview.write( oA )
			
			// start showPreviewTimer // delay - 1 determineIntent interval
			oA.showPreviewTimer = setTimeout( function(){ productPreview.show( oA ) } , productPreview.nShowPreviewDelay - 100 );
		}
		oA.previousMouseX = oA.currentMouseX;
		oA.previousMouseY = oA.currentMouseY;
	}

	oA.determineIntentInterval = setInterval( determineIntent , 100 );
}



/**
* This function stops all timers and hides the preview.
*/
productPreview.write = function( oA )
{
	var oPreviewBubble = document.createElement('DIV');
	oPreviewBubble.className = "productPreview";
	document.body.appendChild(oPreviewBubble);
	
	oA.preview = oPreviewBubble;
	
	// used so preview can find associated oA // do not pass object reference
	oA.preview.oA_id = oA.id;
	
	// make sure there is no 'dead' space when user is moused over preview
	oA.preview.onmouseover = productPreview.startShowTimers;
	oA.preview.onmouseout  = productPreview.startHideTimers;
	oA.preview.onclick = function(){ document.location = oA.href };

	// get product preview image source
	var sSrc =  oA.getElementsByTagName('IMG')[0].src;
	sSrc = sSrc.replace( 'graphics/images/' , 'graphics/nf/' );
	
	if ( document.body.id == 'CATEGORY')
	{
		var aSrc = sSrc.split('/');
		var sFilename_tn = aSrc[aSrc.length-1];
		var sProductId = sFilename_tn.split('_')[2];
		var sFilename_nf = 'tn_' + sProductId + '_nf.jpg'
		sSrc = sSrc.replace( sFilename_tn , sFilename_nf );
	}
	else
	{
		sSrc = sSrc.replace( '.jpg' , '_nf.jpg' );
	}
	
	// place the product preview image
	var oPreviewImage = document.createElement('IMG');
	oPreviewImage.src = sSrc;
	oPreviewBubble.appendChild(oPreviewImage);

	// BKC with the two lines of code uncommented, the image of the product will hide the preview bubble on mouse over
	// used so preview image can find associated oA // do not pass object reference
	// oPreviewImage.oA_id = oA.id;
	// product preview image immediately closes oA when moused over
	// oPreviewImage.onmouseover = productPreview.startHideTimers;

	// detect IE, but block IE7 (both bugs are fixed in IE7)
	if (document.attachEvent&&!window.XMLHttpRequest)
	{
		// get the url of the productPreview background image
		var sSrcBG = oA.preview.currentStyle[ 'backgroundImage' ];
		sSrcBG = sSrcBG.substring( sSrcBG.lastIndexOf('images/') , sSrcBG.length - 2 )
		oA.preview.style.backgroundImage = 'none';

		// attach filter to another DIV so that oPreviewBubble can still handle mouse clicks
		var oPreviewIEBackground = document.createElement('DIV');
		oPreviewIEBackground.style.filter ="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+sSrcBG+"')";
		oPreviewBubble.appendChild(oPreviewIEBackground);

		// if the shield does not already exist, create it
		if ( !document.getElementById('productPreviewShield') )
		{
			// insert iframe to block select boxes from showing through menu
			var oFrame = document.createElement('IFRAME');
			oFrame.id = 'productPreviewShield';
			oFrame.src = "about:blank";
			oFrame.scrolling = "no";
			oFrame.frameborder = "0";
			oFrame.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
			document.body.appendChild(oFrame);
		}
	}
}



/**
* This function returns the absolute position of product link (in relation to the body) so that the bubble can be placed correctly.
*/
productPreview.position = function( oA )
{
	var posT = posL = 0;
	if (oA.offsetParent)
	{
		posT = oA.offsetTop;
		posL = oA.offsetLeft;

		while (oA = oA.offsetParent)
		{
			posT += oA.offsetTop;
			posL += oA.offsetLeft;
		}
	}
	return [posT,posL];
}



/**
* This function shows the product preview.
*/
productPreview.show = function( oA )
{
	var aPos = productPreview.position( oA );

	if ( document.body.id == 'CATEGORY') { aPos[0] += 4; aPos[1] -= 35 }

	var sPosT =  aPos[0] -  93 + 'px';
	var sPosL =  aPos[1] - 332 + 'px';
	
	oA.preview.style.top  = sPosT;
	oA.preview.style.left = sPosL;

	oA.preview.style.display = 'block';
	
	var oShield = document.getElementById('productPreviewShield')

	if ( oShield )
	{
		oShield.style.top  = sPosT;
		oShield.style.left = sPosL;
		oShield.style.display = 'block';
	}
}



/**
* This function stops all timers and hides the preview.
*/
productPreview.startHideTimers = function()
{
	// used to pass object into timer functions
	var oA = this;

	if ( this.nodeName != 'A' )
	{
		oA = document.getElementById(this.oA_id);
	}

	// kill determineIntentTimer
	if ( oA.determineIntentInterval )
	{
		clearInterval(oA.determineIntentInterval);
		oA.determineIntentInterval = null;
	}

	// kill showPreviewTimer
	if ( oA.showPreviewTimer )
	{
		clearTimeout(oA.showPreviewTimer);
		oA.showPreviewTimer = null;
	}

	if ( oA.preview )
	{
		oA.hidePreviewTimer = setTimeout( function(){ productPreview.hide( oA ) } , 25 );
	}
}



/**
* This function shows the product preview.
*/
productPreview.hide = function( oA )
{
	oA.preview.style.display = 'none';

	var oShield = document.getElementById('productPreviewShield')

	if ( oShield )
	{
		oShield.style.display = 'none';
	}
}
