var preload = [];
var currentPage = parseInt(location.hash) || 1;
var pageSize = 3;

navTo = function(el) {
	try {
		var parts = el.target.href.split("#");
		if(parts.length == 2) {
			currentPage = parseInt(parts[1]);
			updateNav();
		}		
	} catch (x) {
		alert("ERROR: " + x);
	}
}

updateNav = function() { 
	var rows = $(".product_row");
	var pages = Math.ceil(rows.length / pageSize);
	if ($("p#nav").length == 0) {
		$("<p id='nav'></p>").appendTo(".main_cel");
	}
	var html = "";
	if (currentPage > 1) {
		html += "<a href='#" + (currentPage - 1) + "'>&lt;</a> &nbsp; ";
	}
	for (i = 0; i < rows.length; i++) {
		page = Math.floor(i / pageSize) + 1;
		row = rows.get(i);
		$(row).css("display", (page == currentPage ? "" : "none"));		
	}
	for (i = 1; i <= pages; i++) {
		if (currentPage == i) {
			html += "<span>" + i + "</span>";
		} else {
			html += "<a href='#" + i + "'>" + i + "</a>";
		}		
	}
	if (currentPage < pages) {
		html += "<a href='#" + (currentPage + 1) + "'>&gt;</a>";
	}
	$("p#nav").html(html);
	$("p#nav a").click(navTo);
}

$(function() {
	updateNav();
	$(".thumb a").hover(function() {
		var url = this.href;
		$(this).parent().parent().find(".detail img").each(function() {
			prev_url = this.src;
			this.src = url;
		});	
	}, function() {
		$(this).parent().parent().find(".detail img").each(function() {
			if (prev_url) {
				this.src = prev_url;			
			}
		});
	}).click(function() {
		prev_url = false;
		var url = this.href;
		$(this).parent().parent().find(".detail img").each(function() {
			this.src = url;
		});		
		return false;
	}).each(function() {
		var i = new Image();
		i.src = this.href;
		preload.push(i);
	});
});
			
function updateFoto(f, sel) {
	return false;
}
