$(document).ready(function() {
	$('#left-menu .flevel 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 = $('#left-menu .flevel ul').index($(this).next()); // The index of the submenu of the clicked link
			if ($(this).next().css('display') == 'none') {

				// When opening one submenu, we hide all same level submenus:
				$(this).parent('li').parent('ul').find('ul').each(function(j) {
					if (j != this_i) {
						$(this).slideUp(200, function () {
							$(this).prev().removeClass('expanded').addClass('collapsed');
							cookieDel($('#left-menu .flevel ul').index($(this)));
						});
					}
				});
				// :end

				$(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($('#left-menu .flevel ul').index($(this)))).prev().removeClass('expanded').addClass('collapsed');
					});
				});
			}
		return false; // Prohibit the browser to follow the link address
		});
	});
	$('a.gallery').fancybox();
	$('a[target=_blank]').each(function(index, obj){
		if($(obj).hasClass('gallery')) return;
		if (obj.href.substr(-3, 3) == 'jpg')
			$(obj).fancybox();
	});
	$('.post a').click(function(){
		$(this).hide();
		$('.post .form').show();
		$('#name').focus();
		return false;
	});
	initScrollUp();
//	$('#flash-news tr:not(:first)').css('display', 'none');
});

function initScrollUp() {
	var showtime = $('#flash-news table:first').attr('showtime');
	window.setTimeout(function(){scrollUp()}, showtime*1000);
	$('#flash-news td').click(function(){
		window.location = $(this).find('a').attr('href');
	});
}

function scrollUp() {
	var count = $('#flash-news table').size();

	var position = $('#flash-news').scrollTop() / 100;

	var scrollTo = 0;
	if (count > position + 1)
		scrollTo = position + 1;

	var showtime = $('#flash-news table:eq('+scrollTo+')').attr('showtime');
	window.setTimeout(function(){scrollUp()}, showtime*1000);

//	console.log('count: ', count, '; position: ', position, '; scrollTo: ', scrollTo);

	$('#flash-news').animate({scrollTop: scrollTo*100}, 200, 'swing');
}

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):
}

