var preload_full;
var preload_partial;
var preload_empty;

window.onload = function(){
    preload_full = new Image();
    preload_full.src = "./images/icons/video_rating_full_small.gif";
    preload_partial = new Image();
    preload_partial.src = "./images/icons/video_rating_part_small.gif";
    preload_empty = new Image();
    preload_empty.src = "./images/icons/video_rating_none_small.gif";
}

function musicheader_click(music_id, user, group) {
    var header = $('music_header_' + music_id);
    var element = $('music_comments_' + music_id);

    if (!header.hasClass('active')) {
        get_comments(music_id, user, group, 1);
    }
    
    header.toggleClass('active');
    element.toggleClass('active');
    element.toggleClass('inactive');
}


function rating_over(music_id, rating) {
    for(var x=1; x<=5; x++) {
        if(x <= rating) {
            $('rate_'+music_id+'_'+x).src = preload_full.src;
        } else {
            $('rate_'+music_id+'_'+x).src = preload_empty.src;
        }
    }
}

function rating_out(music_id, full, part) {
    for(var x=1; x<=5; x++) {
        if(x <= full) {
            $('rate_'+music_id+'_'+x).src = preload_full.src;
        } else if(part != 0 && x == full + 1) {
            $('rate_'+music_id+'_'+x).src = preload_partial.src;
        } else {
            $('rate_'+music_id+'_'+x).src = preload_empty.src;
        }
    }
}

function rate(rating, music_id, user_id) {
    $('music_rating_'+music_id).onmouseout = null;
    var request = new Request.JSON({
        'url' : 'group_music_ajax.php',
        'method' : 'post',
        'secure' : false,
        'data' : {
            'task'  : 'rate_do',
            'user_id'  : user_id,
            'music_id'  : music_id,
        'rating' : rating
        },
        'onComplete' : function(responseObject)
        {
            rating_result(responseObject, music_id);
        }
    });
    request.send();
}

function rating_result(rating, music_id) {
    $('rating_total_'+music_id).innerHTML = rating.rating_total;
    for(var x=1; x<=5; x++) {
        if(x <= rating.rating_full) {
            $('rate_'+music_id+'_'+x).src = preload_full.src;
            $('rate_'+music_id+'_'+x).onmouseover = null;
            $('rate_'+music_id+'_'+x).onclick = null;
        } else if(rating.rating_part != 0 && x == rating.rating_full+1) {
            $('rate_'+music_id+'_'+x).src = preload_partial.src;
            $('rate_'+music_id+'_'+x).onmouseover = null;
            $('rate_'+music_id+'_'+x).onclick = null;
        } else {
            $('rate_'+music_id+'_'+x).src = preload_empty.src;
            $('rate_'+music_id+'_'+x).onmouseover = null;
            $('rate_'+music_id+'_'+x).onclick = null;
        }
    }
    $('music_rating_'+music_id).onmouseout = null;
}

function get_comments(music_id, user, group, p) {
    var request = new Request.JSON({
        'url' : 'misc_js.php',
        'method' : 'post',
        'secure' : false,
        'data' : {
            'task'  : 'comment_get',
            'type'  : 'groupmusic',
            'value'  : music_id,
            'iden'  : 'music_id',
            'object_owner'  : "group",
            'object_owner_id' : group,
            'cpp'   : 5,
            'p'     : p
        },
        'onComplete' : function(responseObject)
        {
            get_comments_callback(music_id, responseObject, user, group);
            update_nrof_comments(music_id);
        }
    });
    request.send();
}

function get_comments_callback(music_id, responseObject, user, group) {
    var commentListDiv = document.getElementById('music_comments_list_'+music_id);
    commentListDiv.innerHTML = '';
    
    if (responseObject.comments && responseObject.total_comments > 0) {
        
        var commentList = document.createElement("ul");
        commentList.id = 'music_comments_ul_' + music_id;
        commentList.setAttribute("class", "comments");
        for (var key in responseObject.comments) {
            if (!isNaN(key)){
 	            var comment = document.createElement("li");
    	        comment.innerHTML = build_comment_item(key, responseObject.comments[key], user, music_id, group);
        	    commentList.appendChild(comment);
            }
        }
        
        var paging = document.createElement("li");
        paging.className = "paging-links";
        paging.appendChild(build_paging_links(responseObject.total_comments, responseObject.p, music_id, user, group));
        commentList.appendChild(paging);
        commentListDiv.appendChild(commentList);
    }
    var comment_textarea = build_comment_area(music_id, user, group);
    var placeholder = $$("#comment_area_placeholder_" + music_id);
    placeholder.set('html', comment_textarea);
}

function post_comment(caller, user, group){
    var music_id = caller.getAttribute("rel");
    var comment_text_field = document.getElementById('comment_text_' + music_id);
    var comment_text = comment_text_field.value;
    var request = new Request.JSON({
        'url' : 'misc_js.php',
        'method' : 'post',
        'secure' : false,
        'data' : {
            'task'  : 'comment_post',
            'type'  : 'groupmusic',
            'iden'  : 'music_id',
            'value'  : music_id,
            'comment_body' : comment_text,
            //'user' : user,
            'col' : 'music',
            'tab' : 'groupmusic',
            'object_owner' : 'group',
            'object_owner_id' : group
        },
        'onComplete' : function(responseObject)
        {
            if (responseObject.isError == null) {
                get_comments(music_id, user, group, 1);
                var text_area = document.getElementById("comment_text_" + music_id);
                text_area.value = "";
            }
        }
    });
    request.send();
}

function delete_comment(comment_id, music_id, user, group) {
    TB_show('Delete comment', '#TB_inline?height=100&width=300&inlineId=confirm_musiccomment_delete', '', '../images/trans.gif');

    $('confirm_musiccomment_delete_button').addEvent('click', function() {
        var request = new Request.JSON({
            'url' : 'misc_js.php',
            'method' : 'post',
            'secure' : false,
            'data' : {
                'task'  : 'comment_delete',
                'type'  : 'groupmusic',
                'iden'  : 'music_id',
                'value'  : music_id,
                //'user' : user,
                'col' : 'music',
                'tab' : 'groupmusic',
                'comment_id' : comment_id,
                'object_owner' : 'group',
                'object_owner_id' : group
            },
            'onComplete' : function(responseObject)
            {
                if (responseObject.isError == null) {
                    get_comments(music_id, user, group, 1);
                }
            }
        });
        request.send();
    });
}

function build_comment_item(comment_id, comment, user, music_id, group) {
    var commentString = '<div style="float:left;width:100%;margin:10px 0;">' +
        '<div style="float:left;width:90px;text-align:center;"><img class="photo" width="' + comment.comment_authoruser_photo_width +  '" src="' + comment.comment_authoruser_photo +'" /></div>' +
        '<div style="overflow:hidden;">' +
            '<div class="music_comment_header">' +
                '<div class="profile_comment_author"><a href="profile.php?user=' + comment.comment_authoruser_username + '"><b>' + comment.comment_authoruser_displayname + '</b></a></div>' +
                '<div class="profile_comment_date">' + comment.comment_date + '</div>' +
            '</div>' +
            '<div class="profile_comment_body">' + comment.comment_body + '</div>' +
        '</div>';
    if(comment.comment_authoruser_username == user) {
        commentString += '<div class="profile_comment_links" style="float:right; margin-bottom:10px;">' +
                '<a href="javascript:void(0);" onclick="delete_comment(' + comment_id + ', ' + music_id + ', \'' + user + '\', ' + group + ')" class="commentDeleteLink">delete</a>' +
            '</div>';
        
    }
    commentString += '</div>';
    return commentString;
}

function build_paging_links(total_comments, page, music_id, user, group) {
    
    var paging_links = document.createElement("div");
    if (total_comments/5 <= 1) return paging_links;
    
    for (var i = 1; i < total_comments/5 + 1; i++){
        var paging_link = document.createElement("a");
        paging_link.className = "paging_link";
        var link_text = document.createTextNode(i);
        paging_link.appendChild(link_text);
        paging_link.setAttribute("rel", i);
        paging_link.onclick = function(){
            var clicked_link = this;
            get_comments(music_id, user, group, clicked_link.rel);
        };
        if (i == page) {
            paging_link.className = "paging_link_current";
        }
        paging_links.appendChild(paging_link);
    }
    paging_links.setAttribute("style", "float:right;padding-bottom:10px;");
    return paging_links;
}

function update_nrof_comments(music_id)
{
    var request = new Request.JSON({
        'url' : 'group_music_ajax.php',
        'method' : 'post',
        'secure' : false,
        'data' : {
            'task'  : 'get_nrof_groupmusic_comments',
            'music_id'  : music_id
        },
        'onComplete' : function(responseObject)
        {
            var comment_holder = $$("#nrof_music_comments_" + music_id);
            comment_holder.set('html', responseObject.nrof_comments);
        }
    });
    request.send();
}

function build_comment_area(music_id, user, group)
{
    if (user == "") return "";
    
    var user_text = "'" + user + "'";
    return '<div style="float:left; width:100%;">' +
        '<div class="comment_headline">Comment:</div>' +
        '<div class="profile_postcomment">' +
            '<textarea id="comment_text_' + music_id + '" class="comment_area" style="font-size:12px; color:#111;" rows="4"></textarea>' +
            '<div style="text-align:right; margin-top:10px;">' +
                '<input type="submit" class="button" value="Post Comment" rel="' + music_id + '" onclick="javascript:post_comment(this, ' + user_text + ', ' + group + ');" />' +
            '</div>' +
        '</div>' +
    '</div>';
}

function stopLinkClickEvent(e) {
    e.stopPropagation();
}

