function filter(filter) {
	closeEveryInfo();
	$('.info').hide();
	
	filter = filter.toLowerCase();
	
	if (filter == 'show all') {
		$('#products > div').each(function() {
			$(this).show(); //show all
			$('#filterList').html(''); //clear filter list
		});
		
		return false;
	}
	
	$('#products > div').each(function() {
		html = $(this).find('.productTitle').html();
		if (html) {
		html = html.toLowerCase();
			if (html.match(filter)) {
				$(this).show();
			} else {
				console.log(html+' does not match '+filter);
				$(this).hide();
			}
		}
	});
	
	return false;
}

$(document).ready(function() {
	
	$('#ASDF').remove();
	
	$('#filters a').click(function() {
		filter(this.innerHTML);
	});
	
	$('.closeInfo').hide();
	
	$('.infoTab').click(function() {
		if ($(this).children('a').length > 0) { return; } //manual link
		//grab item sku and which tab was clicked
		sku = $(this).parents('.product').attr('id');
		tab = $(this).html();
		
		//set .product > .info div we need
		infoDiv = $(this).parents('.product').children('.info');
		
		//enable closeInfo link and bind click
		$('#'+sku+'CloseInfo').fadeIn(100).unbind('click').click(function() {
			infoDiv = $(this).parents('.product').children('.info');
			$(infoDiv).slideUp(100);
			$(this).fadeOut(100);
			$(this).unbind('click');
			$('#'+sku+' .infoTab').css('border-bottom', 'solid 1px #999');
			document.getElementById(sku).info = 'closed';
			return false;
		});
		
		//Change border-bottoms
		$(this).css('border-bottom', '1px solid #fff');
		$(this).siblings('.infoTab').css('border-bottom', '1px solid #999');
		
		if (document.getElementById(sku).info == 'closed') {
			infoDiv.slideDown(100);
			document.getElementById(sku).info = 'open';
		}
		
		$(infoDiv).empty().append('loading');
		
		//get the info and append
		$.get(wrd+'/info.php?sku='+sku+'&info='+tab, function(data) {
			$(infoDiv).html(data);
		});
	});
	
	//set closed state when doc is ready
	$('.product').each(function() {
		sku = this.id;
		if (document.getElementById(sku).info == null) { document.getElementById(sku).info = 'closed'; }
		document.getElementById(sku).info = 'closed';
	});
	
	//easy print SKUs
	/*$('#products > div').each(function() {
		$('body').append(this.id+' ');
	});*/
	
	//show comments
	$('.commentShow').click(function() {
		//grab item info
		sku = $(this).parents('.product').attr('id');
		itemName = $(this).parent().parent().children('.itemName').html();
		infoDiv = $(this).parents('.product').children('.info');
		
		//append loading text
		$(infoDiv).empty().slideDown(300).append('loading');
	
		$.get('./comments.php?sku='+sku+'&itemName='+itemName, function(data) {
		
			$(infoDiv).empty().append(data);
		});
		return false;
	});

	if ($('span.notAvailable')) {
		$('span.notAvailable').each(function() {
			sku = $(this).html();
			$.get('./items/'+sku+'.php', function(data) {
				$('#products').append(data);
			});
			$(this).remove();
		});
	}

	$('a.newsComment').click(function() {
		url = $(this).attr('href');
		newsEntry = 'newsComments_'+this.id;
		$.get(url, function(data) {
			$('#'+newsEntry).empty().append(data+'<br/><a onclick="$(\'#'+newsEntry+'\').slideUp(300);" href="#">close comments</a>').slideDown(300);
		});
		return false;
	});
		
	$('#email').html('tux');
	$('#email').append('@');
	$('#email').append('linuxstudiopro.com');
	
	ref(u);
});

function closeEveryInfo() {
	$('.info').hide();
	$('.closeInfo').hide();
	$('.infoTab').css('border-bottom', 'solid 1px #999');
}
/*
if ($.browser.msie == true) {
		$('body').prepend('<div class="left" style="width:150px;text-align:left;"><p>This site does not support your internet browser (Microsoft Internet Explorer) because so few of my visitors use it. Although you are probably still able to view this page, it is likely that you will encounter odd formatting or behaviors. I recommend downloading Firefox for a generally better internet experience.</p><script type="text/javascript"><!--google_ad_client = "pub-3796114543286657";google_ad_slot = "8829961370";google_ad_width = 120;google_ad_height = 240;google_cpa_choice = ""; // on file//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');
	}
	
	if (!$.browser.msie && !u) {
		$('#header').hide();
		$('#footer').hide();
		$('#content > div').hide();
	}
	
$(window).load(function() {
	if (!u) {
		$('#header').slideDown(100, function() {
			slideDownContent();
		});
	}
});

var c = 0;
var slid = false
function slideDownContent() {
	if ($.browser.msie == false) {
		if (c >= $('#content > div').length) { $('#footer').slideDown(100); slid = true; return; }
		$('#content > div:eq('+c+')').fadeIn(100, function() {
			c++;
			slideDownContent();
		});
	}	
}
*/
function ref(h) {
	if (h == false) {return;}
	h = h.split('?')[1].split('q=')[1].split('&')[0];
	g = [];
	p = '';
	for (i=0;i<h.split('+').length;i++) {
		g.push(h.split('+')[i]);
		p += h.split('+')[i]+' ';
	}
	u = p;
	see(g);
}

function see(g) {
	products = [];
	//for each product title
	$('.product > div > b').each(function(x) {
		id = $(this).parents('.product').attr('id');
		html = $(this).parents('.product').clone(true).get(0);
		matches = [];
		somethingMatches = false;
		s = $(this).html().replace(/^\s*|\s*$/g,'').split(' ');
		
		for (i=0;i<s.length;i++) {//each title word w
			w = s[i].toLowerCase();	
			for (j=0;j<g.length;j++) { //each search term t
				t = g[j].replace(/^\s*|\s*$/g,'').toLowerCase();
				if (t.length > 3) {
					if (w.search(t) != -1) {
						somethingMatches = true;
						duplicate = false;
						for (n=0;n<matches.length;n++) {
							if (matches[n] == t) {
								duplicate = true;
							}
						}
						if (duplicate == false) {
							matches.push(t);
						}
					}
				} else {
					if (w == t) {
						somethingMatches = true;
						duplicate = false;
						for (n=0;n<matches.length;n++) {
							if (matches[n] == t) {
								duplicate = true;
							}
						}
						if (duplicate == false) {
							matches.push(t);
						}
					}
				}
			}
		}
		
		if (somethingMatches == true) {
			products.push({matches: matches, id: id, html: html});		
		}
	});
	
	relavence = [];
	
	n = products.length;
	//sort by most matches
	for (i=0;i<n;i++) {
		var last = {matches: ''};
		var index = 0;
		for (j=0;j<products.length;j++) {
			if (products[j].matches.length > last.matches.length) {
				last = products[j];
				index = j;
			}
		}
		relavence.push(last);
		products.splice(index, 1);
	}
	
	//dump(relavence);
	
	showSearchedItems(relavence);
}

function showSearchedItems(relavence) {
		$('#search').html('Your search <b style="color:#D52942">'+u+'</b> matches the following devices. | <a href="#" onclick="showAllAfterSearch()">show all</a>');
		
		$('.product').each(function() {
			$(this).hide();
		});
		for (i=0;i<relavence.length;i++) {
			//dump(relavence[i].matches);
			//alert('');
			$('.product:eq('+i+')').after($(relavence[i].html));
			$('.product:visible').addClass('search');
		}
		
		offset = $('#products').offset();
		$(window).scrollTop(offset.top);
}

function showAllAfterSearch() {
	$('.search').each(function(){
		$(this).remove();
	});
	$('#search').remove();
	$('#products > div').each(function(){
		$(this).show();
	});
	return false;
}


