		String.prototype.capitalize = function() {
			return this.charAt(0).toUpperCase() + this.slice(1);
		}
		function dif(a,b) {
			if ((a - b) > 0) return a-b
			else return b - a;
		}
		
		$(document).ready(function() {			
			$('.productsmallpic').live('click',function(event){
				event.preventDefault();
				$('.productlargepic')[0].src = $(this).find('img').first().attr('src').replace('_thumb','_largethumb');
			});
			
			$('#cboxPrevious').live('click',function(event){
				event.preventDefault();
				_gaq.push(['_trackPageview',$("#currentCategory").attr('rel')]);
				$("#productlist>tbody>tr.product[rel='"+$(this).attr('rel')+"']").click();
			});
			$('#cboxNext').live('click',function(event){
				event.preventDefault();
				_gaq.push(['_trackPageview',$("#currentCategory").attr('rel')]);
				$("#productlist>tbody>tr.product[rel='"+$(this).attr('rel')+"']").click();
			});
			originalLayoutWrapperWidth = $("#layout_wrapper").width();
			$("#layout_wrapper").css('width',$(window).width()+"px");
			
			$(window).resize(function() {
				$("#layout_wrapper").css('width',Math.max($(window).width()-5,originalLayoutWrapperWidth)+"px");
			});
			
			$("#navigation ul li a").click(function(event){
				$(this).closest('ul').find('li.active').removeClass('active');
				$(this).closest('li').addClass('active');
			});
			if(!($.browser.msie && $.browser.version < "8.0")) {
				$('a.ajax').live('click',function(event){
					event.preventDefault();
					window.location.hash = "#"+$(this).attr('href');					
				});
				$(window).bind('hashchange', function(e) {
					if ($("#cboxLoadedContent").children().size() > 0)
						$("body").colorbox.close(); 
					var url = $.param.fragment();

					if (url != '') {
						if (url.indexOf('prod') >= 0) {
							$("#navigation li.active").removeClass('active');
							$("#navigation li[rel=1]").addClass('active');
						} else if (url.indexOf('search') >= 0) {
							$("#navigation li.active").removeClass('active');
							$("#navigation li[rel=2]").addClass('active');
						} else if (url.indexOf('pageid') >= 0) {
							$("#navigation li.active").removeClass('active');
							$("#navigation li a[href='"+url+"']").closest('li').addClass('active');
						}
											
						 _gaq.push(['_trackPageview',url]);
						$("#content_upper").empty();
						$("#preview").remove();
						$("#ajax_loading").show();
						$("#layout_inner").fadeTo('fast','0.3',function() {
							paginationthispage = 1;
							$('<div class="content_wrapper"></div>').appendTo("#content_upper").load(url,function(){style()});
						});	
					}
				});
			}
			
			style();
			$(".content_wrapper").attr('rel','');

			$('#productlist>tbody>tr.product').live('click', function(event) {
				var prev = $(this).prevAll('.filterok');
				var counta = prev.length;
				if (prev.length == 0)
					prev = null;
				else
					prev = prev.first().attr('rel');

				var next = $(this).nextAll('.filterok');
				var countb = counta + 1 + next.length;
				if (next.length == 0)
					next = null;
				else
					next = next.first().attr('rel');

				
				var orighref = $(this).find('a.product').attr('href');
				var cboxhref = orighref+'&nolayout';
				if ($("#productlist").is(".searchresults"))
					cboxhref += '&search';
				var cboxtitle = $(this).find('a.product').text();

					$.fn.colorbox({
						width: '500px', 
						height: '600px', 
						preloading: false,
						href: cboxhref,
						title: cboxtitle,
						onComplete: function () {
							style();
							$("#cboxTitle").append('  ('+(counta+1)+' / '+countb+')');
							if (prev != null) {
								$('#cboxPrevious').unbind('click').show().attr('rel',prev);
							} else {
								$('#cboxPrevious').hide();
							}
							if (next != null) {
								$('#cboxNext').unbind('click').show().attr('rel',next);
							} else {
								$('#cboxNext').hide();
							}
							 _gaq.push(['_trackPageview',orighref]);
						},
						onClosed:function(){
							_gaq.push(['_trackPageview',$("#currentCategory").attr('rel')]);
						}						
					});
				event.preventDefault();
			});			



			$(window).trigger( 'hashchange' );
			
		});
		
		function style() {
			$("#ajax_loading").hide();

			var s = $(".newpagetitle");
			if (s.length > 0)
				document.title = s.first().text();
			s.remove();

			if(!($.browser.msie && $.browser.version < "8.0"))
				$("#layout_inner").fadeTo('fast','1',function(){if(jQuery.browser.msie) {this.style.removeAttribute('filter');}});
	
			$(".largeimage").colorbox({maxWidth: '90%'});


			
			if ($("#quicksearch").size() == 1) {
				repaginate(paginationthispage);
				$("#quicksearch").closest('div').show();
				$("#quicksearch").keyup(function(){startSearchTimeout()});
			}
			styleVisibleRows();
			
			$("a.product").click(function(event) {
				$(this).find('a.ajax').click();
				event.preventDefault();
			});

//			if(!($.browser.msie && $.browser.version < "8.0"))
//				$("a.ajax").each(function(){$(this).attr('href','#'+$(this).attr('href'));$(this).removeClass('ajax')});
			
		}
		function goToNewPageFancyStyle(newPageId) {
			newPageId = parseInt(newPageId);
			$("#content").slideUp('normal',function() {
				repaginate(newPageId);
				$("#content").slideDown('normal', function(){styleVisibleRows();});
			});
			_gaq.push(['_trackEvent', 'page', ''+newPageId]);
		}
		function styleVisibleRows() {
			var r1 = $("#productlist>tbody>tr:visible.notyetstyled a.preview");
			r1.click(function(event) {
				event.preventDefault();
			}).each(function(){
				imagePreview($(this));
			});	
//			if($.browser.msie && $.browser.version < "8.0")
//				$('#productlist>tbody>tr.product:visible.notyetstyled').height(75);
			var rows = $('#productlist>tbody>tr.product:visible.notyetstyled, #categorylist>tbody>tr');
			rows.hover(	function(){
						$(this).addClass('hover');
					},
					function(){
						$(this).removeClass('hover');
					})
				.css('cursor','pointer')
				.removeClass('notyetstyled')
				.find('img.notyetshown').each(
					function(){
						$(this)[0].src = $(this).attr('rel');
						$(this).removeClass('notyetshown');
					}
				);
			
			$('#categorylist>tbody>tr').click(function(event){
				if($.browser.msie && $.browser.version < "8.0")
					window.location = $(this).find('a.ajax').first().attr('href');
				else
					window.location.hash = '#'+$(this).find('a.ajax').first().attr('href');
				event.preventDefault();
			});
			if ($('.singlecategory').length > 0 || $('.subcategory').length > 6 || $('.product.filterok').length > itemsperpage) {
				$("#title").slideUp();
				$("#footer").hide();
				$("#content").css("height",(865-($("#searchandpaginationbar").height()))+'px');
				$("#content_upper").css("height",'965px');
			} else {
				$("#footer").show();
				$("#title").slideDown();
				$("#content").css("height",'600px');
				$("#content_upper").css("height",'680px');
			} 			
		}
		function repaginate(pagenum) {
			paginationthispage = parseInt(pagenum);
			processSearch();

			$("#pagination").empty();
			$("#nextpage").empty().css('margin-bottom','5px').css('margin-top','20px');
			var productcount = $('#productlist.singlecategory>tbody>tr.product.filterok').length;
			var pagecount = Math.ceil(productcount / itemsperpage);

			if (productcount > itemsperpage) {
				$("#pagination").show().html(textPage+"   ");
				
				
				if (paginationthispage > 1) {
					var n = $("<a href='#' rel='"+(paginationthispage-1)+"'>"+textPrevPage+"</a>");
					n.click(function(event) {
						event.preventDefault();
						goToNewPageFancyStyle($(this).attr('rel'));
					});
					$("#nextpage").append(n);
				}
				$("#nextpage").append(' - ');
				
				if (paginationthispage < pagecount) {
					var n = $("<a href='#' rel='"+(paginationthispage+1)+"'>"+textNextPage+"</a>");
					n.click(function(event) {
						event.preventDefault();
						goToNewPageFancyStyle($(this).attr('rel'));
					});
					$("#nextpage").append(n);
				}
				
				var p = [];
				
				if (pagecount > 200) {
					var sections = [];
					
					sections.push([1,3]);
					sections.push([pagecount,3]);
					for (var i = 1; i < 4; i++)
						sections.push(
							[
								(pagecount / 5)*i
								,
								1
							]);
					sections.push([paginationthispage,6]);
					for (var i = 1; i <= pagecount; i++)
						for (var ii = 0; ii < sections.length; ii++)
							if (dif(sections[ii][0],i) < sections[ii][1]) {
								p.push(i);
								break;
							}
					
				} else {
					for (var i = 1; i <= pagecount; i++)
						p.push(i);
				}
				
				var prev = 0;
				for (var i = 0; i < p.length; i++) {
					var a;
					if (dif(prev,p[i]) > 1)
						$("#pagination").append('.. ');
					if (p[i] == paginationthispage) {
						a = $('<span>'+p[i]+'</span>');
					} else {
						a = $('<a href="#" class="paginationlink">'+p[i]+'</a>');
						a.click(function(event) {
							event.preventDefault();
							goToNewPageFancyStyle($(this).text());
						});
						
					}
					pagePreview(a);
					$("#pagination").append(a);
					$("#pagination").append(' ');
					prev = p[i];
				}
			}

			$("#productlist>tbody>tr.filternotok").hide();
			var a,b,c;
			a = (((paginationthispage-1)*itemsperpage));
			b = itemsperpage;
			c = (((paginationthispage)*itemsperpage));
			
//			$("#productlist>tbody>tr.filterok").show();
			
			$("#productlist>tbody>tr.filterok").each(function(index){
				if (index >= a && index < c)
					$(this).show();
				else
					$(this).hide();
			});
//				$("#productlist>tbody>tr.filterok:lt("+a+")").hide();
//			$("#productlist>tbody>tr.filterok:gt("+a+")").filter(":lt("+b+")").show();
//				$("#productlist>tbody>tr.filterok:gt("+c+")").hide();
		}
		function startSearchTimeout() {
			$("#quicksearchloadingimage").show();
			if (timeoutid > -1) {
				clearTimeout(timeoutid);
			}
			timeoutid = setTimeout(function(){repaginate(1); styleVisibleRows();},300);
		}
		function processSearch() {
			timeoutid = -1;
			if ($("#quicksearch").size() != 1 || $("#quicksearch").val().length == 0) {
				$("#productlist>tbody>tr.product").not('.filterok').addClass('filterok');
				var resultcount = $("#productlist>tbody>tr.product.filterok").length;
			} else {
				var needles = accent_fold($("#quicksearch").val().toLowerCase()).split(' ');
				
				var resultcount = 0;
				$("#productlist>tbody>tr").each(function() {
					var show = true;
					for (var i in needles)
						if (accent_fold($(this).text().toLowerCase()).indexOf(needles[i]) == -1)
							show = false;
					if (show) {
						$(this).addClass('filterok').removeClass('filternotok');
						resultcount++;
					}
					else
						$(this).addClass('filternotok').removeClass('filterok');
				});
				_gaq.push(['_trackEvent', 'searchOnPage', $('#currentCategory').text() ,$("#quicksearch").val()+' ('+resultcount+')']);
			}
			$("#resultcount").html("&nbsp;&nbsp;<i>"+resultcount+" "+textResults+"</i>");			
			$("#quicksearchloadingimage").hide();
		}
		var accentMap = {
			'ä':'a','á':'a','à':'a',
			'ë':'e','é':'e','è':'e',
			'ï':'i','í':'i','ì':'i',
			'ö':'o','ó':'o','ò':'o',
			'ü':'u','ú':'u','ù':'u'
		};
		function accent_fold (s) {
			if (!s) { return ''; }
			var ret = '';
			for (var i=0; i<s.length; i++) {
				ret += accentMap[s.charAt(i)] || s.charAt(i);
			}
			return ret;
		};
		function imagePreview(elem){	
			/* CONFIG */
				
				xOffset1 = 100;
				yOffset1 = 30;
				
				// these 2 variable determine popup's distance from the cursor
				// you might want to adjust to get the right result
				
			/* END CONFIG */
			elem.hover(function(e){
				$("#preview").remove();
				this.t = this.title;
				this.title = "";	
				var c = (this.t != "") ? "<br/>" + this.t : "";
				$("body").append("<div id='preview' class='imagepreview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</div>");								 
				$("#preview")
					.css("top",(e.pageY - xOffset1) + "px")
					.css("left",(e.pageX - yOffset1 - $("#preview").width()) + "px")
					.fadeIn("fast");						
			},
			function(){
				this.title = this.t;	
				$("#preview").remove();
			});	
			elem.mousemove(function(e){
				$("#preview")
					.css("top",(e.pageY - xOffset1) + "px")
					.css("left",(e.pageX - yOffset1 - $("#preview").width()) + "px");
			});			
		};
		function pagePreview(elem){	
			xOffset = -15;
			yOffset = -50;
			elem.hover(function(e){
				$("#preview").remove();
				var pageid = parseInt($(this).text());
				$("body").append(
					"<div id='preview' class='pagepreview'>"+getTextFromFirstItem(pageid)+" - "+getTextFromLastItem(pageid)+"</div>");								 
				$("#preview")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX - yOffset - $("#preview").width()) + "px")
					.fadeIn("fast");						
			},
			function(){
				$("#preview").remove();
			});	
			elem.mousemove(function(e){
				$("#preview")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX - yOffset - $("#preview").width()) + "px");
			});			
		};		
		function getTextFromFirstItem(pagenum) {
			return getTextFromRow($("#productlist>tbody>tr.product.filterok")[(pagenum-1)*itemsperpage]);
		}
		function getTextFromLastItem(pagenum) {
			var elems = $("#productlist>tbody>tr.product.filterok");
			if (elems.length < (pagenum-1)*itemsperpage)
				return '';
			if (elems.length < (pagenum)*itemsperpage)
				return getTextFromRow(elems[elems.length-1]);
			else
				return getTextFromRow(elems[(pagenum)*itemsperpage - 1]);
		}
		function getTextFromRow(rowElem) {
			if ($("#productlist").attr('rel') == 'price')
				return $(rowElem).find('.price').text();
			else
				return $(rowElem).find('.product').text().substr(0,3).toLowerCase().capitalize();
		}

