
function prepareCatalogue() {
	MINUS = "/img/standard/r/minus.jpg";
	PLUS = "/img/standard/r/plus.jpg";
	DEFAULT = "/img/standard/r/out-link.jpg";
// раскроем текущий раздел в меню слева
	function menuInit(pCat) {
		if (typeof pCat === 'undefined') return;
		var a = $('.hierarchy').find("a[href$='" + pCat + "']");
		a.parents('li').css( {listStyleImage : 'url(' + MINUS + ')'} ).find('ul:first').slideDown( 1 );
		a.parent('li').css({listStyleImage : 'url(' + DEFAULT + ')'});
		a.css({color : '#000000', fontWeight : 'bold', cursor : 'default'}).click(function(e){
			e.preventDefault();
		});
	}
// строим каталог
	pars = {
		noAnimationCSS : {
			listStyleImage : 'url(' + DEFAULT + ')'
		}
		,showCSS : {
			listStyleImage : 'url(' + PLUS + ')'
		}
		,hideCSS : {
			listStyleImage : 'url(' + MINUS + ')'
		}
		,durationHide : 400
		,durationShow : 800
		,noAnimationClass : 'simple-li'
		,expand : false
		,callback : function(){menuInit( CATEGORY_ENDING );}
	}
	$('.hierarchy').hierarchy(pars);
}
	
jQuery(function($){
	prepareCatalogue();
	var isIE;
	if ($.browser.msie) {
		isIE = (parseInt($.browser.version) < 8);
	} else {
		isIE = false;
	}
	
	
	var catalogueItems = $("#catalogue .catalogue-item");
	
	
	
	//каталог
	var menu_td = $("#menu td");
	
	// каждому элементу меню каталога добавляем класс no-catalogue, если соответствующий .catalogue-item не содержит ul
	catalogueItems.each(function(index){
		if ( ! $(this).is(':has(ul)') ) {
			$(menu_td[ index ]).addClass('no-catalogue');
		}
	});
	

	
	menu_td.hover(
		function(){
			$(this)
			.find("a:first")
			.addClass("over")
			.end()
			.find(".fade-in")
			.css( setPosition( $(this) ) )
			.fadeIn(600);
		},
		function(){
			$(this)
			.find("a:first")
			.removeClass("over")
			.end()
			.find(".fade-in")
			.fadeOut(200);
		}
	)
	.click(function(e){
		if($(this).attr("class") == "no-catalogue"){
			return true;
		}
		e.preventDefault();
		var inx = menu_td.index(this);
		menu_td.removeClass("active").eq(inx).addClass("active");
		
		catalogueItems.hide().eq( inx ).show();
	});
	
	try {
		var a = $('.hierarchy').find("a[href$='" + CATEGORY_ENDING + "']");
		// определяем, какой по счету раздел явл. текущим и вызываем click() по соотв. пункту меню
		var index = catalogueItems.index( catalogueItems.has(a) );
		catalogueItems.hide();
		menu_td.eq( index ).click();
	} catch (e) {
		catalogueItems.hide();
	}
	
	
	function setPosition(obj){
		return {
			"width" : obj.width(),
			"top"   : obj.offset().top + obj.height(),
			"left"  : obj.offset().left
		}
	}
	
	//верхнее меню (поиск, карта сайта)
	$(".green .top-menu li.no-delim").hover(
		function(){
			$(this).find("ul").show();
		},
		function(){
			$(this).find("ul").hide();
		}
	);
	
	//главное меню
	$("#second_nav ul li.with-inside").hover(
		function(){
			
			var styling = {};
			styling.position = "relative";
			if(isIE) styling.marginBottom = "-3px";
			$(this).css( styling ).find(".inside").show();
		},
		function(){
			
			
			var styling = {};
			styling.position = "static";
			if(isIE) styling.marginBottom = "0";
			$(this).css( styling ).find(".inside").hide();
			
		}
	);
	
	


	
	//подровняем колонки
	var c_first = $("#center .first:first");
	var c_second = $("#center .second:first");
	var c_group = $("#center .group:first");
	var c_third = $("#center .third:first");
	var sw = $("#second_nav");
	
	var tds = new Array();
	for(var i = 0; i < menu_td.length; i++){
		tds.push(menu_td.eq(i));
		menu_td.end();
	}
	
	(getSetCols())();
	$(window).resize(getSetCols());
	
	// функция фабрика - для получения функции, которая будет устанавливать ширину колонки
	function getSetCols() {
		if (isIE) {
			return function() {
				setCols();
				
				/*назначаем ширину элементов меню каталога исходя из размеров получившихся колонок*/
				var width = Math.floor( (c_first.width()) / 2 );
				
				for (var i = 0; i < tds.length; i++) {
					tds[ i ].css({width : width});
				}
			};
			
		/*для Mozilla, IE 8*/
		} else if ($.browser.mozilla || ($.browser.msie && (parseInt($.browser.version) >= 8))) {
			return function(){
				setCols();
				/*после вызова setCols первая колонка может оказаться на 1 пиксель шире или уже первых двух элементов меню каталога. Хотя и это не совсем спасает*/
				var twoElementsWidth = tds[0].width() + tds[1].width() + 1;
				c_first.css({
					width : twoElementsWidth
				});
				
				c_second.css({
					width : c_group.width() - twoElementsWidth - 1
				});
			}
		
		/*для Safari и Chrome и прочих*/
		} else {
			return setCols;
		}
	}
	
	function setCols(){
		var tmp = tds[2].width() + tds[3].width() + tds[4].width() + tds[5].width() + 3
		
		c_first.css({
			width : c_group.width() - tmp - 1
		});
		c_second.css({
			width : tmp
		});
		c_third.css({
			width : $("#main").width() - c_group.width() - 1
		});
	}
	
	//галерея
	var photos = $('a[rel^="prettyPhoto"]');
	if (typeof numberToUpload !== 'undefined') {
		//do nothing
	} else {
		numberToUpload = 4;
	}
	photos.prettyPhoto({theme:'facebook'});
	
		
	//делаем загрузку многих файлов на сервер
	var pars = new Array();
	$('input#multy_upload').multyUpload( {alreadyUploaded: {paths : pars}, uploadForm : $('form[name=frm]'), maxNumber:numberToUpload} );
	
	$('.item_logo').each(function(){
		var me = $(this);
		$(this).find('img:first:not([src$="no-photo.gif"])').add( $(this).find('span.total_photos:first') ).click(function(){
			me.find('.gallery_photos img:first').click();
		});
	});
	
	
});

