function initProdFilter(){
	if(typeof FILTERDATA != 'undefined'){
		getHash();
		var filterXml = FILTERDATA;
		if ($('advancedFilter')) {
			//Load in the XML for the filters....
			$('advancedFilterBtn').addEvent('click', function(e){
				new Event(e).stop();
				if ($('advancedFilter')) {
					var currentHeight = $('advancedFilter').getStyle('height').toInt();
					var maxHeight = 400;
					var minHeight = 24;
					if (currentHeight < maxHeight) {
						$('advancedFilter').effect('height', {
							duration: 500
						}).start(currentHeight, maxHeight);
					}
					else {
						$('advancedFilter').effect('height', {
							duration: 500
						}).start(currentHeight, minHeight);
					}
				}
				urchinTracker('product filter/advanced search');
			});
		}
		$ES('.resetFilter').each(function(el){
			el.addEvent('click',resetFilter);
		});
		var filterData = new Ajax(filterXml,{
			method: 'get',
			onComplete: createFilterElements
		}).request();
	}else if(!$('prodFilter')){
		$('prodFilter').setStyle('display','none');
	}
}

function createFilterElements(txt,xml){
	viewAll();
	var root = xml.getElementsByTagName('filters')[0];
	var children = xml.getElementsByTagName('filter');
	
	//$each(children,function(child){
	for(var i=0;i< children.length; i++){
		var child = children[i];
		
		var filterNode = child.getElementsByTagName('filterType')[0];

		var filterType = child.getAttribute('filterType');
		
		switch(filterType){
			case 'Icon':
				createIconFilter(child);
			break;
			
			case 'Slider':
				createSliderFilter(child);
			break;
			case 'List':
				createListFilter(child);
			break;

			default:
				return 0;
			break;
		}
	}
	if(END_USE !="" && $(END_USE)){
		$(END_USE).setStyle('opacity',1);
		$(END_USE).addClass('SELECTED');
		$(END_USE).removeEvents('mouseout');
	}
}
function createListFilter(dataNode){
	var type = 'List';
	var name = dataNode.getElementsByTagName('title')[0].firstChild.nodeValue;
	var location = dataNode.getElementsByTagName('location')[0].firstChild.nodeValue;
	if(location == 'Advanced') {
		var target =  $('advancedFilter');
	}
	else{
		var target =  $('standardFilter');
	}
	
	
	if (target){
		var title = name;
		var temp = name;
		
		var helpIconText = dataNode.getAttribute('toolTip');
		
		var helpIcon = new Element('a');
		helpIcon.addClass('whatIsThis');
		helpIcon.addClass('clearText');
		helpIcon.addClass('toolTip');
		
		helpIcon.setText(helpIconText);
		helpIcon.setAttribute('href',"#");
		helpIcon.addEvent('click', function(e){new Event(e).stop();});
		helpIcon.setAttribute('title',helpIconText);
		
		var helpTip = new Tips(helpIcon,{
	 		fixed:false,
	 		showDelay:500,
	 		onShow: function(toolTip) {
				toolTip.setStyle('opacity',1);
				//quickFade(this.toolTip,0,1)
			},
			onHide: function(toolTip) {
				toolTip.setStyle('opacity',0);
				//quickFade(this.toolTip,1,0)
			}
		});
		
		var h2 = new Element('h2');
		h2.setText(title);
		helpIcon.injectInside(h2);
		var listContainer = new Element('div',{
			'class':'listContainer',
			'id': dataNode.getAttribute('name').toString()
		});
		
		listContainer.injectInside(target);
		h2.injectBefore(listContainer);
		
		var left = new Element('div',{'class':'left'});
		var right = new Element('div',{'class':'right'});
		
		var valueContainer = dataNode.getElementsByTagName('values')[0];
		var values = valueContainer.getElementsByTagName('label');
		
		for(var i = 0; i < values.length;i++){
			var value = values[i];
			
			if(value.getElementsByTagName('name').length > 0){
				//Get value of label
				var labelName = value.getElementsByTagName('name')[0].firstChild.nodeValue;
			}
			if(value.getElementsByTagName('value').length > 0){	
				//Get value of label
				var labelValue;
				if(labelName == 'All'){
					labelValue = "";
				}
				else{
					labelValue = value.getElementsByTagName('value')[0].firstChild.nodeValue;
				}
			}
			
			var a = new Element('a',{
				'class':'listLabel',
				'id': labelValue,
				'events':{
					'click':function(e){
						new Event(e).stop();
						
						var collection = $ES('a',this.getParent().getParent());
						collection.each(function(el){
							el.removeClass('SELECTED');
							el.setStyle('opacity',1);
						});
						this.toggleClass('SELECTED');
						this.setStyle('opacity',.75);
						serializeFilter(0);
						
						urchinTracker('product filter/advanced filter/'+ this.getParent().getParent().id+'/'+this.id)
					}
				}
			
			});
			
			a.setText(labelName);
			
			if(i%2 == 0){
				a.injectInside(left);
			}else{
				a.injectInside(right);
			}
			
			left.injectInside(listContainer);
			right.injectInside(listContainer);
			
		}		
		var clear = new Element('div',{'class':'clear'});
		clear.injectInside(listContainer);
	}
}
function createIconFilter(dataNode){
	var type = 'Icon';
	
	var name = dataNode.getElementsByTagName('title')[0].firstChild.nodeValue;
	
	var location = dataNode.getElementsByTagName('location')[0].firstChild.nodeValue;
	if(location == 'Advanced') {
		var target =  $('advancedFilter');
	}
	else{
		var target =  $('standardFilter');
	}
	
	if (target)
	{
		var title = name;
		var temp = name;
		
		var helpIconText = dataNode.getAttribute('toolTip');
		
		var helpIcon = new Element('a');
		helpIcon.addClass('whatIsThis');
		helpIcon.addClass('clearText');
		helpIcon.addClass('toolTip');
		
		helpIcon.setText(helpIconText);
		helpIcon.setAttribute('href',"#");
		helpIcon.addEvent('click', function(e){new Event(e).stop();});
		helpIcon.setAttribute('title',helpIconText);
		
		var helpTip = new Tips(helpIcon,{
	 		fixed:false,
	 		showDelay: 500,
	 		onShow: function(toolTip) {
				toolTip.setStyle('opacity',1);
				//quickFade(this.toolTip,0,1)
			},
			onHide: function(toolTip) {
				toolTip.setStyle('opacity',0);
				//quickFade(this.toolTip,1,0)
			}
		});
		
		for(var l =0; l < temp.split(' ').length;l++){
			name = name.replace(' ','');
		}
		
		var clearDiv = new Element('div');
		clearDiv.addClass('clear');
		
		var location = dataNode.getElementsByTagName('location')[0].firstChild.nodeValue;
		var valueContainer = dataNode.getElementsByTagName('values')[0];
		var values = valueContainer.getElementsByTagName('label');
		//alert(values.length);
		
		var h2 = new Element('h2');
		h2.setText(title);
		//CREATE ICON CONTAIINER 
		var iconContainer = new Element('div');
		
		iconContainer.id = dataNode.getAttribute('name').toString();
		iconContainer.className = "iconContainer";
		iconContainer.injectInside(target);
		
		h2.injectBefore(iconContainer);
		
		clearDiv.injectAfter(h2);
		helpIcon.injectInside(h2);
		
		
		//##create icon container
		for(var i = 0; i < values.length; i++){
			var label = values[i];
	
			if(label.nodeName == "label"){
				if(label.getElementsByTagName('name').length > 0){
					//Get value of label
					var labelName = label.getElementsByTagName('name')[0].firstChild.nodeValue;
				}
				if(label.getElementsByTagName('value').length > 0){	
					//Get value of label
					var labelValue = label.getElementsByTagName('value')[0].firstChild.nodeValue;
				}
				
				var iconImg = label.getElementsByTagName('icon')[0].firstChild.nodeValue;
				//create label and inject into slider container
				var icon = new Element('a');
				
				var img = new Element('img');
				img.setAttribute('src',iconImg);
				img.setAttribute('alt','');
				img.injectInside(icon);
				
				var span = new Element('span');
				span.setText(labelName);
				span.injectInside(icon);
				
				//icon.setAttribute('href',labelName);
				icon.id = labelValue;
				icon.setAttribute('title',label.getAttribute('tooltip'));
				icon.addClass('iconLabel');
				icon.addClass('toolTip');
				
			 	var tip = new Tips(icon,{
			 		fixed:false,
			 		showDelay:500,
			 		onShow: function(toolTip) {
						toolTip.setStyle('opacity',1);
						//quickFade(this.toolTip,0,1)
					},
					onHide: function(toolTip) {
						toolTip.setStyle('opacity',0);
						//quickFade(this.toolTip,1,0)
					}
				});
				icon.injectInside(iconContainer);
				
			}
		}
		
		var icons = $ES('a',iconContainer);
		icons.each(iconHoverEvents);
		
		var div = new Element('div');
		div.addClass('clear');
		div.injectInside(iconContainer);
	}
}
/**
 * iconHoverEvents
 */
function iconHoverEvents(el,index,group) {
	if(END_USE !=""){
		if(!el.hasClass('SELECTED')){
			el.setStyle('opacity',.5);
		}
	}
	
 	el.addEvent('mouseover',toggleOpacity.bindWithEvent([el,index,group]));
 	el.addEvent('mouseout',toggleOpacity.bindWithEvent([el,index,group]));
 	el.addEvent('click',selectIcon.bindWithEvent([el,index,group]));
}

/**
 * toggleOpacity
 * @param {Event} e 
 */
 function toggleOpacity(e) {
 	new Event(e).stop();
 	var icon = this[0];
 	var index = this[1];
 	var group = this[2];
	if(icon.getStyle('opacity') != 1){
		icon.setStyle('opacity',1);
	}
	else if(!icon.hasClass('SELECTED')){
		icon.setStyle('opacity',.5);
	}
 }

/**
 * selectIcon
 * @param {event} e 
 */
function selectIcon(e) {
 	new Event(e).stop();
 	var icon = this[0];
 	var index = this[1];
 	var group = this[2];
 	
 	if (icon.hasClass('SELECTED')) {
		icon.removeClass('SELECTED');
 	} else {
		icon.addClass('SELECTED');
 	}
 	group.each(function(el) {
 		if (el != icon) {
	 		el.removeClass('SELECTED');
	 		el.setStyle('opacity',.5);
 		}
 	});
 	serializeFilter(1);
 }

function iconOver(){
	cIcon = this;
	fadeIn = new Fx.Style(cIcon,'opacity',{duration: 500});
	fadeOut = new Fx.Style(cIcon,'opacity',{duration: 500});
	
	//fadeOut.stop();
	var current = this.getStyle('opacity');
	//fadeIn.start(current,1);
	cIcon.setStyle('opacity',1);
}

function iconOut(){
	cIcon = this;
	fadeIn = new Fx.Style(cIcon,'opacity',{duration: 500});
	fadeOut = new Fx.Style(cIcon,'opacity',{duration: 500});
	
	//fadeIn.stop();
	var current = this.getStyle('opacity');
	//fadeOut.start(current,.75);
	cIcon.setStyle('opacity',.50);
}

function createSliderFilter(dataNode){
	var type= 'Slider';
	var name = dataNode.getElementsByTagName('title')[0].firstChild.nodeValue;
	
	var location = dataNode.getElementsByTagName('location')[0].firstChild.nodeValue;
	if (location == 'Advanced') {
		var target =  $('advancedFilter');
	} else {
		var target =  $('standardFilter');
	}
	var helpIconText = dataNode.getAttribute('toolTip');
	var helpIcon = new Element('a');
	helpIcon.addClass('whatIsThis');
	helpIcon.addClass('clearText');
	helpIcon.addClass('toolTip');
	
	helpIcon.setText(helpIconText);
	helpIcon.setAttribute('href',"#");
	helpIcon.addEvent('click', function(e){new Event(e).stop();});
	helpIcon.setAttribute('title',helpIconText);
	
	var helpTip = new Tips(helpIcon,{
 		fixed:false,
 		showDelay: 500,
 		onShow: function(toolTip) {
			toolTip.setStyle('opacity',1);
			//quickFade(this.toolTip,0,1)
		},
		onHide: function(toolTip) {
			toolTip.setStyle('opacity',0);
			//quickFade(this.toolTip,1,0)
		}
	});
	
	var clearDiv = new Element('div');
	clearDiv.addClass('clear');
	
	var title = name;
	name = name.replace(' ','');
	var location = dataNode.getElementsByTagName('location')[0].firstChild.nodeValue;
	var valueContainer = dataNode.getElementsByTagName('values')[0];
	var values = valueContainer.getElementsByTagName('label');
	
	var h2 = new Element('h2');
	h2.setText(title);
	
	if ($('prodFilter')) {
		//CREATE SLIDER CONTAIINER 
		var sliderContainer = new Element('div');
		//sliderContainer.setStyle('opacity',.75);
		/*sliderContainer.addEvent('mouseenter',function (){
			this.setStyle('opacity',1);
		});
		sliderContainer.addEvent('mouseleave',function (){
			this.setStyle('opacity',.75);
		});
		* */
		sliderContainer.id = dataNode.getAttribute('name').toString();
		sliderContainer.className = "sliderContainer";
		sliderContainer.injectInside(target);
		
		h2.injectBefore(sliderContainer);
		clearDiv.injectAfter(h2);
		helpIcon.injectInside(h2);
		
		//CREATE SLIDER MARKER
		var marker = new Element('div');
		marker.className = 'marker';
		marker.injectInside(sliderContainer);
		//CREATE AND ADD LABELS TO THE SLIDER
		
		//$each(values,function(value){
		for (var i=0;i< values.length;i++) {
			var value = values[i];
			if (value.nodeName == "label") {
				if(value.getElementsByTagName('name').length > 0){
					//Get value of label
					var labelName = value.getElementsByTagName('name')[0].firstChild.nodeValue;
				}
				if(value.getElementsByTagName('value').length > 0){	
					//Get value of label
					var labelValue;
					if (labelName == 'All') {
						labelValue = "";
					} else {
						labelValue = value.getElementsByTagName('value')[0].firstChild.nodeValue;
					}
				}
				if (value.getElementsByTagName('tooltip').length > 0) {
					//Get value of label
					var labelToolTip = value.getElementsByTagName('tooltip')[0].firstChild.nodeValue;
				} else {
					var labelToolTip = '';
				}
				
				//create label and inject into slider container
				var label = new Element('a');
				
				var span = new Element('span');
				span.setText(labelName);
				span.injectInside(label);
				
				label.setAttribute('href','#');
				label.id = labelValue;

				label.setAttribute('title',labelToolTip);
				label.setAttribute('alt',labelToolTip);
				
				label.addClass('notchLabel');
				label.setStyle('cursor','pointer');
				label.injectBefore(marker);
			}
		}
		//});	
		
		//instantiate the custom scroll bar
		var scrollTest = new CustomScrollBar({container: sliderContainer, label: '.notchLabel', onSelect: function(selectedLabel){
			selectedLabel.addClass('SELECTED');
			var siblings = selectedLabel.getParent().getChildren();
			siblings = siblings.remove(selectedLabel);
			siblings = siblings.remove(siblings[siblings.length-1]);
			siblings.each(function(el,index,group){
				if (el.hasClass('SELECTED')) {
					el.removeClass('SELECTED');					
				}
			});
			urchinTracker('product filter/'+sliderContainer.id+'/'+selectedLabel.id);
			serializeFilter(1);
		}});
		
	}
}

function createProductWall(txt) {
	var wall = $('wall');
	var pwHeader = $('pwContentHeader');
	wall.empty();
	var pwData = Json.evaluate(txt,true);
	if(!pwData) {
		pwData = {totalpages:0};
	}
	var currentpage = pwData.currentpage;
	var totalpages = pwData.totalpages;
	var onlycustomizable = pwData.onlycustomizable;
	
	var clearDiv = new Element('div');
	clearDiv.addClass('clear');
	
	if (totalpages == 0) {
		noResults($('pwContentHeader'));
		return 0;
	}
	
	var prodWall = new Array();
	var categoryName = pwData.category.categoryname.toUpperCase();
	var categoryCode = pwData.category.categoryCode;
	var categoryImage = pwData.category.headerimagename;
	var subText = pwData.category.subheadtext;

	if (!onlycustomizable) {
		var categoryHeader = new Element('div');
		categoryHeader.id = 'categoryHeader';
		
		var header = new Element('h3');
		header.className = categoryImage;//+' clear_text';
		header.addClass('clearText');
		header.setText(categoryName);
		header.injectInside(categoryHeader);
		
		var headerDesc = new Element('p');
		headerDesc.setHTML(subText);
		headerDesc.injectInside(categoryHeader)
		
		categoryHeader.injectInside(wall)
		createPagination(currentpage,totalpages,wall);
		clearDiv.injectInside(wall);
	} else {
		var categoryHeader = new Element('div');
		categoryHeader.id = 'categoryHeader';
		
		var header = new Element('h3');
		//header.className = categoryImage;//+' clear_text';
		header.addClass('clearText');
		header.setText('Customizable Knives');
		header.injectInside(categoryHeader);
		
		var headerDesc = new Element('p');
		headerDesc.setHTML("No knives were found with the settings you selected.  If you can't find a knife to your liking you can customize one of these knives.");
		headerDesc.injectInside(categoryHeader)
		
		categoryHeader.injectInside(wall)
		createPagination(currentpage,totalpages,wall);
		clearDiv.injectInside(wall);
	}
	
	for (var pn=0; pn<pwData.pw.data.IMAGENAME.length; pn++) {
		var pObj 			= new Object();
		pObj.IMAGENAME 		= pwData.pw.data.IMAGENAME[pn];
		pObj.PRODUCTID 		= pwData.pw.data.PRODUCTID[pn];
		pObj.HASVARIATIONS	= pwData.pw.data.HASVARIATIONS[pn];
		pObj.CANBECUSTOMIZED= pwData.pw.data.CANBECUSTOMIZED[pn];
		pObj.PRODUCTNAME 	= pwData.pw.data.PRODUCTNAME[pn];
		pObj.RETAILPRICE 	= pwData.pw.data.RETAILPRICE[pn];
		pObj.QUANTITY 		= pwData.pw.data.QUANTITY[pn];
		pObj.UPC 			= pwData.pw.data.UPC[pn];
		pObj.DISCOUNT 		= pwData.discount;
		pObj.AVAILABLE 		= pwData.pw.data.AVAILABLE[pn];
		pObj.ISNEW	 		= pwData.pw.data.ISNEW[pn];
		prodWall[pn] 		= pObj;
	}
	pwData = prodWall;
	// pwData = pwData.pw.data;
	
	pwData.each(function(product,index,group) {
		if(index%4 == 0 && index != 0){
			var pwSeperator = new Element('div',{
				'class':'pwSeparator clearText'
			});
			pwSeperator.injectInside(wall);
		}
		//CREATE item container
		var item = new Element('div',{
			'id': "pID_"+product.PRODUCTID,
			'class':'item'
		});
		
		var itemInfo = new Element('div',{
			'class':'itemInfo'
		});
		itemInfo.injectInside(item);
		item.injectInside(wall);
		
		var itemImage = new Asset.image(SCENE7URL+"/is/image/BuckKnives/pwProduct?$pwSharp$&$product=is{BuckKnives/"+product.IMAGENAME+"_1?scl=1}");
		itemImage.addClass('itemImage');
		
		var iconSet = new Element('div',{
			'class':'iconSet'
		});
		var itemDetails= new Element('div',{
			'class':'itemDetails'
		});
		itemDetails.injectInside(itemInfo);
		
		var quickButton = new Element('a');
		quickButton.className = 'quickLookBtn button clearText';
		quickButton.setText('Quick Look');
		
		//iconSet.injectAfter(itemDetails);
		itemImage.injectInside(itemDetails);
		quickButton.injectAfter(itemImage);
		
		var modelNumber = new Element('span',{'class':'modelNumber'});
		modelNumber.setText(product.UPC);
		modelNumber.injectInside(itemDetails);
		
		var itemName = new Element('h3',{
			'class':'itemName'
		});
		itemName.setHTML(product.PRODUCTNAME);
		itemName.injectInside(itemDetails);

		var price = new Element('span',{
			'class':'price'
		});
		price.setText(formatCurrency(product.RETAILPRICE));
		price.injectInside(itemDetails);

		if (product.ISNEW) {
			var isnew = new Element('span',{
				'class':'newproduct'
			});
			isnew.setText('New');
			isnew.injectInside(itemDetails);
		}

		
		if (product.DISCOUNT) {
			price.setStyle('text-decoration','line-through');
			var discountPrice = new Element('span',{
				'class':'discountprice'
			});
			discountPrice.setText(formatCurrency((product.RETAILPRICE * (product.DISCOUNT/100))));
			discountPrice.injectInside(itemDetails);
		}
		
		var itemActions = new Element('div',{
			'class':'itemActions'
		});
		itemActions.injectInside(itemInfo);
		
		var viewDetailsLink = new Element('a',{'href':'/index.cfm?event=product.detail&productid='+product.PRODUCTID});
		viewDetailsLink.setText('View Details');
		viewDetailsLink.injectInside(itemActions);
	 	

	 	/*  --------------------------------- ADD TO CART ----------------------------------- */
		//console.log(product);
			

		var addToCartLink = new Element('a',{'href':'/index.cfm?event=cart.additem&upc='+product.UPC,'id':'pID_'+product.UPC, 'class':'addToCart'});
		addToCartLink.setText('Add To Cart');
		
		var outOfStock = new Element('a',{'id':'pID_'+product.UPC, 'class':'outOfOrder', styles: {'color': '#ff0000', 'text-decoration': 'none'}});
		outOfStock.setHTML('<br />Currently out of stock<br />Will ship as soon as available.');
	
		var notAvailable = new Element('a',{'id':'pID_'+product.UPC, 'class':'outOfOrder', styles: {'color': '#ff0000', 'text-decoration': 'none'}});
		notAvailable.setHTML('<br />Currently not available.');

 		var buildOne = new Element('a',{'id':'pID_'+product.UPC, 'class':'outOfOrder'});
 		buildOne.setText('Build One');
		
		if (product.RETAILPRICE) {
			if (!product.QUANTITY && product.CANBECUSTOMIZED) {
				buildOne.injectInside(itemActions);
				
			} else if (product.QUANTITY && !product.ISNEW && product.AVAILABLE) {
				// 1 (TRUE FALSE TRUE)
				addToCartLink.injectInside(itemActions);
				
			} else if (!product.QUANTITY && !product.ISNEW && product.AVAILABLE) {
				// 2 (FALSE FALSE TRUE)
				addToCartLink.injectInside(itemActions);
				outOfStock.injectInside(itemActions);
				
			} else if (product.QUANTITY && !product.ISNEW && !product.AVAILABLE) {
				// 3 (TRUE FALSE FALSE)
				// notAvailable.injectInside(itemActions);
				
			} else if (product.QUANTITY && product.ISNEW && !product.AVAILABLE) {
				// 4A (TRUE TRUE FALSE)
				
			} else if (!product.QUANTITY && product.ISNEW && !product.AVAILABLE) {
				// 4B (FALSE TRUE FALSE)
				
			} else if (product.QUANTITY && product.ISNEW && product.AVAILABLE) {
				// 5A (TRUE TRUE TRUE)
				addToCartLink.injectInside(itemActions);
				
			} else if (!product.QUANTITY && product.ISNEW && product.AVAILABLE) {
				// 5B (FALSE TRUE TRUE)
				addToCartLink.injectInside(itemActions);
				
			}
		}
		
		
		
/*		
		 	if (product.HASVARIATIONS != 1) {
			 	if (product.AVAILABLE) {
			 		if (!product.ISNEW) {
			 			var addToCartLink = new Element('a',{'href':'/index.cfm?event=cart.additem&upc='+product.UPC,'id':'pID_'+product.UPC, 'class':'addToCart'});
			 			addToCartLink.setText('Add To Cart');
			 		
						if (product.QUANTITY == 0) {
							var addToCartLink = new Element('a',{'id':'pID_'+product.UPC, 'class':'outOfOrder', styles: {'color': '#ff0000', 'text-decoration': 'none'}});
							addToCartLink.setHTML('<br />Out of Stock<br />Will Ship When Available');
						}
			 		}
			 	
			 	} else if (product.CANBECUSTOMIZED == 1){
			 		var addToCartLink = new Element('a',{'id':'pID_'+product.UPC, 'class':'outOfOrder'});
					addToCartLink.setText('Build One');

			 	} else if (!product.AVAILABLE) {
			 		if (!product.ISNEW) {
			 			var addToCartLink = new Element('a',{'id':'pID_'+product.UPC, 'class':'outOfOrder', styles: {'color': '#ff0000', 'text-decoration': 'none'}});
			 			addToCartLink.setHTML('<br />Not Currently Available');
			 		}

			 	} else {
//			 		var addToCartLink = new Element('a',{'id':'pID_'+product.UPC, 'class':'outOfOrder'});
//			 		addToCartLink.setAttribute('href','/index.cfm?event=company.dealerlocator');//+'&upc='+product.productid);
//					addToCartLink.setText('Find a Dealer');
			 	}
				//addToCartLink.injectInside(itemActions);
		 	} else {
		 		if (!product.ISNEW) {
			 		var hasVariations = new Element('a',{'href':'/index.cfm?event=product.detail&productid='+product.PRODUCTID});
					hasVariations.setText('Add To Cart');
					hasVariations.injectInside(itemActions);
		 		}
		 	}
*/
		});
		
		
		
		//addPagination to FOOTER of productwall
		$('pwFooter').empty();
		createPagination(currentpage,totalpages,$('pwFooter'));
		
		wall.setStyle('height','auto');
		
		pdp();
		initAddToCart();
		pwHeader.empty();
}

function viewAll(){
	$ES('.viewAllKnives').each(function(btn,index,group){
		btn.addEvent('click',function(e){
			new Event(e).stop();
			
			var entireFilter = $('prodFilter');
			var selected = $ES('.SELECTED', entireFilter);
			
			selected.each(function(item,index,group){
				item.setStyle('opacity',.75);
				item.removeClass('SELECTED');
			});
			resetSliders();
			serializeFilter(1);

			urchinTracker('product filter/view all knives');
		});
	});
}
function resetFilter(e){
	new Event(e).stop();
		
	var entireFilter = $('prodFilter');
	var selected = $ES('.SELECTED', entireFilter);
	
	selected.each(function(item,index,group){
		item.setStyle('opacity',.75);
		item.removeClass('SELECTED');
	});
	resetSliders();
	if(END_USE !="" && $(END_USE)){
		$(END_USE).setStyle('opacity',1);
		$(END_USE).addClass('SELECTED');
		$(END_USE).removeEvents('mouseout');
	}
	urchinTracker('product filter/filter reset');
	serializeFilter(1);
}
function resetSliders(){
	$ES('.marker').each(function(el){
		el.setStyle('left',0);
	});
}

function coverProductWall(){
	var wall = $('wall');
	var cHeight = wall.getCoordinates();
	var pwHeader = $('pwContentHeader');
	pwHeader.empty();
	if($('wallFeature')){
		$('wallFeature').remove();
	}
	if(cHeight.height > 0){
		wall.setStyle('height',cHeight.height);
	}
	//wall.empty();
	var loadingImage = new Asset.image('/resources/images/embed/loading.gif', {id: 'loadingImage', title: 'Products Loading'});
	loadingImage.injectInside(pwHeader);
}
function serializeFilter(pg,prevFilter){
	var entireFilter = $('prodFilter');
	var selected = $ES('.SELECTED', entireFilter);
	var qString = [];
	selected.each(function(el){
		var parentEl = el.getParent();
		if(parentEl.hasClass('left') || parentEl.hasClass('right')){
			parentEl = parentEl.getParent();
		}
		qString.push(parentEl.id.toString());
	});
	var finalSelection = selected.associate(qString);
	var lastEl = qString.getLast();
	var thisQuery = "{";
	for(name in finalSelection){
		if(finalSelection[name] != finalSelection[lastEl]){
			thisQuery += "'"+name+"':'"+finalSelection[name].id+"',";
		}
		else{
			thisQuery += name+":'"+finalSelection[name].id+"'";
		}
	}
	thisQuery += "}";
	
	if(!prevFilter){
		setHash(thisQuery);	
		var queryString = Json.evaluate(thisQuery);
	}
	else{
		var queryString = prevFilter;
	}
	var url = '/index.cfm?event=product.filter&pg='+pg;
	
	//THIS SHOULD BE A GET AJAX REQUEST
	makeRequest(url,queryString);
}
function getHash(){
	if(location.hash){
		var prevFilter = location.hash.split('#')[1];
		prevFilter = Json.evaluate(prevFilter);
		serializeFilter(1,prevFilter);
	}
}
function setHash(queryString){
	location.hash = queryString;
	updateBreadCrumb(queryString);
}

function updateBreadCrumb(queryString){
	if(!$('pwBreadCrumb')){
		var bc = new Element('div');
		bc.id = 'pwBreadCrumb';
		bc.injectBefore($('pwContent'));
		var clear = new Element('div',{'class':'clear'});
		clear.injectAfter(bc);
	}
	else{
		var bc = $('pwBreadCrumb');
	}
	
	var bcObj = Json.evaluate(queryString);  
	var bcHTML = '<h3 class="currentFilterOptions clearText">Current Filter Settings:</h3>';
	var opt = null;
	var numopts = 0;
	
	var bottomSeperator = new Element('div',{
		'class':'pwSeparator clearText'
	});
	
	bc.empty();
	for(name in bcObj){
		opt = null;
		switch (name)
		{
			case 'retail_price':
				opt = 'PRICE';
			break;
			
			case 'blade_length':
				opt = 'BLADE LENGTH';
			break;

			case 'weight':
				opt = 'WEIGHT';
			break;

			case 'knife_type':
				opt = 'KNIFE TYPE';
			break;
			
			case 'blade_material':
				opt = 'BLADE MATERIAL';
			break;
			
			case 'handle_material':
				opt = 'HANDLE MATERIAL';
			break;
			
			case 'carry_system':
				opt = 'CARRY SYSTEM';
			break;
		}
	
		if (opt && bcObj[name] && bcObj[name].length){
			numopts = numopts + 1;
			bcHTML = bcHTML + '<span><strong>' + opt + ':</strong>&nbsp;' + bcObj[name] + '</span>';
		}
		
	}
	if (numopts > 0){
		bc.setHTML(bcHTML);
		bottomSeperator.injectInside(bc);
	}
}

function updateQueryString(){
	var queryString = location.search;
	queryString = queryString.split('&');
	var end_use = queryString[1];
	var value = end_use.split('=')[1];
	var newEndUse = end_use.split('=')[0]+'='+ 'O';
	//alert(newEndUse);
	location.search = queryString[0]+"&"+newEndUse;
}
function createPagination(currentpage,totalpages,target){
	/**
	 *  
	  	<div class="pagination">
			<a href="" style="display: block;">View All</a>
	* 		<div class="pageLinks">
				<a href="" class="current">1</a> <a href="">2</a> <a href="">3</a> <a href="">4</a> <a href="">5</a> <a href="">next ></a>
			</div>
		</div>
	 * 
	 **/
	 if(totalpages == 0 || totalpages == 1){
	 	return 0;
	 }
	//create pagination links
	var container = new Element('div',{'class':'pagination'});
	var clear = new Element('div',{'class':'clear'});
	var url = './productWall.cfm?getJson=true&pg=';
	var viewAlllink = new Element('a',{
		'class':'viewAllKnives'
	});
	viewAlllink.addClass('viewAll');
	viewAlllink.setText('View All');
	viewAlllink.addEvent('click',function(e){
		new Event(e).stop();
		
		var entireFilter = $('prodFilter');
		var selected = $ES('.SELECTED', entireFilter);
		
		selected.each(function(item,index,group){
			item.setStyle('opacity',.75);
			item.removeClass('SELECTED');
		});
		urchinTracker('product filter/view all lnk');
		serializeFilter(1);
	});
	
	//viewAlllink.injectInside(container);
	
	var pageLinks = new Element('div',{
		'class':'pageLinks'
	});
	pageLinks.injectInside(container);
	if(currentpage != 1){
		var previous = new Element('a',{'href':'#','class':'previous'});
		previous.setText('< Previous');
		
		previous.addEvent('click', previousPageEvent.bindWithEvent(currentpage));
		
		previous.injectInside(pageLinks);
		
	}
	
	for(var i = 0;i<totalpages;++i){
		 var a = new Element('a',{'href':'#','class':'pageNumber'});
		 if(i+1 == currentpage) a.addClass('current');
		 a.setText(i + 1);
		 
		 a.addEvent('click',thisPageEvent.bindWithEvent(a));
		 a.injectInside(pageLinks);
	}
	
	if(currentpage != totalpages){
		var next = new Element('a',{'href':'#','class':'next'});
		next.setText('Next >');
		next.injectInside(pageLinks);
		
		next.addEvent('click', nextPageEvent.bindWithEvent(currentpage));
	}
	//addPagination Events -- next page, previous page, pageNumber
	container.injectInside(target);
	//clear.injectAfter(container);
	
}
function thisPageEvent(e){
	new Event(e).stop();
	serializeFilter(this.getText());
}
function nextPageEvent(e){
	new Event(e).stop();
	var currentpage = this;
	var pg = currentpage + 1;
	serializeFilter(pg);
}
function previousPageEvent(e){
			new Event(e).stop();
			var currentpage = this;
			var pg = currentpage - 1;
			serializeFilter(pg);
		}
function noResults(target){
	target.empty();
	$('pwFooter').empty();
	var noResultContainer = new Element('div',{
		'id':'noResults',
		'class':'clearText'
	});
	
	var h1 = new Element('h1');
	h1.setText('No Results');
	h1.injectInside(noResultContainer);
	
	var p = new Element('p');
	p.setText('Try different settings in the filter');
	p.injectInside(noResultContainer);
	
	noResultContainer.injectInside(target);
	$('wall').setStyle('height','100px');
}
function makeRequest(url,queryString){
	//console.log('QS: ',queryString);
	var filter = new Ajax(url,{
		method:'get',
		async: true,
		autoCancel: true,
		data: queryString,
		onRequest: coverProductWall,
		onFailure: function(){
			//alert('Failed');
		}, 
		onSuccess: function(){
			//alert('Success');
		}, 
		onStateChange: function(){
			//alert('State Change');
		}, 
		onComplete:createProductWall
	}).request();
}
function createInitialPagination(){
	var currentpage = $E('.pagination .current');
	
	if($E('.pagination .previous')){
		$E('.pagination .previous').addEvent('click',previousPageEvent.bindWithEvent(currentpage));
	}
	
	if($E('.pagination .next')){
		$E('.pagination .next').addEvent('click',nextPageEvent.bindWithEvent(currentpage));
	}
	
	if($ES('.pagination .pageNumber')){
		$ES('.pagination .pageNumber').each(function(el){
			el.addEvent('click',thisPageEvent.bindWithEvent(el));
		});
	}
	
}Onload.add(createInitialPagination);


