$(document).ready(function()
{
	$('a[@rel*=lightbox]').lightBox({
		txtImage: textImage,
		txtOf: textOf
	});
	//---------------------------------------------------------------
	
	$("#searchField").focus(function(){
		if ($(this).val() == shHint)
			$(this).val('');
	});
	//---------------------------------------------------------------

	$("#searchField").blur(function(){
		if ($(this).val() == '')
			$(this).val(shHint);
	});
	//---------------------------------------------------------------

	$("form#searchForm").submit(function(){
		var q = trimString($("#searchField").val());
		if ((q) && (q != '') && (q != shHint))
		{
			//q = escape(q);
			q = q.replace(/&/, '');
			q = q.replace(/=/, '');
			var baseUri = $("#searchForm").attr('action');
			if (!baseUri)
				baseUri = host+'lt';
			window.location = baseUri + '?sq=' + q;
		}

		return false;
	});
	//---------------------------------------------------------------
	
	var addToCart_working = 0;
	$(".form_addToCart").submit(function(){
		if (addToCart_working == 1) {
			return false;
		}
		addToCart_working = 1;
		var url = $(this).attr('action');
		var name = $(this).attr('name');
		name = name.replace(/cartForm_/, '');
		var cartItemName = "cartItem["+name+"]";
		var cartItemQty = parseInt($("#cartItem_"+name).val());

		if (cartItemQty >= 0)
		{
			$.ajax({
				type: "POST",
				url: url+'?ajax=1', 
				data: [{ name: cartItemName, value: cartItemQty }],
				cache: false,
				dataType: "json",
				success: function(data) {
					if (data.info) {
						$('#cartBlock').show();
						$('#cartInfo').html(data.info);
					} else {
						$('#cartBlock').hide();
					}
					if (data.name) {
						$('#cart_popup_content').html(data.name);
						showBlock('cart_popup');
						var timer = setTimeout("closeCartPopupAnime()", 4000);
					}
					//$('#cartItem_'+name).val('');
					$('#emptyCartBlock').hide();
					$('#btn_cartItem_'+name).blur();
					$('#cartItem_'+name).addClass('add_success');
					addToCart_working = 0;
				}
			});
		}
		
		return false;
	});
	//---------------------------------------------------------------
	
	/*
	$('a.products_read_more').click(function(){
		var href = $(this).attr('href');
		if (href) {
			getInfoContent(href);
		}
		return false;
	});
	*/
	
	getInfoContent = function(url)
	{
		var yy = document.all? truebody().scrollTop : pageYOffset;
		
		setOverlay();
		
		if (yy >= 0)
			$('#popup_box').css({	top: 100+yy+'px' });
		$('#popup_box').show();
		
		var n = 0;
		$("#popup_box_content").ajaxStart(function() {
			if (n++ > 0) return;
			$("#popup_box_content").html('<img src="'+host+'public/images/loading_small.gif" alt="Kraunasi..." />');
		});
		
		$.ajax({
			url: url+'?ajax=1', 
			cache: false,
			success: function(html) {
				if (html) {
					$("#popup_box_content").html(html);
				}
			}
		});
		
		
		return false;
	}
	//---------------------------------------------------------------
	
	$('#overlay').click(function() {
		closeDescription();
	});
	//---------------------------------------------------------------
	
	
	//var items = document.getElementById("list_images_inner").getElementsByTagName("div");
	var items = $('#pgallery .img');
	var numItems = 0;
	var maxWidth = 0;
	var marginLeft = parseInt($("#pgallery table").css("marginLeft"));
	var containerW = parseInt($('#pgallery').css('width'));
	var clicks = 0;
	var itemW = 104;
	var inline = 4;
	
	numItems = items.length;
	
	if (numItems > inline) {
		activateRight();
	}
	
	moveLeft = function() {
		if (clicks >= (numItems - inline)) {
			clicks = (numItems > inline) ? numItems - inline : 0;
			deActivateRight();
		}
		if (clicks > 0)
			activateLeft();
		marginLeft = clicks * itemW;
		$("#pgallery table").animate({"marginLeft": -marginLeft+"px"}, 500);
	}
	//---------------------------------------------------------------

	moveRight = function() {
		if (clicks <= 0) {
			clicks = 0;
			deActivateLeft();
		}
		if (clicks < (numItems - inline))
			activateRight();
		marginLeft = clicks * itemW;
		$("#pgallery table").animate({"marginLeft": -marginLeft+"px"}, 500);
	}
	//---------------------------------------------------------------
	
	$("#btn_move_left").click(function() {
		clicks--;
		moveRight();
		$(this).blur();
		return false;
	});					
	//---------------------------------------------------------------

	$("#btn_move_right").click(function() {
		clicks++;
		moveLeft();
		$(this).blur();
		return false;
	});
	//---------------------------------------------------------------
});
//---------------------------------------------------------------

setOverlay = function() {
	var obj = document.getElementById('top');
	var w = $('body').width();
	var h = obj.scrollHeight;
	
	$('#overlay').css({	width: w });
	$('#overlay').css({	height: h });
	$('#overlay').show();
}
//---------------------------------------------------------------

showBlock = function(id) {
	var yy = parseInt(document.all? truebody().scrollTop : pageYOffset);
	var ws = getWindowSizes();
	var extra = 0;
	if (ws[1] > 0) {
		extra = parseInt((ws[1]/2) - (127 / 2));
	}
	//alert(ws[1]);
	
	//if (yy >= 0)
	if ((extra+yy) > 0)
		$('#'+id).css({ top: extra+yy+'px' });
	$('#'+id).show();
}
//---------------------------------------------------------------

closeDescription = function() {
	$('#overlay').hide();
	$('#popup_box').hide();
	$('#popup_box_content').html('');
}
//---------------------------------------------------------------

closeCartPopup = function() {
	$('#cart_popup').hide();
}
//---------------------------------------------------------------

closeCartPopupAnime = function() {
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	if (isIE)
		closeCartPopup();
	else
		$('#cart_popup').fadeOut(500);
}
//---------------------------------------------------------------

activateLeft = function() {
	$('#btn_move_left').attr({src: host+'/public/images/arrow-left-a.gif'});
}
//---------------------------------------------------------------

activateRight = function() {
	$('#btn_move_right').attr({src: host+'/public/images/arrow-right-a.gif'});
}
//---------------------------------------------------------------

deActivateLeft = function() {
	$('#btn_move_left').attr({src: host+'/public/images/arrow-left.gif'});
}
//---------------------------------------------------------------

deActivateRight = function() {
	$('#btn_move_right').attr({src: host+'/public/images/arrow-right.gif'});
}
//---------------------------------------------------------------

