﻿// JScript File
jQuery.lineup = function() {
    var priv = {
        padlock: false,

        setLock: function() {
            priv.padlock = true;
            $("body").css("cursor", "wait");
        },

        unLock: function() {
            priv.padlock = false;
            $("body").css("cursor", "default");
        },

        isLocked: function() {
            //            try {
            //                console.log("lineup is locked = " + priv.padlock);
            //            }
            //            catch(err){
            //                //nothing here, simply test whether console.log exists
            //            }
            return priv.padlock;
        }
    }
    return {

        //get the html design of players in lineup
        GetLineUp: function(roundId, currentRound) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();

            $("div#benchPlayers").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $("div#field-players").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 180px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $(".roundMatches").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $(".coach").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $(".h2h").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=1&roundid=" + roundId + "&currentRound=" + currentRound + "&m_id=-1&m_num=1",
                async: false,
                success: function(data) {
                    //unlock
                    priv.unLock();

                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    //returned data's format is field player + '#' + 'bench player' + '#' + 'round matches' + '#' + 'head2head'
                    var players = new Array();
                    players = data.split('#');
                    $("div#field-players").html(players[0]);
                    $(".roundMatches").html(players[2]);

                    $(".coach").html(players[5].replace("[t]", "#").replace("[t]", "#"));
                    $(".h2h").html(players[3]);

                    //if (editable == 1) new week
                    if (players[6] == 1) {
                        $("div#benchPlayers").html(players[1]);
                        $("div#benchPlayers").css("display", "block");
                        $("div#benchPlayers").css("background-image", "url(../images/bench_" + players[4] + ".gif)");
                        $(".side-column").css("padding", "185px 0 0");
                        $(".info-column-2").css("padding", "185px 0 0");
                        $("#showComments").hide();
                        $("#showCommentsText").hide();
                        $(".news-column").hide();
                        $(".news-block").show();
                        // $("div.footballer-stat").css("display","none");
                    }
                    //old matches
                    else {
                        $("div#benchPlayers").css("display", "none");
                        $(".side-column").css("padding", "0");
                        $(".info-column-2").css("padding", "0");

                        jQuery.lineup.getSubLeagueMessages(1, 7);
                        $("input#currentMessagePage").val(1);
                        $(".news-column").show();
                        $(".news-block").hide();
                        $('.matchStatistics').hide();
                        //$("div.footballer-stat").css("display","block");

                        //players[7] holds footballers statistics
                        //$(".footballer-stat").html(players[7]);
                    }


                    $("div.player").hover(function() {
                        $(this).addClass('player-hover');
                    }, function() {
                        $(this).removeClass('player-hover');
                    });
                    $("li.match").hover(function() {
                        $(this).addClass('hover');
                    }, function() {
                        $(this).removeClass('hover');
                    });
                },
                error: function(data) {
                    //unlock
                    priv.unLock();
                }
            });
        },

        SaveSocialShare: function(participantId, roundId, isFbShareTeam, isTwShareTeam, isFbShareScore, isTwShareScore, obj) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();

            $.ajax({
                type: "POST",
                url: "../DataProvider/SocialShare.ashx",
                data: "roundid=" + roundId + "&participantId=" + participantId + "&isFbShareTeam=" + isFbShareTeam + "&isTwShareTeam=" + isTwShareTeam + "&isFbShareScore=" + isFbShareScore + "&isTwShareScore=" + isTwShareScore,
                async: false,
                success: function(data) {
                    //unlock
                    priv.unLock();

                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;

                    if (data == -1) return;

                    $(obj).removeAttr('href');
                    $(obj).removeAttr('onclick');
                    $(obj).addClass('disabled');
                    $(obj).children('img').css({ opacity: 0.8 });
                    $(obj).attr('title', 'Je kunt een keer per week delen');

                    return false;

                },
                error: function(data) {
                    //unlock
                    priv.unLock();
                }
            });
        },

        playerOut: function(roundId, pout, id, currentRound) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();

            $("#field-players div").removeClass("player-active");
            $("#benchPlayers div div").removeClass("player-active");
            $("#" + id).addClass("player-active");
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=3&pout=" + pout + "&roundid=" + roundId + "&currentRound=" + currentRound,
                async: false,
                success: function(data) {
                    //unlock
                    priv.unLock();
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    $("div#benchPlayers").html(data);
                    $("div.player").hover(function() {
                        $(this).addClass('player-hover');
                    }, function() {
                        $(this).removeClass('player-hover');
                    });
                },
                error: function(data) {
                    //unlock
                    priv.unLock();
                }
            });
        },
        ///////////////////////////////////////////////////////////////	    
        playerIn: function(roundId, pout, pin, newtactic, nameout, namein, currentRound) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            $("#benchPlayers div div").removeClass("player-active");
            $("#field-players div").removeClass("player-active");
            var id = "bench" + pin;
            $("#" + id).addClass("player-active");
            var save_changes = confirm("Weet je zeker dat je " + namein + " wilt vervangen door " + nameout + "?");
            //Weet je zeker dat je [player out] wilt vervangen door [player in]?
            if (save_changes == true) {
                //set lock
                priv.setLock();

                $.ajax({
                    type: "POST",
                    url: "../DataProvider/LineUpRounds.ashx",
                    data: "do=4&pout=" + pout + "&pin=" + pin + "&tactic=" + newtactic + "&roundid=" + roundId + "&currentRound=" + currentRound,
                    async: false,
                    success: function(data) {
                        //unlock
                        priv.unLock();
                        // check session expire or not
                        if (jQuery.utils.checkSessionExpire(data))
                            return;

                        //returned data's format is field player + '#' + 'bench player'
                        var players = new Array();
                        players = data.split('#');
                        $("div#field-players").html(players[0]);
                        $(".roundMatches").html(players[2]);
                        $(".coach").html(players[5]);
                        $(".h2h").html(players[3]);

                        if (players[6] == 1) {
                            $("div#benchPlayers").html(players[1]);
                            $("div#benchPlayers").css("display", "block");
                            $("div#benchPlayers").css("background-image", "url(../images/bench_" + players[4] + ".gif)");
                            $(".side-column").css("padding", "185px 0 0");
                            $(".info-column-2").css("padding", "185px 0 0");
                        }
                        else {
                            $("div#benchPlayers").css("display", "none");
                            $(".side-column").css("padding", "0");
                            $(".info-column-2").css("padding", "0");
                        }
                        $("div.player").hover(function() {
                            $(this).addClass('player-hover');
                        }, function() {
                            $(this).removeClass('player-hover');
                        });
                        $("li.match").hover(function() {
                            $(this).addClass('hover');
                        }, function() {
                            $(this).removeClass('hover');
                        });
                    },
                    error: function(data) {
                        //unlock
                        priv.unLock();
                    }
                });
            }
        },

        //opposite way line up
        playerIn2: function(roundId, pout, id, currentRound) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();
            $("#field-players div").removeClass("player-active");
            $("#benchPlayers div div").removeClass("player-active");
            $("#" + id).addClass("player-active");
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=7&pout=" + pout + "&roundid=" + roundId + "&currentRound=" + currentRound,
                async: false,
                success: function(data) {
                    //unlock
                    priv.unLock();

                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    $("div#field-players").html(data);
                    $("div.player").hover(function() {
                        $(this).addClass('player-hover');
                    }, function() {
                        $(this).removeClass('player-hover');
                    });
                },
                error: function(data) {
                    //unlock
                    priv.unLock();
                }
            });
        },

        playerOut2: function(roundId, pout, pin, newtactic, nameout, namein, currentRound) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //	         $("#benchPlayers div div").removeClass("player-active");
            //	         $("#field-players div").removeClass("player-active");
            //	         var id="bench" + pin;
            //             $("#"+id).addClass("player-active");
            var save_changes = confirm("Weet je zeker dat je " + namein + " wilt vervangen door " + nameout + "?");
            //Weet je zeker dat je [player out] wilt vervangen door [player in]?
            if (save_changes == true) {
                //set lock
                priv.setLock();

                $.ajax({
                    type: "POST",
                    url: "../DataProvider/LineUpRounds.ashx",
                    data: "do=8&pout=" + pout + "&pin=" + pin + "&tactic=" + newtactic + "&roundid=" + roundId + "&currentRound=" + currentRound,
                    async: false,
                    success: function(data) {
                        //unlock
                        priv.unLock();
                        // check session expire or not
                        if (jQuery.utils.checkSessionExpire(data))
                            return;

                        //returned data's format is field player + '#' + 'bench player'
                        var players = new Array();
                        players = data.split('#');
                        $("div#field-players").html(players[0]);
                        $(".roundMatches").html(players[2]);
                        $(".coach").html(players[5]);
                        $(".h2h").html(players[3]);

                        if (players[6] == 1) {
                            $("div#benchPlayers").html(players[1]);
                            $("div#benchPlayers").css("display", "block");
                            $("div#benchPlayers").css("background-image", "url(../images/bench_" + players[4] + ".gif)");
                            $(".side-column").css("padding", "185px 0 0");
                            $(".info-column-2").css("padding", "185px 0 0");
                        }
                        else {
                            $("div#benchPlayers").css("display", "none");
                            $(".side-column").css("padding", "0");
                            $(".info-column-2").css("padding", "0");
                        }
                        $("div.player").hover(function() {
                            $(this).addClass('player-hover');
                        }, function() {
                            $(this).removeClass('player-hover');
                        });
                        $("li.match").hover(function() {
                            $(this).addClass('hover');
                        }, function() {
                            $(this).removeClass('hover');
                        });
                    },
                    error: function(data) {
                        //unlock
                        priv.unLock();
                    }
                });
            }
        },

        doLineUp: function(roundId) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();

            var divControl = "div#round_" + roundId + " div.lineupcontainer";
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=1&roundid=" + roundId,
                async: false,
                success: function(data) {
                    //unlock
                    priv.unLock();
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;

                    $(divControl).html(data);
                },
                error: function(data) {
                    //unlock
                    priv.unLock();
                }
            });
        },

        saveLineUp: function(roundId) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();

            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=2&roundid=" + roundId,
                async: false,
                success: function(data) {
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                },
                error: function(data) {
                    //unlock
                    priv.unLock();
                }
            });
            //$('div.savelineup img.load').fadeOut(2500);
            //$('div.LineUpMid span.savelineupsuccessfull').fadeIn(2500);
        },

        //get the html design of head to head part
        GetHeadToHead: function(matchId, homeClub, awayClub) {
            $(".h2h").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=5&matchId=" + matchId + "&homeClub=" + homeClub + "&awayClub=" + awayClub + "&roundid=0",
                async: false,
                success: function(data) {
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;

                    $(".h2h").html(data);
                },
                error: function(data) {
                }
            });
        },

        //get the html design of coach message
        GetCoachMsg: function(message_id, direction) {
            var msg_num = $("#MessageNum").val();
            if (direction == '+') msg_num = (msg_num * 1) + 1
            else if (direction == '-') msg_num = (msg_num * 1) - 1
            $("#MessageNum").val(msg_num);
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=9&m_id=" + message_id + "&m_num=" + msg_num + "&roundid=0",
                async: false,
                success: function(data) {
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;

                    $(".coach").html(data);
                },
                error: function(data) {
                }
            });
        },

        //get html of player statistics
        GetFootballerStats: function(match_id, home_club, away_club, home_goals, away_goals, home_player, away_player, round_id) {
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=6&matchId=" + match_id + "&homeClub=" + home_club + "&awayClub=" + away_club + "&roundid=" + round_id + "&homeGoals=" + home_goals +
                        "&awayGoals=" + away_goals + "&homePlayer=" + home_player + "&awayPlayer=" + away_player,
                async: false,
                success: function(data) {
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;

                    $("#footballer-stat").html(data);
                },
                error: function(data) {
                }
            });
        },
        //get html of match statistics
        GetMatchStats: function(match_id, round_id) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();
            var res;
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpRounds.ashx",
                data: "do=10&matchId=" + match_id + "&roundid=" + round_id,
                async: false,
                success: function(data) {
                    priv.unLock();
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    res = data;
                },
                error: function(data) {
                }
            });
            return res;
        },
        //get the html design of players in lineup
        GetMatchResult: function(match_id, home_club, away_club, home_player, away_player, home_goal, away_goal, participant_id) {
            $("#MatchStats").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpParticipant.ashx",
                data: "do=2&matchId=" + match_id + "&homeClub=" + home_club + "&awayClub=" + away_club + "&homeGoals=" + home_goal +
                "&awayGoals=" + away_goal + "&homePlayer=" + home_player + "&awayPlayer=" + away_player + "&lineUpParticipantId=" + participant_id + "&roundid=0",
                success: function(data) {
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    $("#MatchStats").html(data);
                },
                error: function(data) {
                }
            });
        },

        //get the html design of players in lineup
        GetParticipantLineUp: function(roundId, currentRound, participantId) {
            $("div#benchPlayers").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $("div#field-players").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 180px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $(".roundMatches").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 170px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $(".coach").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $(".h2h").html("<span class='loaderimg' id='forgotPwd' style='margin:50px 10px 10px 80px'>&nbsp;&nbsp;<img src='../Images/ajax-loader.gif' alt='' /></span>");
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpParticipant.ashx",
                data: "do=1&roundid=" + roundId + "&currentRound=" + currentRound + "&lineUpParticipantId=" + participantId,
                success: function(data) {
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    //returned data's format is field player + '#' + 'bench player' + '#' + 'round matches' + '#' + 'head2head'
                    var players = new Array();
                    players = data.split('#');
                    $("div#field-players").html(players[0]);
                    $(".roundMatches").html(players[2]);
                    $(".coach").html(players[5]);
                    $(".h2h").html(players[3]);
                    $("div#benchPlayers").css("display", "none");
                    $(".side-column").css("padding", "0");
                    $(".info-column-2").css("padding", "0");
                    $("#teamPage").css("border", "solid 2px #31affb")
                    $("#teamPage").css("width", "188px")
                    $("#teamPage h4").css("color", "#666666");
                    $('.matchStatistics').hide();
                    //hover not needed because not clickable
                    //                    $("li.match").hover(function(){
                    //                        $(this).addClass('hover');
                    //                    }, function() {
                    //                        $(this).removeClass('hover');
                    //                    });
                },
                error: function(data) {
                }
            });
        },

        //get the html design of player pass of given player
        GetPlayerPass: function(player_id, round_id, playertype) {
            $.ajax({
                type: "POST",
                url: "../DataProvider/LineUpParticipant.ashx",
                data: "do=3&player_id=" + player_id + "&roundid=" + round_id,
                success: function(data) {
                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    //returned data's format is field player + '#' + 'bench player' + '#' + 'round matches' + '#' + 'head2head'
                    //alert(playertype);
                    if (playertype == 2) {
                        $("div.playerPass").css("background-image", "url(../images/playerPassRes-bg.gif)");
                        $("div.playerPass").css("position", "relative");
                        $("div.playerPass").css("left", "-255px");
                        $("div.passTitle").css("padding-left", "13px");
                    } else {
                        $("div.playerPass").css("background-image", "url(../images/playerPass-bg.gif)");
                        $("div.playerPass").css("position", "relative");
                        $("div.playerPass").css("left", "0px");
                        $("div.passTitle").css("padding-left", "22px");
                    }
                    $("#playerPassport").html(data);
                },
                error: function(data) {
                }
            });
        },
        //get messages
        getSubLeagueMessages: function(pageNum, pageSize) {
            $.ajax({
                type: "POST",
                url: resources.path_prefix + "/DataProvider/League.ashx",
                data: "pageNumber=" + pageNum + "&pageSize=" + pageSize + "&job=6", //job=6 is get all messages for all subleagues for participant
                async: false,
                success: function(data) {
                    var retVal = new Array();
                    retVal = data.split('|');
                    if (retVal[0] == '') {
                        $("#showComments").hide();
                        $("#showCommentsText").show();
                    }
                    else {
                        $("#showComments").show();
                        $("#showCommentsText").hide();
                    }

                    $("#messages").html(retVal[0]);
                    if (retVal[1] == '0') {
                        $('#L_backmessage').hide();
                    }
                    else if (retVal[1] == '1') {
                        $('#L_backmessage').show();
                    }

                    if (retVal[2] == '0') {
                        $('#L_formessages').hide();
                    }
                    else if (retVal[2] == '1') {
                        $('#L_formessages').show();
                    }
                    $("input#currentMessagePage").val(pageNum);
                },
                error: function(data) {
                }
            });
        },

        //get the html design of players in lineup of the best team
        GetBestLineUp: function(type) {
            //check lock
            if (priv.isLocked()) {
                return;
            }
            //set lock
            priv.setLock();

            $.ajax({
                type: "POST",
                url: "../DataProvider/StatisticsPage.ashx",
                data: "job=1&bestType=" + type,
                async: false,
                success: function(data) {
                    //unlock
                    priv.unLock();

                    // check session expire or not
                    if (jQuery.utils.checkSessionExpire(data))
                        return;
                    $("div#field-players").html(data);
                    $('#team-points').html($('#teamPoints').val() + " " + messages.POINTS);
                },
                error: function(data) {
                    //unlock
                    priv.unLock();
                }
            });
        }
    };
} ();

