$(document).ready(
  function ()
  {
    vote_init();

    if ($('.lit_topicrow_bb').length > 0)
    {
      $('.lit_topicrow_bb').hover(
        function ()
        {
          $('em',$(this)).css({color:'#6699CC'});
          $('.lit_bb_img',$(this)).attr('src_normal',$('.lit_bb_img',$(this)).attr('src'));
          $('.lit_bb_img',$(this)).attr('src',$('.lit_bb_img',$(this)).attr('src_hover'));
        },
        function ()
        {
          $('em',$(this)).css({color:'#666666'});
          $('.lit_bb_img',$(this)).attr('src',$('.lit_bb_img',$(this)).attr('src_normal'));
        }
      ).click(
        function ()
        {
          if ($(this).attr('url') != '')
          {
            window.location.href = $(this).attr('url');
          }
          else
          {
            textarea = $('#comments_' + $(this).attr('topic_id'));
            textarea.attr('location', '/index.php?&expired_or_spam&type='+$(this).attr('type')+'&topic_id='+$(this).attr('topic_id'));
            textarea.fadeIn('fast');
            if ($(this).attr('type') == 'expired')
            {
              $('.spanexp_reason',textarea).hide();
            }
            else
            {
              $('.spanexp_reason',textarea).show();
            }
            $('em',$(this)).text($('em',$(this)).attr('after_click_text'));
          }
        }
      );
    }
    
    
    if ($('.select_all').length > 0)
    {
      $('.select_all').click(
        function()
        {
          $('.inp_checkbox',$(this).parent().parent()).attr('checked',true);
        }
      );
      $('.unselect_all').click(
        function()
        {
          $('.inp_checkbox',$(this).parent().parent()).attr('checked',false);
        }
      );
    }

    setTimeout(
      function()
      {
        $('h2').fadeIn('slow');
      }
    ,300);

    $('#cancel',$('.spanexp_comments')).click(
      function ()
      {
        $('.expspam_explayn',$(this).parent()).val('');
        $(this).parent().fadeOut('fast');
      }
    );
    $('#ok',$('.spanexp_comments')).click(
      function ()
      {
        var parent   = $(this).parent();
        var textarea = $('.expspam_explayn',parent);

        $.post(parent.attr('location'), {
          text  :  textarea.val(),
          reason  : $('#reason_id',parent).val()
        })

        textarea.val('');
        parent.fadeOut('fast');
      }
    );

    $('.post_button').hover(
      function()
      {
        $(this).css({'background-position':'bottom'});
      },
      function()
      {
        $(this).css({'background-position':'top'});
      }
    )
  }
);


function refresh_onmouse(block,e)
{
  var offset = block.offset();
  x = e.clientX - offset.left;
  // block is .vote_box

  for (i = 0; i < 5; i++)
  {
    if (x > 25*(i))
    {
      $('.star[id="'+i+'"]',block).attr('src','/styles/prosilver/theme/images/z'+i+'.png');
    }
  }
}

function mo(text)
{
  $('#mo').append(' '+text);
}

function vote_init()
{
    var ee;
    $('.vote_box').mousemove(
      function(e)
      {
        clearTimeout($(this).attr('timerout'));
        clearTimeout($('.caption',$(this)).attr('timerout'));
        ee = e;
        $(this).attr('timerout','');
        if ($(this).attr('showed') == 'average' && $(this).attr('allow') == '1')
        {
          $(this).attr('showed','new');
          $('.stars',$(this)).attr('change','true');
          for (i = 0; i < 5; i++)
          {
            $('.star[id="'+i+'"]',$(this)).fadeOut(300 + ((i+1)*180),
              function ()
              {
                $(this).attr('src','/styles/prosilver/theme/images/z.png');
              }
            );
          }

          for (i = 0; i < 5; i++)
          {
            $('.star[id="'+i+'"]',$(this)).fadeIn(1000 - ((i+1)*250),
              function()
              {
                $(this).attr('showed','new');
                var temp = $(this).parent();
                if ($(this).attr('id') == 4)
                  setTimeout(
                    function()
                    {
                      temp.attr('change','false');
                      refresh_onmouse(temp.parent(),ee);
                    }
                  ,100);
              }
            );
          }
          var temp = $(this);
          setTimeout(
            function ()
            {
              $('.caption',temp).fadeOut('slow',
                function()
                {
                  var temp = $(this);
                  setTimeout(
                    function ()
                    {
                      temp.text('Your Rating').fadeIn('slow');
                    }
                  ,200);
                }
              );
            }
          ,2000);
        }
        else
        {
          if ($(this).attr('allow') == '0' || $(this).attr('allow') == 'no_login')
          {
            if ($(this).attr('allow') == '0')
              var text = 'You already voted';
            if ($(this).attr('allow') == 'no_login')
              var text = 'Login to Vote';
            clearTimeout($('.caption',$(this)).attr('timerout'));
            if ($('.caption',$(this)).attr('now_show') != 'already')
            {
              $('.caption',$(this)).fadeOut('fast',
                function()
                {
                  var temp = $(this);
                  setTimeout(
                    function()
                    {
                      temp.text(text).attr('now_show','already').fadeIn('fast');
                      temp.attr('timerout',
                        setTimeout(
                          function()
                          {
                            temp.fadeOut('fast',
                              function()
                              {
                                setTimeout(
                                  function()
                                  {
                                    temp.text('Rating Average ('+temp.parent().attr('vote_count')+')').attr('now_show','average').fadeIn('fast');
                                  }
                                ,200);
                              }
                            );
                          }
                        ,10000)
                      );
                    }
                  ,200);
                }
              );
            }
          }
        }
      }
    ).mouseout(
      function()
      {
        clearTimeout($(this).attr('timerout'));
        var temp = $(this);
        $(this).attr('timerout',
          setTimeout(
            function()
            {
              $('img',temp).attr('showed','average');
              if (temp.attr('showed') == 'new')
              {
                temp.attr('showed','average');
                for (i = 0; i < 5; i++)
                {
                  $('.star[id="'+i+'"]',temp).fadeOut(300 + ((i+1)*180),
                    function()
                    {
                      $(this).attr('src',$(this).attr('real_src'));
                    }
                  );
                }
                for (i = 0; i < 5; i++)
                {
                  $('.star[id="'+i+'"]',temp).fadeIn(1000 - ((i+1)*250));
                }
                setTimeout(
                  function ()
                  {
                    $('.caption',temp).fadeOut('slow',
                      function()
                      {
                        var temp = $(this);
                        setTimeout(
                          function ()
                          {
                            temp.text('Rating Average ('+temp.parent().attr('vote_count')+')').fadeIn('slow');
                          }
                        ,200);
                      }
                    );
                  }
                ,2000);
              }
            }
          ,5000)
        );
      }
    );

    $('.star').mouseover(
      function()
      {
        clearTimeout($(this).parent().attr('timerout'));
        if ($(this).parent().parent().attr('showed') == 'new')
        {
          for (i = 0; i < 5; i++)
          {
            if (i <= $(this).attr('id') && $(this).parent().attr('change') == 'false')
            {
              if ($('#'+i,$(this).parent()).attr('showed') == 'new')
                $('#'+i,$(this).parent()).attr('src','/styles/prosilver/theme/images/z'+i+'.png');
            }
            else
            {
              if ($('#'+i,$(this).parent()).attr('showed') == 'new')
                $('#'+i,$(this).parent()).attr('src','/styles/prosilver/theme/images/z.png');
            }
          }
        }
      }
    ).mouseout(
      function()
      {
        var temp = $(this).parent();
        $(this).parent().attr('timerout',
          setTimeout(
            function()
            {
              $('.star[showed="new"]',temp).attr('src','/styles/prosilver/theme/images/z.png');
            }
          ,1000)
        );
      }
    ).click(
      function()
      {
        if ($(this).parent().parent().attr('allow') == '1')
        {
          topic_id = $(this).parent().parent().attr('topic_id');
          value    = $(this).attr('value');
          var parent   = $(this).parent().parent().parent();
          $.get('/index.php?&func=do_vote&topic_id='+topic_id+'&value='+value,
            function(data)
            {
              parent.html(data);
              vote_init();
            }
          );
        }
      }
    );

    $('.vote_box').each(
      function()
      {
        var reiting = $(this).attr('reiting');
        $('.star',$(this)).attr('src','/styles/prosilver/theme/images/z.png').attr('real_src','/styles/prosilver/theme/images/z.png');;

        if (reiting > 0)
        {
          cur_star = $('.star:first',$(this));
          i = 0;
          while (i < reiting)
          {
            cur_star.attr('src','/styles/prosilver/theme/images/z'+i+'.png').attr('real_src','/styles/prosilver/theme/images/z'+i+'.png');
            i++;
            cur_star = cur_star.next();
          }
        }
      }
    );

    $('form').bind('submit',
      function ()
      {
        var result = true;
        var form   = $(this);
        $('.field_zirka',form).each(
          function ()
          {
            if ($(this).parent().is(':visible') && $(this).parent().parent().is(':visible') && $(this).is(':visible'))
            {
              if ($(this).attr('name').indexOf('mail') > -1)
              {
                var filter_mail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
                if (!filter_mail.test($(this).val()))
                {
                  $(this).focus();
                  result = false;
                  alert('Please enter a valid email address.')
                  return false;
                }
              }
              else
              {
                if ($(this).val() == '')
                {
                  $(this).focus();
                  text = $(this).prev().text();
                  if (text == '' && $(this).attr('custom_text'))
                  {
                    text = $(this).attr('custom_text');
                  }
                  result = false;
                  alert('All mandatory fields must be field in. ('+text+')')
                  return false;
                }
              }
            }
          }
        );
        return result;
      }
    );
}


