// This will remove an element in the array based on it's value...
function removeItem(array, item) {
	var i = 0;
	while (i < array.length) {
		if (array[i] == item) {
			array.splice(i, 1);
		} else {
			i++;
		}
	}
	return array;
}

// inArray() :-D!
Array.prototype.inArray = function(value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

// PPK Cookie Functions...
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}


$(function(){
	
	/*
		This unobtrusively adds a checkbox to whatever you choose. I choose to use 
		<h2> within <div class="product">. The below example would be the ideal code for 
		this feature. I put "product-" in the id because you shouldn't start id's with numbers.
		Example: <element id="$string.concat('product-', $product.productNumber)"><ss:link source="$product.name"/></element>	
	*/
	$("div.cataloglist span.compare").each(function() {
		
		var products = readCookie('comparison');
		var id = $(this).attr('id').replace('product-', '');
		var checked = '';
		
		if(products) {
			if(products.split(',').inArray(id)) {
				checked = ' checked="checked"';
			}
		}
		
		$(this).prepend('<div class="compare-checkbox"><input class="compare" value="' + id + '" type="checkbox"' + checked + '> Compare</div>');
		
	});
	
	// When an input element is changed, start the process...
	$('input[class=compare]').change(function() {
		
		// We'll need this a lot...
		var id = $(this).attr('value');
		var checked = $(this).attr('checked');
		var products = readCookie('comparison');
		var list = new Array();
		
		if(checked) {				
			if(products) {
				list = products.split(',');
				list = removeItem(list, id);
				list.push(id);
				createCookie('comparison', list.join(','));
			} else {
				createCookie('comparison', id);
			}
		} else {
			if(products) {
				list = products.split(',');
				list = removeItem(list, id);
				createCookie('comparison', list.join(','));
			}
		}
		
		// alert(readCookie('comparison'));
			
	});
		
	$('#compare-table span.remove').click(function() {
			
		// We'll need this a lot...
		$(this).parents('td.product').fadeOut('slow');
				
	});
		
	// When the click the id with
	$('#compare').click(function() {
		
		if(readCookie('comparison')) {
			window.location = '/Page.bok?template=Compare&products=' + readCookie('comparison');	
		} else {
			window.location = '/Page.bok?template=Compare';
		}
		
		// alert('/TestStore/Page.bok?template=Compare&products=' + readCookie('comparison'));
		
	});

});
	