$(document).ready(function(){
	var TYPE_PRODUCT = 10;
	var TYPE_PACKAGE = 11;
	
	// Set controller
	$.voCnd.setController('/action/public/cmc/catalog-cnd');
	$.voCnd.setContentDom('#product-container #results','li'); // DOM block and item row
	$.voCnd.setFilterPanel(1); // Set results to filter panel
	$.voCnd.setLimit(20); // Set the pagination result limit
	/*if($.jqPHP.getVar('filter_conditions')){
		$.voCnd.filterArray = $.jqPHP.getVar('filter_conditions');
	}*/
	if($.voCnd.filterArray.search){
		$('#cnd-search').val($.voCnd.filterArray.search);
	}
	
	$.voCnd.ajaxSuccessFunction = function( data, status )
	{
		if(data.product_reroute){
			$.voCnd.filterArray.search = '';
			//$.voMessage.debug(data.product_reroute);
			window.location.href = data.product_reroute;
		}
	};
	
	// Search Input
	/*$('#cnd-search-button').unbind('click').click(function(e){
		$.voCnd.filterArray = {};
		$.voCnd.hierarchyArray = {};
		$.voCnd.filterArray.limit = 10;
		$.voCnd.filterArray.search = $(this).val();
		$.voCnd.init();
	});
	
	$('#cnd-search').keyup(function(e){
		if(e.keyCode == 13){
			$('#cnd-search-button').trigger('click');
		}
	});*/
	
	// Set cnd panel groups
	$.voCnd.customContentFunction = function( key, rowData, rowObject ){
		
		var productName = $.trim( (rowData.brand_name ? rowData.brand_name : '' ) + ' ' + rowData.name);
		var priceRetail = rowData.price_retail; //rowData.lowestPrice ? rowData.lowestPrice : 'Items Missing';
		var priceDiscount = rowData.priceDiscount;
		var productUrl = ( rowData.type == TYPE_PRODUCT ? './product/' : './package/' ) + rowData.id + '/' + $.voSlugFormat( rowData.brand_name+' '+rowData.name );
		var imageUrl = rowData.image_url;
		$(rowObject).data('id',rowData.id);
		$('.name',rowObject).text( productName );
		$('.link',rowObject).attr('href', productUrl);
			
		imageUrl ? $('.image img',rowObject).attr('src',imageUrl) : '';
		
			
		// Set pre-price text based on following conditions
		var multiItemText = '';
		if( rowData.lowestPrice < rowData.highestPrice ){
			multiItemText = '+';
		}
		
		// Handle discounts
		if(rowData.priceDiscount){
			$('.price .retail',rowObject).addClass('sale').text( '$' + (priceRetail - priceDiscount).toFixed(2) + multiItemText );
			$('.price .discount',this).text( '$' + priceDiscount.toFixed(2));
		}else{
			$('.price .retail',rowObject).text( '$' + priceRetail + multiItemText );
		}
		
			
	};
	// This the ajax function that is called after the success function, no data is in here, just status info
	//$.voCnd.ajaxCompleteFunction = function( xhr, status ){};
	
	// Exception for tags
	// set cnd content events
	$.voCnd.init();
});
