// ************************************************************************
// ***                     SIZE AND POSITIONING                          **
// ************************************************************************
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	foo = new Array(0);
	foo[0] = scrOfX;
	foo[1] = scrOfY;
	return [ scrOfX, scrOfY ];
}

// return the X and Y pixels of the current window
function windowSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

var cursor = {x:0, y:0};
function getPosition(e) {
	//var cursor = {x:0, y:0};
	e = e || window.event;
	if (e.pageX || e.pageY) {
		cursor.x = e.pageX;
		cursor.y = e.pageY;
	} 
	else {
		try {
			var de = document.documentElement;
			var b = document.body;
			cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
			cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
		} catch (e) {
			//do nothing
		}
	}
	return cursor;
}


// ************************************************************************
// ***                         BALLOON CHECKS                            **
// ************************************************************************
function gotoCategory (selectObj) {

	var selectedIndex = selectObj.selectedIndex;
	var destinationID = selectObj.options[selectedIndex].value;
	window.location.href = '/category.php?id=' + destinationID;
}

function gotoPage (selectObj) {

	var selectedIndex = selectObj.selectedIndex;
	var destinationURL = selectObj.options[selectedIndex].value;
	window.location.href = destinationURL;
}


// ************************************************************************
// ***                         BALLOON CHECKS                            **
// ************************************************************************
var OUT_BOUNDS = 0; //should be const, but only supported by gecko browsers
var IN_BOUNDS = 1; //should be const, but only supported by gecko browsers
var ON_MOUSEOVER = 2; //should be const, but only supported by gecko browsers
function cursorInBounds(cursorPos, mouseoverNode, balloonNode, mouseoverLoc) {
	
	//alert("cursorPos = " + cursorPos + " mouseoverNode = " + mouseoverNode + " balloonNode = " + balloonNode + " mouseoverLoc = " + mouseoverLoc)
	
	var balloonSize = Element.getDimensions(balloonNode);
	var mouseoverSize = Element.getDimensions(mouseoverNode);
	var balloonPos = Position.cumulativeOffset(balloonNode);
	var mouseoverPos = Position.cumulativeOffset(mouseoverNode);
	
	//alert("balloonSize = " + balloonSize + " mouseoverSize = " + mouseoverSize + " balloonPos = " + balloonPos + " mouseoverPos = " + mouseoverPos)
	
	var cursorNearMouseover = false;
	mouseoverLoc = mouseoverLoc.toUpperCase();
	
	switch (mouseoverLoc) {
		case 'BELOW':
			cursorNearMouseover = (cursorPos.y >= mouseoverPos[1]);
			break;
		case 'ABOVE':
			cursorNearMouseover = (cursorPos.y <= mouseoverPos[1] + mouseoverSize.height);
			break;
		case 'RIGHT':
			cursorNearMouseover = (cursorPos.x >= mouseoverPos[0]);
			break;
		case 'LEFT':
			cursorNearMouseover = (cursorPos.x <= mouseoverPos[0] + mouseoverSize.width);
			break;
		default:
			alert(mouseoverLoc);
	}

	if (cursorNearMouseover && (cursorPos.x < mouseoverPos[0] || cursorPos.x > mouseoverPos[0] + mouseoverSize.width || cursorPos.y < mouseoverPos[1] || cursorPos.y > mouseoverPos[1] + mouseoverSize.height)) {
		return OUT_BOUNDS;
	} else if (cursorPos.x > mouseoverPos[0] && cursorPos.x < mouseoverPos[0] + mouseoverSize.width && cursorPos.y > mouseoverPos[1] && cursorPos.y < mouseoverPos[1] + mouseoverSize.height) {
		return ON_MOUSEOVER;
	} else if (cursorPos.x > balloonPos[0] && cursorPos.x < balloonPos[0] + balloonSize.width && cursorPos.y > balloonPos[1] && cursorPos.y < balloonPos[1] + balloonSize.height) {
		return IN_BOUNDS;
	} else {
		return OUT_BOUNDS;
	}
}

function hideOtherBalloons(thisBalloonType) {
	switch (thisBalloonType) {
		case 'cart':
			hideInfo(curInfoDot);
			hidePdf(curPdfDot);
			break;
		default:
			hideCart(curCartDot);
			break;
	}
}


function setID(obj, id) {
	var oldObj = document.getElementById(id);
	
	while (oldObj) {
		oldObj.removeAttribute('id');
		oldObj = document.getElementById(id);
	}
	
	obj.setAttribute('id', id);
}



// ************************************************************************
// ***                          CART DOT                                 **
// ************************************************************************
var curCartDot = null;
function do_showCart(cartDotID) {
	setTimeout('showCart("' + cartDotID + '")', 50);
}
function showCart(cartDotID) {
	cartDotNode = document.getElementById(cartDotID);

	//don't show the cart balloon if product state is open.
	//if (currentDetailsNode && currentDetailsNode.style.display == 'block') return;

	//hide the cart before re-positioning it
	if (curCartDot != cartDotNode) {
		hideCart();
	}
	curCartDot = cartDotNode;

	//Don't display the balloon if the details table is showing.
	var cartDotId = curCartDot.getAttribute('id');
	var parts = cartDotId.split('_');
	var id = parseInt(parts[parts.length - 1], 10);
	//var detailsTableNode = document.getElementById('product_details_table_' + id);
	//if (detailsTableNode.style.display != 'none') return;

	Position.prepare();
	var cartDotPos = Position.cumulativeOffset(curCartDot);
	var cartDotSize = Element.getDimensions(cartDotNode);

	var cartBoxNode = document.getElementById('balloon-info');
	var cartBoxSize = Element.getDimensions(cartBoxNode);
	
	var left = cartDotPos[0] - (cartBoxSize.width / 2) + (cartDotSize.width / 2) + 8;
	var top = cartDotPos[1] - cartBoxSize.height + 10;

	var winSize = windowSize();
	var winScroll = getScrollXY();

	cartBoxNode.style.left = left + 'px';
	cartBoxNode.style.top = top + 'px';
	cartBoxNode.style.display = 'block';
	
	//alert(cartBoxNode.style.display);

	//hideOtherBalloons('cart');
	Event.observe(document, 'mousemove', checkCart);
}

function checkCart(e) {
	var pos = getPosition(e);
	//alert(pos.y);
	//var pos = cursor;
	var cartBoxNode = document.getElementById('balloon-info');
	var boundsCheck = cursorInBounds(pos, curCartDot, cartBoxNode, 'BELOW');

	if (boundsCheck == OUT_BOUNDS) {
		Event.stopObserving(document, 'mousemove', checkCart);
		hideCart(curCartDot);
	} else if (boundsCheck == ON_MOUSEOVER && cartBoxNode.style.display != 'block') {
		showCart(curCartDot);
	}
}

function checkCartClick(e) {
	//var pos = getPosition(e);
	var pos = cursor;
	var cartBoxNode = document.getElementById('balloon-info');
	var boundsCheck = cursorInBounds(pos, curCartDot, cartBoxNode, 'BELOW');

	if (boundsCheck == ON_MOUSEOVER && curCartDot.parentNode.onclick) {
		var onclick = curCartDot.parentNode.onclick.toString();
		var strToFind = 'itemCartClicked(';
		var strPos = onclick.indexOf(strToFind);

		if (strPos != -1 && onclick.length > strPos) {
			var id = parseInt(onclick.substring(strPos + strToFind.length), 10);
			itemCartClicked(id);
		}
	}
}

function hideCart(refNode) {
	cartBoxNode = document.getElementById('balloon-info');
	
	if (cartBoxNode) {
		cartBoxNode.style.display = 'none';
	}
}


function setCurrentItemVars(productGroupID) {
	currentProductGroupID = productGroupID;
	currentDetailsNode = document.getElementById(productGroupID);
	currentDetailsTable = document.getElementById(productGroupID);
}


// ************************************************************************
// ***                        BROWSER ACTIONS                            **
// ************************************************************************

function setBalloonClass() {
	if (browser.isIE && browser.versionMajor <= 6) {
		cartBoxNode = document.getElementById('balloon-info');
		cartBoxNode.className = 'bv2';
	}
}


// ************************************************************************
// ***                        DEFAULT ACTIONS                            **
// ************************************************************************

if (self.Event && Event.observe) {
	Event.observe(document, 'mousemove', getPosition);
}


// ************************************************************************
// ***                       POPUP WINDOW                               ***
// ************************************************************************

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


// ************************************************************************
// ***                       TOGGLE LAYER                               ***
// ************************************************************************

function toggleObject(objectID) {
	var el = document.getElementById(objectID);
	if (!el) return;

	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}


// ************************************************************************
// ***                      IMAGE PRE-LOADER                            ***
// ************************************************************************

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}


// ************************************************************************
// ***                          SWAP IMAGES                             ***
// ************************************************************************

function MM_findObj(n, d) { //v4.0
	var p,i,x;	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
	
function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}



// ************************************************************************
// ***                        OPEN NEW WINDOW                           ***
// ************************************************************************

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}


