function addHints() {
    $(".hint").hover(
        function(evt) {
            var text = $(this).parent().parent().find('.form_input').attr('alt');
            var div = '<div class="hint_div" id="hint_div">' + text + '</div>';
            $("#content").append(div).find("#hint_div").css("left", evt.pageX).css("top", evt.pageY).corner("10px");
    },
        function(evt) {
            $("#hint_div").remove();
    });
}

$(document).ready( function () {

        function updateComments(id) {
            $.ajax({
                url : "/main/getcomments?id=" + id,
                success: function(data) {
                    //data = $.parseJSON(data.substring(data.indexOf("["), data.lastIndexOf("]") + 1));
                    data = $.parseJSON(data);
                    var result = "";
                    for (i = 0; i < data.length; i++) {
                       var comment = data[i];
                        result +=  "<div class='comment'> \
                                   <span class='comment_author'> " + comment.author + " </span>: <span class='comment_title'>" + comment.title + "</span> <span class='comment_date'>" + comment.date + "</span><br/> \
                                   <span class='comment_text'> " + comment.text + " </span><br/> \
                                   </div> <br/>";
                    }
                    $("#comments_folder").html(result).hide().fadeIn('slow');
                }
            });
            $("#comment_form_info_id").attr('value',id);
        }

        $("#current_picture").load( function () {
            $(this).stop(true,true).fadeTo("slow",1);
            if ($(this).height() < 480)
                $(this).css("margin-top", (480 - $(this).height()) / 2);
            else
                $(this).css("margin-top", "0");
        })

        //Ajax picture loader
        $(".picture_link").click( function() {
            var id = $(this).attr('id').split('|');

            var img_src = '/main/getimage?fn=' + id[1];

            if ($("#current_picture").attr("src") == img_src)
                return false;

            $("#current_picture")
                .stop(true,true)
                .fadeTo("fast",0.1)
                .attr("src", img_src)
                .parent()
                .attr("href", img_src);

            currentId = id[0];
            updateComments(id[0]);

            var codes = '\
                <table width="100%">\
                    <tr>\
                        <td align="left">' + linkText + ':</td>\
                        <td width="100%"><input style="width:100%" type="text" value="http://pic4.org/' + galleryId + '?first=' + id[2] + '" onClick="select();"></td>\
                    </tr><tr>\
                        <td align="left">BBCode ' + pictureText + ':</td>\
                        <td><input style="width:100%" type="text" value="[URL=http://pic4.org/' + galleryId + '?first=' + id[2] + '][IMG]http://pic4.org/storage/' + id[1] + '[/IMG][/URL]" onClick="select();"></td>\
                    </tr><tr>\
                        <td align="left">BBCode ' + previewText + ':</td>\
                        <td><input style="width:100%" type="text" value="[URL=http://pic4.org/' + galleryId + '?first=' + id[2] + '][IMG]http://pic4.org/smalls/' + id[1] + '[/IMG][/URL]" onClick="select();"></td>\
                    </tr><tr>\
                        <td align="left">HTML:</td>\
                        <td><input style="width:100%" type="text" value="&lt;a href=&quot;http://pic4.org/' + galleryId + '?first=' + id[2] + '&quot;&gt;&lt;img src=&quot;http://pic4.org/storage/' + id[1] + '&quot; border=&quot;0&quot; alt=&quot;&quot; /&gt;&lt;/a&gt;" onClick="select();"></td>\
                    </tr>\
                </table>';
            $("#codes").html(codes);

            $.ajax({
                url : '/api/getInfo?imgId=' + id[0],
                success : function(result) {
                    var info = $.parseJSON(result);
                    $("#description_text").html(info.description);
                }
            });
            return false;
        });

        //Ajax authorization
        $("#password_form").submit( function() {
            $(this).ajaxSubmit({
                success: function() {
                    location.reload();
                }
            });
            return false;
        });

        $("#navi_next").click( function() { return false; } );
        $("#navi_prev").click( function() { return false; } );

        //Send the comment form
        $("#comment_form_form").submit( function() {
            $(this).ajaxSubmit({
                success: function (response) {
                    response = $.parseJSON(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1));
                    if (response.result == "success") {
                        updateComments(currentId);
                        $("#comment_errors").html("");
                    } else {
                        $("#comment_errors").html(formErrorText);
                    }
                }
            });
            return false;
        });

        $("#get_codes").click( function () {
            $("#codes").stop(true,true).toggle(200);
            return false;
        });

});
