var menuTimer = 0;
var sloganTimer = 0;
//var slogan = false;
//var sloganCount = 0;

$(document).ready(function() {
	$('#mainmenu a.main').each(function () {
		$(this).mouseenter(show_menu).mouseleave(startMenuTimer);
		var top = $('#mainmenu').height()-2;
		var pos = $(this).position();
		var sub = $('#sub'+this.id); 
		sub.mouseenter(show_menu).mouseleave(startMenuTimer);
		sub.css('top',top+'px').css('left',pos.left-1+'px');
	});
	if ($("#leftmenu").length) {
		if (!sideMenuAutoHeight) {
			$("#leftmenu_div").height(sideMenuHeight);
		}			
	    $("#leftmenu").accordion({
	        event: "mouseover",
	        autoHeight: !sideMenuAutoHeight,
	        fillSpace: !sideMenuAutoHeight
	    });
	}
	/*
    $("#counters_bottom .inner").hide('slow');    
	$("#counters_control").click(function(e) {
    	var t = $(e.target); 
    	if (t.attr('alt')=='+') {
    		t.attr('alt', '-');
    		t.attr('src', t.attr('src').replace('expand', 'collapse'));
    		$("#counters_bottom .inner").show('slow');
    	} else {
    		t.attr('alt', '+');
    		t.attr('src', t.attr('src').replace('collapse', 'expand'));
    		$("#counters_bottom .inner").hide('slow');
    	}
    });
    */
    $("#ask").click(show_ask);
    
    slogan = $('#slogan');
    sloganTimerStart();
});

function show_menu(e) {
	stopMenuTimer(true);
	if (this.id.substr(0,3)!='sub') {
		var m = $('#'+this.id);
		var s = $('#sub'+this.id);
		if (!m.hasClass('hover')) {
			time_hide_menu(true);
			m.addClass('hover');
			s.fadeIn('fast');
		}
	}
	$('embed').hide();
}

function time_hide_menu(cloudnoshow) {
	stopMenuTimer(true);
	$('#mainmenu a.main.hover').removeClass('hover');
	$('#mainmenu div.submenu:visible').fadeOut('fast', function () { if (!cloudnoshow) $('embed').show();});	
}

function startMenuTimer() {	stopMenuTimer(true); menuTimer = window.setTimeout(time_hide_menu, 500); }
function stopMenuTimer(cloudnoshow) { 
	if (menuTimer) window.clearTimeout(menuTimer); 
	menuTimer = false; 
	if (!cloudnoshow) $('embed').show();
}

function show_ask() {
	var dlg = $('#dlgAsk');
	if (dlg.length==0) {
		$('body').append('<div id="dlgAsk" title="Задать вопрос" style="display:none"></div>');
		dlg = $('#dlgAsk');
		dlg.load('/js/dlgAsk.php', function() {
			dlg.dialog({
				height: 480,
				width: 400,
				modal: false,
				buttons: {
					'Отправить': function() {
						if ($('input[name="from"]',this).val()=='') {
							alert('Заполните поле "Имя"'); 
							return;
						}
						if ($('input[name="email"]',this).val()=='') {
							alert('Заполните поле "E-mail или телефон"'); 
							return;							
						}
						if ($('textarea[name="text"]',this).val()=='') {
							alert('Введите сообщение'); 
							return;							
						}
						$.ajax({
							type: 'POST',
							url: '/js/dlgAsk.php',
							cache: false,
							data: {
								from: $('input[name="from"]',this).val(),
								email: $('input[name="email"]',this).val(),
								text: $('textarea[name="text"]',this).val()
							},
							success: function() {
								dlg.dialog('close');
							}							
						});
					},
					'Закрыть': function() {
						$(this).dialog('close');
					}
				},
				open: function() {
					$('embed').hide();
				},
				close: function() {
					$('embed').show();					
				}
			});
		});
	} else dlg.dialog('open');
}

function sloganTimerStart() {
	sloganTimer = window.setTimeout(function() {slogan.fadeIn(7000);}, 5000);
}

