﻿function LabelsClean(){
	$('input.form-input').each(function(i){
		var	inputLabel=$(this).val();
		$(this).focus(function(){if ($(this).val() == inputLabel) {$(this).val('')}});
		$(this).blur(function(){if ($(this).val() == '') {$(this).val(inputLabel)}})
	});
	$('textarea.form-textarea').each(function(i){
		var	textareaLabel=$(this).val();
		$(this).focus(function(){if ($(this).val() == textareaLabel) {$(this).val('')}});
		$(this).blur(function(){if ($(this).val() == '') {$(this).val(textareaLabel)}})
	});
};
function heightContent() {
	if ($('#layout').hasClass('sidebar-left')) {
		$('#main-colume').removeAttr('style');
		var	hAll=$('#layout').height(),
				hHeader=$('#header').height(),
				hContent=$('#main-colume').height(),
				Space=3+25+2+14+115+2+16,		//бордер хидера + паддинги хидера + бордеры центра + паддинги центра + маргины центра + бордеры контента + паддинги контента
				hContentNew=hAll-hHeader-Space;
		if (hContent<hContentNew) {$('#main-colume').height(hContentNew)}
	}
};
function clearBigPhoto(){
	$('#img').remove();
	$('#big-photo').removeAttr('style');
	$('#big-photo .close').removeAttr('style');
	$('#big-photo .progress').removeAttr('style');
};
function CarsAlign(){
//	Выравнивание страницы списка автомобилей
	if (document.getElementById('cars')) {
		var carItemH=0;
		$('#cars .item').each(function(i){if (carItemH<$(this).height()){carItemH=$(this).height()}});
		$('#cars .item').height(carItemH)
	}
};
function BuildPage() {
	LabelsClean();
//	Запуск всплывающих окошек
	$('a[rel]').overlay({top:'center'});
//	Скрипт верхнего меню
	$('#top-menu > ul > li:first').css('border-left','0 none');
//	Скрипт основного меню
	var	menuW=0;
	$('#main-menu > ul > li:last').css({'background':'none','padding':'0px'});
	$('#main-menu > ul > li').each(function(i){menuW=menuW+$(this).width()});
	menuW=$('#main-menu > ul > li').length-1+menuW;
	$('#main-menu > ul').width(menuW);
	$('#main-menu li').each(function(i){
		if ($(this).find('ul').length>0) {
			$(this).mouseover(function(){
				$(this).find('ul:first').css({'z-index':'1000'});
				if (($.browser.msie && $.browser.version == 6) || ($.browser.msie && $.browser.version == 7)) {
					$(this).css('margin-bottom','-3px');
					$(this).find('ul:first').show(0)
				}
				else {$(this).find('ul:first').show(300)}
			});
			$(this).mouseleave(function(){
				$(this).find('ul:first').css({'z-index':'900'});
				if (($.browser.msie && $.browser.version == 6) || ($.browser.msie && $.browser.version == 7)) {
					$(this).css('margin-bottom','0px');
					$(this).find('ul:first').hide(0)
				}
				else {$(this).find('ul:first').hide(300)};
			})
		}
	});
//	Скрипт нижнего меню
	if (document.getElementById('bottom-menu')) {$('#bottom-menu ul li:first').css('border-left','0 none')};
//	Скрипт левого меню
	if (document.getElementById('left-menu')) {
		$('#left-menu ul li:last').css('border-bottom','0 none')
	};
//	Скрипт галереи
	if (document.getElementById('gallery')) {
		var	itemH=0,
				imageH=0,
				imageM=0,
				k=1;
		$('#gallery .image').each(function(i){
			if (imageH<$(this).height()) {imageH=$(this).height()};
			if (k==3) {
				$(this).height(imageH);
				$('#gallery .image').eq(i-1).height(imageH);
				$('#gallery .image').eq(i-2).height(imageH);
				imageH=0;
				k=0
			};
			if (k==2 && i==$('#gallery .image').length-1) {
				$(this).height(imageH);
				$('#gallery .image').eq(i-1).height(imageH);
			};
			k++
		});
		k=1;
		$('#gallery .item').each(function(i){
			if (itemH<$(this).height()) {itemH=$(this).height()};
			if (k==3) {
				$(this).height(itemH);
				$('#gallery .item').eq(i-1).height(itemH);
				$('#gallery .item').eq(i-2).height(itemH);
				itemH=0;
				k=0
			};
			if (k==2 && i==$('#gallery .item').length-1) {
				$(this).height(itemH);
				$('#gallery .item').eq(i-1).height(itemH);
			};
			k++
		});
		$('#gallery .image').each(function(i){
			imageM=Math.floor($(this).height())*0.5-Math.floor($(this).find('img:first').height())*0.5+'px';
			$(this).find('img:first').css('margin-top',imageM)
		});
		if (!$('#gallery').hasClass('album-list')) {
			$('body').append('<div class="overlay" id="big-photo"></div>');
			$('#big-photo').append('<a class="prev">назад</a>');
			$('#big-photo').append('<a class="next">вперед</a>');
			$('#big-photo').append('<div class="info"></div>');
			$('#big-photo').append('<img class="progress" src="img/loading.gif" />');
			$('#gallery a').overlay({
				target: '#big-photo',
				expose: '#111',
				closeOnClick: false,
				onClose: function() {clearBigPhoto()}
			}).gallery({
				disabledClass: 'inactive',
				template: '<strong>${title}</strong> <span>Картинка ${index} из ${total}</span>',
				speed: 100 
			})
		}
	};
//	Растягиваем область контента на внутренних страницах, если содержимого сильно мало
	if ($('#layout').hasClass('sidebar-left')) {
		$("#main-colume img[align='left']").css('margin-left','0px');
		$("#main-colume img[align='right']").css('margin-right','0px');
	}
	if (($.browser.msie && $.browser.version == 6) || ($.browser.msie && $.browser.version == 7)) {
		$('#footer').hide(0);
		$('#footer').show(0);
	};
};
//Запуск функций построения страницы
$(document).ready(function(){BuildPage();});
$(window).resize(function(){heightContent();});
if (window.addEventListener) window.addEventListener('load', function () {
	CarsAlign()
	heightContent();
}, false)
else window.attachEvent('onload', function () {
	CarsAlign()
	heightContent();
});



