!window.jQuery && document.write('<script src="/js/jqmin.js"><\/script>');
var blank = new Image();
blank.src = '/img/blank.gif';
function fixPng(png) {
   var src = png.src;
   if (!png.style.width) { png.style.width = $(png).width(); }
   if (!png.style.height) { png.style.height = $(png).height(); }
   png.onload = function() { };
   png.src = blank.src;
   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
 }
function hide_message() {
	$('div.message_board')
		.css('opacity', '1')
		.animate({
			opacity:0
		}, 200, function() {
			$(this).css('display', 'none');
		});
}
function show_message() {
	$('div.message_board')
		.css('opacity', '0')
		.css('display', 'block')
		.animate({
			opacity:1
		}, 200);
}
$(document).ready(function() {
	var ie = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	var ie7 = (/MSIE (7)/.test(navigator.userAgent) && navigator.platform == "Win32");
	var ie8 = (/MSIE (8)/.test(navigator.userAgent) && navigator.platform == "Win32");
	var $pm_hidden_background = $('div.pm_hidden_background');
	var $topmenu = $('div.top_nav_menu_element');
	var big_img = $('table.big_img').position();
	var left_delim = 215;
	var FF = false, Gecko = false;
	var $top_nav_menu_sub = $('div.top_nav_menu_sub');
	var $red_line = $('div.red_line');
	var active_menu = false;
	var left_pos = 0,red_line_height = 0;
	var doc_width = $(window).width();
	var hidden_back_left = 0;
	var middle_position = $('div.middle').position();
	if (ie) {
		$('img[src$=.png]').each(function() {
			if (!this.complete) {
				this.onload = function() {
					fixPng(this)
				};
			} else {
				fixPng(this);
			}
		});
		var t_top = $pm_hidden_background.position();
	}
	if (ie || ie7) {
		$pm_hidden_background.css('top', '160');
		if ($('div.top_nav_menu_sub:last').length) {
			$('div.top_nav_menu_sub:last').addClass('last_top_nav_menu_sub');
		}
	}
	if (ie8) {
		if ($('div.top_nav_menu_sub:last').length) {
			$('div.top_nav_menu_sub:last').addClass('last_top_nav_menu_sub');
		}
	}


	/*if ( /chrome/.test(navigator.userAgent.toLowerCase()) ) {
	 left_delim = 250;
	 alert(2);
	 }*/
	/*if (window.navigator.userAgent.indexOf ("WebKit") >= 0) {
	 Gecko = true;
	 left_delim = 215;
	 }else if (window.navigator.userAgent.indexOf ("Firefox/4") >= 0) {
	 FF = true;
	 Gecko = false;
	 left_delim = 215;
	 }else if (window.navigator.userAgent.indexOf ("Gecko") >= 0) {
	 Gecko = true;
	 //left_delim = 250;
	 }*/
	hidden_back_left = big_img.left + left_delim;

	$pm_hidden_background.css('left', hidden_back_left + 'px').width(950 - hidden_back_left + middle_position.left);
	$(window).resize(function() {
		big_img = $('table.big_img').position();
		hidden_back_left = big_img.left + left_delim;
		$pm_hidden_background.css('left', hidden_back_left + 'px').width(950 - hidden_back_left + middle_position.left);
	});
	$pm_hidden_background.hover(function() {
		active_menu = true;
	}, function () {
		active_menu = false;
	});
	$topmenu.hover(function() {
		var $sub = $(this).find('div.top_nav_menu_sub');
		if ($sub.length) {
			doc_width = $(window).width();

			$pm_hidden_background.show();
			red_line_height = $sub.height() + 108;
			if (ie && red_line_height > 460) {
				red_line_height = 460;
			}
			left_pos = $(this).find('a.top_nav_menu_a').position();
			$sub.show().css('left', (left_pos.left - 30) + 'px').next().addClass('top_nav_menu_a_hover');
			$red_line.show().css('left', left_pos.left).css('height', (red_line_height) + 'px');
		}
	}, function() {
		//return true;
		var $sub = $(this).find('div.top_nav_menu_sub');
		if ($sub.length) {
			$red_line.hide();
			$pm_hidden_background.hide();
			$sub.hide().next().removeClass('top_nav_menu_a_hover');
		}
	});

	var $right_search = $('div.right_search span.button');
	$right_search.click(function() {
		var search_string = $('div.right_search input').val();
		$.post(
				'/ajax.php',
				{ ajax:true, project_name: search_string },
				function (data) {
					$('div.middle_left').html(data);
				}
		);
	});
	$('div.right_search_navig a').live('click', function() {
		var search_string = $('div.right_search input').val();
		var start = parseInt($(this).attr('start'));
		$.post(
				'/ajax.php',
				{ ajax:true, project_name: search_string, start: start },
				function (data) {
					$('div.middle_left').html(data);
				}
		);
	});
	$('div.right_search input').keyup(function(e) {
		if (e.keyCode == 13) {
			$right_search.click();
		}
	});

	if ($('div.area_slider').length) {

		var $area_from = $('div.projects_search span.area_from'), $area_to = $('div.projects_search span.area_to'),
				$show_search_results = $('span.show_search_results'), $search_results = $('div.search_results'),
				$search_navigation = $('div.search_navigation a'), $left_slider = $('div.area_slider a').eq(0),
				$right_slider = $('div.area_slider a').eq(1), value = 0, $div_left_slider = $('div.slide_value div.hide_area_from'),
				$div_right_slider = $('div.slide_value div.hide_area_to'), $staple = $('div.staple'),
				$staple_left = $('div.staple div.staple_left'), $staple_right = $('div.staple div.staple_right'),
				$staple_center = $('div.staple div.staple_center'), $staple_count = $('div.staple div.projects_in_range');

		$area_from.show();
		$area_to.show();
		$('div.material_type input[type=checkbox]').change(function() {
			var type = '', count_area = $area_from.html() + '-' + $area_to.html();
			if ($('div.material_type input[type=checkbox]:checked').length == 0) {
				$('div.material_type input[name=gasobeton]').attr('checked', ' ');
			}
			$('div.projects_search input[type=checkbox]').each(function(index) {
				if ($(this).attr('checked')) {
					if (index != 0) {
						type += ',';
					}
					type += $(this).attr('name');
				}
			});
			$staple_count.html('');
			$.post(
					'/ajax.php',
					{ ajax: true, type: type, count_area: count_area },
					function(data) {
						$staple_count.html(data);
					}
			);
			$show_search_results.click();
		});

		$('body').delegate('span.show_search_results', 'click', function(e) {
			var page = 0;
			if ( window.location.hash.length ) {
				var hash = parse_hash( window.location.hash );
				page = hash.page;
			}

			$search_results.html('<img src=\"/img/loading.gif\" />');
			var type = '', type_bin = 0;
			$('div.projects_search input[type=checkbox]').each(function(index) {
				if ( $(this).attr('checked') ) {
					if ( type != '' ) {
						type += ',';
					}
					type += $(this).attr('name');
					type_bin += Math.pow( 2, index );
				}
			});
			var area = $area_from.html() + '-' + $area_to.html();
			$.post(
					'/ajax.php',
					{ ajax: true, type: type, area: area, start: page },
					function(data) {
						$search_results.html(data);
						$('div.hide_when_done').hide();
						if (ie8) {
							if ($('#ie8upd').height() < $('div.middle').height()) {
								$('#ie8upd').height($('div.middle').height() + 296);
							} else {
								$('#ie8upd').height('auto');
							}
						}
						page = parseInt( $('div.search_navigation span').html() ) - 1;
						window.location.hash = '#area=' + area + ';' + type_bin + ';' + page;
					}
			);
		});


		var $div_area_slider = $("div.area_slider");
		$div_area_slider.slider({
					range: true,
					min: 0,
					max: 1500,
					step: 10,
					values: [ parseInt($area_from.html()), parseInt($area_to.html()) ],
					create: function(event, ui) {
						var hash = {};
						if ( window.location.hash.length ) {
							$('div.projects_search input[type=checkbox]').attr( 'checked', '' );
							hash = parse_hash( window.location.hash );
							var string_type = hash.type.toString( 2 ).split("").reverse().join("");
							$('div.projects_search input[type=checkbox]').each( function(index) {
								if ( '1' == string_type.charAt( index ) ) {
									$(this).attr( 'checked', true );
								}
							});
							$area_from.html( hash.area_from );
							$area_to.html( hash.area_to );
							$div_area_slider.slider( 'values', 0, hash.area_from );
							$div_area_slider.slider( 'values', 1, hash.area_to );
						}
						$left_slider = $('div.area_slider a').eq(0);
						$right_slider = $('div.area_slider a').eq(1);
						value = parseInt(100 * $area_from.html() / 1500);
						$div_left_slider.width(( value + 1 ) + '%');
						$staple.css('left', value + '%');
						$area_from.css('left', ( value - 8 ) + '%');
						value = parseInt(100 * parseInt($area_to.html()) / 1500);
						$div_right_slider.width(( 100 - value - 1 ) + '%');
						$area_to.css('left', ( value + 2 ) + '%');
						value = parseInt(100 * ( parseInt($area_to.html() - $area_from.html()) ) / 1500);
						$staple.css('width', ( value + 2 ) + '%').show();
						value = parseInt(( $staple.width() - 19 ) / 2);
						$staple_left.width(value);
						$staple_right.width(value);

						var type = '', count_area = $area_from.html() + '-' + $area_to.html();

						$('div.projects_search input[type=checkbox]').each(function(index) {
							if ( $(this).attr('checked') ) {
								if ( type != '' ) {
									type += ',';
								}
								type += $(this).attr('name');
							}
						});
						$staple_count.html('');
						if ( window.location.hash.length ) {
							count_area = hash.area_from + '-' + hash.area_to;
						}
						$.post(
								'/ajax.php',
								{ ajax: true, type: type, count_area: count_area },
								function(data) {
									$staple_count.html(data);
								}
						);
						if ( window.location.hash.length ) {
							$show_search_results.click();
						}
					},
					slide: function(event, ui) {
						value = parseInt(100 * ui.values[0] / 1500);
						$staple.css('left', value + '%');
						$area_from.html(ui.values[0]).css('left', ( value - 8 ) + '%');
						$div_left_slider.width(( value + 1 ) + '%');
						value = parseInt(100 * ( ui.values[1] / 1500 ));
						$div_right_slider.width(( 100 - value - 1 ) + '%');
						$area_to.html(ui.values[1]).css('left', ( value + 2 ) + '%');
						value = parseInt(100 * ( parseInt(ui.values[1] - ui.values[0]) ) / 1500);
						$staple.css('width', ( value + 2 ) + '%').show();
						value = parseInt(( $staple.width() - 19 ) / 2);
						$staple_left.width(value);
						$staple_right.width(value);
					},
					stop: function(event, ui) {
						var type = '', count_area = ui.values[0] + '-' + ui.values[1];
						$('div.projects_search input[type=checkbox]').each(function(index) {
							if ($(this).attr('checked')) {
								if (index != 0) {
									type += ',';
								}
								type += $(this).attr('name');
							}
						});
						$staple_count.html('');
						$.post(
								'/ajax.php',
								{ ajax: true, type: type, count_area: count_area },
								function(data) {
									$staple_count.html(data);
								}
						);
						$show_search_results.click();
					}
				});

		$('body').delegate('div.search_navigation a', 'click', function(e) {
			var type = $(this).attr('type'), area = $(this).attr('area'), start = $(this).attr('start');
			$search_results.html('<img src=\"/img/loading.gif\" />');
			$.post(
					'/ajax.php',
					{ ajax: true, type: type, area: area, start: start },
					function(data) {
						$search_results.html(data);
						$('div.hide_when_done').hide();
						var page = parseInt( $('div.search_navigation span').html() ) - 1;
						var type_bin = 0;
						$('div.projects_search input[type=checkbox]').each(function(index) {
							if ($(this).attr('checked')) {
								type_bin += Math.pow( 2, index );
						}});
						var area = $area_from.html() + '-' + $area_to.html();

						window.location.hash = '#area=' + area + ';' + type_bin + ';' + page;
					}
			);

			e.preventDefault();
		});

	}
	$('input.noemail').click(function() {
		if ($(this).attr('checked')) {
			$('div.contact_form input[name=email]').attr('disabled', 'disabled').val('');
		} else {
			$('div.contact_form input[name=email]').attr('disabled', false);
		}
	});
	$('div.contact_form input.button').click(function() {
		var error_message = '', focus_element;
		if ($('div.contact_form input[name=name]').val().length < 2) {
			error_message = 'Введите имя';
			focus_element = 'div.contact_form input[name=name]';
		} else if ($('div.contact_form input[name=phone]').val().length < 2) {
			error_message = 'Введите номер телефона';
			focus_element = 'div.contact_form input[name=phone]';
		} else if ($('div.contact_form input[name=email]').val().length < 2 && !$('div.contact_form input.noemail').attr('checked')) {
			error_message = 'Введите адрес электронной почты';
			focus_element = 'div.contact_form input[name=email]';
		} else if ($('div.contact_form textarea[name=timeplace]').length && $('div.contact_form textarea[name=timeplace]').val().length < 3) {
			error_message = 'Введите время и место строительства';
			focus_element = 'div.contact_form textarea[name=timeplace]';
		} else if ($('div.contact_form textarea[name=text]').val().length < 5) {
			error_message = 'Введите дополнительную информацию';
			focus_element = 'div.contact_form textarea[name=text]';
		}
		if (error_message != '') {
			show_error_message(error_message, focus_element);
			return false;
		}

	});
	$('body').append('<div class="message_board"></div>');
	if ($('input.send_file').length) {
		var $self = $('input.send_file').removeClass('send_file');
		$('head').append('<script type="text/javascript" src="/js/jInputFile.js"><\/script>');
		$self.jInputFile();
	}

	function show_error_message(message, focus_element) {

		var screen_width = $(window).width();
		var screen_height = $(window).height();
		var screen_scroll = $(window).scrollTop();

		$("div.message_board")
				.html(message)
				.css('left', parseInt(screen_width / 2) - 100)
				.css('top', parseInt(screen_height / 2) - 50 + screen_scroll);

		show_message();
		setTimeout(hide_message, 2000);
		setTimeout(function() {
			$(focus_element).focus();
		}, 2200);
		return true;
	}
	var block_height = $('div.content').outerHeight();
	$('div.info_block').css('height', block_height );
	
	$(function (){
		$('#slides').slides({
			preload: true,
			preloadImage: '/img/loading.gif',
			play: 2000,
			slideSpeed: 10,
			effect: 'fade',
			hoverPause: false
		});
	});
	
	
	$(function (){
		$('#slides1').slides({
			preload: true,
			preloadImage: '/img/loading.gif',
			play: 2000,
			slideSpeed: 10,
			effect: 'fade',
			hoverPause: false
		});
	});
});

function parse_hash( hash )
{

	var area_from = 0, area_to = 1500, type = 15, page = 0, temp = [], result = {};

	hash = hash.substring( 6 );
	temp = hash.split( ';' );
	if ( 3 == temp.length ) {
		temp[0] = temp[0].split( '-' );
		if ( 2 == temp[0].length ) {
			area_from = parseInt( temp[0][0] );
			area_to = parseInt( temp[0][1] );
			type = parseInt( temp[1] );
			page = parseInt( temp[2] );
		}

	}
	result = {
		area_from: area_from,
		area_to: area_to,
		type: type,
		page: page
	};

	return result;
}

