$(document).ready(function () {
  // Login forms
  if ($('.-email').val() == '') { $('.-email').val('e-mail'); };
  if ($('.-password').val() == '') { $('.-password').val('Пароль'); };  
  if ($('.-search').val() == '') { $('.-search').val('Поиск'); };

  $('.-email, .-password, .-search').focus(function(e) {
    var val = $(this).val();
    if ((val == 'e-mail') || (val == 'Пароль') || (val == 'Поиск')) { $(this).val(''); };
  });
  $('.-email').blur(function(e) {
    if ($(this).val() == '') { $(this).val('e-mail'); }
  });
  $('.-password').blur(function(e) {
    if ($(this).val() == '') { $(this).val('Пароль'); }
  });
  $('.-search').blur(function(e) {
    if ($(this).val() == '') { $(this).val('Поиск'); }
  });
  $('.-login').click(function(e) { $(this).parents('form').submit(); });

	// News scrolling
	var scroll_current = 1;
	var to_scroll = true;
		
	$("div.-updown-scrollable a.up").click(function () {
		if (to_scroll) {
			to_scroll = false;
			
			new_margin =  (scroll_current > 1) ? '+=' + ($("#tr_" + (scroll_current - 1)).height() + 3) : '-=' + ($("table.news").height() - $("#tr_" + scroll_last).height());
			
			if (scroll_current == 1)
				scroll_current = scroll_last + 1;
			
			$("table.news").animate({
				"margin-top": new_margin
			}, 500, "swing", function () {
				scroll_current--;
				to_scroll = true;
			});
		}
		
		return false;
	});

	$("div.-updown-scrollable a.down").click(function () {
		if (to_scroll) {
			to_scroll = false;
			
			new_margin =  (scroll_current < scroll_last) ? '-=' + ($("#tr_" + scroll_current).height() + 2) : 0;
			
			if (scroll_current == scroll_last)
				scroll_current = 0;
			
			$("table.news").animate({
				"margin-top": new_margin
			}, 500, "swing", function () {
				scroll_current++;
				to_scroll = true;
			});
		}
		
		return false;
	});

	// Authors
	$('.-author').click(function(e) {
	  e.preventDefault();
	  var rel = $(this).attr('rel');
	  var source = $('#modal_source_'+rel);
	  $('#modal_container').html($(source).html());
	  $('#modal_container > div:first').show().css('top', $(window).scrollTop() + 100);
	});	
	
	$('p.close-modal').live('click', function(e) {
	  e.preventDefault();
	  $('#modal_container > div:first').hide();
	});	
	
	// Article switchers
	$(".view1").click(function() { $(".wrap_flash").show();$("#view-options2").show();$("#view-options").hide();$(".wrap_pdf").hide(); $(window).scrollTop(355); });
	$(".view2").click(function() { $(".wrap_pdf").show();$("#view-options").show();$("#view-options2").hide();$(".wrap_flash").hide(); $(window).scrollTop(355); });  		
	
	// Comments
	$('#lt1, #lt2').live('change', function(e) {
	  if (this.checked) {
	    if (this.id == 'lt1') {
  	    $('#inline_login').hide();
  	  } else {
  	    $('#inline_login').show();
  	  }
  	  $(this).parents('form').find('p').removeClass('quick-login-checked');
  	  $(this).parents('p').addClass('quick-login-checked');	    
    };	  
	});
	
	$('#comments form input[type=submit]').live('click', function(e) {
	  e.preventDefault();
	  $(this).parents('form').ajaxSubmit({
	    dataType: 'script'
	  });
	});
	
	$("a.reply").live('click', function () {
	  var id = $(this).attr('rel');
		$("#comment_parent_id").val(id);
		var username = $(this).attr('ref');
		if (username == '') { username = 'Гость'; }
		$("#reply_name").html(' в ответ на комментарий <a href="#comment' + id + '">' + username + '</a>. <small>[<a href="#" id="cancel_reply">отменить</a>]</small>');	
	});

  $("a.del").live('click', function(e) {
    e.preventDefault();
	  var id = $(this).attr('rel');
	  var url = $(this).attr('href');
	  $.ajax({
	    type: 'post',
	    url: url, 
	    data: {id: id, _method: 'delete'},
	    dataType: 'script'
	  });   
	  return false;
  });

	$("#cancel_reply").live('click', function () {
		$("#comment_parent_id").val("");
		$("#reply_name").html("");			
		return false;
	});
	
	$("#do-login").click(function () {
		$(".login form").submit();
	});
		
	
	$("div.main form").submit(function () {
		return ($(this).find("textarea").val().length > 0);
	});
	
	$(".modallink").click(function () {
		$("#modal").hide();
		$("#modal .text").html($(this).parent().parent().find(".modalinfo").html());
		$("#modal").fadeIn();
		
		return false;
	});
	
	$(".closemodal").click(function () {
		$("#modal").fadeOut();
	});
	
  $('#new_offer_form input[type=submit]').live('click', function(e) {
    e.preventDefault();
    $(this).parents('form').ajaxSubmit({
      success: function(response) {
        $('#offers_page').html(response);
      }
    }); 
    return false;
  });	
  
  $('.-vote').live('click', function(e) {
    $.post($(this).attr('href'), {'_method': 'put'}, function(response) {
      $('#offers_page').html(response);      
    });
    return false;
  });    
  
	$("a.madelink").hover(function () {
		$("a.madelogo").addClass("hover");
	}).mouseleave(function () {
		$("a.madelogo").removeClass("hover");
	});
	
	$("a.rememberme").click(function () {
		$("a.rememberme").blur();
		
		if ($("#remember").val() == 0) {
			$("a.rememberme").addClass("checked");
			$("#remember").val("1");
		}
		else {
			$("a.rememberme").removeClass("checked");
			$("#remember").val("0");
		}
		
		return false;
	});  
	
	$('.-show-se').live('click', function(e) {
	  e.preventDefault();
    $('#se').show();
	})
});
