
function mouseIn(e, box) {
	return ( (e.pageX >= box.offset().left) &&
		(e.pageX <= box.offset().left+box.width()) &&
		(e.pageY >= box.offset().top) &&
		(e.pageY <= box.offset().top+box.height()) );
}

function mouseIn1(e, box) {
	return ( (e.pageX > box.offset().left+1) &&
		(e.pageX < box.offset().left+box.width()) &&
		(e.pageY > box.offset().top+1) &&
		(e.pageY < box.offset().top+box.height()) );
}


var cat_popup_blocks = new Array();

function checkCPBlock(id) {
	if (cat_popup_blocks[id]) {
		cat_popup_blocks[id].hide(100);
	}
}

$(document).ready(function() {
	$("a.redarrow").each(function(i) {
		var arr = $(this);
		var blockid = "#"+arr.attr("id")+"_p";
		var block = $(blockid);
		var repos = function(e) {
			var lefo = arr.offset().left - 2;
			var topo = arr.offset().top+arr.height()-2;
			if ( arr.offset().left + block.width() + 20 > 
				$("body").scrollLeft() + $(window).width() ) {
				lefo = arr.offset().left + arr.width() - block.width() + 2;
			}
			if ( arr.offset().top + block.height() + 20 > 
				$("body").scrollTop() + $(window).height() ) {
				topo = arr.offset().top - block.height() + 2;

			}
			block.css({
				left: lefo+"px",
				top: topo+"px"
			});		
		}
		// positioning

		var inner = $("#"+arr.attr("id")+"_p > div");
		if (! (jQuery.browser.msie && (jQuery.browser.version < 7) ) ) {
			inner.dropShadow({left: 0, top: 0, blur: 2, opacity: 0.2});
		}
		arr.hover(
			function() {
				repos();
				if (block.css('display') == 'none') {
					block.show(300, function() {
						if (inner.shadowId())
							inner.redrawShadow();
					});
					cat_popup_blocks[block.attr('id')] = null;
				}
			},
			function(e) {
				var shd = $('#'+$(this).attr('id')+'_p > div');
				if ( !mouseIn1(e, $(this)) &&  !mouseIn(e, block) && !mouseIn(e, shd) &&
					(block.css('display') != 'none')) {
					cat_popup_blocks[block.attr('id')] = block;
					setTimeout(
					   'checkCPBlock("'+block.attr('id')+'")',
					   400);
				}
			}
		);
		block.hover(function() {
			if (cat_popup_blocks[block.attr('id')]) {
				cat_popup_blocks[block.attr('id')] = null;
			}
		},
		function(e) {
			if (!mouseIn(e, arr) && !mouseIn(e, inner)
				&& (block.css('display') != 'none')) {
				cat_popup_blocks[block.attr('id')] = block;
				setTimeout(
				   'checkCPBlock("'+block.attr('id')+'")',
				   400);
			}
		}); 

	});
	
		$(".ajaxbuy").click(function(ev) {
		var pp = $(".orderajx");
		var btn = $(this);	
		var pid = parseInt(btn.attr('id').replace('buybutton_',''));
		var numbuy = 1;
		if ( $('#buycount_'+pid).length != 0 ) {
			numbuy = parseInt($('#buycount_'+pid).val());
			if (isNaN(numbuy)) {
				alert('Неверно заполнено поле "количество"!');
				return false;
			}
			if (numbuy == 0) {
				alert('Неверно заполнено поле "количество"!');
				return false;
			}
			if (numbuy < 0) {
				alert('Неверно заполнено поле "количество"!');
				return false;
			}
			
		}
		pp.css('left', (btn.offset().left-175)+'px');
		pp.css('top', (btn.offset().top+22)+'px');

		// ***
		// insert ajax call here
		// use PID and NUMBUY variables
		// ***
		if(this.req)
		{
			this.req.abort();
		}
		this.req = $.ajax({
		  type: "GET",
		  cache: false,
		  async: false,
		  url: "/products/add2basket_ajax.php",
		  data: {pid:pid, num:numbuy },
		  success: function(data) {
		  	  $('#top-basket-right').html(data);
		  	// alert(data);
		  	  pp.fadeIn(300, function() {
				var x = $('<div id="clickunder">&nbsp;</div>');
				x.css('height', ''+$(document).height()+'px');
				x.click(function() {
					$(".orderajx").fadeOut(300);
					$("#clickunder").remove();
				});
				x.appendTo('body');
			  });
		  	
		  }
		  });


		return false;
	});

	$(".orderajx a.close").click(function(ev) {
		$(".orderajx").fadeOut(300);
		$("#clickunder").remove();
		return false;
	});
});

