var cookieName = 'CompareProductSelections';

function updateSpecifier()
{
	var elAjaxUrl = $('#ajaxUrl').get(0);
	if (elAjaxUrl)
	{
		updateForm('#productspecifier', elAjaxUrl.innerHTML);
	}
}

function updateForm(formId, ajaxUrl)
{
	var frm = $(formId).get(0);
	if (frm)
	{
		$.ajax({
			type:	"GET",
			url:	ajaxUrl + '?' + Form.serialize(frm),
			dataType: "xml",
			success: function(xml){
				if (xml && xml.text != '')
				{
					updateFormElements(xml);
					updateProductCount(xml);
				}
			}
		})
	}
	return false;
}

function updateFormElements(response)
{
	var frm = $('#productspecifier').get(0);
	var els = frm.elements;
	var currentEl;
	
	for (var i=0; i<els.length; i++)
	{
		if (els[i].type == 'checkbox')
		{
			currentEl = getEl(els[i].id + '_container');
			if (currentEl)
			{
				if (!isContainedInXmlDoc(els[i], response))
				{
					els[i].checked = false;
					currentEl.style.display = 'none';
				}
				else
				{
					currentEl.style.display = 'block';
				}
			}
		}
	}
}

function updateProductCount(response)
{
	var productCount = response.getElementsByTagName('ProductCount')[0].firstChild.nodeValue;
	if (productCount)
	{
		$('#productCount').html(productCount);
	}
}

function isContainedInXmlDoc(el, response)
{
	var selectName = el.id.split(':')[0];
	var itemValue = el.id.split(':')[1];
	var xpath = '//AttributeOptions/AttributeOption[@SelectName = \'' + selectName + '\']/Item[Value = \'' + itemValue + '\']';
	var els = response.selectNodes(xpath);
	if (els && els.length > 0)
	{
		return true;
	}
	return false;
}

function ajaxInvoke(target)
{
	$.ajax({
		type:	"GET",
		url:	target
	});
}

$(document).ready(function(){

	//Product Image Toggle
	$('#productImageToggle').click(function(){
		var productImage = getEl('productImage');
		var productImageInscene = getEl('productImageInscene');
		if (productImage && productImageInscene)
		{
			if (productImage.style.display == 'none')
			{
				productImage.style.display = 'block';
				productImageInscene.style.display = 'none';
				this.innerHTML = 'View in Scene';
			}
			else
			{
				productImage.style.display = 'none';
				productImageInscene.style.display = 'block';
				this.innerHTML = 'View Product';
			}
		}
	});
	
	
	// Brand Use
	var has_quality_rating = document.getElementById('qualityRatingStatistics')!=null;
	$('#brandUseContainers > div').hide();
	    $('#brandUseContainers > div:first-child').show();
	    $('#brandUseTabs > div:first-child').addClass('selected');
	if (has_quality_rating) {
	    $('#brandUseContainers > div:first-child').show();
	    $('#brandUseTabs > div:first-child').addClass('selected');
	} else {	
		$('#brandUseTabs > div').removeClass('selected');
        if($('#tabQualityRating').get()[0]) {
    		$('#tabQualityRating').get()[0].style.cursor = 'default';
    	}
		$('#containerAwareness').show();
		$('#tabAwareness').addClass('selected');
	}
	
	$('#brandUseTabs div').click(function(){
	    if (!has_quality_rating && this.id=='tabQualityRating') { return; }
		$('#brandUseContainers > div').hide();
		$('#' + this.id.replace('tab', 'container')).show();
		
		$('#brandUseTabs > div').removeClass('selected');
		$('#' + this.id).addClass('selected');
	});
	
	$('#awarenessStatistics > div').hide();
	$('#awarenessStatistics > div:first-child').show();;
	
	$('#awareOptions select').change(function(){
		$('#awarenessStatistics > div').hide();
		$('#' + 'container' + this.value).show();
		$('#' + this.id).addClass('selected');
	});



	//List Price Help Toggle
	$('#listPriceButton').mouseover(function(){
		showElById('listPriceHelp');
	});

	$('#listPriceButton').mouseout(function(){
		hideElById('listPriceHelp');
	});

	//Compare Plans Checkbox Selections
	$('#comparelist input[@type = "checkbox"][@name = "productId"]').click(function(){
		var selectedProducts = getCookie(cookieName);
		if (this.checked)
		{
			var newValue = this.value;
			if (selectedProducts && selectedProducts != '')
			{
				newValue = selectedProducts + ',' + newValue;
			}
		}
		else
		{
			var newValue = '';
			if (selectedProducts && selectedProducts != '')
			{
				newValue = selectedProducts;
				newValue = newValue.replace(',' + this.value, '');
				newValue = newValue.replace(this.value + ',', '');
				newValue = newValue.replace(this.value, '');
			}
		}
		var newSelections = newValue.split(',');
		if (newSelections.length > 4)
		{
			this.checked = false;
			alert('You can only compare up to 4 products.');
			return;
		}
		setCookie(cookieName, newValue, null, '/', null);
		for (var i=0; i<newSelections.length; i++)
		{
			if ($('#comparelist input[@type = "checkbox"][@name = "productId"][@value = ' + newSelections[i] + ']').size() == 0)
			{
				$('#comparelist').append('<input style="display:none" type="checkbox" name="productId" value="' + newSelections[i] + '" checked="true" />');
			}
		}
	});

	$('#compareTop').click(function(){
		setCookie(cookieName, '', null, '/', null);
	});

	$('#compareBottom').click(function(){
		setCookie(cookieName, '', null, '/', null);
	});

	$('#productspecifier div.subnav a').click(function(){
		setCookie(cookieName, '', null, '/', null);
	});


	//Product Specifier
	updateSpecifier();

	$('#productspecifier input[@type = "checkbox"]').click(function(){
		updateSpecifier();
	});
});

