function widthcheck () {
	if ($(window).width() < 1024) {
		$('td.right-col').hide();
	} else if ($(window).width() > 1250){ 
		$('td.right-col').show();
	}
	return this;
}


		function setval(el, tovid){
				$('div.add-pp').show().css({top:getAbsolutePosition(el).y-7,left:getAbsolutePosition(el).x-160}).overlay();
				$('#tovid').val(tovid);
				$("#f5val").focus();
				return false;  
		}
		var prevSearchTimer = null;

		function ScheduleSearch() {
        if (prevSearchTimer != null) {
                clearTimeout(prevSearchTimer);
        }
        prevSearchTimer = setTimeout("StartSearch()", 900);
		}

		function StartSearch(){
         var txt = $("#str").val();
				if (txt != '' && txt.length > 2) {

						$.ajax({  
							type: "POST",  
							url: "index.php",  
							data: "m=catalog&mode=l&ajax=1&str="+txt,
							success: function(html){
								$("div.cont-inner").html(html);	

									jQuery(function($) {
										$("a[rel^='lightbox']").slimbox({
										/* Put custom options here */
									counterText: "\u0418\u0437\u043E\u0431\u0440\u0430\u0436\u0435\u043D\u0438\u0435 {x} \u0438\u0437 {y}"
										}, null, function(el) {
											return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
										});
									});
								
							}  
						});  

				}
		}
		function sslk() {
        if (prevSearchTimer != null) {
                clearTimeout(prevSearchTimer);
        }
        prevSearchTimer = setTimeout("ssl()", 900);
		}

		function ssl(){
         var txt = $("#str").val();
				if (txt != '' && txt.length > 2) {

						$.ajax({  
							type: "POST",  
							url: "index.php",  
							data: "m=catalog&mode=fsearch&ajax=1&str="+txt,
							error: function(){
									$("#orderinfoblock").html("К сожалению, сервер не ответил. Возможно это временная ошибка - повторите попытку");
									},
							success: function(html){
								$("td.center-col").html('<h2 class="mrg0-0-10 pd0-0-0-15 black"><a href="?m=catalog" class="float-right red">Выбрать из каталога</a>Формирование заказа</h2><div id="orderinfoblock"></div><div class="inner-lk"></div>');
								$("div.inner-lk").html(html);
								$('#overlay').remove();
								$("#orderinfoblock").addClass("inner-lk");
								$.ajax({  
									type: "POST",  
									url: "index.php",  
									data: "m=clients&op=orderinfo&ajax=1",
									error: function(){
									$("#orderinfoblock").html("К сожалению, сервер не ответил. Возможно это временная ошибка - повторите попытку");
									},
									success: function(html){
										$("#orderinfoblock").html(html);
											}  
									});  
					
									
									
							}  
						});  
				}
		}
function getAbsolutePosition(el) {
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}
function Mail() {
	var n = 'mail';
	var s = '@';
	var m = 'masterglass.ru';
	document.write(n + s + m);
}
function Mail_spb() {
	var a = '<a href="mailto:';
	var n = 'spb';
	var s = '@';
	var m = 'masterglass.ru';
	var y = '">'
	var z = '</a>'
	document.write(a + n + s + m + y + n + s + m + z);
}

$.fn.overlay=function(cl) {
	var el=$(this);
	$('body').prepend('<div id="overlay"></div>');
	if (cl) {$('#overlay').addClass(cl);}
	$('#overlay').click(function(){el.hide();$('#overlay').remove();});
	$('#overlay').show('slow');
	return this;
}
$.fn.overlay_sel=function(cl) {
	$('body').prepend('<div id="overlay_dark"></div>');
	$('#overlay_dark').click(function(){$('div.select-opt').hide();$('div.select-act').removeClass('select-act');$('#overlay_dark').remove();});
	$('#overlay_dark').show();
	return this;
}

$.fn.centering=function() {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}

	jQuery.fn.check = function(mode) {
	var mode = mode || 'on';
	return this.each(function() 
	{
	switch(mode) {
		case 'on':
			this.checked = true;
			break;
		case 'off':
			this.checked = false;
			break;
		case 'toggle':
			this.checked = !this.checked;
			break;
			}
		});
	};
 

$(document).ready(function(){

	$('div.pp h3 .x').click(function(){
		$(this).parents("div.pp").hide();
		$('#overlay').remove();
		return false;
	});
	
	$('.pp .submit input:checkbox').click(function(){
		if (this.checked == true) {
			$(this).parents('div.pp').find('input:checkbox').check('off');
		}
		return true;
	});
	
	
	$('div.select-block div.select').click(function(){
		if (!$(this).hasClass('select-act')) {$(this).overlay_sel();}
		else {$('#overlay_dark').remove();}
		$(this).toggleClass('select-act');
		$(this).siblings('div.select-opt').toggle().css({top:getAbsolutePosition(this).y+21,left:getAbsolutePosition(this).x}); //.width($(this).parent().parent().width()-2)
		var optheight = $(this).siblings('div.select-opt').children('ul').height();
		if (optheight > 280) {$(this).siblings('div.select-opt').addClass('select-opt-big');}
		else {$(this).siblings('div.select-opt').removeClass('select-opt-big');}
	});
		
		$('table.tovlist tr').live('mouseover', function(){
            $(this).find('div.tooltip').css({opacity:0.8}).fadeIn(10);
        }).live( 'mousemove' , function(kmouse){
			// тут надо вычислить, помещается ли подсказка, или если нет
			// то для предотвращения скроллинга - перемещаем ее в другую сторону от курсора
			kpos = ($(window).width() <  kmouse.pageX+38+$(this).find('div.tooltip').width()) ? (kmouse.pageX - 15 - $(this).find('div.tooltip').width()) : kmouse.pageX + 15;
            //alert($('document').clientWidth )
			$(this).find('div.tooltip').css({left:kpos, top:kmouse.pageY+15});
        }).live('mouseout', function(){
            $(this).find('div.tooltip').fadeOut(10);				  
        });

		
		
            $('#f1').submit(function(){  
                $.ajax({  
                    type: "POST",  
                    url: "index.php",  
					data: "m=catalog&mode=l&ajax=1&str="+$("#str").val(),
					success: function(html){
						$("div.cont-inner").html(html);						
					}  
                });  
                return false;  
            }); 
           $('#f5').submit(function(){ 
		
                $.ajax({  
                    type: "POST",  
                    url: "index.php",  
					data: "m=clients&op=add2order&ajax=1&tovid="+$("#tovid").val()+"&val="+$("#f5val").val(),
					success: function(html){
						$('div.add-pp').hide();	
						$('#f5val').val('');
						$('#overlay').remove();
								$.ajax({  
									type: "POST",  
									url: "index.php",  
									data: "m=clients&op=orderinfo&ajax=1",
									error: function(){
									$("#orderinfoblock").html("К сожалению, сервер не ответил. Возможно это временная ошибка - повторите попытку");
									},
									success: function(html){
										$("#orderinfoblock").html(html);
											}  
									}); 						
					}  
                });  
                return false;  
            });	

           $('#fn5').submit(function(){ 
		
                $.ajax({  
                    type: "POST",  
                    url: "index.php",  
					data: "m=catalog&mode=add2order&ajax=1&tovid="+$("#tovid").val()+"&val="+$("#f5val").val(),
					success: function(html){
						$('div.add-pp').hide();	
						$('#f5val').val('');
						$('#overlay').remove();
								$.ajax({  
									type: "POST",  
									url: "index.php",  
									data: "m=catalog&mode=orderinfonoreg&ajax=1",
									error: function(){
									$("#orderinfoblock").html("К сожалению, сервер не ответил. Возможно это временная ошибка - повторите попытку");
									},
									success: function(html){
										$("#orderinfoblock").html(html);
											}  
									}); 						
					}  
                });  
                return false;  
            });				

});


