$(function(){
		var xhr;
		$('.sort-name').html('<span title="Sort by Product Name-(Ascending)" class="sortable up">Product Name</span>');
		$('.sort-rating').html('<span title="Sort by Customer Rating-(Ascending)" class="sortable">Customer Rating</span>');
		$('.sort-price').html('<span title="Sort by Price-(Ascending)" class="sortable">Price</span>');

		$('.sort-name span, .sort-rating span, .sort-price span').click(function(){
				var ob = (/\bsort-(.+?)\b/).exec($(this).closest('th').attr('class'));
				if(!ob)
					return;
				var cid = (/-(\d+)$/).exec($(this).closest('table').attr('id'));
				if(!cid)
					return;
				if(xhr)
					xhr.abort();
				ob = ob[1];
				cid = cid[1];
				od = ($(this).is('.up'))?'desc':'asc';
				var url = location.href.replace(/^(.*)\.asp(?:\?.*?)?$/i, '$1-ajax.asp');
				var s = location.search;
				url += (s + ((s.length > 0)?'&':'?') + 'ob=' + ob + '&od=' + od + '&CategoryID=' + cid);
				$(this).closest('.productTable').children('tbody').addClass('loading');
				xhr = $.ajax({
						type: "GET",
						url: url,
						complete: function(data){
								if(data.status == 200 || data.status == 0){
									$('#productTable-' + cid + ' tbody tr:not(.footerBtn)').remove();
									$('#productTable-' + cid + ' tbody').prepend(data.responseText);
									var objRegExp = /<script[^>]*>([\s\S]+?)<\/script>/gm;
									var match;
									while((match = objRegExp.exec(data.responseText)) != null)
										eval(match[1]);
									$('#productTable-' + cid + ' tr:nth-child(2n)').addClass("light");

									if ($('#productTable-' + cid + ' .imgBtn') != null) {
										$('#productTable-' + cid + ' .imgBtn').hover(function(){
												if(!$(this).is('.disabled'))
													$(this).attr("src", $(this).attr("src").replace(/^(.*-hover|.*)(\..*?)$/i,'$1-hover$2'));
											},
											function(){
													if(!$(this).is('.disabled'))
														$(this).attr("src", $(this).attr("src").replace(/^(.*)-hover(\..*?)$/i, '$1$2'));
												});
									}
									iBox.checkTags(document.getElementById('#productTable-' + cid), 'a');
									$('#productTable-' + cid + ' tbody').removeClass('loading');
								}
								else{
									window.location='/500.asp'
								}
							}
					});
				$(this).closest('tr').find('span').removeClass('up').removeClass('down');
				$(this).addClass((od == 'asc')?'up':'down')
	
			});
	});