<!--
function setUpDoc()
{
	var mainImage = document.getElementById('productImage');
	if(mainImage)
	{
		mainImageSource = mainImage.getAttribute('src');
	}
	setEventListeners();
}

function setEventListeners()
{
	var $options = $('#options');
	if($options.length > 0)
	{
		var $optionsSelect = $('<select name="options"/>');
		$optionPara    = $('<p id="optionText"/>');
		$('#purchaseInput').data('swap', $('<p class="stock">Out of stock</p><p><a href="stock-alert/">Request an email stock alert.</a></p>'));
		$('#purchaseInput').data('stock', true);
		$options.find('label').each(function()
        {
            var $optionNode = $('<option/>');
            var links = $(this).parent().find('a');
            var linkFound = false;
            if($(this).parent().find('a').length > 0)
            {
                var $link = $(this).parent().find('a').eq(0);
                linkFound = true;
            }
            $optionNode.data('stock', true);
            if($(this).find('.stock').length > 0)
            {
            	$optionNode.data('stock', false);
            }	
            if($(this).attr('for') !== '')
            {
                var optionId = $(this).attr('for');
                if(linkFound)
                {
                	$optionNode.data('href', $link.attr('href'));
                    var preImage = new Image(300, 300);
                    preImage.src = $link.attr('href');
                }
                
                $optionNode.attr('id', optionId);
                $optionInput = $('#' + optionId);
                if($optionInput.attr('checked') != false)
                {
                    $optionNode.attr('selected', 'selected');
                    $optionPara.text($(this).text());

                    if(linkFound)
                    {
                        $('#productImage').attr('src', $link.attr('href'));
                    }
                    if($optionNode.data('stock') != $('#purchaseInput').data('stock'))
                    {
                    	var toSwap = $('#purchaseInput').children();
                    	$('#purchaseInput').children().remove();
                    	$('#purchaseInput').append($('#purchaseInput').data('swap'));
                    	$('#purchaseInput').data('swap', toSwap);
                    	$('#purchaseInput').data('stock', $optionNode.data('stock'));
                    	$('#purchaseInput').find('a').attr('href', 'stock-alert/' + $optionInput.val() + '/');
                    }
                }   
                $optionNode.attr('value', $optionInput.val());
            }
            optionText = $(this).text();
            $optionNode.data('optionText', optionText);
            $optionNode.text(optionText.substr(0, optionText.indexOf('-')));
            $optionsSelect.append($optionNode);
        });
		$options.children().remove();
        $options.append('<legend>options</legend>');
        $options.append($optionsSelect);
        $options.append($optionPara);
        $optionsSelect.change(function()
        {
        	optionIndex = $(this).val();
        	if($(this).find(':selected').data('href') != '')
            {
                $('#productImage').attr('src', $(this).find(':selected').data('href'));
            }
            $('#optionText').children().remove();
            $('#optionText').text($(this).find(':selected').data('optionText'));
            var $currentContent = $('#purchaseInput').children();
            if($(this).find(':selected').data('stock') != $('#purchaseInput').data('stock'))
            {
            	var toSwap = $('#purchaseInput').children();
            	$('#purchaseInput').children().remove();
            	$('#purchaseInput').append($('#purchaseInput').data('swap'));
            	$('#purchaseInput').data('swap', toSwap);
            	$('#purchaseInput').data('stock', $(this).find(':selected').data('stock'));
            }
            $('#purchaseInput').find('a').attr('href', 'stock-alert/' + optionIndex + '/');
        });
	}
	
	extrasFieldset = document.getElementById('optionalExtras');
	if(extrasFieldset)
	{
		links = extrasFieldset.getElementsByTagName('a');
		var imageLinks = new Array();
		for(j=0; j < links.length; j++)
		{
			if(links[j].className == 'extraImageLink')
			{
				imageLinks.push(links[j]);
			}
		}
		for(j=0; j < imageLinks.length; j++)
		{
			
			optionId = imageLinks[j].parentNode.getElementsByTagName('label').item(0).htmlFor;
			option = document.getElementById(optionId);
			imageSource[optionId] = imageLinks[j].getAttribute('href');
			//imageLinks[j].parentNode.removeChild(imageLinks[j]);
			addEvent(imageLinks[j], 'click', imageLinkClick, false);
		}
	}
}
/*
function optionClick(e)
{
	select = getCallingElement(e);
//    alert(select.selectedIndex);
		productImage = document.getElementById('productImage');
		if(imageSource[select.selectedIndex])
        {
            productImage.src = imageSource[select.selectedIndex];
        }
        optionTextNode = document.createTextNode(optionInfo[select.selectedIndex]);
        optionPara = document.getElementById('optionText');
        removeChildrenFromNode(optionPara);
        optionPara.appendChild(optionTextNode);
}
*/
function imageLinkClick(e)
{
	cancelClick(e);
	link = getCallingElement(e);
	if(link.firstChild.nodeValue == 'view picture')
	{
		optionId = link.parentNode.getElementsByTagName('label').item(0).htmlFor;
		productImage = document.getElementById('productImage')
		productImage.setAttribute('src', imageSource[optionId]);	
		link.firstChild.nodeValue = 'view main picture'
	}
	else if(link.firstChild.nodeValue == 'view main picture')
	{
		productImage = document.getElementById('productImage')
		productImage.setAttribute('src', mainImageSource);
		link.firstChild.nodeValue = 'view picture'
	}
}

function removeChildrenFromNode(node)
{
   var len = node.childNodes.length;
    /*
    while (node.hasChildNodes())
    {
      alert('blah');
      node.removeChild(node.firstChild);
      alert('ffs');
    }
    */
    while (node.childNodes[0])
    {
        node.removeChild(node.childNodes[0]);
    }
}

var mainImageSource;
var imageSource = new Object();
var optionInfo = new Object();
//alert(document.mimeType);
if(!document.all && 
      document.getElementById)
{
    var txtNode = document.createTextNode('.imageLink, #options input, #options label, #options span {display:none;} #options {height: 12em;}'); 
    var styleNode = document.createElement('style');
    styleNode.appendChild(txtNode);
    styleNode.setAttribute('type', 'text/css');
    var head = document.getElementsByTagName('head').item(0);
    head.appendChild(styleNode);
}
else
{
    document.write('<style type="text/css">.imageLink, #options input, #options label, #options span {display:none;} #options {height: 12em;}</style>');
}
addEvent(window, 'load', setUpDoc, false);
-->
