$(document).ready(function(){
		if ($.browser.msie && $.browser.version == 6) {
			$(document).pngFix();
			$('.printonly').hide();
		}
		
		if($('.card_sh .right table td .lnk').length>0){
			$('.card_sh .right table td .lnk').after('<sup><img src="/i/img/qv.png"></sup>')
		}
		if($('.table_sh').length>0){
			$('.table_sh .table-head th:first').css({'text-align':'left','vertical-align':'middle'})
			$.each($('.table_sh tr'),function(i,n){
				$(n).find('td:eq(1),td:eq(2),td:eq(3)').css({'text-align':'center'})							  
			  /* $(n).find('td:eq(2)').css({'padding-left':'40px'})
			   $(n).find('td:eq(1)').css({'padding-left':'30px'})*/
		   })
		}
		/*if($('.leftc ul').length>0){
			$('.leftc li.level2>span>a').click(function(){return false})	
		}*/
		if($('.center_sub ul').length>0){
			//$('.center_sub li.level1>span>a').click(function(){return false})	
		}
		var activeIndex=1;
		var activeMode;

		if (window.location.hash != '') {
			var param = window.location.hash;
			if ((pos = param.indexOf('#saving')) >= 0)
				activeIndex = 2;
		}
		promoInit();

		function promoInit(){

			updatePromo();
		
			$(".promo h2.marine").hover(
				function () {
				$(".promo h2.marine").removeClass('active');
				$(this).addClass('active');
				id = $(this).attr('id');
				id = id.substr(1);
				//window.location.hash = '#products-block-'+id;
				activeIndex = id;
				updatePromo();
				return false;
					
			},
				function () {
					$(".promo h2.marine").removeClass('active');
					$(".promo #p"+activeIndex).addClass("active");
			});			
			
			/*$(".promo #p1 a").click(function(){
				window.location.hash = '#compensation';
				activeIndex = 1;
				updatePromo();
				return false;
			});
			$(".promo #p2 a").click(function(){
				window.location.hash = '#saving';
				activeIndex = 2;
				updatePromo();
				return false;
			});*/
			
			/* $(".promo h2 a").click(function(){
				id = $(this).parent().attr('id');
				id = id.substr(1);
				window.location.hash = '#products-block-'+id;
				activeIndex = id;
				updatePromo();
				return false;
			}); */

			$("#products-block-1 .description a").click(function(){
				$("#products-block-1 .installations").fadeIn(300);
				return false;
			});		
			
			$("#products-block-1 .close-button").click(function(){
				$("#products-block-1 .installations").fadeOut(300);
				return false;
			});							
		}

		function updatePromo() {

			$(".promo .marine").removeClass("active");
			$("#p"+activeIndex).addClass("active");
			
			activeMode = $("#p"+activeIndex).attr("id");
			
			/*$("#products-block-1").hide(); 	
			$("#products-block-2").hide();*/
			$(".promo-blocks").hide();
				
			$("#products-block-"+activeIndex).fadeIn(0);
			$("#products-block-"+activeIndex+" .description a, #p"+activeIndex+">a").click(function(){ 
				$("#products-block-"+activeIndex+" .installations").fadeIn(300); 
				
				$(".promo .marine").each(function(){
					if($(this).attr('id') != "p"+activeIndex){
						$(this).hide();	
					}
				});
				/* if(activeIndex==2){
					$("#p"+(activeIndex-1)).hide();
				}else {
					$("#p"+(activeIndex+1)).hide();
				} */
				
				return false;
				});
							
			$("#products-block-"+activeIndex+" .close-button").click(function(){
				//$("#p"+(activeIndex-1)).show(); 
				//$("#p"+(activeIndex+1)).show();
				$("#p1, #p2, #p3, #p4").show();
				$("#products-block-"+activeIndex+" .installations").fadeOut(300);
				return false; 
				});	
				
			$('.promo').attr('style', 'background:#fff url(/i/bg/promo-bg-'+activeIndex+'.jpg) no-repeat;');	
		}


		/*$(".product-info TD:nth-child(3)")
		
			$(".product-info TR").hover(
				function () { $(this).css("background", "#E7FCFB"); }, 
				function () { $(this).css("background", "none");
			});		*/

		imgPreload();
		darkbox();

		techInit();

		if (window.location.hash != '') {
			var param = window.location.hash;
			param = param.substr(1);
			param = parseInt(param);
			$('#p' + param + ' a').trigger('click');
		}

		function techInit(){

			$('.tech_promo').attr('style', 'background:#fff url(/i/bg/promo-bg-1.jpg);');

			updateTech();

			/* $(".tech_promo h2.marine").hover(
				function () { if( $(this).attr("id") != activeMode) $(this).addClass('active'); }, 
				function () { if( $(this).attr("id") != activeMode) $(this).removeClass('active');
			}); */
			
			clickOn = 0;

			$(".tech_promo h2.marine").hover(
				function () {
					if(clickOn != 1){
						$(".tech_promo h2.marine").removeClass('active');
						$(this).addClass('active');
					}
			},
				function () {
					if(clickOn != 1){
						$(".tech_promo h2.marine").removeClass('active');
						$(".tech_promo  #p"+activeIndex).addClass("active");
					}
			});


			$(".tech_promo .marine a").click(function(){
				clickOn = 1;
				var id = $(this).parent().attr('id');
				id = id.substr(1);
//				if (id == activeIndex)
//					return false;
				activeIndex = id;
				updateTech();
				$(".marine").hide();
				$("#p" + id).show();
				$("#tech-block-" + id + " .installations").fadeIn(300);
				return false;
			});
/*
			$(".tech_block .description a").click(function(){
				var id = $(this).parent().parent().attr('id');
				id = id.substr(11);
				$(".marine").hide();
				$("#p" + id).show();
				$("#tech-block-" + id + " .installations").fadeIn(300);
				return false;
			});		
*/

			$(".tech_block .close-button").click(function(){
				$(".tech_block .installations").fadeOut(300);
				$(".marine").show();
				clickOn = 0;
				return false;
			});							
		}

		function updateTech() {

			$(".tech_promo .marine").removeClass("active");
			$("#p" + activeIndex).addClass("active");

			activeMode = $("#p" + activeIndex).attr("id");

			$(".tech_block").hide(); 	
			$("#tech-block-" + activeIndex).fadeIn();

			$('.tech_promo').attr('style', 'background:#fff url(/i/bg/promo-bg-' + activeIndex + '.jpg);');
		}

	if ($('.table_filter').length > 0){

                //tables with scroll: begin
                var oTable = $('.table_filter').dataTable( {
                    "oLanguage": {
                        "sSearch": "Search all columns:",
                        "sZeroRecords": "Извините, ничего не найдено",
                        "sInfo": "Showing _START_ to _END_ of _TOTAL_ records",
                        "sInfoEmpty": "Showing 0 to 0 of 0 records",
                        "sInfoFiltered": "(filtered from _MAX_ total records)"
                    },
                       "sScrollY": "200px",
                       "bPaginate": false,
                       "bSort" : false
                } );

                $('input.search_init').keyup(function(){
                        oTable.fnFilter( this.value, $('input.search_init').index(this));
                });
                //tables with scroll: end
	}
 
});

$(function() {
    // Знает ли браузер о onbeforeprint (IE6, IE7 иIE8)
    if (window.onbeforeprint !== undefined) {
        // Вешаем обработчик перед печатью (добавление ссылок)
        window.onbeforeprint = changeStyleIE7;
        // Удаляем обработчик после печати
        window.onafterprint = resetStyleIE7;
    }
});

function changeStyleIE7() {
    $('.dataTables_scrollBody').addClass('special-print-style');
}
function resetStyleIE7() {
    $('.dataTables_scrollBody').removeClass('special-print-style');
}

function imgPreload()
{
	$(".promo h2.marine").each(function(){
		var id = $(this).attr('id');
		id = id.substr(1);
		img = new Image();
		img.src = '/i/bg/promo-bg-' + id + '.jpg';
		//console.log('Preload: /i/bg/promo-bg-' + id + '.jpg');
	});


	$(".tech_promo .marine").each(function(){
		var id = $(this).attr('id');
		id = id.substr(1);
		img = new Image();
		img.src = '/i/bg/promo-bg-' + id + '.jpg';
		//console.log('Preload: /i/bg/promo-bg-' + id + '.jpg');
	});
}


function darkbox()
{
		var num = 0;
		$('ul.photos a img').each(function() {
			$(this).attr('id', 'img' + num);
			num++;
		});

		// Вешаем обработчик на ссылки с нужным классом
		$('ul.photos a img').click(function() {
				var id = $(this).attr('id');
				id = id.substr(3);

				var link = $(this).parent();
		
				if(!$('div.darkbox-frame').length) {
					// Если попап прежде не вызывался,
					// создаём его и цепляем к BODY
					darkbox = $('<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><div class="darkbox-button"></div></div></div>').appendTo('body');
				}

				// Клонируем попап,
				// прицепляем клон к BODY и показываем его
				var frame = darkbox.clone().appendTo('body').addClass('darkbox-frame-on');
				var shadow = frame.find('div.darkbox-shadow').animate({opacity:0.6},300);
				var canvas = frame.find('div.darkbox-canvas');
				var button = frame.find('div.darkbox-button');

				// Цепляем к попапу картинку и ждём её загрузки
				var image = $('<img src="'+ link.attr('href') +'" alt="'+ link.attr('title') +'"/>');

				image.appendTo(canvas);

				image.click(function() {
					var count = $('ul.photos a img').length;
					if (id < count - 1) {
						var newsrc = $('#img' + (parseInt(id) + 1)).parent().attr('href');
						id = $('#img' + (parseInt(id) + 1)).attr('id');
						id = id.substr(3);
					}
					else {
						var newsrc = $('#img0').parent().attr('href');
						id = 0;
					}
					$(image).attr({src: newsrc});
					//	console.log('image: ' + imageWidth + 'x' + imageHeight + ' frame: ' + frameWidth + 'x' + frameHeight);
				});

				image.load(function(){

					var imageWidth = image.width();
					var imageHeight = image.height();
					var frameWidth = frame.width()-40;
					var frameHeight = frame.height()-40;

					// Вписываем картинку в размер окна,
					// если она шире, чем окно
					if(imageWidth > frameWidth) {

						imageWidth = frameWidth;
						image.width(imageWidth);					
						while(image.height() > frameHeight) {
							image.width(imageWidth);
							imageWidth--;
						}

						imageHeight = image.height();
					}

					// Вписываем картинку в размер окна,
					// если она выше, чем окно
					if(imageHeight > frameHeight) {

						imageHeight = frameHeight;
						image.height(imageHeight);						
						while(image.width() > frameWidth) {
							image.height(imageHeight);
							imageHeight--;
						}

						imageWidth = image.width();
					}

					// Анимируем загрузчик до размеров картинки
					// и одновременно смещаем к центру
					canvas.addClass('darkbox-canvas-load').animate({

						width:imageWidth,
						marginLeft:-imageWidth/2,
						height:imageHeight,
						marginTop:-imageHeight/2

					},500,function() {

						// После завершения анимации показываем кнопку и картинку
						canvas.addClass('darkbox-canvas-done');
						button.addClass('darkbox-button-on');
						button.addClass(navigator.platform.toLowerCase().indexOf('mac')+1?'darkbox-button-left':'darkbox-button-right');

						image.animate({opacity:1},500,function() {

							// Вешаем обработчики закрытия
							shadow.click(closer);
							button.click(closer);

						});
					});
				});


				// Функция закрытия попапа
				var closer = function() {
					canvas.remove();
					shadow.animate({opacity:0},300,function() {
						frame.remove();
					});
				}

				// Внимательно слушаем клавишу Esc
				$(document).keydown(function(e) {
					if(e.which==27) closer();
				});

				return false;
			});

		
}





/*
		function techInit(){
			$('.tech_promo').attr('style', 'background:#fff url(/i/bg/promo-bg-1.jpg) no-repeat;');

			updateTech();

			$(".tech_promo h2.marine").hover(
				function () { if( $(this).attr("id") != activeMode) $(this).addClass('active'); }, 
				function () { if( $(this).attr("id") != activeMode) $(this).removeClass('active');
			});

			$(".tech_promo .marine a").click(function(){
				var id = $(this).parent().attr('id');
				id = id.substr(1);
				if (id == activeIndex)
					return false;
				activeIndex = id;
				updateTech();
				return false;
			});

			$(".tech_block .description a").click(function(){
				var id = $(this).parent().parent().attr('id');
				id = id.substr(11);
				$(".marine").hide();
				$("#p" + id).show();
				$("#tech-block-" + id + " .installations").fadeIn(300);
				return false;
			});		

			$(".tech_block .close-button").click(function(){
				$(".tech_block .installations").fadeOut(300);
				$(".marine").show();
				return false;
			});							
		}


		function updateTech() {

			$(".tech_promo .marine").removeClass("active");
			$("#p" + activeIndex).addClass("active");

			activeMode = $("#p" + activeIndex).attr("id");

			$(".tech_block").hide(); 	
			$("#tech-block-" + activeIndex).fadeIn();

			$('.tech_promo').attr('style', 'background:#fff url(/i/bg/promo-bg-' + activeIndex + '.jpg) no-repeat;');
		}
*/

            $( function() {
                $("#tNY td").bind("mouseover", function() {
                    if($(this).hasClass("tn1")) {
                        $(".tn1").addClass("hover");
                    }
                    if($(this).hasClass("tn2")) {
                        $(".tn2").addClass("hover");
                    }
                    if($(this).hasClass("tn3")) {
                        $(".tn3").addClass("hover");
                    }
                });
                $("#tNY td").bind("mouseout", function() {
                    if($(this).hasClass("tn1")) {
                        $(".tn1").removeClass("hover");
                    }
                    if($(this).hasClass("tn2")) {
                        $(".tn2").removeClass("hover");
                    }
                    if($(this).hasClass("tn3")) {
                        $(".tn3").removeClass("hover");
                    }
                });
            });

