$(function() {
  $('.check_form').change(function() {
    var fieldList = $(this).closest('form').find('.check_form');
    for (var i = 0; i < fieldList.length; i++) {
      if (!fieldList.eq(i).val()) {
        $(this).closest('form').find('input[type=submit]').attr('disabled', true);
        return false;
      }
    }
    $(this).closest('form').find('input[type=submit]').attr('disabled', false);
    return true;
  });

  $('.check_form').blur(function() {
    var fieldList = $(this).closest('form').find('.check_form');
    for (var i = 0; i < fieldList.length; i++) {
      if (!fieldList.eq(i).val()) {
        $(this).closest('form').find('input[type=submit]').attr('disabled', true);
        return false;
      }
    }
    $(this).closest('form').find('input[type=submit]').attr('disabled', false);
	    return true;
	  });
  
  $('input.placeholder').each(function() {
    $(this).val($(this).attr('placeholder'));
  }).css('color', '#aaa').focus(function() {
    if ($(this).val() == $(this).attr('placeholder'))
      $(this).css('color', '#000').val('');
  }).blur(function() {
    if (!$(this).val())
      $(this).css('color', '#aaa').val($(this).attr('placeholder'));
  });

  $('.main_menu .mm_off').hover(function() { $(this).removeClass('mm_off'); }, function() { $(this).addClass('mm_off'); });

    $('.news_main_block ol li[class!=nmb_last]').hover(function(e ,data) {
        if (!$(this).hasClass('nmb_first')) {
            //if (typeof(data) != 'undefined') {
                $('#n' + $('.nmb_first').attr('id')).fadeOut('slow');
                $('#n' + $(this).attr('id')).fadeIn('slow');
            /*
            } else {
                $('.nmb_foto').css('display', 'none');
                $('#n' + $(this).attr('id')).css('display', 'block');
            }*/
            $('.news_main_block ol li').removeClass('nmb_first');
            $(this).addClass('nmb_first');
        }
    }, false);

  $('.authLink').click(function() {
    $('#authForm').css('display', 'block');
    return false;
  });

  $('.errorLink').click(function() {
    $('#errorForm').css('display', 'block');
    return false;
  });

  $('.sh_f_close').click(function() {
    $(this).closest('.shadow').css('display', 'none');
    return false;
  });

  $('.n_caro_block a').click(function() {
    $('.n_b_foto img').attr('src', '/resize.php?image=' + $(this).attr('href') + '&w=670&h=500');
    $('.n_caro_block a').removeClass('n_caro_current_photo');
    $(this).addClass('n_caro_current_photo');

    $('#photo_comment').text($(this).attr('title'));

    return false;
  });

  $('.n_caro_foto .caro_back, .n_caro_foto .caro_next').click(function() {
    if ($(this).hasClass('caro_back')) {
      if ($('.n_caro_block a:hidden').length)
        $('.n_caro_block a:hidden:last').parent().css('display', 'block');
    } else {
      if ($('.n_caro_block a:visible').length > 6)
        $('.n_caro_block a:visible:first').parent().css('display', 'none');
    }

    return false;
  });

  $('.n_b_foto .caro_back').click(function() {
    $('.n_caro_block a.n_caro_current_photo').parent().prev().find('a').click();
    return false;
  });

  $('.n_b_foto .caro_next').click(function() {
    $('.n_caro_block a.n_caro_current_photo').parent().next().find('a').click();
    return false;
  });

  $('.menu-collapser h4 a').click(function() {
    var p = $(this).parent().parent();
    var isHidden = p.find('ul').css('display') == 'none';
    p.find('h4 b').html(isHidden ? '&uarr;' : '&darr;');
    p.find('ul').css('display', isHidden ? 'block' : 'none');
    (isHidden) ? p.find('ol').slideDown() : p.find('ol').slideUp();
    return false;
  });

  $('.h_collapsable').hide();
  $('.collapsable b, .p_collapsable').show();
  $('.collapsable').attr('title', 'развернуть').click(function() {
    var isVisible = $('#h_' + $(this).attr('id')).css('display') == 'block';
    //var newAnswer = $('#h_cl_comment').addClass('h_cl_X').clone(true).appendTo('#h_cl_comment').removeClass('h_cl_X');

    //alert($('#h_cl_comment').html());
    isVisible ? $('#h_' + $(this).attr('id')).slideUp() : $('#h_' + $(this).attr('id')).slideDown();
    //isVisible2 ? $('#h_cl_comment').slideUp() : $('#h_cl_comment').slideDown();
    $(this).attr({'title': isVisible ? 'развернуть' : 'свернуть'}).find('b').html(isVisible ? '&darr;' : '&uarr;');
    $('#p_' + $(this).attr('id')).html(isVisible ? '&darr;' : '&uarr;');

    return false;
  });

  $('.answer_to').show().click(function() {
    var newAnswer = $('#h_cl_comment').addClass('h_cl_X').clone(true).appendTo('#answer_to_' + $(this).closest('li').attr('id')).removeClass('h_cl_X').find('a').hide().parent().parent().slideDown().find('input[name=COMMENT_PARENT_ID]').val($(this).closest('li').attr('id').substr(3));
    tpr = $(this).closest('li').attr('id').substr(3);
    $('[name=COMMENT_PARENT_ID]').val(tpr);
    $('.h_cl_X').remove();
    var isVisible = $('#h_cl_comment').css('display') == 'block';
    //alert($('#h_cl_comment').html());
    if (isVisible)
    {
    	//alert("up");
    	$('#h_cl_comment').slideUp();
    	//$('#h_cl_comment').css('display',"none");
    } else 
    {
    	//$('#answer_to_' + $(this).closest('li').attr('id')).show();
    	//alert("down");
    	$('#h_cl_comment').css('display',"block");
    	//$('#h_cl_comment').show();
    	$('#h_cl_comment').slideDown();
    }

    return false;
  });

  $('.kons_link').click(function() {
    $('#consultingForm').css('display', 'block');
    return false;
  });
  
  $('.leader_link').click(function() {
    $('#consultingForm').css('display', 'block');
    return false;
  });

  var RATES = {};
  $('.bls_rate a').filter(':not(.novote)').click(function() {
    RATES[$(this).attr('rel')] = ($(this).parent().hasClass('bls_r') ? 'M' : 'P');
    $(this).closest('.bls_rate').find('b').load('/ajax-score.php', {
      PID: $(this).attr('rel'),
      SC: ($(this).parent().hasClass('bls_r') ? 'M' : 'P')
    }, function() {
      var thisID = $(this).parent().find('a').attr('rel');
      $(this).parent().find((RATES[thisID] == 'M' ? 'em.bls_r a' : ':not(em.bls_r) a')).addClass('voted');
    });
    return false;
  });

  $('.bls_rate a.novote').click(function() { return false; });

  $('.trp_b').click(function() {
    var t = $(this).closest('div').find('textarea');
    t.val(t.val() + '[b]...[/b]');
    return false;
  });

  $('.trp_k').click(function() {
    var t = $(this).closest('div').find('textarea');
    t.val(t.val() + '[i]...[/i]');
    return false;
  });

  $('.trp_u').click(function() {
    var t = $(this).closest('div').find('textarea');
    t.val(t.val() + '[u]...[/u]');
    return false;
  });

  $('.trp_l').click(function() {
    var t = $(this).closest('div').find('textarea');
    var url = prompt('Введите URL ссылки:');
    var title = prompt('Введите название ссылки:', 'http://');
    t.val(t.val() + '[url ' + url + ']' + title + '[/url]');
    return false;
  });

  $('.trp_p').click(function() {
    var t = $(this).closest('div').find('textarea');
    var url = prompt('Введите адрес изображения:', 'http://www.');
    t.val(t.val() + '[img ' + url + ']');
    return false;
  });

  $('.trp_v').click(function() {
    var t = $(this).closest('div').find('textarea');
    var url = prompt('Введите полный HTML-код вставки ролика:');
    t.val(t.val() + '[video]' + url + '[/video]');
    return false;
  });

  $('.trp_c').click(function() {
    var t = $(this).closest('div').find('textarea');
    t.val(t.val() + '[cut]');
    return false;
  });

  $('.switcher-caro .caro_back, .switcher-caro .caro_next').click(function() {
    var p = $(this).parent();
    if ($(this).hasClass('caro_back')) {
      if ($(p).find('li:hidden').length)
        $(p).find('li:hidden:last').css('display', 'block');
    } else {
      if ($(p).find('li:visible').length > 3)
        $(p).find('li:visible:first').css('display', 'none');
    }

    return false;
  });

  $('.load_foto a').click(function() { $('#avatarForm').css('display', 'block'); return false; });

  $('.send_feedback').click(function() { $('#bonusForm').css('display', 'block'); return false; });

  if ($.fn.lightBox !== undefined) $('a.map').lightBox({
    imageLoading: '/gfx/lb/lightbox-ico-loading.gif',
    imageBtnClose: '/gfx/lb/lightbox-btn-close.gif',
    imageBtnPrev: '/gfx/lb/lightbox-btn-prev.gif',
    imageBtnNext: '/gfx/lb/lightbox-btn-next.gif',
    imageBlank: '/gfx/lb/lightbox-blank.gif'
  });
});
