$(document).ready(function(){
	var path = document.location.pathname;		
	if ( path.charAt(path.length-1) != '/' ) path = path+'/';
	var epos = document.location.pathname.indexOf('/',1) == -1 ? document.location.pathname.length : document.location.pathname.indexOf('/',1)-1;
	var section = document.location.pathname.substr(1, epos );
	
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}

	$('#menu a[href="/production/energoeff/energoeff_presentation/"]').click(function(){
		$('#locker').show();
		$('#presentation').show();
		return false;
	});
	
	$("#menu a[href='/stock/corporate_management/']").click(function() {
		var li = $(this).parent();
		if (li.hasClass('active')) {
			li.find('ul.lvl2').hide();
			li.removeClass('active');
		}
		else {
			li.find('ul.lvl2').show();
			li.addClass('active');
		}
		return false;
	});
	
	// MENU -------------------------------------------------------------------
	/*function itemActivate(item) {
		item.addClass("active");
		if (item.find("ul.lvl1").length) {
			//alert("3");
			item.find("ul.lvl1").slideDown(500);
		//	return false;
		}
		//else 
		//	return true;
	}*/
	
	/*if (section == 'news_company' || section == 'appeal' || section == 'korp_newspaper' || section == 'calend') {
		itemActivate($("#menu div.item7"));
	}
	else {
		itemActivate($("#menu div a[href='/" + section + "/']").parent());
	}*/
	
	/*var active_li = $("#menu div.active a[href='" + path +"']").parent();
	active_li.addClass("active");
	if (active_li.parent().hasClass('lvl1')) {
		active_li.find('ul').show();
	}
	else {
		active_ul = active_li.parent();
		active_ul.show();
		active_ul.parent().addClass("active");
	}*/
	
	
	//$("#menu a.js-list ul").slideDown(500);
	/*$("#menu div a.js-list").click(function(){
		var li = $(this).parent();
		if (li.hasClass('active')) {
			li.find('ul').slideUp(500);
			li.removeClass('active');
		}
		else {
			li.find('ul').slideDown(500);
			li.addClass('active');
		}
		return false;
	});*/
	
	/*
	$("#menu div").each(function(){
		var item = $(this);
		$(this).find("a:first").click(function(){
			if ($("#menu div.active").length) {
			//alert("jr");
				if ($("#menu div").index($("#menu div.active")) != $("#menu div").index(item)) {
					if ($("#menu div.active ul.lvl1").length) {
						$("#menu div.active ul.lvl1").slideUp(300, function(){
							$("#menu div.active").removeClass("active");
							itemActivate(item);
						});
						//if (item.find("ul.lvl1").length)
						//	return false;
						//else
						//	return true;
					}
					else {
						$("#menu div.active").removeClass("active");
						return itemActivate(item);
					}
				}
				else {
					if ($("#menu div.active ul.lvl1").length) {
						$("#menu div.active ul.lvl1").slideUp(300, function(){
							$("#menu div.active").removeClass("active");
						});
						//return false;
					}
					else {
						$("#menu div.active").removeClass("active");
						//return false;
					}
				}
			}
			else {
				//return itemActivate(item);
			}
		});
	});*/
	
	
	// NEWS -------------------------------------------------------------------
	var speed = 150;
	var top = [];
	top[1] = 38;
	top[2] = 102;
	top[3] = -93;
	top[4] = -29;

	$(".main-news .item1").mouseover(function(){
		$(".main-news .item3").removeClass("active");
		$(".main-news .item2").stop().removeClass("active").animate({top: top[2]}, speed);
		$(".main-news .item1").stop().addClass("active").animate({top: top[1]}, speed);
	});
	
	$(".main-news .item2").mouseover(function(){
		$(".main-news .item3").removeClass("active");
		$(".main-news .item2").stop().addClass("active").animate({top: top[2]}, speed);
		$(".main-news .item1").stop().removeClass("active").animate({top: top[3]}, speed);
	});
	
	$(".main-news .item3").mouseover(function(){
		$(".main-news .item3").addClass("active");
		$(".main-news .item2").stop().removeClass("active").animate({top: top[4]}, speed);
		$(".main-news .item1").stop().removeClass("active").animate({top: top[3]}, speed);
	});
	
	
	// HIDDEN-BLOCK's ----------------------------------------------------
	
	$("a.display-hidden").each(function() {
		$(this).click(function() {
			$("#" + $(this).attr('rel')).slideToggle(400);
			return false;
		});
	});
	
	
	
	
	
	$("#subscribe").click(function() {
		var form = $("#subscribe-form");
		var info = $("#subscribe-info");
		$.ajax({
			type: "POST",
			dataType: "html",
			url: form.attr('action') + '?mode=add',	
			data: form.serialize(),
			beforeSend: function() {
				info.html('<span style="color: #ccc;">Отправка...</span>');
			},
			success: function(data) {
				info.html(data);
			},
			error: function() {
				info.html('<span style="color: #f00;">Ошибка отправки!</span>');
			}
		});
		return false;
	});
	$("#unsubscribe").click(function() {
		var form = $("#subscribe-form");
		var info = $("#subscribe-info");
		$.ajax({
			type: "POST",
			dataType: "html",
			url: form.attr('action') + '?mode=del',	
			data: form.serialize(),
			beforeSend: function() {
				info.html('<span style="color: #ccc;">Отправка...</span>');
			},
			success: function(data) {
				info.html(data);
				form.find('input').val('');
			},
			error: function() {
				info.html('<span style="color: #f00;">Ошибка отправки!</span>');
			}
		});
		return false;
	});
	
	
	
	$("#ajax-form").submit(function(){
		var form = $(this);
		var info = $("#ajax-info");

		$.ajax({
			type: "POST",
			dataType: "html",
			url: '/ajax/' + form.attr('action'),	
			data: form.serialize(),
			beforeSend: function() {
				info.html('Отправка...');
			},
			success: function(data) {
				if(data == 'ok') {
					form.slideUp(300);
					info.html('<b style="color: #000;">Ваше сообщение успешно отправлено.</b>');
				}
				else
					info.html(data);
				//form.find('input').val('');
			},
			error: function() {
				info.html('<span style="color: #f00;">Ошибка отправки!</span>');
			}
		});
		return false;
	});
	
	
	
	
	
	
});







var sb = {
	scroll: null,
	count: 0,
	now: 0,
	init: function() {
		sb.scroll = $("#banners .scroll");
		sb.scroll.scrollTop(0);
		sb.count = parseInt( sb.scroll.find("a").length );
		
		$("#banners .navi-down").click(function() {
			sb.scroll.append(sb.scroll.find("a").eq(0));
			sb.scroll.stop().scrollTop(0);
			sb.scroll.animate({scrollTop : '101px'}, 400);
			return false;
		});
		$("#banners .navi-up").click(function() {
			sb.scroll.prepend(sb.scroll.find("a").eq(sb.count - 1));
			sb.scroll.stop().scrollTop(202);
			sb.scroll.animate({scrollTop : '101px'}, 400);
			return false;
		});
	}
}
