﻿var lastCommentID = 0;
var lastReplyParentCommentID = 0;
var maxItems = 10;

var itemCount = 0;

var basePath = '';

var loading = false;

/*******************************************
ADD COMMENT
*******************************************/

function loadAddComment(url) {
    $("#addcomment-container").load(
        url,
        function(html) {
            $('#addcomment-container')[0].value = html;
        }
    );
}

function expandComment() {
    $("#comment").addClass("clicked");
    $("#comment").val('');
    $(".character-count").show();
}

function unexpandComment() {
    //$("#comment").removeClass("clicked");

}

function initBinding(url) {

    $("#autocomplete").autocomplete(
                                            url,
                                            {
                                                dataType: 'json',
                                                parse: function(data) {
                                                    var rows = new Array();
                                                    for (var i = 0; i < data.length; i++) {
                                                        rows[i] = { data: data[i], value: data[i].TeamPlayerID, result: data[i].AutoCompleteResult };
                                                    }
                                                    return rows;
                                                }
                                                ,
                                                formatItem: function(row, i, max) {
                                                    return row.AutoCompleteResult;
                                                },
                                                matchSubset: false
                                            }
                                        ).result(getSelectedId);

    $('#comment').keyup(function() {
        limitChars('comment', 200, 'char-count');
    });

}

function getSelectedId(event, data, formatted) {
    document.getElementById('hiddenPlayerID').value = data.TeamPlayerID;
}

function limitChars(textid, limit, infodiv) {
    var text = jQuery('#' + textid).val();
    var textlength = text.length;

    if (textlength > limit) {
        alert('You cannot enter more than ' + limit + ' characters!');
        jQuery('#' + textid).val(text.substr(0, limit));
        return false;
    }
    else {
        jQuery('#' + infodiv).text((limit - textlength) + ' characters');
        return true;
    }
}



/*******************************************
FEED
*******************************************/


function loadFeed(url) {

    url += "&maxItems=" + maxItems;

    $("#feed-container").load(
        url,
        function(html) {
            $('#feed-container')[0].value = html;
        },
        loadFeed_callback()
    );
}

function loadFeed_callback() {
    loading = false;
}


function feed_showMoreComments(commentID, showPleaseLogin) {

    $("#feed_comments" + commentID + "_replies").fadeIn("slow")

    if (showPleaseLogin) {
        $("#pleaseLogin" + commentID).fadeIn("slow");
    }
    $(".reply" + commentID).addClass("inside-reply");
    $(".reply" + commentID).removeClass("inside-spoton");
    $(".reply" + commentID).removeClass("inside-noway");
    $(".reply" + commentID).show();
    $("#commentType" + commentID).val("comment");

    $("#replytextbox" + commentID).focus();
}

function feed_showSpotOn(commentID, showPleaseLogin) {

    $("#feed_comments" + commentID + "_replies").fadeIn("slow")

    if (showPleaseLogin) {
        $("#pleaseLogin" + commentID).fadeIn("slow");
    }

    $("#pleaseLogin" + commentID).fadeIn("slow");
    $(".reply" + commentID).addClass("inside-spoton");
    $(".reply" + commentID).removeClass("inside-reply");
    $(".reply" + commentID).removeClass("inside-noway");
    $(".reply" + commentID).show();
    $("#commentType" + commentID).val("spoton");

    $("#replytextbox" + commentID).focus();

}

function feed_showNoWay(commentID, showPleaseLogin) {

    $("#feed_comments" + commentID + "_replies").fadeIn("slow")

    if (showPleaseLogin) {
        $("#pleaseLogin" + commentID).fadeIn("slow");
    }    
    
    $("#pleaseLogin" + commentID).fadeIn("slow");
    $(".reply" + commentID).addClass("inside-noway");
    $(".reply" + commentID).removeClass("inside-reply");
    $(".reply" + commentID).removeClass("inside-spoton");
    $(".reply" + commentID).show();
    $("#commentType" + commentID).val("noway");

    $("#replytextbox" + commentID).focus();

}

function feed_Hide(commentID, showPleaseLogin) {
    $("#feed_comments" + commentID + "_replies").fadeOut("slow")
}

/*******************************************
RIGHT PANEL
*******************************************/
function loadRightPanel(url) {
    $("#mostchampionschops-container").load(
        url,
        function(html) {
            $('#mostchampionschops-container')[0].value = html;
        }
    );
}

function loadRightPanel_Champions(url) {
    $("#rightpanel-container").load(
    url,
    function(html) {
        $('#rightpanel-container')[0].value = html;
    }
    );
}

function loadRightPanel_Chops(url) {
    $("#rightpanel-container").load(
    url,
    function(html) {
        $('#rightpanel-container')[0].value = html;
    }
    );
}
function loadRightPanel_playerlisting(url) {
    $("#rightpanel-container").load(
        url,
        function(html) {
            $('#rightpanel-container')[0].value = html;
        }
    );
}
function loadRightPanel_teamlisting(url) {
    $("#rightpanel_teamlisting").load(
        url,
        function(html) {
            $('#rightpanel_teamlisting')[0].value = html;
        }
    );
}


/*******************************************
SEARCH RESULTS
*******************************************/

function searchTabClick(tab) {

    if (tab == 'all') {
        $("#searchresults-teams").show();
        $("#searchresults-sports").show();
        $("#searchresults-players").show();
        $("#searchresults-comments").show();

        $("#searchresults-all-tab").addClass('selected');
        $("#searchresults-teams-tab").removeClass('selected');
        $("#searchresults-sports-tab").removeClass('selected');
        $("#searchresults-players-tab").removeClass('selected');
        $("#searchresults-comments-tab").removeClass('selected');
    }
    else if (tab == 'teams') {
        $("#searchresults-teams").fadeOut('fast');
        $("#searchresults-sports").fadeOut('fast');
        $("#searchresults-players").fadeOut('fast');
        $("#searchresults-comments").fadeOut('fast');
        $("#searchresults-teams").fadeIn('fast');

        $("#searchresults-all-tab").removeClass('selected');
        $("#searchresults-teams-tab").addClass('selected');
        $("#searchresults-sports-tab").removeClass('selected');
        $("#searchresults-players-tab").removeClass('selected');
        $("#searchresults-comments-tab").removeClass('selected');
    }
    else if (tab == 'sports') {
        $("#searchresults-sports").fadeOut('fast');
        $("#searchresults-teams").fadeOut('fast');
        $("#searchresults-players").fadeOut('fast');
        $("#searchresults-comments").fadeOut('fast');
        $("#searchresults-sports").fadeIn('fast');

        $("#searchresults-all-tab").removeClass('selected');
        $("#searchresults-teams-tab").removeClass('selected');
        $("#searchresults-sports-tab").addClass('selected');
        $("#searchresults-players-tab").removeClass('selected');
        $("#searchresults-comments-tab").removeClass('selected');
    }
    else if (tab == 'players') {
        $("#searchresults-players").fadeOut('fast');
        $("#searchresults-teams").fadeOut('fast');
        $("#searchresults-sports").fadeOut('fast');
        $("#searchresults-comments").fadeOut('fast');
        $("#searchresults-players").fadeIn('fast');

        $("#searchresults-all-tab").removeClass('selected');
        $("#searchresults-teams-tab").removeClass('selected');
        $("#searchresults-sports-tab").removeClass('selected');
        $("#searchresults-players-tab").addClass('selected');
        $("#searchresults-comments-tab").removeClass('selected');
    }
    else if (tab == 'comments') {
        $("#searchresults-comments").fadeOut('fast');
        $("#searchresults-teams").fadeOut('fast');
        $("#searchresults-sports").fadeOut('fast');
        $("#searchresults-players").fadeOut('fast');
        $("#searchresults-comments").fadeIn('fast');

        $("#searchresults-all-tab").removeClass('selected');
        $("#searchresults-teams-tab").removeClass('selected');
        $("#searchresults-sports-tab").removeClass('selected');
        $("#searchresults-players-tab").removeClass('selected');
        $("#searchresults-comments-tab").addClass('selected');
    }
}

function preReply(commentID) {

    lastReplyParentCommentID = commentID;
}

function afterReply() {

    parentCommentID = lastReplyParentCommentID;
    
    if(basePath != '/')
    {
        basePath += '/';
    }
    
    url = basePath + "Shared/FeedControl_Template_Replies_Template/";

    $.get(url, function(data) {
        $("#feed_comments" + parentCommentID + "_replies .add-comment").after(data);
    });

    $("#replytextbox" + parentCommentID).val('');
}


function afterPost() {
    
    if(basePath != '/')
    {
        basePath += '/';
    }

    url = basePath + "Shared/FeedControl_Template/";

    $.get(url, function(data) {
        $(".feed").prepend(data);
    });

    $("#autocomplete").val('Search players');
    $("#comment").val('Click to comment');
}


function loadRightPanel_Container(url) {
    $("#rightpanel-container").load(
    url,
    function(html) {
        $('#rightpanel-container')[0].value = html;
    }
    );
}

function playerPageLoadHeader(url) {
    $("#player-header-container").load(
        url,
        function(html) {
            $('#player-header-container')[0].value = html;
        }
    );
}