var flipFlop=0;

var boggle=0;


function changeIcon(newColor)
{
	document.getElementById("viewCart").style.backgroundImage = ++flipFlop&1 ? 'url(/images/icons/cartFullIcon.gif)' : 'url(/images/icons/cartEmptyIcon.gif)';
}
// Find me a matching DOM element
function getElementsByCondition(condition,container)
{
	container = container||document;
	var all = container.all||container.getElementsByTagName('*');
	var arr = [];
	for(var k=0;k<all.length;k++)
	{
		var elm = all[k];
		if(condition(elm,k))
			arr[arr.length] = elm;
	}
	return arr;
}
// Recursive DOM walker that looks for first instance of a class
function findClassElem( className, node )
{
	if ( node.hasChildNodes )
		for ( var i=0; i<node.childNodes.length; i++ )
		{
			if ( node.childNodes[i].className == className )
			{
				// We found it - get out quick
				return node.childNodes[i];
			}
			if ( node.childNodes[i].hasChildNodes )
			{
				el = findClassElem( className, node.childNodes[i] );
				if ( el )
				{
					// We found it - abandon the search
					return el;
				}
			}
		}
	return 0;
}
function maxOutAnchor( tdClass, el )
{
	// Find the table cell
	tableCell = findClassElem( tdClass, el );
	// Find the link
	cellLink = findClassElem( "box-link", tableCell );
	// Set link height to cell height
	cellLink.style.height = tableCell.clientHeight + "px";
}
function balanceBoxRow( el )
{
	// Get the two image elements
	var imgLeft = findClassElem( "img-left", el );
	var imgRight = findClassElem( "img-right", el );
	// Figure out which is taller
	var taller = ( imgLeft.height > imgRight.height ) ? imgLeft : imgRight;
	var shorter = ( taller == imgRight ) ? imgLeft : imgRight;
	// Calculate pixel offsets required to center vertically
	var diff = taller.height - shorter.height;
	var topOffset = ( diff > 0 ) ? parseInt( diff / 2 ) : 0;
	var bottomOffset = diff - topOffset;
	// Add offsets to the shorter image
	shorter.style.marginTop = topOffset + "px";
	shorter.style.marginBottom = bottomOffset + "px";
	// Expand box row links to fill containing table cell
	maxOutAnchor( "left-cell", el );
	maxOutAnchor( "right-cell", el );
	return false;
}
function addLinkEvents( className )
{
	getElementsByCondition(
		function sortOutBoxLinks( el )
		{
			if ( el.className == className )
			{
					el.addEventListener( "mouseover",
						function( event )
						{
							this.parentNode.style.backgroundColor = '#dcddde';
						}, false );					
					el.addEventListener( "mouseout",
						function( event )
						{
							this.parentNode.style.backgroundColor = '#fff';
						}, false );

			} 
		}
	);
}
function hoverOn( elem ) { elem.parentNode.style.backgroundColor = '#dcddde' }
function hoverOff( elem ) { elem.parentNode.style.backgroundColor = '#fff' }

function balanceBoxRows()
{
	// For every box row call balanceBoxRow()
	getElementsByCondition( function(el){ if(el.className=='box-row') { balanceBoxRow(el) } } );
	//addLinkEvents( 'box-link' );
}

function writeInSection( a )
{
	switch(a)
	{
		case 1:
			document.write( '<h3 style="color:#d82620">Delivery Information<span style="color:#333"> - boxes marked with <sup>*</sup> are required.</span></h3>');
			break ;  
		case 2:
			document.write( '<div class="grey-section">');
			document.write( '<p style="margin:0"><label for="SameAddress" style="color:#d82620; font-weight:bold">Click Here &raquo;</label> &nbsp;');
			document.write( '<input id="SameAddress" type="checkbox" value="1" onchange="toggleOff();" /> &nbsp;if your Delivery Address is the same as your Billing Address</p>');
			document.write( '</div>');
			break;
		default:
			break;
	}
}

function toggleOn( )
{
	new Effect.BlindDown(document.getElementById('delivery-info'));
	new Effect.BlindUp(document.getElementById('no-delivery-info'));
	document.getElementById('DifferentAddress').checked=false;
}
function toggleOff( )
{
	new Effect.BlindUp(document.getElementById('delivery-info'));
	new Effect.BlindDown(document.getElementById('no-delivery-info'));
	document.getElementById('SameAddress').checked=false;
}

function BoggleOn( )
{
	if( boggle==0 )
	{
		new Effect.BlindDown(document.getElementById('Boggle'));
		boggle=1;
	}
}
function BoggleOff( )
{
	if( boggle==1 )
	{
		new Effect.BlindUp(document.getElementById('Boggle'));
		boggle=0;
	}
}