﻿function BuildPage() {
//	Раздвижные иконки в меню
	if ($('#left-menu')) {
		$('#left-menu > ul > li').each(function(i){
			if ($(this).children('ul').length>0) {
				//Вставляем иконки
				var inhtml='<span>&nbsp;</span>'+$(this).html();
				$(this).html(inhtml);
				//Обрабатыаем клик по иконке
				$(this).children('span').click(function(){
					if ($.browser.msie && $.browser.version < 8) {
						if ($(this).parent().hasClass('active')) {$(this).parent().removeClass('active')}
						else {
//							$('#left-menu').find('ul > li').each(function(k){if ($(this).hasClass('active')) {$(this).removeClass('active')};});
							$(this).parent().addClass('active');
						};
						$('#footer').css('display','none');
						$('#footer').css('display','block');
						$('#gruzovik').css('display','none');
						$('#gruzovik').css('display','block');
					}
					else {
						if ($(this).parent().hasClass('active')) {
							$(this).parent().children('ul').slideUp('300');
							$(this).parent().removeClass('active');
						}
						else {
//							$('#left-menu').find('ul > li').each(function(k){
//								if ($(this).hasClass('active')) {
//									$(this).children('ul').slideUp('300');
//									$(this).removeClass('active');
//								};
//							});
							$(this).parent().children('ul').slideDown('300');
							$(this).parent().addClass('active');
						};
					};
				});
			};
		});
//		Второй подуровень - потом как-нибудь оптимизировать будем
		$('#left-menu > ul > li > ul > li').each(function(i){
			if ($(this).children('ul').length>0) {
				//Вставляем иконки
				var inhtml='<span>&nbsp;</span>'+$(this).html();
				$(this).html(inhtml);
				//Обрабатыаем клик по иконке
				$(this).children('span').click(function(){
					if ($.browser.msie && $.browser.version < 8) {
						if ($(this).parent().hasClass('active')) {$(this).parent().removeClass('active')}
						else {
//							$('#left-menu').find('ul > li').each(function(k){if ($(this).hasClass('active')) {$(this).removeClass('active')};});
							$(this).parent().addClass('active');
						};
						$('#footer').css('display','none');
						$('#footer').css('display','block');
						$('#gruzovik').css('display','none');
						$('#gruzovik').css('display','block');
					}
					else {
						if ($(this).parent().hasClass('active')) {
							$(this).parent().children('ul').slideUp('300');
							$(this).parent().removeClass('active');
						}
						else {
//							$('#left-menu').find('ul > li').each(function(k){
//								if ($(this).hasClass('active')) {
//									$(this).children('ul').slideUp('300');
//									$(this).removeClass('active');
//								};
//							});
							$(this).parent().children('ul').slideDown('300');
							$(this).parent().addClass('active');
						};
					};
				});
			};
		});
	};
//	Ставим отступы вокруг картинок в контенте
	if ($('#content-block').find('img')) {
		$('#content-block').find('img').each(function(i){
			if ($(this).attr('align')=='left') {$(this).css('margin-left','0px')};
			if ($(this).attr('align')=='right') {$(this).css('margin-right','0px')};
		});
	};
//	Запускаем галарейки продажных авто
	if ($('#content-block > .gallery-auto > .auto')) {
		$('#content-block > .gallery-auto > .auto').each(function(i){
			$(this).find('.items a').overlay({ 
				target: '#gallery', 
				expose: '#f1f1f1' 
			}).gallery({
				template: '<strong>${title}</strong> <span>Фотография ${index} из ${total}</span>',
				speed: 300 
			});
		});
	};
	if ($('#content-block > .gallery-auto > .one-auto')) {
		$('#content-block > .gallery-auto > .one-auto').each(function(i){
			$(this).find('.items a').overlay({ 
				target: '#gallery', 
				expose: '#f1f1f1' 
			}).gallery({
				template: '<strong>${title}</strong> <span>Фотография ${index} из ${total}</span>',
				speed: 300 
			});
		});
	};
//	Убираем последний бордер в блоках новостей
	if ($('.news')) {$('.news:last').css('border-bottom','none')};
// Растягиваем блок контента на полную высоту
	if ($('#content-block > .contents')) {
		var Hcontents=$('#content-block > .contents:last').height(),
			Pmain=$('#content-block').css('padding-bottom').replace('px','')-0,
			Hmain=$('#content-block').height(),
			HCenter=$('#center').height()-1;
		if (HCenter>Hmain+Pmain) {
			hNew=HCenter-(Hmain-Hcontents)-Pmain;
			$('#content-block > .contents:last').height(hNew);
		};
	};
};
//Запуск функции построения страницы
$(document).ready(function(){BuildPage();});


