$(document).ready(function() {
    $('#PicScoreForm').click(function() {
        $('#PicScoreForm').submit();
    });
});

//HoH "namespace"
if(hoh === undefined)
{
    var hoh = {};
}

hoh.loggingEnabled = (window.console !== undefined) && (window.console.log !== undefined) && (window.console.info !== undefined);

hoh.info = function(message)
{
    if(hoh.loggingEnabled)
    {
        console.info(message);
    }
}

hoh.baseURL = '/index.php';

hoh.commentBaseURL = hoh.baseURL + '/comments';

hoh.CommentDAO = function(url)
{
    var daoBaseURL = (url === undefined ?  hoh.commentBaseURL : url);
    
    function doAjax(action, params, success, error)
    {
        var type = 'POST';
        
        params["_method"] = type;
        
        var undefs = [];
        
        $.ajax({
            url: daoBaseURL + '/' + action,
            data: params,
            type: type,
            dataType: 'html',
            timeout: 1000,
            success: success
        });
    };
    
    this.getByPicID = function(picID, callback)
    {
        doAjax('getForPic/' + picID, {}, callback);
    };
    
    this.save = function(picID, replyTo, author, contents, callback)
    {
        var params = {};
        
        if(picID !== undefined)
        {
            params["data\[Pic\]\[id\]"] = picID;
        }
        
        if(replyTo !== undefined)
        {
            params["data\[Comment\]\[replyto\]"] = replyTo;
        }
        
        params["data\[Comment\]\[author\]"] = author;
        params["data\[Comment\]\[contents\]"] = contents;
        
        doAjax('add', params, callback);
    };
};

hoh.commentDAO = new hoh.CommentDAO();

hoh.getCommentsForPic = function(picID)
{
    if(picID === undefined)
    {
        throw new ValueError("undefined picID");
    }
    
    hoh.info("Getting comments for pic: " + picID)
    
    hoh.commentDAO.getByPicID(picID, function(result) {
        $('#comments').html(result);
    });
};

hoh.saveComment = function(picID, parentCommentID)
{
    $('#commentSubmitButton').attr("disabled", "true");

    var author = $("#newCommentAuthor").val();
    var contents = $("#newCommentContents").val();
    
    hoh.info("author: '" + author + "'");
    hoh.info("contents: '" + contents + "'");
    
    hoh.commentDAO.save(picID, parentCommentID, author, contents, function(result) {
        $('#commentInput').remove();
        
        $('#comments').html('').html(result);
    });
};

hoh.openReplyBox = function(divID, picID, commentID)
{
    hoh.info("Replying to pic: " + picID + ", appending to '" + divID + "': " + $('#' + divID));
    
    var commentInputDiv = $('#commentInput'); 
    
    if(commentInputDiv)
    {
        hoh.info("removing comment entry div");
        
        commentInputDiv.remove();
    }
    
    if(commentID === undefined)
    {
        commentID = -1;
    }
    
    hoh.info("commentID: '" + commentID + "'");
    
    var replyEntryHTML = ['<div id="commentInput" class="hohText">Your name: <br/> <input type="text" id="newCommentAuthor" name="data[Comment][author]" columns="30"/> <br/> Comment: <br/> <textarea id="newCommentContents" name="data[Comment][contents]"></textarea><br/><input id="commentSubmitButton" type="submit" value="Save Comment" onClick="hoh.saveComment(', picID, ', ', commentID, ')"/></div>'].join('');
    
    hoh.info("Made reply entry HTML: " + replyEntryHTML);
    
    $('#' + divID).append(replyEntryHTML);
    
    hoh.info("appended to #" + divID);
};

hoh.info("JS loaded");
