	var basketHovered    = false;
	var loginHovered    = false;
	
	function goTo(url) {
		window.location = SiteUrl + url;
	}
	function changeProductImage(id) {
		$('.ProductImageS').show();
		$('#ProductImageS' + id).hide();
		$('.ProductImage').hide();
		$('#ProductImage' + id).show();
		if (!$('#ProductImage' + id).hasClass('ProductImageCurrent')) {
			$('#ProductImage' + id).addClass('ProductImageCurrent')
			$("#ProductImage" + id + " a[rel=zoom_images]").jqzoom({
				'zoomWidth': 350,
				'zoomHeight': 350,
				'position': "right"
			});
		}
	}
	function toggle(title) {
		$('#' + title + 'Title').toggleClass('Selected');
		if ($('#' + title).is(":visible")) {
			$.get(SiteUrl +"/session/saveBlock/" + title + "/hidden");
		} else {
			$.get(SiteUrl + "/session/saveBlock/" + title + "/show");
		}
		$('#' + title).slideToggle();		
	}	
	function hideBigImage() {
		$('#BigImages').slideUp('slow', function(){
			$('#BigImagesHidden').show();
		});
		$.get(SiteUrl + "/session/saveBlock/BigImage/hidden");
	}
	function showBigImage() {
		$('#BigImages').slideDown();
		$('#BigImagesHidden').hide();
		$.get(SiteUrl + "/session/saveBlock/BigImage/show");
	}
	
	function hideBasket() {
		$("#BasketUpListWrapper").slideUp(function(){basketHovered = false;});
		clearTimeout(hideBasketTimeout);
	}
	function showBasket() {
		$("#BasketUpListWrapper").slideDown(function(){basketHovered = true;});
		clearTimeout(hideBasketTimeout);
	}
	function hideLogin() {
		$("#LoginUpListWrapper").slideUp(function(){loginHovered = false;});
		clearTimeout(hideLoginTimeout);
	}
	function showLogin() {
		$("#LoginUpListWrapper").slideDown(function(){loginHovered = true;});
		clearTimeout(hideLoginTimeout);
	}
	function changeNav(id, val) {
		$("#" + id).val(val);
		$("#NavButtons").hide();
		$("#NavLoading").show();
		$.get(PageUrl + '?is_ajax=1&'+$("#ProductsNav").serialize(), function(data) {
			$("#ProductsTable").html(data);
			window.location.hash=$("#ProductsNav").serialize();
			init_functions();
		});
	}
	
	// show popup for adding item to whish list and 
	// show button for adding or 
	// show login invitation
	function showWishListDialog(id) {	    
	    $('#wishList' + id).dialog();
	    $('#wishList' + id ).dialog('open'); 
	    if (isLogin) {
		$('#wishListAllow').show();
		$('#wishListDeny').hide();
	    } else {
		$('#wishListAllow').hide();
		$('#wishListDeny').show();
	    }
	    return false;
	}
	
	// add item to Wishlist and show resulting message
	function addWishList(url) {
		//PageUrl + 
		$.getJSON(url, function(data) {
			$("#wishListAllow").html(data.msg);			
		});
	}
	
	var hideBasketTimeout;
	var hideLoginTimeout;
	
	function init_functions() {
		$(".buyProductDialog").dialog({
				autoOpen: false,
				width:'350px'
			});
			
		$(".popUp").click(function(){
			$.get(this.href, function(data) {
				$("#popUpDialog").dialog({ title: this.title});
				$("#popUpDialog").html(data);
				$("#popUpDialog").dialog("open");
				$("#popUpDialog").click(function(){$("#popUpDialog").dialog("close");});
			});
			return false;
		});
	}
	$(document).ready(function() {
		hideBasketTimeout = setTimeout("hideBasket()", 1000);
		
		if (window.location.hash) {
			value = window.location.hash.substring(1, window.location.hash.length);
			if (value) {
				$("#NavButtons").hide();
				$("#NavLoading").show();
				$.get(PageUrl + '?is_ajax=1&'+$("#ProductsNav").serialize(), function(data) {
					$("#ProductsTable").html(data);
					window.location.hash=$("#ProductsNav").serialize();
					init_functions();
				});
			}
		}
		$("a[rel=product_images]").fancybox({
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
				'titlePosition' 	: 'over',
				'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
				    return ((currentIndex + 1) + ' / ' + currentArray.length);
				}
		});
		$(".ProductImageCurrent a[rel=zoom_images]").jqzoom({
			'zoomWidth': 350,
			'zoomHeight': 350,
			'position': "right"
		});
		
		init_functions();
		
		$(".Basket").hover(function() {
			if (!basketHovered) {
				clearTimeout(hideBasketTimeout);
				hideBasketTimeout = setTimeout("showBasket()", 300);
			} else {
				clearTimeout(hideBasketTimeout);
				hideBasketTimeout = setTimeout("hideBasket()", 1000);
			}
			return false;
		}, function(){
			clearTimeout(hideBasketTimeout);
			hideBasketTimeout = setTimeout("hideBasket()", 1000);
		});
		
		$(".Login").hover(function() {
			if (!loginHovered) {
				clearTimeout(hideLoginTimeout);
				hideLoginTimeout = setTimeout("showLogin()", 300);
			} else {
				clearTimeout(hideLoginTimeout);
				hideLoginTimeout = setTimeout("hideLogin()", 600);
			}
			return false;
		}, function(){
			clearTimeout(hideLoginTimeout);
			hideLoginTimeout = setTimeout("hideLogin()", 600);			
		});
						
		$("#popUpDialog").dialog({
			autoOpen: false,
			width:'450px'
		});		
		$(".buyProductDialog").dialog({
			autoOpen: false,
			width:'350px'
		});
		$("#fastSearchInput").autocomplete({
			source: SiteUrl + "/search/auto",
			minLength: 2,
			select: function( event, ui ) {
				$("#fastSearchInput").val(ui.item.value);
				$("#fastSearch").submit();
			}
		});
	});

