$(document).ready(function()
{
  $("a[rel='photogallery']").colorbox();
  $("a[rel='contact']").colorbox({width:"660px", height:"550px", scrolling:"false", iframe:true});
  $("a[rel='map']").colorbox({width:"660px", height:"550px", scrolling:"false", iframe:true});
  $("#click").click(function(){ $('#click').css({"background-color":"#f00", "color":"#fff", "cursor":"inherit"}).text("Open this window again and this message will still be here."); return false; });

  $('#button_new_topic').click(function() { $('#new_topic_box').slideDown('slow'); });

  /*** SCROLLABLE ANCHOR LINKS ***/
  $('a.scroll').click(function()
  {
    var target = $(this).attr('href');

    if($(target).length>0)
    {
      $('html,body').animate({ scrollTop: $(target).offset().top - 100 }, 'slow');
      return false;
    }
  });

  /*** PAGING FORM ***/
  $('#goto_page').submit(function()
  {
    var path = $('#goto_path').val();
    var page = parseInt($('#goto_input').val());

    if(page>0) document.location.href = path.replace('?', page);

    return false;
  });

  $('#comment_add form').not('.create_topic').submit(function(e)
  {
    $.post('/actions/ajax.php?action=add_forum_comment', $(this).serialize(), function(result, b, c)
    {
      if(result.substring(0, 5)!='error')
      {
        $('#comment_add textarea').val('');
        $('#comments div.item:last').after(result);
        $('#comments div.item:last').slideDown('slow');
        $('html,body').animate({ scrollTop: $('#comments div.item:last').offset().top - 100 }, 'slow');
      }
      else if(result=='error name') alert('Zadané meno je registrované, prosím použite iné meno.');
      else alert('Komentár sa nepodarilo odoslať.');
    });

    return false;
  });

  /*** COMMENT - REPLY ***/
  $('#comments a.reply').click(function(e)
  {
    var me = $(this);
    var cid = me.attr('rel');
    var page_id = me.attr('target');
    var mod = me.hasClass('mod');
    page_id = page_id.substring(5, page_id.length);

    if(mod) var username = 'MUDr. Roman Sokol';
    else var username = '';

    var out = '<div id="comment_reply" style="display: none;"><form action="#" class="forum" onsubmit="return ValidateForm(this);">';
    out += '<input type="hidden" name="reply_to" value="' + cid + '" />';
    out += '<input type="hidden" name="page_id" value="' + page_id + '" />';
    out += '<label for="field_username">Vaše meno:</label>';
    out += '<input type="text" id="field_username" name="username" title="Vaše meno" value="' + username + '" maxlength="64" class="required" />';
    out += '<label for="field_comment">Text príspevku:</label>';
    out += '<textarea rows="10" cols="50" id="field_comment" name="comment" title="Text príspevku" class="required markitup"></textarea>';
    out += '<button type="submit">odoslať komentár</button>';
    out += '</form>';
    out += '<div class="clear">&nbsp;</div>';
    out += '</div>';

    var ex = $('#comment_reply');
    if(ex.length>0) ex.slideUp('slow', function() { $(this).remove(); $('#cid-' + cid + ' div.options').before(out); $('#comment_reply').slideDown('slow'); EnableReplyForm(); });
    else { $('#cid-' + cid + ' div.options').before(out); $('#comment_reply').slideDown('slow'); EnableReplyForm(); }

    return false;
  });

  function EnableReplyForm()
  {
    $('#comment_reply form').submit(function(e)
    {
      $.post('/actions/ajax.php?action=add_forum_comment', $(this).serialize(), function(result, b, c)
      {
        if(result.substring(0, 5)!='error')
        {
          $('#comment_reply').slideUp('slow', function() { $(this).remove(); });
          $('#comments div.item:last').after(result);
          $('#comments div.item:last').slideDown('slow', function() { $('html,body').animate({ scrollTop: $('#comments div.item:last').offset().top -100 }, 'slow'); });
        }
        else if(result=='error name') alert('Zadané meno je registrované, prosím použite iné meno.');
        else alert('Komentár sa nepodarilo odoslať.');
      });

      return false;
    });
  }

  /*** REMOVE COMMENT ***/
  $('a.remove_comment').click(function(e)
  {
    if(confirm('Určite?'))
    {
      var cid = $(this).attr('rel');

      $.post('/actions/ajax.php?action=remove_forum_comment', { id: cid }, function(result, b, c)
      {
        if(result=='1') $('#cid-' + cid).slideUp('slow');
        else alert('Operácia zlyhala');
      });
    }
  });
});

function ValidateForm(form)
{
  var x = 0;

  while((x < form.elements.length))
  {
    if(form.elements[x].value=='' && form.elements[x].className.indexOf('required')!=-1)
    {
      alert('Prosím zadajte hodnotu v poli "'+form.elements[x].title+'"');
      form.elements[x].focus();
      return false;
    }

    x++;
  }

  return true;
}
