﻿
	var resizeTimer = null;

	var IEVersion=/*@cc_on function(){ switch(@_jscript_version){ case 1.0:return 3; case 3.0:return 4; case 5.0:return 5; case 5.1:return 5; case 5.5:return 5.5; case 5.6:return 6; case 5.7:return 7; }}()||@*/0;



	function LargeImageToCenter(height, width){

		var w = $(window);


		if ($.browser.msie)
			h = w.height()
		else
			h = window.innerHeight;		


		// Move large photo to center of screen
		$('#full-size-photo-block').animate({"top": ((h - height)/2 - 75 + w.scrollTop() + "px"), "left": ((w.width() - width)/2 + w.scrollLeft() + "px")});

		}



	// Hide large photo
	function HideLargePhoto(){
	
		// Remove previous large photo
		$('#full-size-photo-block').remove();


		// Remove cover
		$('#description').removeClass('opacity-40');


		// Unbind LargePhotoToCenter on resizing of window
		$(window).unbind('resize');


		// Unbind LargePhotoToCenter on resizing of window
		$(window).unbind('scroll');

		}



	// Recieve position of current large photo and direction and load next or previous one
	function ShowAnotherPhoto(pos, direction){

		if (direction == "next")
		{

			var next_pos = pos + 1;
			
			var next_name = $('.decor h3:eq('+next_pos+')').text();

			var next_path = $('.decor a:eq('+next_pos+')').attr('HREF');


			if ($.browser.msie)
			{
				$('#full-size-photo-block img').remove();

				$('<img SRC="'+next_path+'">').appendTo('#full-size-photo-block');
			}
			else
				$('#full-size-photo-block img').attr('SRC', next_path);


			$('#full-size-photo-block h3').text(next_name);


			SetUpArrows(next_pos);

		}
		else
		{

			var prev_pos = pos - 1;

			var prev_name = $('.decor h3:eq('+prev_pos+')').text();

			var prev_path = $('.decor a:eq('+prev_pos+')').attr('HREF');


			// IE don't allow change image SRC, so I remove image and append a new one with neccessary SRC
			if ($.browser.msie)
			{
				$('#full-size-photo-block img').remove();

				$('<img SRC="'+prev_path+'">').appendTo('#full-size-photo-block');
			}
			else
				$('#full-size-photo-block img').attr({"SRC": prev_path});


			$('#full-size-photo-block h3').text(prev_name);


			SetUpArrows(prev_pos);

		}

	}



	// Draw arrows to next and previous photo
	function SetUpArrows(pos){


		// Remove arrows
		$('#left-arrow').remove();

		$('#right-arrow').remove();


		// Define next and previous positions
		var prev_pos = pos - 1;

		var next_pos = pos + 1;


		// Define paths to next and previous image
		var prev_path = $('.decor a:eq('+prev_pos+')').attr('HREF');

		var next_path = $('.decor a:eq('+next_pos+')').attr('HREF');


		// If previous image exists, show arrow, pointing to it
		if (prev_pos != -1)
		$('<a HREF=\"'+prev_path+'\" ID=\"left-arrow\">&larr;</a>').appendTo("#full-size-photo-block-content");


		// If next image exists, show arrow, pointing to it
		if (next_path)
		$('<a HREF=\"'+next_path+'\" ID=\"right-arrow\">&rarr;</a>').appendTo("#full-size-photo-block-content");


		// Bind ShowAnotherPhoto on left arrow
		$('#left-arrow').click(function(e){

			e.preventDefault();

			ShowAnotherPhoto(pos, "previous");

			});


		// Bind ShowAnotherPhoto on right arrow
		$('#right-arrow').click(function(e){

			e.preventDefault();

			ShowAnotherPhoto(pos, "next");

			});

		}



	// Recieve path to large image, coords of small image and enlarge small image to center of screen
	function ShowLargeImage(pos, x, y, margin_left, margin_top, path, alt, height, width){

		// Hide previous large photo
		HideLargePhoto();


		// Create large photo and set it's coords
		$("<div ID=\"full-size-photo-block\"><div CLASS=\"fixpng\" ID=\"full-size-photo-block-shadow\"></div><div ID=\"full-size-photo-block-content\"><div ID=\"full-size-photo-block-bg\"></div><h3>"+alt+"</h3></div><img CLASS=\"small-image\" SRC=\""+path+"\"></div>").css({"left": x-margin_left, "top": y-margin_top}).appendTo("body");


		// Append arrows to next and previous photos
		SetUpArrows(pos);


		// Bind animated enlargment on large image load
		$('#full-size-photo-block img').load(function(){

			// Set opacity to #description 
			$('#description').addClass('opacity-40');


			//Move large image to center of screen
			LargeImageToCenter(height, width);


			// Enlarge photo with animation
			$(this).animate({"height": "450px", "width": "396px"}, function(){

				// Prevent closing large photo when click it
				$('#full-size-photo-block img').click(function(e){

					e.stopPropagation();

					});


				// Prevent closing large photo content when click it
				$('#full-size-photo-block-content').click(function(e){e.stopPropagation();});


				// Bind LargePhotoToCenter on resizing of window
				$(window).bind('resize', function() { 

					if (resizeTimer) clearTimeout(resizeTimer);

					resizeTimer = setTimeout('LargeImageToCenter(450, 396)', '100');

					});


				// Bind LargePhotoToCenter on scroll
				$(window).bind('scroll', function() { 

					if (resizeTimer) clearTimeout(resizeTimer);

					resizeTimer = setTimeout('LargeImageToCenter(450, 396)', '100');

					});


				// Show content of the large photo block
				$('#full-size-photo-block-content').show();


				// Show content of the large photo block
				$('#full-size-photo-block-shadow').show();

				});

			});

		}



	// Initialization
	$(document).ready(function(){

		// Делаем подсветку в меню для говнобраузеров IE и Mozilla, неспособных обработать ховер для блока
		if ($.browser.msie || $.browser.mozilla)
		{

			$('.sub-menu-ul').hover(function(){

				// Определяем порядковый номер элемента начиная с 1
				var pos = $('.sub-menu-ul').index(this) + 1;


				// Формируем имя класса
				var className = 'ul_' + pos + '_hover';


				// Добавляем этот класс элементу
				$(this).addClass(className);
				
				}, function(){
				
				// Определяем порядковый номер элемента начиная с 1
				var pos = $('.sub-menu-ul').index(this) + 1;


				// Формируем имя класса
				var className = 'ul_' + pos + '_hover';


				// Удаляем этот класс у элемента
				$(this).removeClass(className);			
				
				});

			}



		// Делаем подсветку ссылки на прайс для говнобраузера IE
		if ($.browser.msie)
		{

			$('#price').hover(function(){
				
				$(this).addClass('price-hover');

				}, function(){
				
				$(this).removeClass('price-hover');

				});

		}



		// Click on page closes prices window
		$().click(function(){

			// Hide price block
			$('#download-block-bg').addClass('hidden');


			// Hide previous large photo
			HideLargePhoto();

			});



		// Pressing Esc closes popup windows
		$(window).keydown(function(e){

			if (e.which == 27)
				$().trigger('click');

			});



		// Click on price-list link opens windows of price-lists
		$('#price').click(function(e){

			$('#download-block-bg').toggleClass('hidden');

			e.stopPropagation();
			
			});



		// Prevent closing of popup window when user click it
		$('#download-block-bg').click(function(e){

			e.stopPropagation();

			});



		// Lightbox for decoration of pvc-panels
		$('.decor img').click(function(e){

			e.preventDefault();


			e.stopPropagation();


			var path = $(this).parent().attr("HREF");


			var left = $(this).offset().left;


			var top = $(this).offset().top;

			var alt = $(this).attr("ALT");


			// Define position of clicked image
			var pos = $('.decor img').index(this);


			// Show large photo. Arguments: 
			// 		pointer to small image, 
			// 		coords of small image, 
			// 		top and left margin of large image in it's container, 
			// 		path to large photo, 
			// 		height and width of large-photo
			ShowLargeImage(pos, left, top, 48, 5, path, alt, 450, 396);

			});

	});