jQuery.noConflict();

jQuery(document).ready(function() {
    updateComments();
    jQuery('#comment').keyup(function() {
        limitChars('comment', 250, 'lengthInfo');
    });
    jQuery('#author').keyup(function() {
        limitCharsNoCount('author', 40);
    });
    jQuery('#email').keyup(function() {
        limitCharsNoCount('email', 100);
    });
    jQuery('#product').keyup(function() {
        limitCharsNoCount('product', 40);
    });
    function limitChars(textid, limit, infodiv)
    {
        var text = jQuery('#'+textid).val(); 
        var textLength = text.length;
        if(textLength > limit) {
            jQuery('#'+infodiv).html('Limit długości komentarza wynosi <b>'+limit+'</b> znaków');
            jQuery('#'+textid).val(text.substr(0,limit));
            return false;
        } else {
            jQuery('#'+infodiv).html('Pozostało jeszcze <b>'+(limit - textLength)+'</b> znaków.');
            return true;
        }
    }
    function limitCharsNoCount(inputId, inputCharsLimit)
    {
        var inputValue = jQuery('#'+inputId).val();
        var inputLength = inputValue.length;
        if(inputLength > inputCharsLimit) {
            jQuery('#'+inputId).val(inputValue.substr(0,inputCharsLimit));
            return false;
        }
    }
    function clientImageName(mark) {
        if (mark == 4 || mark == 5) {
            return 'zad_kl.png';
        } else if (mark == 3) {
            return 'neu_kl.png';
        } else {
            return 'nieza_kl.png';
        }
    }
    function showComments(xml) {
        jQuery('#comment_box').fadeOut("slow", function() {
            htmlCode = '';
            jQuery("comment",xml).each(function(id) {
                comment = jQuery("comment",xml).get(id);
                htmlCode += "<div class=\"op1\"><p class=\"autor\"><img src=\"images/"+clientImageName(jQuery("mark",comment).text())+"\" alt=\"Ikona klienta\" title=\"Ikona klienta\" class=\"img_kl\" /><span class=\"k9\">"+jQuery("author",comment).text()+" kupił: "+jQuery("product",comment).text()+"</span><br /><div class=\"o_"+jQuery("mark",comment).text()+"\"><span class=\"date\">"+jQuery("date",comment).text()+",</span> Ocena:</div>"+jQuery("content",comment).text()+"</div>";
                htmlCode += "<img src=\"images/linia3.png\" alt=\"WoW pre-paid\" title=\"WoW pre-paid\" height=\"1\" width=\"364\" /><br />";
            });
            jQuery('#comment_box').html(htmlCode).fadeIn("slow");
        });
    }
    function updateComments() {
        jQuery.ajax({
            url: "./backend/getRandomComment.php",
            dataType: "xml",
            success: function(xml){
                showComments(xml);
            }
        });

        setTimeout(updateComments, updateDelay);
    }    
});