$(document).ready(function(){

    var siteurl="http://www.ispraznise.mk/";
    //var siteurl="http://localhost/ispraznise.mk/";

    //$("#ss_text").mktransliteration();

    $(".hide").hide();

    $(".searchterm").click(function(){
        $(this).val("");
    })


    $("#username").keyup(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13) { //Enter keycode
            $("#loginform").submit();
        }
    })

    $("#password").keyup(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13) { //Enter keycode
            $("#loginform").submit();
        }
    })
    $("#loginbtn").click(function(){
        $("#loginform").submit();
    })


    $("#zapocni_btn").click(function(){
        $("#submit_pr").slideToggle();
    })

    $("#zatf").click(function(){
        $("#submit_pr").slideUp();
        $("#response1").hide();
        $("#finished").show();
    })

    $("#finished").click(function(){
        submit_praznenje();
	
    })



    var options_form = {
        dataType:  'json',
        //target:        '#output1',   // target element(s) to be updated with server response 
        beforeSubmit:  validateRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    }; 
    $('#ss_form').ajaxForm(options_form);
	
    function submit_praznenje(){
        $('#ss_form').ajaxSubmit(options_form);
    }


    function validateRequest(formData, jqForm, options) {
        // formData is an array; here we use $.param to convert it to a string to display it
        // but the form plugin does this for you automatically when it submits the data
   
	
        
        var psevdonim = formData[0].value;
        //var kategorija = formData[1].value;
        //var pol = formData[2].value;
        var p_text = formData[3].value;
	
	
        if ($.trim(psevdonim) == ""){
            $("#errormsg").html("<span class='error'>Мора да внесеш псевдоним</span>")
            .hide()
            .slideDown('fast');
            $("#psevdonim").addClass('error3').focus();
            return false;
        }
        if ($.trim(p_text) == "" || p_text.length < 50){
            $("#errormsg")
            .html("<span class='error'>Мора да внесеш некој текст, барем 50 букви да има.</span>")
            .hide()
            .slideDown('fast');
            $("#ss_text").addClass('error3');
            return false;
        }
        if (formData.length == 4){
            $("#errormsg").html("<span class='error'>Мора да се согласиш со условите.</span>")
            .hide()
            .slideDown('fast');
            return false;
        }
        $("#finished").hide();
        $("#loading").show();
	
    
        return true;
    }


    $("#psevdonim").keypress(function(){
        $(this).removeClass('error3');
        $("#errormsg").slideUp('fast').html("");
    });
    $("#ss_text").keypress(function(){
        $(this).removeClass('error3');
        $("#errormsg").slideUp('fast').html("");
    })
    $('#iagree').click(function(){
        if ($(this).is(':checked')){
            $("#errormsg").slideUp('fast').html("");
        }
    })


 
    function showResponse(responseText, statusText)  {
        //console.debug(responseText);
        //console.debug(statusText);
        if(statusText == 'success'){
            if(responseText.status == 'Ok'){
                $("#loading").hide();
                $("#response1").show();
                $('#ss_form').resetForm();
            }else{
                $("#loading").hide();
                $("#errormsg").html("<span class='error'>"+responseText+"</span>")
            .hide()
            .slideDown('fast');
            }
        }else{
		$("#loading").hide();
                $("#errormsg").html("<span class='error'>"+responseText+"</span>")
            .hide()
            .slideDown('fast');
        }
    }



    $('.voteup').live("click",function(){
        $(this).parent().addClass("working");
        //$(this).next(".voteload").show();
        $(this).fadeOut("fast");
		
        $.post(siteurl+"ajax.php?task=voteup",{
            e: $(this).attr("rel")
        }, function(data){
            if (data.status=="ok"){
                //$(".working .voteload").hide();
                //$(".working .voteup").fadeOut("slow");
                $(".working .votein").html(data.votes).show();
                //$(".working .vinactive").show();
                $(".working").removeClass("working");
			
            }
        },"json")
    })



    $("#ss_text").keypress(function(){
        var text = $(this).val();
        var num = $(this).val().length;
        if (num > 349){
            var new_text = text.substr(0, 350);
            $(this).val(new_text);
        }
        $("#tcounter").html(num);
    })


    $("#clickbox").mousedown(function(){
        $(this).css("background-color","#5ED4FD");
    }).mouseup(function(){
        $(this).css("background-color","#DEEFF6");
        var n = parseInt($("#clickboxrez").html());
        $("#clickboxrez").html(n+1);
    });

    $(".reglink").click(function(){
        $("#register").slideToggle();
        $("#login").slideToggle();
	
        return false;
    })


    $("#fukbtn").click(function(){
        ispratiFuk();
        return false;
    })

    $("#nakoj").keyup(function(){
        $("#fukerr2").slideUp();
        $("#fukerr").slideUp();
    })

    $("#nakoj").keyup(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        if(code == 13) { //Enter keycode
           ispratiFuk();
        }
    })

    function izbrisiVisok(){
        if ($(".ff").length > 7){
            $(".ff:last").remove();
        }
    }

    function ispratiFuk(){
        $("#fukbtn").hide();
        $(".fukl").show();
        var nakoj = $("#nakoj").val();
        if (nakoj.length < 1){
           $("#fukerr2").slideToggle();
           $("#fukbtn").fadeIn();
           $(".fukl").hide();
           return false;
        }

        $.post(siteurl+"ajax.php?task=fukni",{
            nakoj: nakoj
        }, function(data){
            if (data.logiran == "ne"){
                $("#fukerr").slideDown();
                $(".fukl").hide();
                $("#nakoj").val("");
                $("#fukbtn").fadeIn();
            }else{
                $(".fukl").hide();
                $("#nakoj").val("");
                var element = "<div class=\"ff\"><span class=\"fukuser\">"+data.username+"</span> му го фукна на:<br/><span class=\"n\">"+nakoj+"</span></div>";
                $(".ff:first").before(element);
                $(".ff:first").hide().show('slow');
                $("#fukbtn").fadeIn();
                izbrisiVisok();
            }

        },"json")
    }



    // ajax za kategoriiite
    /*
$(".catlink").click(function(){
	$("#sevcituva").show();
	$(".catlink").removeClass("active");
	$(this).addClass("active");
	$.get("ajax.php?task=getpbycat",{
		cat_id: $(this).attr("rel")
	}, function(data){
			$(".leftbox").html(data);
			$("#sevcituva").hide();
		}		
	)
	return false;
})
*/


    function isValidEmailAddress(emailAddress) {
        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return pattern.test(emailAddress);
    }

    $(".comment_content").focus(function(){
        $(".error").slideUp().html("");
        $(this).animate({
            'height':68
        },300, function() {
            // Animation complete.
        });
       
    })

    $(".sendcoment").click(function(){
        var context=$(this).parent().parent();

        $(".comload",context).show();
        
        var comment=$(".comment_content",context).val();
        //var praznenje_id=$("#praznenje_id").val();
        var praznenje_id=$("#praznenje_id").val();
        var comment_type=$(".comment_type",context).val();
        
        $.post(siteurl+'ajax.php?task=addcom',
        {
            comment: comment,
            comment_type: comment_type,
            praznenje_id: praznenje_id
        }, function(data){
            
            var errorplaceid = data.errorplace;
            var errors='';
            if(data.error.length>0){
                for(var i=0;i<data.error.length;i++){
                    errors = errors + data.error + "<br/>";
                }

                $("#"+errorplaceid).html(errors).slideDown();
            }else{
                $("#"+errorplaceid).before(data.comment);
            }
            $(".comload",context).hide();
            $(".comment_content",context).val("");
        }, "json")
    })



}) //end document ready