
var timer;

function rozwin(id,button1Id,button2Id) {
  $("#"+id).toggle("slow");
}

function guzik(g1,g2) {
  $("#"+g1).toggle();
  $("#"+g2).toggle();
}

  $("document").ready(function() {

  $('div.lastRegistredUser, div.highRankedJournal, .geoblogBox').mouseover(function(){
      $(this).css('background','#FCF5DC');
  });

  $('div.lastRegistredUser, div.highRankedJournal, .geoblogBox').mouseout(function(){
      $(this).css('background','#FFFFFF');
  });

  if ($("input#plecg").attr("checked")==true) {
    $(".genderPerson").hide();
  } else {
    $(".genderGroup").hide();
  }



    $("input[name='user_gender'], label.genderLabel").click(function() {
        if ($("input#plecg").attr("checked")==true) {
          $(".genderPerson").hide();
          $(".genderGroup").show();
          $("#imienazwisko").slideUp();
          $("#imiona").slideDown();
      } else {
          $(".genderPerson").show();
          $(".genderGroup").hide();
          $("#imiona").slideUp();
          $("#imienazwisko").slideDown();
      }
    });

/*       $("input[name='user_login']").keyup(

        function() {
          clearTimeout(timer);
          timer = setTimeout(function() {

          var login = $("input[name='user_login']").val();
          $.ajax({
            type: "POST",
            url: "/rejestracja-sprawdz-login",
            data: "l=" + login,
            success: function(xml){
              if (xml=='error') {
                $("input[name='user_login']").css('border','1px solid red');
                $("#labelLogin").html('<span class="red">Login jest zajęty</span>')
              } else {
                $("input[name='user_login']").css('border','1px solid #06aae5');
                $("#labelLogin").html('Login')
              }
            }
          });
          }, 1000);
        }); */

    $("input[name='user_avatar']").change(function() {
      checkInputValue('avatar','labelAvatar',$(this));
    });

    $("input[name='user_email']").keyup(function() {
      checkInputValue('email','labelEmail',$(this));
    });

    function substr_count(string,substring,start,length)
    {
     var c = 0;
     if(start) { string = string.substr(start); }
     if(length) { string = string.substr(0,length); }
     for (var i=0;i<string.length;i++) {
       if(substring == string.substr(i,substring.length))
         c++;
      }
     return c;
    }


    $("textarea[name='journeyBody']").keyup(function() {
/*         if ( (substr_count($(this).val(),'\n'))>2) {

        } */
    });

    $("input[name='user_password']").blur(function() {
      if ($(this).val().length < 5) {
        $(this).css({'border':'1px solid red','background':'#FDE2E3'});
        $("#labelPassword").html('<span class="red">Za krótkie hasło</span><br/><span style="font-size: 7pt;">(min. 5 znaków)</span>');
      } else {
        $(this).css( {'border':'1px solid #06aae5','background':'#FFFFFF'});
        $("#labelPassword").html('Hasło <span style="font-size: 7pt;">(min. 5 znaków)</span>');
      }
    });

    $("input[name='user_password2']").focus(function() {
      $(this).css( {'border':'1px solid #06aae5','background':'#FFFFFF'});
      $("#labelPassword2").html('Powtórz hasło');
    });

    $("input[name='user_password2']").blur(function() {
      if ($(this).val()!=$("input[name='user_password']").val()) {
        $(this).css({'border':'1px solid red','background':'#FDE2E3'});
        $("#labelPassword2").html('<span class="red">Hasła nie są takie same</span>');
      } else {
        $(this).css( {'border':'1px solid #06aae5','background':'#FFFFFF'});
        $("#labelPassword2").html('Powtórz hasło');
      }
    });

});

  function checkInputValue(t,l,h) {
    clearTimeout(timer);
    timer = setTimeout(function() {
      $.ajax({
        type: "POST",
        url: "/rejestracja-waliduj-pola",
        data: "t="+t+"&v="+h.val(),
        success: function(result) {
          var array = result.split(';');
          if (array[0]!='ok') {
            h.css({'border':'1px solid red','background':'#FDE2E3'});
            $("#"+l).html('<span class="red">'+array[1]+'</span>');
          } else {
            h.css( {'border':'1px solid #06aae5','background':'#FFFFFF'});
            $("#"+l).html(array[1]);
          }
        }
      });
      }, 1000);
  }

function reloadWebsite() {
  location.reload();
}

function drawCommentForm() {
  var sWidth = $(document).width();
  var sHeight = $(document).height();
  var margin = (sWidth - 443) / 2;

    $.ajax({
     type: "GET",
     url: "/dodaj-komentarz/"+eId,
     /* url: "/content/commentForm.php", */
     success: function(data){
       $("body").prepend(data);
       $("#commentFormContainer").css("height",sHeight+"px");
       $("#commentFormContainer").css("width",sWidth+"px");
       $("#commentFormContainer, #komentarzAnuluj").click(function() {
         closeCommentForm();
       });
       $("#commentForm").css("left",margin+"px");
       $("#komentarzDodaj").click(function() {
         var body      = $("#commentBody").val();
         var signature = $("input[name = commentSignature]").val();
         $.ajax({
          type: "POST",
          url: "/dodaj-komentarz",
          /* url: "/logic/addComment.php", */
          data: "b=" + body + "&s=" + signature+"&id="+eId,
          success: function(xml){
            $("#commentBody").removeClass('body_error');
            $("#commentSignature").removeClass('signature_error');
            $(xml).find('content').each(function(){
              $(xml).find('errors').find('error').each(function(){
                if ($(this).text()=='body') $("#commentBody").addClass('body_error');
                if ($(this).text()=='signature') $("#commentSignature").addClass('signature_error');
              });
              $(xml).find('succes').each(function() {
              closeCommentForm();
              setTimeout("reloadWebsite()",1500);
              });
            });
          }
        });
      });
     }
    });

}

function closeCommentForm() {
  $("#commentBody").val("");
  $("#commentSignature").val("");
  $("#commentForm").slideUp(1000,function() {
    $("#commentFormContainer").slideUp(500);
  });
  return false;
}

function showCommentForm() {
  $("#commentFormContainer").slideDown(1000,function() {
    $("#commentForm").slideDown(500);
  });

}

function  redirectUrl(typ,v) {
  if (v!='') {
  window.location="/"+typ+"/"+v;
  } else {
    window.location="/"+typ;
  }
}

function showYT(id,key) {
  if ($(".yt"+id).html() !='') {
    $(".yt"+id).html('');
  } else {
    $(".yt"+id).html('<div style="padding-bottom: 50px;"><object width="560" height="340"><param name="movie" value="http://www.youtube.com/v/'+key+'&hl=pl&fs=1&color1=0xe1600f&color2=0xfebd01"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+key+'&hl=en_GB&fs=1&color1=0xe1600f&color2=0xfebd01" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="560" height="340"></embed></object></div>');
  }
}