$(function(){
    $(".sk-tip").cluetip({splitTitle:'|',showTitle:false});
	$(".rankin_image").cluetip({splitTitle:'|',showTitle:false});
	
    $("#form-add-comment").submit(sk_addComment);
    $("#comment_text").focus(function() {
        var text = $.trim($(this).val());
        if (text=='Enter your comment...') {
            $(this).val('');
            $(this).css('color', '#555555');
        }
    });

    $("#comment_text").blur(function() {
        var text = $.trim($(this).val());
        if(text=="") {
            $(this).val('Enter your comment...');
            $(this).css('color', 'gray');
        }
    });
	
});

function sk_addComment() {

    var comment = $.trim($("#comment_text").val());
    if (comment=="" || comment=="Enter your comment...") {
        return false;
    }

    var callback = function(response) {
        
        if(response.result==false) {
           $("#msg")
               .html(response.msg)
               .attr('class', 'error-short')
               .fadeIn()
               .animate({opacity: 1.0}, 2000)
               .fadeOut('slow');
        } else {
           $("#form-add-comment").resetForm();
           $("#msg")
               .html(response.msg)
               .attr('class', 'ok-short')
               .fadeIn()
               .animate({opacity: 1.0}, 2000)
               .fadeOut('slow');
           
           $("#image_random_value").attr('src', response.image_random_value);
           $("#div-comment-list").html(response.html);
        }
        $("#submit").attr('disabled', '');
    };

    $("#submit").attr('disabled', 'diabled');
    sk_ajaxSubmit('form-add-comment', callback, 'addComment');
    return false;
}

function sk_deleteComment(commentId, sectionNo) {
    if(confirm("Do you really want to delete this comment?")==false) {
        return false;
    }
    
    var data = {
        'comment_id': commentId,
        'section_no': sectionNo
    }
    
    var callback = function(response) {
        if (response.result==false) {
            $("#msg")
               .html(response.msg)
               .attr('class', 'error-short')
               .fadeIn()
               .animate({opacity: 1.0}, 2000)
               .fadeOut('slow');
        } else {
            $("#div-comment-list").html(response.html);
        }
    };
    
    sk_ajaxCall(data, callback, 'deleteComment')
}

function sk_showCommentList(sectionNo) {
    var data = { 'section_no': sectionNo };
    var callback = function(response) {
        if (response.result) {
            $("#div-comment-list").html(response.html); 
        }
    };
    
    sk_ajaxCall(data, callback, 'showCommentList');
}

function sk_toggleSport(button, sportId) {
    var cls = $(button).attr('class');
    if (cls=='icon-hide') {
        $(button).attr('class', 'icon-show');
        $('#sport-'+sportId).hide('slow');
    } else if (cls=='icon-show') {
        $(button).attr('class', 'icon-hide');
        $('#sport-'+sportId).show('slow');
    }

}

function sk_quickInvite(uid) {
    var msg = "You are going to invite this person to be your friend.\n";
    msg += "An invitation will be sent to him/her if you continue. Continue?";
    var ok = confirm(msg);
    if(ok) {
        var data = {'owner_uid':uid};
        var callback = function(response) {
            if (response.result) {
                $("#div-invite")
                    .attr('class', 'ok-short')
                    .html(response.msg);
            } else {
                $("#div-invite")
                    .attr('class', 'error-short')
                    .html(response.msg);
            }
        };
        sk_ajaxCall(data, callback, 'quickInvite');
    }
    return false;
}
