var defaultSettings = {
	duration:300,
	offset:6, 
	fontSize:30
};
var clicked = false;
var links = {};
$.fn.slideLeft =  function (type, offset, duration, marginLeft, outerWidth, outerHeight, p, obj)
{
	var maxOffset = (type=='left' ? p.left - offset : p.left + outerWidth + offset);
	var startOffset = maxOffset + (3 * offset) * (type == 'left' ? -1 : 1) + (type == 'left' ? -1 : 1) * marginLeft;
	$(this).css ({
		'position':'absolute', 
		'top':p.top + ($(this).outerHeight() > outerHeight ? -Math.floor(($(this).outerHeight() - outerHeight) / 2) : Math.floor((outerHeight - $(this).outerHeight()) / 2))-2, 
		'left':startOffset
	});
	$(this).show ();
	$(this).animate ({'left':maxOffset}, duration, function ()
	{
		if (typeof links [obj.parent ().attr ('id')] != 'undefined')
		{
			window.location.href = links [obj.parent ().attr ('id')];
		}
	});
}
$.fn.slideRight = function (type, offset, duration, outerWidth, p)
{
	var maxOffset = (type=='left' ? p.left - offset : p.left + outerWidth + offset);
	$(this).animate ({'left':parseInt ($(this).css ('left')) + (type == 'left' ? -2 : 2) * offset, 'alpha':'hide'}, Math.floor (duration / 2),
		function ()
		{
			$(this).remove ();
		}
	);
}
$.fn.newRub = function (symbol, type, fontSize)
{
	styles = ['color', 'font-family', 'font-weight'];
	var rub = $('<div>'+symbol+'</div>');
	rub.appendTo ($(this).parent());
	rub.attr ('id', type+$(this).attr ('id'));
	for (i = 0; i < styles.length; i++)
	{
		rub.css (styles [i], $(this).css (styles [i]));	
	}
	rub.css ('font-size', fontSize);
	rub.hide ();
	return rub;
}
$.fn.rollLink = function (settings)
{
	var $this = $(this);
	settings = (typeof settings == 'undefined' ? defaultSettings : settings);
	var leftRub, rightRub;
	if (typeof links [$(this).parent ().attr ('id')] == 'undefined')
	{
		links [$(this).parent ().attr ('id')] = $(this).attr ('href');
		$(this)[0].removeAttribute ('href');
	}
	$(this).click (
		function ()
		{
			$(this).parent().parent().removeClass ('hovered');
			if (!clicked)
			{
				clicked = true;
				$('#rubLeft').remove ();
				$('#rubRight').remove ();
				$('li span[id^=menu] a').removeClass ('active');
				$(this).addClass ('active');
				$(this).animate ({'margin-left': 6}, 'fast');
				parents = $(this).parent().parent();
				$('div.content div.leftcolumn div.menu li.active').removeClass ('active');
				if (!$(this).hasClass ('first'))
				{
					parents.css ('margin-top', 10);
				}
				if (!$(this).hasClass ('last'))
				{
					parents.css ('margin-bottom', 10);
				}
				idn = $(this).attr ('id');
				$('#left'+$this.attr ('id')).remove ();
				$('#right'+$this.attr ('id')).remove ();
				leftRub = $(this).newRub ('[', 'left', settings.fontSize);
				rightRub = $(this).newRub (']', 'right', settings.fontSize);
				leftRub.slideLeft ('left', settings.offset, settings.duration, parseInt ($(this).css ('margin-left')), $(this).outerWidth(), $(this).outerHeight(), $(this).position (), $(this));
				rightRub.slideLeft ('right', settings.offset, settings.duration, parseInt ($(this).css ('margin-left')), $(this).outerWidth(), $(this).outerHeight(), $(this).position (), $(this));
			}
		}
	);
}
$.fn.hovered = function ()
{
	$(this).find ('ul')
		.hover (
			function ()
			{
				
			},
			function (event)
			{
				if (event.target != $(this).parent ()[0])
				{
					$(this).find ('li').removeClass ('hovered')
						.end ()
						.hide ()
						.parent ().removeClass ('hovered');
				}
			}
		)
		.find ('li')
		.hover (
		function ()
		{
			$(this).addClass ('hovered');	
		},
		function ()
		{
			$(this).removeClass ('hovered');	
		}
	);
	$(this).hover (
		function ()
		{
			$(this).addClass ('hovered');	
			$(this)
				.find ('ul')
				.show ();
		},
		function ()
		{
			$(this).removeClass ('hovered');
			$(this).find ('ul').hide ();
		}
	);
}
$(document).ready (function ()
{
	/*$('li span[id^=menu] a:not(.active)').each (function () 
	{ 
		$(this).rollLink ();
	});*/
	$('div.menu ul li').each (function ()
	{
		if (typeof $(this).find ('span a.active')[0] == 'undefined' && typeof $(this).find ('ul')[0] != 'undefined')
		{
			$(this).hovered ();
		}
	});
});
