﻿//-------------------------------
//
// Modified:   
//
//          2011-09-03   Ryan Weir
//                          -Added space for ads if they are used, whether to show them is managed by a cookie read in at load
//
//          2011-10-03   Ryan Weir
//                          -Fixed AJAX requests and parsing
//                            -added detection for when draft has ended
//                            -added French language support
//                            
//          2011-14-03   Ryan Weir
//                          -Added a bunch of comments, changed variables to meaningful names
//                          -Fixed update so that new players from draft in progress are added automatically
//                          -Update interval is read from a cookie, which can be set in the project's config file
//          2011-15-03   Ryan Weir
//                          -Added a ticker to see update status in the browser title
//                          
//          2011-16-03   Ryan Weir
//                          -Modified code to accept player information out of order and insert it properly
//
//          2011-18-03   Ryan Weir
//                          -Added French translations for the months using function toFrenchBirthdate()
//
//          2011-24-03   Ryan Weir
//                          -Added an MD5 checksum to compare the client information to the server's periodically.
//                          -If the information is ever out of date, DraftCentre will perform a full update
//
//          2011-24-03   Ray Wang
//                          - Modified function DraftPrint() to pass language as the second parameter
//
//          2011-03-05   Ryan Weir
//                          - Added a web method called 'ScrollToNumber()' which returns the number that we should be scrolling to - this guarantees that the client knows what number should be scrolled.
//
//          2011-05-13   Ray Wang
//                          - Fixed logic of showAds of whether a draft should have the ad bar showing at the buttom of the page
//-------------------------------

var win1 = null;
var Teams = null;
var checkInterval = null;
var lastcheck = null;
var DraftOrder = new Array();
var hasStarted = false;
//var globalLeagueID = null;

var lastOverall = -1; //used to determine if player information being received is new or duplicate

var showAds = "false"; //flag for whether any ads are displayed

var language; //store the language to translate any controls into
var DraftEnded = false;

var updateInterval = 6000; //default updating interval
var stillUpdating = true; //flag for whether the program is still updating
var updateCounter = 0;
var originalTitle; //stores the original title of the page
var draftMD5 = ""; //stores the MD5 signature of the lastUpdated times for all picks in the draft

var highestRound = 1; //stores the value of the highest round that's been displayed, so we know when to add a ROUND bar

function readCookie(name) {
    var ca = document.cookie.split(';');
    var nameEQ = name + "=";
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length); //delete spaces
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

//given a pick, return true if it's the highest pick so far, or false otherwise
function isNewAndHighest(pick) {
    var highest = 0;
    if ((DraftOrder != null) && (DraftOrder.length > 0)) {

        $(DraftOrder).each(function() {
            if ((this.Overall > highest) && (this.SelectedPlayerName != null) && (this.SelectedPlayerName != "") && (this.hasScrolled === false)) {
                highest = this.Overall;
            }
        });
    }

    if ((highest > 0) && (pick != null) && (pick.Overall > highest)) {
        return true;
    } else {
        return false;
    }
}

originalTitle = document.title;

window.setInterval(function() {
    if (stillUpdating) {
        updateCounter++;
        var temp = "";
        var i = 0;
        for (i = 0; i < updateCounter; i++)
            temp += ".";
        document.title = originalTitle + "   " + temp;
        updateCounter %= 5;
    }
}, 500);

window.setInterval(function() {
    stillUpdating = false;
}, 15000);

$(document).ready(function() {
    var height = $(window).height();

    //read in the update interval
    updateInterval = readCookie("updateInterval");
    if ((updateInterval == null) || (isNaN(updateInterval) == true)) {
        updateInterval = 6000;
    }

    //read the language, default to english
    language = readCookie("language");
    if (language == null)
        language = "en";

    //read whether this page should display ads
    showAds = readCookie("showAds");
    if (showAds == null)
        showAds = "false";

    //flag for whether the team has ads
    if (showAds == "true") {
        $('#pnlData').height(height - 105 - $('#adFooter').height()); //changed to subtract the league ads at the bottom
        $(window).resize(function() {
            $('#pnlData').height($(this).height() - 105 - $('#adFooter').height());
        });
    } else {
        $('#pnlData').height(height - 105);
        $('#adFooter').css("display", "none"); //show no ads at the bottom of the screen, hide the ad footer
        $(window).resize(function() {
            $('#pnlData').height($(this).height() - 105);
        });
    }

    //globalLeagueID = $('#hidLeagueID').val();

    LeagueSetUp();
    $.ajax({
        type: "POST",
        url: "Default.aspx/GetTeams",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(m) {
            if (m != null) {
                if (m.d != null && m.d != "") {
                    Teams = jQuery.parseJSON(m.d);
                    lastcheck = new Date();
                    highestRound = 0;
                    var count = 0;
                    var round = 1;

                    $.ajax({
                        type: "POST",
                        url: "Default.aspx/GetDraft",
                        data: "{}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function(m) {
                            if (m != null) {
                                hasStarted = true;
                                $('#pnlData').html(" ");
                                $(m.d).each(function() {
                                    var o = jQuery.extend(new Pick, this);
                                    DraftOrder.push(o);
                                    lastOverall = o.Overall;

                                    if (count != this.Round) {
                                        if (language == "fr") {
                                            $('#pnlData').append("<div class='RoundSeperator'>RONDE " + this.Round + "<div>");
                                        } else {
                                            $('#pnlData').append("<div class='RoundSeperator'>ROUND " + this.Round + "<div>");
                                        }
                                        count = this.Round;
                                        round++;
                                        highestRound = round;
                                    }
                                    $('#pnlData').append(CreateDom(o));
                                    currentRound = round; //store the last round that was loaded
                                });
                            }
                        } //End Success
                    }); //end ajax: GetDraft
                } else {
                    //$('#pnlData').html("There Is Currently No Draft In Progress");
                }
            }
        } //End Success
    }); //end ajax: GetTeams

    $('#ddlTeams').selectmenu({ style: 'dropdown', maxHeight: 300 });
    $('#ddlTeams').change(function() {
        $("#pnlData > div").show();
        var blah = $(this).val()
        if (blah != "000") {
            $("#pnlData > div:not([name='" + blah + "'])").hide();
        }
    });

    $('#chkAutoScroll').button();
    $('#radio1').buttonset();

    $('#lblRadio2').click(function() {
        $('#ddlTeams').val("000");
        var tmpDraftOrder = DraftOrder.slice(0);
        var teams = new Array();
        tmpDraftOrder.sort(function(a, b) {
            var thisObject = a.SelectedPlayerName;
            var thatObject = b.SelectedPlayerName;
            if (thisObject == "") {
                if (a.Overall > b.Overall) {
                    return 1;
                }
                else if (a.Overall < b.Overall) {
                    return -1;
                }
            }
            else if (thatObject == "") { return -1; }
            if (thisObject > thatObject) { return 1; }
            else if (thisObject < thatObject) { return -1; }
            return 0;
        });
        $('#pnlData').html(""); //Clear the list
        $(tmpDraftOrder).each(function() {
            //Add new list to the viewport
            $('#pnlData').append(CreateDom(this));
        });
    });

    $('#lblRadio1').click(function() {
        $('#ddlTeams').val("000");
        $('#pnlData').html(""); //Clear the list
        var count = 0;
        var round = 1;
        $(DraftOrder).each(function() {
            //Add new list to the viewport  
            if (count == 0) {
                if (language == "fr") {
                    $('#pnlData').append("<div class='RoundSeperator'>RONDE " + round + "<div>");
                } else {
                    $('#pnlData').append("<div class='RoundSeperator'>ROUND " + round + "<div>");
                }
                count = Teams.length;
                round++;
            }
            $('#pnlData').append(CreateDom(this));
            count--;
        });
    });

    //reloads the full draft information
    function LoadDraft() {
        var count = 0;
        var round = 1;

        $.ajax({
            type: "POST",
            url: "Default.aspx/GetDraft",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(m) {
                if (m != null) {
                    hasStarted = true;
                    DraftOrder = new Array();
                    $('#pnlData').html(" ");
                    $(m.d).each(function() {
                        var o = jQuery.extend(new Pick, this);
                        DraftOrder.push(o);
                        lastOverall = o.Overall;

                        if (count != this.Round) {
                            if (language == "fr") {
                                $('#pnlData').append("<div class='RoundSeperator'>RONDE " + this.Round + "<div>");
                            } else {
                                $('#pnlData').append("<div class='RoundSeperator'>ROUND " + this.Round + "<div>");
                            }
                            count = this.Round;
                            round++;
                            highestRound = round;
                        }
                        $('#pnlData').append(CreateDom(o));
                        currentRound = round; //store the last round that was loaded

                        //check and see if any players have been received
                        if (DraftOrder == null) {
                            hasStarted = false;
                        }

                    });
                }
            } //End Success
        }); //end ajax: GetDraft
    }

    // update the status of the current draft, and determine when to stop
    function GetDraftStatus() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetDraftStatus",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(res) {
                if (res != null) {
                    stillUpdating = true;
                    var temp = jQuery.parseJSON(res.d);

                    // computes the MD5 signature of the timestamps for the draft picks that have been made
                    var timeTotal = "";
                    if (DraftOrder[0] != null) {
                        for (i = 0; i < DraftOrder.length; i++) {
                            var tmp = DraftOrder[i].LastUpdated;
                            tmp = tmp.substring(6, tmp.length - 2);
                            timeTotal += tmp;
                        }

                        if (temp.md5 != MD5(timeTotal)) {
                            //the client information differs from the server
                            LoadDraft();
                        }
                    }
                }
            }
        });
    }

    //check for any draft status changes every 12 seconds
    window.setInterval(function() {
        GetDraftStatus();
    }, 12222);

    var lid = $('#hidLeagueID').val();
    checkInterval = window.setInterval(function() {
        var tmp = lastcheck;
        lastcheck = new Date();

        //only have one outgoing request for data at any given time to reduce server load
        $.ajax({
            type: "POST",
            url: "Default.aspx/GetDraftUpdate",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(m) {
                if (m != null) {
                    if (m.d != null) {
                        if (hasStarted == false) {
                            $('#pnlData').html("");
                            //If the draft has just started then load the teams and draft order.
                            $.ajax({
                                async: false,
                                type: "POST",
                                url: "Default.aspx/GetTeams",
                                data: "{}",
                                contentType: "application/json; charset=utf-8",
                                dataType: "json",
                                success: function(m) {
                                    if (m.d != null && m.d != "") {
                                        Teams = jQuery.parseJSON(m.d);
                                        if (language == "fr") {
                                            $('#ddlTeams').html("<option value='000'>Toutes les équipes</option>");
                                        } else {
                                            $('#ddlTeams').html("<option value='000'>All Teams</option>");
                                        }
                                        $(Teams).each(function() {
                                            $('#ddlTeams').append("<option value='" + this.TeamID + "'>" + this.Name + "</option>");
                                        });
                                        $('#ddlTeams').selectmenu('destroy');
                                        $('#ddlTeams').selectmenu({ style: 'dropdown', maxHeight: 300 });
                                        lastcheck = new Date();
                                        var count = 0;
                                        var round = 1;
                                        //Get player information to show in new tab
                                        $.ajax({
                                            async: false,
                                            type: "POST",
                                            url: "Default.aspx/GetDraft",
                                            data: "{}",
                                            contentType: "application/json; charset=utf-8",
                                            dataType: "json",
                                            success: function(m) {
                                                hasStarted = true;
                                                $('#pnlData').html(" ");
                                                $(m.d).each(function() {
                                                    var o = jQuery.extend(new Pick, this);
                                                    DraftOrder.push(o);
                                                    lastOverall = o.Overall;
                                                    if (count == 0) {
                                                        if (language == "fr") {
                                                            $('#pnlData').append("<div class='RoundSeperator'>RONDE " + round + "<div>");
                                                        } else {
                                                            $('#pnlData').append("<div class='RoundSeperator'>ROUND " + round + "<div>");
                                                        }
                                                        count = Teams.length;
                                                        round++;
                                                        highestRound = round;
                                                    }
                                                    $('#pnlData').append(CreateDom(o));
                                                    count--;
                                                });
                                            } //End Success
                                        }); //end ajax: GetDraft
                                    }
                                } //End Success
                            }); //end ajax: GetTeams
                        }
                        else {
                            //Extend the current objects with the Pick object

                            //count the number of draft picks received
                            var count = 0;
                            $(m.d).each(function() { count++; });

                            $(m.d).each(function() { //process the new player data that was received
                                var o = jQuery.extend(new Pick, this);

                                var d = o.DomItem();
                                var old = DraftOrder[o.Overall];

                                //alert("Overall: " + o.Overall + ": " + o.SelectedPlayerName);
                                //if this is a new player trade, add it to the screen, and avoid duplicates
                                if (o.SelectedPlayerName != "") {
                                    DraftOrder[o.Overall] = o;
                                }

                                if (lastOverall < o.Overall) {
                                    d.after(CreateDom(o)).remove();
                                    var arrIndex = o.Overall - 1;

                                    // check if a new round was added later
                                    if (o.Round > highestRound - 1) {
                                        highestRound = o.Round + 1;
                                        if (language == "fr") {
                                            $('#pnlData').append("<div class='RoundSeperator'>RONDE " + this.Round + "<div>");
                                        } else {
                                            $('#pnlData').append("<div class='RoundSeperator'>ROUND " + this.Round + "<div>");
                                        }
                                        count = this.Round;
                                        round++;
                                    }

                                    $('#pnlData').append(CreateDom(o));
                                    lastOverall = o.Overall;


                                    //autoscroll to the newly-added player if the user has enabled this option, and only one player was received
                                    if ((($('#lblAutoScroll').attr('aria-pressed') == 'true')) && (count < 3)) {
                                        //$('#pnlData').scrollTo(o.DomItem(), 800, { over: -8 });
                                    }

                                    //var bl = DraftOrder.splice(arrIndex, 1, o);

                                    //updates the status ticker at the top of whether teams have traded their draft positions
                                    if (this.RinknetTeamID != this.TradedTo) {
                                        if (language == "fr") {
                                            var strTrade = FindTeamName(this.RinknetTeamID) + "'s  " + this.Overall + " Choix au repêchage échangé avec " + FindTeamName(this.TradedTo);
                                            $('#lblDraftMessage').html(strTrade).show('slide', { direction: 'up' });
                                        } else {
                                            var strTrade = FindTeamName(this.RinknetTeamID) + "'s  " + this.Overall + " Overall Pick Traded To " + FindTeamName(this.TradedTo);
                                            $('#lblDraftMessage').html(strTrade).show('slide', { direction: 'up' });
                                        }
                                        setTimeout(function() {
                                            $('#lblDraftMessage').hide('slide', { direction: 'down' });
                                        }, 6000);
                                    }
                                }
                                else {
                                    //we've received some player information out of the expected order, update what's on the screen
                                    DraftOrder[o.Overall] = o;

                                    //                                // check if a new round was added later
                                    //                                if (o.Round > highestRound) {
                                    //                                    highestRound = o.Round;
                                    //                                    $('#pnlData').append("<div class='RoundSeperator'>ROUND " + this.Round + "<div>");
                                    //                                    count = this.Round;
                                    //                                    round++;
                                    //                                }

                                    if (typeof old != 'undefined') {
                                        if (old.SelectedPlayerName == "") {
                                            DraftOrder[o.Overall] = o;
                                            d.after(CreateDom(o)).remove();
                                            var arrIndex = o.Overall - 1;

                                            //insert the updated player information directly into where it already was
                                            var after = "#dft" + o.Overall;
                                            $(after.toString()).html(CreateDomReplacement(o));

                                            //autoscroll to the newly-added player if the user has enabled this option
                                            if (($('#lblAutoScroll').attr('aria-pressed') == 'true') && (count < 3)) {
                                                //$('#pnlData').scrollTo(o.DomItem(), 800, { over: -8 });
                                            }
                                        } else {
                                            d.after(CreateDom(o)).remove();
                                            var arrIndex = o.Overall - 1;

                                            //insert the updated player information directly into where it already was
                                            var after = "#dft" + o.Overall;
                                            $(after.toString()).html(CreateDomReplacement(o));
                                        }
                                    }
                                }
                            }); // End loop


                        }
                    } //End isNull Check
                }
            } //End Success
        }); //End ajax: GetplayerInfo
    }, updateInterval);

    //periodically checks to see what number was the last one scrolled, and whether we've already scrolled to it
    lastScrolled = -1;
    setInterval(function() {
        if (hasStarted) { //only check once the draft has started
            $.ajax({
                type: "POST",
                url: "Default.aspx/ScrollToNumber",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(m) {
                    if (lastScrolled < m.d) { //don't scroll if the information is older than what we've already received
                        lastScrolled = m.d;   //now scroll the last number we're scrolling to
                        if ($('#lblAutoScroll').attr('aria-pressed') == 'true') {
                            $('#pnlData').scrollTo(DraftOrder[lastScrolled].DomItem(), 800, { over: -8 }); //perform the scrolling
                        }
                    }
                },
                error: function(m) {
                    // this shouldn't be possible unless there's a timeout, or some sort of exception is thrown on the server
                }
            });
        }
    }, 5000);

});

//added by Ryan, used to create a replacement for the contents of the player panel when new information is received
function CreateDomReplacement(itm) {
    if (itm.DraftInProgress == "false") //check to see if the draft has ended
        this.DraftEnded = true;

    var tn = FindTeamName(itm.TradedTo);
    var otn = FindTeamName(itm.RinknetTeamID);

    var h = "";
    h += "<span class='Team'>";
    //h += "<input type='checkbox' />";
    h += itm.Overall;
    h += " &nbsp;<span>";
    h += tn;
    h += "</span>";
    if (tn != otn) {
        if (language == "fr") {
            h += " <br/><span style='font-size:x-small;'>(de " + otn + ")</span>";
        } else {
            h += " <br/><span style='font-size:x-small;'>(From " + otn + ")</span>";
        }
    }
    h += "</span>";
    h += "<span class='Player'>";
    if (itm.SelectedPlayerName != null && itm.SelectedPlayerName != "") {

        if ((itm.SelectedPlayerName == "Pass") && (language == "fr")) {
            h += "<span class='Element' style='width:205px;'>" + itm.SelectedPlayerName + "e</span>";
        } else {
            h += "<span class='Element' style='width:205px;'>" + itm.SelectedPlayerName + "</span>";
        }

        if (itm.SelectedPlayerID != null && itm.SelectedPlayerID != 0) {

            //if the language is French, change the positions to their french equivalents
            h += "<span class='Element' style='width:42px;'>";
            if (language == "fr") {
                switch (itm.SelectedPlayerPosition) {
                    case "LD": h += "DG"; break;
                    case "RD": h += "DD"; break;
                    case "C": h += "C"; break;
                    case "RW": h += "AD"; break;
                    case "LW": h += "AG"; break;
                    case "F": h += "A"; break;
                    case "G": h += "G"; break;
                    case "C/LW": h += "C/AG"; break;
                    case "C/RW": h += "C/AD"; break;
                    default: h += itm.SelectedPlayerPosition; break;
                }
            } else {
                h += itm.SelectedPlayerPosition;
            }
            h += "</span>";

            if ($('#hidLeagueID').val() == -163525706) {
                h += "<span class='Element' style='font-size:1em;'>" + itm.BirthPlace + "</span>";
            } else {
                h += "<span class='Element' style='font-size:1em;' >" + itm.HomeTown + "</span>";
            }
            if ($('#hidLeagueID').val() != -141419917) {
                h += "<span class='Element' style='width:58px;'>" + itm.Height + "</span>";
                h += "<span class='Element' style='width:33px;'>" + itm.Weight + "</span>";
            }
            h += "<span class='Element' style='width:197px;'>" + itm.PreviousTeam;
            if (itm.League != "") {
                h += "(" + itm.League + ")"
            }
            h += "</span>";
            h += "<span class='Element' style='width:80px;'>" + itm.Birthdate + "</span>";
        }
    }
    h += "</span>";
    return h;

}

// returns the french verison of the birthdate given the english one. English: MMM(en) DD/YY => DD MMM(fr)/YY
function toFrenchBirthdate(english) {
    var frenchAbbreviations = new Array(12);
    for (i = 0; i < 12; i++)
        frenchAbbreviations[i] = new Array(2);
        
    frenchAbbreviations[0][0] = "Jan"; frenchAbbreviations[0][1] = "Janv";  
    frenchAbbreviations[1][0] = "Feb"; frenchAbbreviations[1][1] = "Fév";  
    frenchAbbreviations[2][0] = "Mar"; frenchAbbreviations[2][1] = "Mars";  
    frenchAbbreviations[3][0] = "Apr"; frenchAbbreviations[3][1] = "Avr";  
    frenchAbbreviations[4][0] = "May"; frenchAbbreviations[4][1] = "Mai";  
    frenchAbbreviations[5][0] = "Jun"; frenchAbbreviations[5][1] = "Juin";  
    frenchAbbreviations[6][0] = "Jul"; frenchAbbreviations[6][1] = "Juil";  
    frenchAbbreviations[7][0] = "Aug"; frenchAbbreviations[7][1] = "Août";  
    frenchAbbreviations[8][0] = "Sep"; frenchAbbreviations[8][1] = "Sept";  
    frenchAbbreviations[9][0] = "Oct"; frenchAbbreviations[9][1] = "Oct";  
    frenchAbbreviations[10][0] = "Nov"; frenchAbbreviations[10][1] = "Nov";  
    frenchAbbreviations[11][0] = "Dec"; frenchAbbreviations[11][1] = "Déc";
    for (i = 0; i < 12; i++) {
        if (english.indexOf(frenchAbbreviations[i][0]) != -1) {
            var tmp = english.replace(frenchAbbreviations[i][0], frenchAbbreviations[i][1]);
            var pieces = tmp.split("/");
            var pieces2 = pieces[0].split(" ");
            tmp = pieces2[1] + " " + pieces2[0] + "/" + pieces[1];
            return tmp;
            break;
        }
    }
    return english;
}

function CreateDom(itm) {
    
    if (itm.DraftInProgress == "false") //check to see if the draft has ended
        this.DraftEnded = true;
        
    var tn = FindTeamName(itm.TradedTo);
    var otn = FindTeamName(itm.RinknetTeamID);
    
    var h = "";
    h += "<div class='DraftElement' id='dft";
    h += itm.Overall;
    h += "' name='";
    h += itm.TradedTo;
    h += "'>";
    h += "<span class='Team'>";
    //h += "<input type='checkbox' />";
    h += itm.Overall;
    h += " &nbsp;<span>";
    h += tn;
    h += "</span>";
    if (tn != otn) {
        if (language == "fr") {
            h += " <br/><span style='font-size:x-small;'>(de " + otn + ")</span>";
        } else {
            h += " <br/><span style='font-size:x-small;'>(From " + otn + ")</span>";
        }
    }
    h += "</span>";
    h += "<span class='Player'>";
    if (itm.SelectedPlayerName != null && itm.SelectedPlayerName != "") {
        
        if ((itm.SelectedPlayerName == "Pass") && (language == "fr")) {
            h += "<span class='Element' style='width:205px;'>" + itm.SelectedPlayerName + "e</span>";
        } else {
            h += "<span class='Element' style='width:205px;'>" + itm.SelectedPlayerName + "</span>";
        }

        if (itm.SelectedPlayerID != null && itm.SelectedPlayerID != 0) {

            //if the language is French, change the positions to their french equivalents
            h += "<span class='Element' style='width:42px;'>";
            if (language == "fr") {
                switch (itm.SelectedPlayerPosition) {
                    case "LD": h+="DG"; break;
                    case "RD": h+="DD"; break;
                    case "C" : h+="C"; break;
                    case "RW": h+="AD"; break;
                    case "LW": h+="AG"; break;
                    case "G": h += "G"; break;
                    case "C/LW" : h+="C/AG"; break;
                    case "C/RW": h += "C/AD"; break;
                    case "F": h += "A"; break;
                    default: h+=itm.SelectedPlayerPosition; break;
                }
            } else {
                h += itm.SelectedPlayerPosition;
            }
            h += "</span>";

            if ($('#hidLeagueID').val() == -163525706) {
                h += "<span class='Element' style='font-size:0.9em;'>" + itm.BirthPlace + "</span>";
            } else {
                h += "<span class='Element' style='font-size:0.9em;' >" + itm.HomeTown + "</span>";
            }
            if ($('#hidLeagueID').val() != -141419917) {
                h += "<span class='Element' style='width:58px;'>" + itm.Height + "</span>";
                h += "<span class='Element' style='width:33px;'>" + itm.Weight + "</span>";
            }
            h += "<span class='Element' style='width:197px;'>" + itm.PreviousTeam;
            if (itm.League != "") {
                h += "(" + itm.League + ")"
            }
            h += "</span>";
            h += "<span class='Element' style='width:80px;'>";
            if (language == "fr") {
                h += toFrenchBirthdate(itm.Birthdate);
            } else {
                h += itm.Birthdate;
            }
            h += "</span>";
        }
    }
    h += "</span>";
    h += "</div>";
    return h;
}  // End CreateDom


function Pick() {
    this.DraftOrderID = 0;
    this.DraftType = 0;
    this.RinknetTeamID = 0;
    this.TradedTo = 0;
    this.Overall = 0;
    this.DomID = '';
    this.SelectedPlayerID = -1;
    this.SelectedPlayerName = "";
    this.SelectedPlayerPosition = "";
    this.PreviousTeam = "";
    this.Height = "";
    this.weight = "";
    this.HomeTown = "";

    this.BirthPlace = "";
    
    this.Birthdate = "";
    this.League = "";
    this.Proposed = false;
    this.Round = "";
    this.LastUpdated = "";

    this.hasScrolled = false; //stores whether this pick has already been scrolled to once

    this.DomItem = function() {
        var tmp = "#dft" + this.Overall;
        return $(tmp);
    }

    this.md5 = function() {
        return MD5(this.lastUpdated);
    }
}

function FindTeamName(id) {
    var l = Teams.length
    for (var i = 0; i < l; i++) {
        var tmp = Teams[i];
        if (tmp.TeamID == id) {
            return tmp.Name;
        }
    }
} //End Find Name


function LeagueSetUp() {

    //Do initial Setup

    var leagueVal = $('#hidLeagueID').val();
    var strLogo;
    switch (leagueVal) {
        case '-1183003911':
            strLogo = 'images/CHL/';
            break;
        case '-1096075095':
            strLogo = 'images/WHL/';
            break;
        case '-1881824417':
            strLogo = 'images/NHL/';
            break;
        case '-1462202327':
            strLogo = 'images/NHL/';
            break;
        case '-585474264':
            strLogo = 'images/NHL/';
            break;
        case '-255723114':
            strLogo = 'images/NHL/';
            break;
        case '-243139839':
            strLogo = 'images/CJHL/';
            break;
        case '-163525706':
            strLogo = 'images/QMJHL/';
            var map = "<map name='adMap'>";
            map += "<area shape='rect' coords='0,0,115,72' href='http://www.telus.com/regionselect.html' target='_new' />";
            map += "<area shape='rect' coords='116,0,221,72' href='http://www.hockeycanada.ca/' target='_new' />";
            map += "<area shape='rect' coords='221,0,334,72' href='http://olddutchfoods.ca/eng/index.php' target='_new' />";
            map += "<area shape='rect' coords='334,0,485,72' href='http://www.cordonbleu.edu/' target='_new' />";
            map += "<area shape='rect' coords='485,0,581,72' href='http://www.allweatherwindows.com/area/corporate/' target='_new' />";
            map += "<area shape='rect' coords='581,0,728,72' href='http://www.comfortinn.com/' target='_new' />";
            map += "<area shape='rect' coords='728,0,862,72' href='http://www.grafcanada.com/hockey/hockey_en.php' target='_new' />";
            map += "<area shape='rect' coords='863,0,1000,72' href='http://www.bauer.com/' target='_new'/>";
            map += "</map>";
            $('#adFooter').append(map);

            break;
        case '-141419917':
            strLogo = 'images/USHL/Future/';
            break;
        case '62199540':
            strLogo = 'images/USHL/';
            break;
        case '-48935412':
            strLogo = 'images/KHL/';
            break;
        case '-69950528':
            strLogo = 'images/OHL/';
            break;
        case '901639240':
            strLogo = 'images/OHL/';
            break;
        case '209132757':
            strLogo = 'images/NAHL/';
            break;           
    }
    $('#imgLeagueLogo').attr('src', strLogo + 'logo.png');

    //load the team ad if there is one present
    var height = $(window).height();

    if (showAds == "true") {
        //if ushl future, then set the background to black
        if (leagueVal == '-141419917') {
            //$('#adFooter').style.backgroundColor = '#000000';
            $('#adFooter').css('backgroundColor', '#606060');
        }
        $('#imgAdSponsors').attr('src', strLogo + 'Ad.png');
        $('#imgAdSponsors').attr('usemap', '#adMap');
        $('#pnlData').height(height - 105 - $('#adFooter').height()); //changed to subtract the league ads at the bottom
        $(window).resize(function() {
            $('#pnlData').height($(this).height() - 105 - $('#adFooter').height());
        });
    } else {
        $('#pnlData').height(height - 105);
        $('#imgAdSponsors').attr('src', 'Images/AdBlank.png');
        $('#adFooter').css('display', 'none'); //show no ads at the bottom of the screen, hide the ad footer
        $(window).resize(function() {
            $('#pnlData').height($(this).height() - 105);
        });

    }    

    // load the team's logo as the background
    //$('body').css('background-image', 'url(' + strLogo + 'BackLogo.png)');
}

function DraftPrint() {
    var leagueVal = $('#hidLeagueID').val();
    if (language == null || language == "")
        {language = "en";}
    if (win1 != null && !win1.closed) { win1.close(); }
    //alert('../print.aspx?id=' + leagueVal + '&language=' + language, 'p', 'width=360,height=205,scrollbars=0,resizable=1');
    win1 = window.open('../print.aspx?id=' + leagueVal+'&language='+language, 'p', 'width=360,height=205,scrollbars=0,resizable=1');
    win1.moveTo(screen.availWidth / 2 - (360 / 2), screen.availHeight / 2 - (205 / 2));
}
