
var mouseoffset = [30,30];
var picsize = [250,250];
var shadowsize = [15,15];
var bordersize = 5;
var cartsto, picsto;
var preview, shadow;
var docheight, docwidth;

var dumpObj = function (obj) {
    var dump = "";
    for (var i in obj) {
        dump += i + ":" + obj[i] + "; ";
    }
    return dump;
}

//function truebody()
//{
//	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
//}

function screenSize() {
    var w, h; // Объявляем переменные, w - длина, h - высота
    w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
    h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
    return {w:w, h:h};
}

	$(document).ready(function()
	{
		preview = $("#preview_div");
		shadow  = $("#shadow_div");

		$('#search input[type="text"]').focus(function() 
		{
			this.value=''; 
		});
		$('#search input[type="text"]').blur(function()
		{ 
			if (this.value == '') this.value='Поиск товаров'; 
		});
		$("#checkall").click(function()
		{
			$('input[type="checkbox"]').attr("checked", this.checked);
		});
		$("img.up").click(function()
		{
			var id = this.id;
			var input = $('#Quantity'+id);
//			input.attr("value", parseInt(input.attr("value")) + 1);
			input.val(parseInt(input.val()) + 1);
		});
		$("img.dn").click(function()
		{
			var id = this.id;
			var input = $('#Quantity'+id);
			var value = parseInt(input.attr("value"));
			if (value > 0) 
			{
				input.val(parseInt(value - 1));
//				input.attr("value", value - 1);
			}
		});
		$(".photo").mouseover(function(e) 
		{
			var reg = /^(\w+)\/(\d+)\.(.+)$/;
			var file = $(this).attr('src');
			var b_file = file.replace(reg, "/pics/$1/b_$2.$3");
			docwidth  = screenSize().w;
			docheight = screenSize().h;
//			picsto = setTimeout(function()
//			{
				preview.css({
					'width':  picsize[0]+'px', 
					'height': picsize[1]+'px', 
					'border': bordersize+'px solid #ccf',
					'padding': '0px',
					'background': "url('/@/img/load.gif') 50% 50% no-repeat #000"
				});
				shadow.css({
					'width':  picsize[0]+bordersize*2+'px',
					'height': picsize[1]+bordersize*2+'px' 
				});
				movePreview(e);
				$(document).mousemove(movePreview);
				preview.show();
				shadow.show();

				preview.html('<img src="'+b_file+'" onLoad="imgLoaded(this)" width="'+picsize[0]+'" height="'+picsize[1]+'" border="0" style="display: none;">');
//			}, 200);
		});
		$(".photo").mouseout(function () 
		{
//			clearTimeout(picsto);
			$(document).mousemove(function() {});
			$("#preview_div img").html('');
//			$("#preview_div img").load(function () {});
			preview.hide();
			shadow.hide();
//					$("#preview_div").html('<img src="/@/img/wait3.gif"/>');
//					$("#preview_div").html('');
		});
		$("#frame").fancybox(
		{
			'autoDimensions': false,
			'width'		: 470,
			'height'	: 520,
			'autoScale'	: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'type'		: 'iframe',
			'scrolling'	: 'no',
			'modal'		: false,
			'centerOnScroll': true,
			'hideOnOverlayClick': false

		});
		$(".register").fancybox(
		{
			'autoDimensions': false,
			'width'		: 435,
			'height'	: 470,
			'autoScale'	: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'type'		: 'iframe',
			'scrolling'	: 'no',
			'modal'		: false,
			'centerOnScroll': false,
			'hideOnOverlayClick': false
		});
		$(".auth").fancybox(
		{
			'autoDimensions': false,
			'width'		: 260,
			'height'	: 240,
			'autoScale'	: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'type'		: 'iframe',	
			'scrolling'	: 'no',
			'modal'		: false,
			'centerOnScroll': false,
			'hideOnOverlayClick': false
		});
		$("#print").click(function()
		{
			$("#counters").fadeOut(500);
			$("#print_pad").slideUp(500, function() 
			{
				window.print();
				window.close(); 
			});
		});
		$("#ahide").click(function()
		{
			$("div #subs").slideUp("slow", function()
			{
				$("div#hideSubs").hide();
				$("div#showSubs").show();
			});
		});
		$("#showSubs").click(function()
		{
			$("div#subs").slideDown("slow", function()
			{
				$("div#showSubs").hide();
				$("div#hideSubs").show();
			});
		});
	});
	
	$(document).blur(function() 
			{
				clearTimeout(picsto);
				$(document).mousemove(function() {});
				$("#preview_div img").html('');
//				$("#preview_div img").load(function () {});
				preview.hide();
				shadow.hide();		
			});

	$(document).focus(function() 
			{
				clearTimeout(picsto);
				$(document).mousemove(function() {});
				$("#preview_div img").html('');
//				$("#preview_div img").load(function () {});
				preview.hide();
				shadow.hide();		
			});

function movePreview(e)
{
	if (docheight < (e.clientY + picsize[1] + bordersize*2 + shadowsize[1] + mouseoffset[1]))
	{
		preview.css('top', e.pageY - (picsize[1] + bordersize*2 + mouseoffset[1]) + 'px');
		shadow.css ('top', e.pageY - (picsize[1] + bordersize*2 + mouseoffset[1]) + shadowsize[1] + 'px');
	}
	else
	{
		preview.css('top', e.pageY + mouseoffset[1] + 'px');
		shadow.css ('top', e.pageY + mouseoffset[1] + shadowsize[1] + 'px');
	}
	if (docwidth < (e.clientX + picsize[0] + bordersize*2 + shadowsize[0] + mouseoffset[0]))
	{
		preview.css('left', e.pageX - (picsize[0] + bordersize*2 + mouseoffset[0]) + 'px');
		shadow.css ('left', e.pageX - (picsize[0] + bordersize*2 + mouseoffset[0]) + shadowsize[0] + 'px');
	}
	else
	{
		preview.css('left', e.pageX + mouseoffset[0] + 'px');
		shadow.css ('left', e.pageX + mouseoffset[0] + shadowsize[0] + 'px');
	}
}

function imgLoaded(img)
{
	preview.css({'background': '#000'});
	$(img).fadeIn(300); //show();
}

function duzzleCart(cartTotal, article, quantity)
{
	$(document).ready(function()
	{
		clearTimeout(cartsto);
//alert(cartTotal);
		$("#cart_total").text(cartTotal);
		var inCart = $("#incart"+article+" div");
		inCart.text(parseInt(inCart.text())+quantity);
		$("#incart"+article).css("visibility", "visible").show();
		$("#cart_added").fadeIn('slow', function()
		{
			cartsto = setTimeout(function ()
			{
				$("#cart_added").fadeOut('fast');
			}, 1000);
		});
	});		
}

function OpenWindow(th, width, height, scroll)
{
	if (window.screen)
	{
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		str = ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
	}
	var newwin = window.open(th, '', 'width=' + width + ',height=' + height + ',resizable=no,location=no,toolbar=no,menubar=no,status=no,scrollbars=' + scroll + str);
	newwin.focus();
	return newwin;
}