$(document).ready(function() {
	$('ul#my-menu ul').each(function(i) { // Check each submenu:
		if ($.cookie('submenuMark-' + i)) {  // If index of submenu is marked in cookies:
			$(this).show().prev().removeClass('collapsed').addClass('expanded'); // Show it (add apropriate classes)
		}else {
			$(this).hide().prev().removeClass('expanded').addClass('collapsed'); // Hide it
		}
		$(this).prev().addClass('collapsible').click(function() { // Attach an event listener
			var this_i = $('ul#my-menu ul').index($(this).next()); // The index of the submenu of the clicked link
			if ($(this).next().css('display') == 'none') {
				$(this).next().slideDown(200, function () { // Show submenu:
					$(this).prev().removeClass('collapsed').addClass('expanded');
					cookieSet(this_i);
				});
			}else {
				$(this).next().slideUp(200, function () { // Hide submenu:
					$(this).prev().removeClass('expanded').addClass('collapsed');
					cookieDel(this_i);
					$(this).find('ul').each(function() {
						$(this).hide(0, cookieDel($('ul#my-menu ul').index($(this)))).prev().removeClass('expanded').addClass('collapsed');
					});
				});
			}
		return false; // Prohibit the browser to follow the link address
		});
	});
});
function cookieSet(index) {
	$.cookie('submenuMark-' + index, 'opened', {expires: null, path: '/'}); // Set mark to cookie (submenu is shown):
}
function cookieDel(index) {
	$.cookie('submenuMark-' + index, null, {expires: null, path: '/'}); // Delete mark from cookie (submenu is hidden):
}


$(document).ready(function() {
	$("#topnav li").prepend("<span></span>"); //Throws an empty span tag right before the a tag
	$("#topnav li").each(function() { //For each list item...
		var linkText = $(this).find("a").html(); //Find the text inside of the a tag
		if ((linkText == 'О Нас' && document.location.href == 'http://21sto.ru/o_nas.php')
		|| (linkText == 'Запчасти' && document.location.href == 'http://kuzovnye-zapchasti.21sto.ru/')
		|| (linkText == 'Эвакуация' && document.location.href == 'http://21sto.ru/evakuatsia.php')
		|| (linkText == 'Автосалон' && document.location.href == 'http://21sto.ru/avtosalon.php')
		|| (linkText == 'Прайс лист' && document.location.href == 'http://21sto.ru/price.php')
		|| (linkText == 'Контакты' && document.location.href == 'http://21sto.ru/kontakti.php')
		|| (linkText == 'Схема проезда' && document.location.href == 'http://21sto.ru/shema.php')
		|| (linkText == 'Отзывы' && document.location.href == 'http://21sto.ru/otzivi.php')
		) {
			//alert(linkText+'+'+document.location.href);
		} else {
			$(this).find("span").show().html(linkText); //Add the text in the span tag
		}
	}); 
	$("#topnav li").hover(function() {	//On hover...
		$(this).find("span").stop().animate({ 
			marginTop: "-40" //Find the span tag and move it up 40 pixels
		}, 250);
	} , function() { //On hover out...
		$(this).find("span").stop().animate({
			marginTop: "0" //Move the span back to its original state (0px)
		}, 250);
	});
});

