$(document).ready(function(){
	// bind magnifier position to mouse move
	$(".bigPicts").bind("mouseover", function(){$('#magnifier').show('fast');});
	$(".bigPicts").bind("mouseout", function(){$('#magnifier').hide('fast');});
	$(".bigPicts").bind("mousemove", function(e){
		mpos=mousePageXY(e);
		$('#magnifier').css('left', mpos['x']+10);
		$('#magnifier').css('top', mpos['y']+10);
	});
});

function winop(page, w, h, s) {
	popupWin=window.open(page, "win", "width="+w+", height="+h+", left=40, top=40, scrollbars="+s);
	popupWin.focus();
	return false;
}

function coloriseTables(){
	$("#zebra tr:even td").css("background", "#eee");
}

function showCityOpt(){
	  $("#cityopt").css("display", "block");
	  $("#selected_city").css("display", "none");
}

function sel(f){f.value='';}

function hide(id){$("#"+id).hide();}
function show(id){$("#"+id).show();}

function showWait(){return '<span id="wait"><img src="'+aload_preload.src+'" class="icons16"> Подождите пожалуйста...</span>';}

function addToBin(itemid, product){
	$("#howMuch"+product).hide();
	var howMuch=$("#howMuch"+product+" input:first").attr('value');
	$("#buyLink"+product).html(showWait());
	if(howMuch>0){
	$.post("../ajax/add_to_bin.php", {"item":itemid, "product":product, "howMuch":howMuch},
		function(data){
			$("#buyLink"+product).html('Добавлено в <a href="/14/">корзину</a>');
			$("#binCount").text(data);
		}
	);}
}

function delFromBin(item, product){
	$("#buyLink"+product).html(showWait());
	$.post("../ajax/del_from_bin.php", {"item":item, "product":product},
		function(data){
			$(".items input[name='i_"+product+"']").remove();
			$("#buyLink"+product).html('Удалено');
			$("#binCount").text(data);
			makeSum();
		}
	);
}

function makeSum(){
	var id=0;
	var itemarr=Array();
	var price=0;
	var count=0;
	var sum=0;
	$(".items input[type='hidden']").each(
		function(){
			price=$(this).attr('value');
			itemarr=$(this).attr('name').split('_');
			id=itemarr[1];
			count=$("input[name='ic_"+id+"']").attr('value');
			sum+=count*price;
		}
	);
	sum=Math.round(sum.toFixed(2));
	$("#sum").text(sum);
	if(sum>0){
		$("#buyForm input[type='button']").attr('disabled', false);
	}else{
		$("#buyForm input[type='button']").attr('disabled', true);
	}
}

function buyFormSubm(){
	var id=0;
	var itemarr=Array();
	var price=0;
	var count=0;
	var items='';
	$("#formNotify").html(showWait());
	$("#buyForm").attr("action", "../ajax/buy_form_check.php");
	$("#buyForm").ajaxSubmit(function(data){
		if(data==1){
				$("#formNotify").html('OK, sending...');

				$(".items input[type='hidden']").each(
					function(){
					price=$(this).attr('value');
					itemarr=$(this).attr('name').split('_');
					id=itemarr[1];
					count=$("input[name='ic_"+id+"']").attr('value');
					if(items){items+=' #'+id+'@'+count;}else{items=id+'@'+count;}
					}
				);

				$("input[name='items']").attr('value', items);

				action='';
				$("#buyForm").attr("action", action);
				$("#buyForm").submit();
			}else{
				$("#wait").remove();
				$("#formNotify").html(data);
			}
		});
}

function mousePageXY(e){
      var x = 0, y = 0;
      if (!e) e = window.event;
      if (e.pageX || e.pageY){
        x = e.pageX;
        y = e.pageY;
      }else if (e.clientX || e.clientY){
        x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft)
            - document.documentElement.clientLeft;
        y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop)
            - document.documentElement.clientTop;
      }
return {"x":x, "y":y};
}

function fixPNG(element){
  if (/MSIE (5\.5|6|7).+Win/.test(navigator.userAgent)){
    var src;
	if (element.tagName=='IMG'){
      if (/\.png$/.test(element.src)){
        src = element.src;
        element.src = "../pict/empty.gif";
      }
    }else{
      src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      if (src){
        src = src[1];
        element.runtimeStyle.backgroundImage="none";
      }
    }
    if(src)element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
  }
}

function showHowMuch(product){
	$(".howMuch").hide();
	$("#howMuch"+product).show();
}
