STOP = false;

function makeCheckbox(where)
{
	$(where + ' input[type="checkbox"]').each
	(
		function(i)
		{
			ch = $(this).attr('checked');
			cl = ch == true ? ' now' : '';
			$(this).after('<a href="#" class="chbox' + cl + '"></a>').hide();
		}
	);
}

function makeRadiobutton(where)
{
	$(where + ' input[type="radio"]').each
	(
		function(i)
		{
			ch  = $(this).attr('checked');
			now = ch == true ? ' now' : '';
			cl  = $(this).attr('name');
			$(this).after('<a href="#" class="radio' + now + '" alt="' + cl + '"></a>').hide();
		}
	);
}

function ajaxStart(href, hwnd)
{
	if(hwnd == null)
	{
		$('#top .menu').each
		(
			function()
			{
				if($(this).attr('href') == href)
				{
					$('#top .menu').removeClass('now');
					$(this).addClass('now');
					hwnd = this;
				}
			}
		);
	}
	$(hwnd).parents('ul').find('.menu').removeClass('now');
	$(hwnd).addClass('now');
	
	if(STOP)
		return false;
	
	STOP = true;
	//h 	 = $('#wrap').height();
	//$('#wrap').css({height: h + 'px'});				
	
	h	= $('#content').outerHeight(true) / 2;
	
	$('#content').slideUp('slow');
	$('#top').animate(
						{marginTop: h}, 
						'slow', 
						function()
						{
							$.get(
									   href,
									   function(data)
									   {
										   html = $(data).find('#content').html();
										   $('#content').html( html );
										   initAjax('.lmenu .menu');
										   animateEnd();
									   }
									  );
						}
					  );
}

function animateEnd()
{
	$('#content').slideDown('slow');
	$('#top').animate({marginTop: 0}, 'slow', 'swing', function(){STOP = false});	
}

function initAjax(hwnd)
{
	$(hwnd).each
	(
		function()
		{
			href = $(this).attr('href');
			href = $(this).hasClass('amsou') ? '/' : href;
			$(this).attr('href', '#' + href);
		}
	);
}

$
(
	function()
	{
		/*
		 * Всякая гавнЯная инициализация
		 */
		makeCheckbox('body');		
		makeRadiobutton('body');

		$('#top li .menu').hover
		(
			function()
			{
				if(!$(this).hasClass('now'))
					$(this).addClass('selected');
			}
			,
			function()
			{
				if(!$(this).hasClass('now'))
					$(this).removeClass('selected');
			}
		);
		
		/*
		 * Форма логина и ссылка логина
		 */
		$('#loginenter').click
		(
			function()
			{
				$(this).hide();
				$('#loginform').fadeIn();
			}
		);
		
		$('#loginform .submit').click
		(
			function()
			{
				$(this).parents('form').submit();
				return false;
			}
		);
		
		/*
		 * Плейсхолдеры
		 */
	  	$('#loginform input').click
	  	(
	  		function()
	  		{
	  			if($(this).hasClass('placeholder'))
	  			{
	  				$(this).val('');
	  				$(this).removeClass('placeholder');
	  			}
	  		}
	  	);

		/*
		 * Хинты на линки
		 */
	  	$('.hint').hover
	  	(
	  		function()
	  		{
	  			tooltip.show($(this).attr('alt'));
	  		},
	  		function()
	  		{
	  			tooltip.hide();
	  		}
	  	);
	  	
	  	$('.hint').click
	  	(
	  		function()
	  		{
	  			return false;
	  		}
	  	);
	  	
		/*
			Клик на чекбоксе
		*/
		$('.chbox').live
		(
		 	'click',
			function()
			{
				hwnd  = $(this).prev();
				check = $(hwnd).attr('checked') && $(this).hasClass('now');
				check ? $(hwnd).removeAttr('checked') : $(hwnd).attr('checked', 'checked');
				$(this).toggleClass('now');
				return false;
			}
		);
		
		/*
			Клик на радио
		*/
		$('.radio').live
		(
		 	'click',
			function()
			{
				cl = $(this).attr('alt');
				$('.radio[alt="' + cl + '"]').removeClass('now').prev().removeAttr('checked');
				$(this).addClass('now').prev().attr('checked', 'checked');
				return false;
			}
		);
		
		/*
			Ховеры на чекбоксы и радио
		*/
		$('.chbox,.radio').live
		(
		 	'mouseover',
		 	function()
			{
				$(this).addClass('over');
			}
		);
	
		$('.chbox,.radio').live
		(
		 	'mouseout',
		 	function()
			{
				$(this).removeClass('over');
			}
		);
		
		/*
		 *	Ajax me softly, baby... 
		 */
		/*
		url = document.location.hash.replace('#', '');
		if(url.length > 0)
			ajaxStart(url, null);

		initAjax('#top .menu, .lmenu .menu');
		
		
		$('#top .menu, .lmenu .menu').live
		(
			'click',
			function()
			{
				href = $(this).attr('href').substr(1);
				ajaxStart(href, this);
			}
		);
		*/
	}
);
