///// Плагин ввода нужного типа символов в текстовые поля
(function($){
	$.fn.alphanumeric = function(p) { 
		p = $.extend({
			ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- ",
			nchars: "",
			allow: ""
		  }, p);	
		return this.each
			(
				function() 
				{
					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЪЫЭЮЯ";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyzабвгдеёжзийклмнопрстуфхцчшщьъыэюя";
					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');
					
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');
					$(this).keypress
						(
							function (e)
								{
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();
								}
						);
					$(this).bind('contextmenu',function () {return false});
				}
			);
	};
	$.fn.numeric = function(p) {
		var az = "abcdefghijklmnopqrstuvwxyzабвгдеёжзийклмнопрстуфхцчшщьъыэюя";
		az += az.toUpperCase();
		p = $.extend({
			nchars: az
		  }, p);	
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
	};
	$.fn.alpha = function(p) {
		var nm = "1234567890";
		p = $.extend({
			nchars: nm
		  }, p);	
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
	};	
})(jQuery);


///// Ф-я определяет является ли аргумент типом int
function is_int( mixed_var ) {
    // version: 905.412
    // discuss at: http://phpjs.org/functions/is_int
    // +   original by: Alex
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Matt Bradley
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %        note 1: 1.0 is simplified to 1 before it can be accessed by the function, this makes
    // %        note 1: it different from the PHP implementation. We can't fix this unfortunately.
    // *     example 1: is_int(23)
    // *     returns 1: true
    // *     example 2: is_int('23')
    // *     returns 2: false
    // *     example 3: is_int(23.5)
    // *     returns 3: false
    // *     example 4: is_int(true)
    // *     returns 4: false
    if (typeof mixed_var !== 'number') {
        return false;
    }
    if (parseFloat(mixed_var) != parseInt(mixed_var, 10)) {
        return false;
    }
    return true;
}












	var IEVersion = 0;
	if (/*@cc_on!@*/false)
	{
		var ua = window.navigator.userAgent;
		var msie = ua.indexOf ( "MSIE " );
		if ( msie > 0 )			// If Internet Explorer, return version number
			{IEVersion = parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));}
		else {IEVersion = 0;}		// If another browser, return 0
	}
	else {IEVersion = 0;}

   	   function resizeIE6()
   	   {
			if (IEVersion>0 && IEVersion<=6)
			{
				setTimeout("$('body').css('height','99.9%');", 500);
				setTimeout("$('body').css('width','99.9%');", 500);
				setTimeout("$('body').css('height','100%');", 1000);
				setTimeout("$('body').css('width','100%');", 1000);
			}
   	   }
   	   
     $(document).ready(function(){
		if (sessvars.producers == "closed") {$('#producers_list h1.showhide + ul').hide();}
		if (sessvars.findb == "closed" || !(sessvars.findb)) {$('#findb h1.showhide + ul').hide();}
		if (sessvars.nav_catalog == "closed" || !(sessvars.nav_catalog)) {$('#nav_catalog h1.showhide + ul').hide();}


		////// Если ie, исправляем hover для p в маленькой корзине
			if (IEVersion<=6)
			{
				$('#nav_col li#basket_goods_list p.basket_goods_list_good').hover(
					function () {
						$(this).css('border','1px solid #ddd');
					},
					function () {
						$(this).css('border','1px solid #fff');
					}
				);
			}
		////////////////////////////////

    $('#main_news').fadeOut(500).fadeIn(1000);

		$('#producers_list h1.showhide').click (function(){
			if ($('#producers_list h1.showhide + ul').css('display')=='none')
			{
				$('#producers_list h1.showhide + ul').fadeIn(1000);
				sessvars.producers = "opened";
			}
			else
			{
				$('#producers_list h1.showhide + ul').fadeOut(500);
				sessvars.producers = "closed";
			}
		});
		
		$('#findb h1.showhide').click (function(){
			if ($('#findb h1.showhide + ul').css('display')=='none')
			{
				$('#findb h1.showhide + ul').fadeIn(1000);
				sessvars.findb = "opened";
			}
			else
			{
				$('#findb h1.showhide + ul').fadeOut(500);
				sessvars.findb = "closed";
			}
		});
		
		$('#nav_catalog h1.showhide').click (function(){
			if ($('#nav_catalog h1.showhide + ul').css('display')=='none')
			{
				$('#nav_catalog h1.showhide + ul').fadeIn(1000);
				sessvars.nav_catalog = "opened";
			}
			else
			{
				$('#nav_catalog h1.showhide + ul').fadeOut(500);
				sessvars.nav_catalog = "closed";
			}
		});
		


		//// жмём на ссылку "подобрать котлы" - отправляем форму
		$('#go_find_boilers').click (function(){
			$('#findb form').submit();
			return false;
		});






var flagResize = 0;

$("a.small_img").click(function(){
	var newtop = document.documentElement.scrollTop;
	var xh = document.documentElement.clientHeight;
	$("#img_dlg").css('top', newtop+'px');
	$("#img_dlg").css('height', xh+'px');
	$("#img_dlg").css('backgroundImage', 'url('+$(this).attr('href')+')');
	$("#img_dlg").fadeIn(800);
	flagResize = 1;
	return false;
});

$(window).resize(function(){
	if (flagResize == 1)
	{
		var newtop = document.documentElement.scrollTop;
		var xh = document.documentElement.clientHeight;
		$("#img_dlg").css('top', newtop+'px');
		$("#img_dlg").css('height', xh+'px');
	}
});

$("#img_dlg").click(function(){
	flagResize = 0;
	$(this).fadeOut(400);
});


if (sessvars.nt_cat) {var nt_cat_at = new Array(); nt_cat_at = sessvars.nt_cat;}
else {var nt_cat_at = new Array();}
	////// Расставляем плюсы/минусы
	$('ul.goods_catalog_main').children('li').each(
		function (i)
		{
			if ($(this).children('ul').length == 0)
			{
				$(this).children('img.ico_plus:first').css('cursor', 'default').attr('src','../i/trans.gif');
				$(this).children('img.ico_minus:first').css('cursor', 'default').attr('src','../i/trans.gif');
			}
			else
			{
				tmp_a_txt = $(this).children('a:first').text();
				$(this).children('a:first').replaceWith('<span class="cat_tog">' + tmp_a_txt + '</span>');
				tmp_a_txt = $(this).children('strong:first').text();
				$(this).children('strong:first').replaceWith('<span class="cat_tog"><strong>' + tmp_a_txt + '</strong></span>');
				if (IEVersion>0) {$(this).children('span:first').css('borderBottom','1px dotted #000').css('cursor','hand');}
				else {$(this).children('span:first').css('borderBottom','1px dotted #000').css('cursor','pointer');}
				
				
				var part_id = $(this).children('img.ico_plus:first').attr('id');

				if (nt_cat_at[part_id]=='1') {$(this).children('ul').show();$(this).children('img.ico_plus').toggle();$(this).children('img.ico_minus').toggle();}
			}
		}
	);




	////// Скрываем/показываем дочерние разделы
	$('span.cat_tog, img.ico_plus, img.ico_minus').click(function(){
		if (sessvars.nt_cat) {var nt_cat_a = new Array(); nt_cat_a = sessvars.nt_cat;}
		else {var nt_cat_a = new Array();}
		var part_id = $(this).parent('li').children('img.ico_plus:first').attr('id');
		if ($(this).parent('li').children('ul').css('display')=='block') {nt_cat_a[part_id]='0';}
		else {nt_cat_a[part_id]='1';}
		sessvars.nt_cat = nt_cat_a;
		$(this).parent('li').children('ul').slideToggle(400);
		$(this).parent('li').children('img.ico_plus').toggle();
		$(this).parent('li').children('img.ico_minus').toggle();
		//resizeIE6();
	});
	
	
	
	
	///// +/- в корзине для количества товара
	$('td.td_amount div.am_plus').click(function(){
		if (($(this).prev().attr('value') * 1) < 9999)
		{
			$(this).prev().attr('value',($(this).prev().attr('value') * 1) + 1);
			pricepo = $(this).parent('td').next().children('input.price_per_one').attr('value');
			am_g = $(this).prev().attr('value');
			if (is_int(pricepo * 1))
				{sss = pricepo * am_g;}
			else
				{sss = pricepo + '*' + am_g;}
			$(this).parent('td').next().children('span.cost_sum').text(sss);
			$(this).parent('td').next().children('input.inp_txt').attr('value',sss);
			get_busket_sum_cost();
		}
	});
	$('td.td_amount div.am_minus').click(function(){
		if (($(this).next().attr('value') * 1) > 1)
		{
			$(this).next().attr('value',$(this).next().attr('value') - 1);
			pricepo = $(this).parent('td').next().children('input.price_per_one').attr('value');
			am_g = $(this).next().attr('value');
			if (is_int(pricepo * 1))
				{sss = pricepo * am_g;}
			else
				{
					if (am_g == 1)
					{sss = pricepo;}
					else
					{sss = pricepo + '*' + am_g;}
				}
			$(this).parent('td').next().children('span.cost_sum').text(sss);
			$(this).parent('td').next().children('input.inp_txt').attr('value',sss);
			get_busket_sum_cost();
		}
	});
	
	$('td.td_amount input.inp_txt').numeric();
	$('td.td_amount input.inp_txt').focus(function(event){
		$(this).select();
	});
	$('td.td_amount input.inp_txt').keyup(function(event){
		if ($(this).attr('value')=="" || $(this).attr('value')=="0") {$(this).attr('value','1')}
		pricepo = $(this).parent('td').next().children('input.price_per_one').attr('value');
		am_g = $(this).attr('value');
		if (is_int(pricepo * 1))
			{sss = pricepo * am_g;}
		else
				{
					if (am_g == 1)
					{sss = pricepo;}
					else
					{sss = pricepo + '*' + am_g;}
				}
		$(this).parent('td').next().children('span.cost_sum').text(sss);
		$(this).parent('td').next().children('input.inp_txt').attr('value',sss);
		get_busket_sum_cost();
	});
	
	var is_has_bad_prices = 0;
	
	get_busket_sum_cost();

	function get_busket_sum_cost()
	{
		var sum_cost = 0;
		
		$('td.td_cost').each(
			function (i)
			{
				if (is_int($(this).children('span.cost_sum').text() * 1))
					{sum_cost += ($(this).children('span.cost_sum').text() * 1);}
				else
					{is_has_bad_prices = 1;}
			}
		);
		
		$('#basket_sum_cost').text(sum_cost);
	}

	if (is_has_bad_prices==1)
	{
		$('#sum_price_warning').fadeIn(2000);
	}






	});
