//When deploy to production ,set HOST_URL to "http://tennislink.usta.com/".
var HOST_URL = "http://tennislink.usta.com/";

$(function() {
    $(".tournaments-widget").each(function() {
        _build_tmts_widget($(this));
    });
});

function _build_tmts_widget(widget) {
    
    //Get parameters
    var Section = widget.attr("Section");

    var District = widget.attr("District");

    var ZipCode = widget.attr("ZipCode");

    var Radius = Number(widget.attr("Radius"));
    Radius = isNaN(Radius) ? 0 : Radius;

    var CurrentlyRegistering = true;

    var MaxNumber = Number(widget.attr("MaxNumber"));
    MaxNumber = isNaN(MaxNumber) ? 0 : MaxNumber;
    
    //Build html content
    var html = '';
    html += 'Register for Upcoming Tournaments';
    html += '<table cellspacing="0">';
    html += '<tr>';
    html += '<td><a href="#" class="_widget_menu_junior"><img src="' + HOST_URL + 'tournaments/Widgets/junior.gif" /></a><a href="#" style="margin-left:10px" class="_widget_menu_adult"><img src="' + HOST_URL + 'Widgets/adult.gif"/></a></td>';
    html += '</tr>';
    html += '<tr><td>';
    html += '<ul></ul>';
    html += '<tr><td>';
    html += '<a class="_widget_view_all" target="_blank"></a>';
    html += '</td></tr>';
    html += '</table>';

    widget.html(html);

    //Set up menu
    widget.find("._widget_menu_junior").click(function() {
        widget.find("._widget_menu_junior").find("img").attr("src", HOST_URL + "tournaments/Widgets/junior-hover.gif");
        widget.find("._widget_menu_adult").find("img").attr("src", HOST_URL + "tournaments/Widgets/adult.gif");
        $(this).attr("selected", "1").siblings().attr("selected", "0");
        _load_tournaments(Section, District, false, true, ZipCode, Radius, CurrentlyRegistering, MaxNumber, widget);
    });

    widget.find("._widget_menu_adult").click(function() {
        widget.find("._widget_menu_junior").find("img").attr("src", HOST_URL + "tournaments/Widgets/junior.gif");
        widget.find("._widget_menu_adult").find("img").attr("src", HOST_URL + "tournaments/Widgets/adult-hover.gif");
        $(this).attr("selected", "1").siblings().attr("selected", "0");
        _load_tournaments(Section, District, true, false, ZipCode, Radius, CurrentlyRegistering, MaxNumber, widget);
    });

    widget.find("._widget_menu_junior").hover(
        function() {
            $(this).find("img").attr("src", HOST_URL + "tournaments/Widgets/junior-hover.gif");
        },
        function() {
            if ($(this).attr("selected") == "0")
                $(this).find("img").attr("src", HOST_URL + "tournaments/Widgets/junior.gif");
        });

    widget.find("._widget_menu_adult").hover(
        function() {
            $(this).find("img").attr("src", HOST_URL + "tournaments/Widgets/adult-hover.gif");
        },
        function() {
            if ($(this).attr("selected") == "0")
                $(this).find("img").attr("src", HOST_URL + "tournaments/Widgets/adult.gif");
        });

    //Click a menu.
    widget.find("._widget_menu_junior").click();
}

function _load_tournaments(sSection,
            sDistrict,
            bContainsAdult,
            bContainsJunior,
            sZipCode,
            iRadius,
            bCurrentlyRegistering,
            iMaxNumber,
            widget
            ) {
	var dtNow = new Date();
	var currentMonth = dtNow.getMonth() + 1;
	var currentYear = dtNow.getFullYear();
	var division;
    if (bContainsJunior) {
		division = "G8";
        widget.find("._widget_view_all").text("View All Junior Tournaments");
    }
    else {
		division = "G9";
        widget.find("._widget_view_all").text("View All Adult Tournaments");
    }
	widget.find("._widget_view_all").attr("href", HOST_URL + "tournaments/Schedule_New/SearchResults.aspx?typeofsubmit=&Action=2&Keywords=&TournamentID=&SectionDistrict=" + sSection + "&City=&State=&Zip=&Month=" + currentMonth + "&Day=&Year=" + currentYear + "&Division=" + division + "&Category=&Surface=&OnlineEntry=&DrawsSheets=&WIDGET=TDSV1");
        
    widget.find("li").remove();
    widget.find("ul").append($("<li>Loading...</li>"));
    $.getScript(
        HOST_URL + "PublicWebServices/GetTournamentsJSON.ashx?var=widget_tournaments&sSection=" + sSection + "&sDistrict=" + sDistrict + "&bContainsAdult=" + bContainsAdult + "&bContainsJunior=" + bContainsJunior + "&sZipCode=" + sZipCode + "&iRadius=" + iRadius + "&bCurrentlyRegistering=" + bCurrentlyRegistering + "&iMaxNumber=" + iMaxNumber,
        function() {
            widget.find("li").remove();

            for (var i = 0; i < widget_tournaments.length; i++) {
                var html = '';
                html += '<li>';
                html += '<a target="_blank" href="' + HOST_URL + 'tournaments/TournamentHome_New/Tournament.aspx?T=' + widget_tournaments[i].TournamentId + '&WIDGET=TDSV1">';
				var displayName = widget_tournaments[i].TournamentName;
				if(displayName.length >= 28)
					displayName = displayName.substr(0, 28) + "...";
                html += displayName;
                html += '</a>';
                html += '</li>';

                widget.find("ul").append($(html));
            }
        }
    );
}
