$(document).ready(function() {
    $("*:has(>div.hint)").mouseover(function() {
        $(this).tipbox("hint", "tipbox");
    });

    $("p.log .login").click(function() {
        $("p.log").css("display", "none");
        $("#login").css("display", "block");
    });

    $("#stornoLogin").click(function() {
        $("p.log").css("display", "block");
        $("#login").css("display", "none");
    });

    $("#loginRegister").click(function() {
        $("#registerLogin").toggle("fast");
    });

    $("#notification").click(function() {
        $("#mail-info").toggle("fast");
    });

    $("#articleLogin input:not(:submit), #registerLogin input:not(:submit)").focus(function() {
        $(this).val('');
    });

    $("#sort").change(function() {
        $("#sortForm").submit();
    });

    if(document.getElementById("success")) {
        $("#success").fadeOut(5000);
    }

    $(".close_error_mail").click(function() {
        $(".error_mail").css("display", "none");
    });

    $("input#email").blur(function() {
        if(this.value != "") {
            $.ajax({
                url: "../scripts/common_ajax.php",
                data: {"action":"checkEmail","email":this.value},
                success: function(data) {
                    if(data == "1") {
                        $("#error_mail_1").css("display", "block");
                        $("#error_mail_2").css("display", "none");
                    } else if(data == "2") {
                        $("#error_mail_2").css("display", "block");
                        $("#error_mail_1").css("display", "none");
                    } else {
                        $("#email_error").html(data);
                        $("#error_mail_1").css("display", "none");
                        $("#error_mail_2").css("display", "none");
                    }
                }
            });
        } else {
            $("#email_error").html('<span class="hint">Nápověda:</span> <span>E-mail slouží jako přihlašovací údaj.</span>');
        }
    });

    $("input#url").blur(function() {
        if(this.value != "") {
            $.ajax({
                url: "../scripts/common_ajax.php",
                data: {"action":"checkUrl","url":this.value},
                success: function(data) {
                    if(data) {
                        $("#url_error").html(data);
                    }
                }
            });
        } else {
            $("#url_error").html('<span class="hint">Nápověda:</span> <span>Povolena jsou pouze malá písmena, čísla a pomlčka.</span>');
        }
    });

    $("#editreader").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php",
            data: {"action":"edit_reader"},
            success: function(data) {
                if(data) {
                    $("#replace").replaceWith(data);
                }
            }
        });
    });

    $("#editProfileReader").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php?action=save_reader",
            type: "POST",
            data: {"fname":$("#fname").val(),
                   "lname":$("#lname").val(),
                   "email":$("#email").val(),
                   "mobile":$("#mobile").val(),
                   "passwd":$("#passwd").val(),
                   "repasswd":$("#repasswd").val()
                  },
            success: function(data) {
                if(data) {
                    $("#replace").replaceWith(data);
                    $("#success").fadeOut(5000);
                }
            }
        });
        return false;
    });

    $("#editblogger").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php",
            data: {"action":"edit_blogger"},
            success: function(data) {
                if(data) {
                    $("#replace").replaceWith(data);
                }
            }
        });
    });

    $("#editProfileBlogger").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php?action=save_blogger",
            type: "POST",
            data: {"email":$("#email").val(),
                   "passwd":$("#passwd").val(),
                   "repasswd":$("#repasswd").val(),
                   "street":$("#street").val(),
                   "city":$("#city").val(),
                   "zip":$("#zip").val(),
                   "title":$("#title").val(),
                   "county":$("#county").val(),
                   "account":$("#account").val()
                  },
            success: function(data) {
                if(data) {
                    $("#replace").replaceWith(data);
                    $("#success").fadeOut(5000);
                }
            }
        });
        return false;
    });

    $("#editdesc").live("click", function() {
        $(this).css("display", "none");
        $("#desc1").css("display", "none");
        var desc = $("#desc1").html();
        $("#desc2 textarea").val(desc.replace(/<br>/g, ''));
        $("#desc2").css("display", "block");
    });

    $("#savedesc").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php?action=save_blogger_desc",
            type: "POST",
            data: {"description":$("#desc2 textarea").val()},
            success: function(data) {
                $("#desc1").html(data);
                $("#desc2").css("display", "none");
                $("#desc1").css("display", "block");
                $("#editdesc").css("display", "block");
            }
        });
    });

    $("#editprofile").live("click", function() {
        $(this).css("display", "none");
        $("#profile1").css("display", "none");
        var profile = $("#profile1").html();
        $("#profile2 textarea").val(profile.replace(/<br>/g, ''));
        $("#profile2").css("display", "block");
    });

    $("#saveprofile").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php?action=save_blogger_profile",
            type: "POST",
            data: {"profile":$("#profile2 textarea").val()},
            success: function(data) {
                $("#profile1").html(data);
                $("#profile2").css("display", "none");
                $("#profile1").css("display", "block");
                $("#editprofile").css("display", "block");
            }
        });
    });

    $("#editeditorial").live("click", function() {
        $(this).css("display", "none");
        $("#editorial1").css("display", "none");
        var editorial = $("#editorial1").html();
        $("#editorial2 textarea").val(editorial.replace(/<br>/g, ''));
        $("#editorial2").css("display", "block");
    });

    $("#saveeditorial").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php?action=save_blogger_editorial",
            type: "POST",
            data: {"description":$("#editorial2 textarea").val()},
            success: function(data) {
                $("#editorial1").html(data);
                $("#editorial2").css("display", "none");
                $("#editorial1").css("display", "block");
                $("#editeditorial").css("display", "block");
            }
        });
    });

    $("#publicateeditorial").live("click", function() {
        $.ajax({
            url: "../scripts/common_ajax.php?action=save_blogger_editorial_publication",
            type: "POST",
            data: {"description":$("#editorial2 textarea").val()},
            success: function(data) {
            //alert(data);
                $("#editorial1").html(data);
                $("#editorial2").css("display", "none");
                $("#editorial1").css("display", "block");
                $("#editeditorial").css("display", "block");
            }
        });
    });

    $(".delete").click(function() {
        if(window.confirm("Opravdu smazat?")) {
            return true;
        } else {
            return false;
        }
    });

    $("a.answer").click(function() {
        var ths = $(this);
        var id = ths.parents("li").attr("id");
        var buf = id.split("-");

        $.ajax({
            url: "../../scripts/common_ajax.php",
            data: {"action":"reply", "forum":buf[1]},
            success: function(data) {
                if(data) {
                    $("div.answer").remove();
                    ths.parent().after(data);
                }
            }
        });

    });

    $("#defaultPhoto").click(function() {
        $("#profile table").css("display", "none");
        $("#defaultPhoto").css("display", "none");
        $("#loadPhoto").css("display", "block");
    });
    
	/*$('.soutez_inic').click(function(){
      $('.popup').slideDown();
    });*/
    
    $('.soutez_letak').click(function(){
      $('.popup').slideUp('slow');
    });
    
    $('.kategorie_list').click(function(){    
      $('.popup-kategorie').slideUp('slow');
    });
    $('.library_list').click(function(){    
      $('.popup-library').slideUp('slow');
    });
	
	$('.iniquity-open').click(function(){	  
      $('.iniquity').slideDown('slow');
    });
	$('.iniquity-close').click(function(){
      $('.iniquity').slideUp('slow');
    });

	$('.infobox a').click(function(){
      $('.infobox').hide('slow');  
    });

  // Seznamka - pridat inzerat
  if ($("#failure").length == 0)
    $("#newadv").hide();

  $("#addadv").click(function() {
    if ($(".show_update_user_on_click").length == 0) {
      // spravne vyplneny profil bloggera
      if ($("#newadv").is(":hidden")){
        $("#newadv").css('visibility','visible');
        $("#newadv").slideDown("slow");
      } else {
        $("#newadv").css('visibility','hidden');
        $("#newadv").slideUp("slow");
      }
    } else {
      // chybne vyplneny profil bloggera
      $("#update-user").css('visibility','visible');
      $("#update-user").show();
    }
  });

  $("#cancel").click(function(){
    $("#update-user").css('visibility','hidden');
    $("#update-user").hide();
    return false;
  });
  
  if ($("#failure").length == 0)
    $("#inbox-new-message-form").hide();
  else
    $("#inbox-new-message-form").css('visibility','visible');
  
  $("#inbox-new-message").click(function() {
    if ($("#inbox-new-message-form").is(":hidden")){
      $("#inbox-new-message-form").css('visibility','visible');
      $("#inbox-new-message-form").show();
    } else {
      $("#inbox-new-message-form").css('visibility','hidden');
      $("#inbox-new-message-form").hide();
    }
  });
  $(".inbox-text").hide();
  $(".inbox-line").show();
  $(".inbox-line").css('visibility','visible');
  $(".inbox-line").fadeIn("slow");

  $(".inbox-line").click(function() {
    if ($(this).children("td").children(".inbox-perex").is(":hidden")){
      $(this).children("td").children(".inbox-perex").show();
      var id = $(this).attr("id");
      var id1 = id.substring(11,id.length);
      var id_text = "#inbox_line_"+id1+"_text";
      id1 = "#inbox_line_edit_"+id1;
      $(id_text).hide();
      id = "#"+id+"_footer";
      $(id1).hide();
      $(id).hide();
      $(this).children("td").children(".inbox-text").hide();
    } else {
      $(this).children("td").children(".inbox-perex").hide();
      var idd = $(this).attr("id");
      var id = idd.substring(11,idd.length);
      element = "#"+idd+"_footer";
      id_text = "#"+idd+"_text";
      $(id_text).show();
      $(element).show();
      $(this).children("td").children(".inbox-text").show();

      var root = $("#root").attr("href");

      $.ajax({
          url: root+"scripts/common_ajax.php?action=read_message",
          data: {"message":id},
          success: function(data) {
              $("#inbox_line_text_"+data).css('font-weight','normal'); 
          }
      });

    }
  });

  $(".seznamka_reply_form").hide();
  $(".seznamka_reply_link").click(function() {
    var id = $(this).attr("id");
    id = "#"+id+"_form";
    if ($(id).is(":hidden")){
      $(id).css('visibility','visible');
      $(id).show();
    } else {
      $(id).css('visibility','hidden');
      $(id).hide();
    }
  });

  if ($(".reply_error").length != 0){
    if ($(".reply_error").attr("id")){
      var elem = "#inbox_line_edit"+$(".reply_error").attr("id");
      $(elem).show();
      location.href = location.href+"#error";
      if ($(".error").length != 0){
        $(".error").css('top',elem.position().top);
      }
    } else {
      var parent = $(".reply_error").parent();
      var parent_id = "#" + parent.attr("id");
      location.href = location.href+"#error";
      if ($(".error").length != 0){
        $(".error").css('top',parent.position().top);
      }
      $(parent_id).css('visibility','visible');
      $(parent_id).show();
    }
  }
  $("#add_inzerat_title").hide();
  $("#seznamka-title-new-adv").click(function() {
    if ($("#add_inzerat_title").is(":hidden")){
      $("#add_inzerat_title").css('visibility','visible');
      $("#add_inzerat_title").show();
    } else {
      $("#add_inzerat_title").hide();
      $("#add_inzerat_title").css('visibility','hidden');
    }
  });
  $(".inbox_delete, .moje_delete").click(function(){
    return confirm("Opravdu smazat?");
  }); 

  $("#deleteprofil").click(function(){
    if (confirm("Opravdu si přejete smazat profil?")){
      window.location.replace(document.url+"?deleteprofile=tajnyhash");
    }
  }); 
  
});

function reply(line){
    var id1 = "#inbox_line_edit_"+line;
    $(id1).show();
}

function showList(){
	$('.popup').slideDown();
}

function showKategorie(){
	$('.popup-kategorie').slideDown();
}
function showLibrary(){
	$('.popup-library').slideDown();
}


function openPreview(url) {
    window.open(url);
}




function counter(obj, num, i) {
    if(!i) i = "";
    var chars = num ? num : 300;
    var str = obj.value;
	var strlen = str.length;

    $("#counter"+i).text(strlen);
    if(strlen > chars) {
        obj.value = obj.value.substring(0, chars);
        $("#counter"+i).text(chars);
    }
}

