$(document).ready( function() {

	$('.slide_link').click( function() {

		if ( $(this).parent().hasClass('active') )
			return false;
		else $('.tabs.m2 .active').removeAttr('class');

		var id = $(this).attr('id');

		if ( $('.slide.visible').length ) {

			$('.slide.visible').css({display:'none'}).removeClass('visible');
		}

		if ( $('#slide_'+id).css("display") == 'none' ) {

			$('#slide_'+id).css({display:'block'}).addClass('visible');
			$(this).parent().addClass('active');
		}

		return false;
	});

	$('#required_show').live( 'click', function() {

		if ( $('.required').parent().hasClass('light') )
			$('.required').parent().removeClass('light');
		else $('.required').parent().addClass('light');

		return false;
	});

	if ( $('.fly').length ) {

		$('.fly').addClass('ajx');
		/*h = $('.fly:first').height() + 120;
		$('body').css({height:h+'px'});*/
	}

	ajx_edit = function( href ) {

		var t = $(window).scrollTop() + 100;
		$('body').append('<div style="top:'+t+'px" id="load_block"><img src="/a/templates/default/img/load_b.gif" alt="" id="load" /></div>');

		var b = $.ajax({url:href,async:false}).responseText;

		$('#load_block').remove();
		if ( $('.fly.ajx').length ) $('.fly.ajx').css({display:'none'}).remove();

		$('body').append( b );
		t -= 70;
		$('.fly').css({top:t+'px'}).prepend('<a class="close" href="#" title="Закрыть"></a>').fadeIn('slow').removeClass('hidden').addClass('ajx');
		$('.fly .tabs a').addClass('ajx');

		return false;
	}

	$('a.ajx').live( 'click', function() {

		ajx_edit( $(this).attr('href') );

		return false;
	});

	$('.debug').live( 'dblclick', function() {

		$(this).remove();
	});

	$('tr.ajx').click( function() {

		var href = $(this).children('td:first').children('a.ajx').attr('href');
		ajx_edit( href );

		return false;
	});

	$('.ajx form').live( 'submit', function() {

		$('.required').removeClass('err');
		if ( $('.result').length ) $('.result').remove();

		$(".required").each( function() {

			if ( ! $(this).val() ) {

				$(this).addClass("err").focus();
			}
		});

		if ( $('.err').length ) {

			$('.err:first').focus();
			return false;
		}

		$('.ajx p.submit span').remove();
		$('.ajx p.submit').prepend( '<img src="/templates/default/img/load.gif" alt="" id="load" />' );

		$.post( $(this).attr('action'), $(this).serialize(), function( data ) {

				$("#load").remove();
				if ( $('.fly').length ) $('.fly').css({display:'none'}).remove();
				$('body').append( data );
				$('.fly').prepend('<a class="close" href="" title="Закрыть"></a>').fadeIn('slow').removeClass('hidden').addClass('ajx');
		});

		return false;
	});

	$('.close').live( 'click', function() {

		$('.fly').fadeOut('slow').remove();
		return false;
	});

	$('a.out').click( function() {

		$(this).attr( 'target', '_blank' );
		return true;
	});
});

