(function($) {
	var sImageDirectory = "/images/products/";
	var sThumbDirectory = "/images/products/thumbs/";
	var aProducts;
	var aType = new Array();
	var aStyle = new Array();
	var aFinish = new Array();
	var oMessage;
	var oSlider;
	
	if(!Array.indexOf){ Array.prototype.indexOf = function(obj) { for(var i=0; i<this.length; i++){ if(this[i]==obj){ return i; }; }; return -1; }; };
	
	function _jsonLoadedHandler(res) {
		oMessage.hide();
		
		aProducts = res.products;
		$(aProducts).each(function(i,oProduct) {
			var aHtml = new Array();
			aHtml.push('<a href="'+sImageDirectory+oProduct.image+'" class="product lightbox">');
			aHtml.push('<img src="'+sThumbDirectory+oProduct.thumb+'" alt="'+oProduct.name+'" /><br />');
			aHtml.push('<span class="product-name">'+oProduct.name+'</span>');
			aHtml.push('</a>');
			$(aHtml.join('')).appendTo('.main');
		});
		
		var nIndex = 0;
		$('a.product').each(function(i,prod) {
			$(prod).css({ left:(i%3*150)+20, top:(Math.floor(i/3)*165)+50, position:'absolute' });
			nIndex++;
		});	
		$('.main').animate({ height:(Math.floor(nIndex/3)+1)*165+50 });
		
		$('.lightbox').lightbox();
	};
	
	function _addAttribute() {
		var sName = this.selectbox.attr('name');
		var sValue = this.getHiddenValue();
		var sText = this.getTextValue();
		
		this.setComboValue('');
		this.applyEmptyText();
		
		if(!sName || !sValue || !sText) return;
	
		switch(sName) {
			case 'type': 	if(aType.indexOf(sValue)>=0) return; aType.push(sValue); _addAttributeToList('typeFilter', sText, sValue); break;
			case 'style': 	if(aStyle.indexOf(sValue)>=0) return; aStyle.push(sValue); _addAttributeToList('styleFilter', sText, sValue);break;
			case 'finish': 	if(aFinish.indexOf(sValue)>=0) return; aFinish.push(sValue); _addAttributeToList('finishFilter', sText, sValue);break;
		};	
		_updateList();
	};
	
	function _addAttributeToList(sList, sName, sValue) {
		$('#'+sList).find('.none-selected').hide();
		$('<li>'+sName+'<a class="'+sList+'" rel="'+sValue+'" href="#"> <span class="remove">(Remove)</span></a></li>').appendTo('#'+sList).click(_removeAttribute);
	};
	
	function _removeAttribute(event) {
		var sList = $(this).find('a').attr('class');
		var sID = $(this).find('a').attr('rel');
		$(this).remove();
		
		switch(sList) {
			case 'typeFilter': _removeAttributeFromArray(aType,sID,'typeFilter'); break;
			case 'styleFilter': _removeAttributeFromArray(aStyle,sID,'styleFilter'); break;
			case 'finishFilter': _removeAttributeFromArray(aFinish,sID,'finishFilter'); break;
		};
		
		return false;
	};
	
	function _removeAttributeFromArray(aArray, sID, sList) {
		var nIndex = aArray.indexOf(sID);
		if(nIndex > -1) {
			aArray.splice(nIndex,1);
			if(aArray.length < 1) $('#'+sList).find('.none-selected').show();
			_updateList();
		};
	};
	
	function _updateList() {
		oMessage.hide();
		
		var iActive = 0;
		$(aProducts).each(function(i,oProduct) {
			//$('.main .product').eq(i).fadeOut(400);
			if((aType.length > 0 && aType.indexOf(oProduct.type)<0) || (aStyle.length > 0 && aStyle.indexOf(oProduct.style)<0) || (aFinish.length > 0 && aFinish.indexOf(oProduct.finish)<0)) { 
				$('.main .product').eq(i).removeClass('pActive').fadeOut();
			} else {
				$('.main .product').eq(i).addClass('pActive').fadeIn().animate({ left:(iActive%3*150)+20, top:(Math.floor(iActive/3)*165)+50 });
				iActive++;
			};
		});
		$('.main').animate({ height:(Math.floor(iActive/3)+1)*165+50 });
		//$('.main .product').find('.pActive').fadeIn(500).end().not('.pActive');
		if(!$('.main .product.pActive')[0]) oMessage.text('There are no products that match your query.').show();
	};
		  
	$(document).ready(function() {			   
		$('.main').css({ overflow:'hidden' });
		oMessage = $('<p>Loading Products...</p>').appendTo('.main').css({ position:'absolute', top:40, left:20, fontSize:'122%', color:'#58595B', textAlign:'center', width:$('.main').width() });
		$('.main').height(70);
		
		$('#type').sexyCombo({ emptyText: "Add Wood Types...", triggerSelected: true, changeCallback:_addAttribute });
		$('#finish').sexyCombo({ emptyText: "Add Finishes...", triggerSelected: true, changeCallback:_addAttribute });
		$('#style').sexyCombo({ emptyText: "Add Styles...", triggerSelected: true, changeCallback:_addAttribute });

		$.getJSON('/ajax-data.php', { action:'products' }, _jsonLoadedHandler); 
	});
})(jQuery);