// Controls this expand and collapse and the linking to the club display

function GoToClub(clubtoshow) {

    //Update donation URL
    $("#donation_url").val('donation.aspx?ClubID=' + clubtoshow);
    
    $('#clubinfo').fadeOut()
    hideallregions();
    $('#clubinfo_scrollingarea').html('');
    $('#clubinfo_total').html('');

    $.getJSON("ws.aspx?type=ClubInfo&ClubID=" + clubtoshow, function(json) {
        $("#clubinfo_title").text(json.name);
        $("#clubinto_header").text('Latest comments: ' + json.name);
        $("#clubinfo_total").text('Total Raised: $' + json.total);
        $("#clubinfo_description").html(json.description);

        // Load Comments into area
        $.getJSON("ws.aspx?type=Comments&Count=100&Length=250&ClubID=" + clubtoshow, function(data) {
            $.each(data.items, function(i, item) {
                $('#clubinfo_scrollingarea').append('<div class="clubinfo_comment">&quot;' + item.comment + '&quot;</div><div class="clubinfo_from">' + item.name + '</div>');
            });
            //Call back	
        });


    });
    $('#clubinfo').fadeIn();
}

function DisplayClubListing(RegionID) {
    $.getJSON("ws.aspx?type=ClubsListing&RegionID=" + RegionID, function(data) {
    //$.getJSON("json/clubtotalslisting.js?type=ClubsListing&RegionID=" + RegionID, function(data) {
        $.each(data.items, function(i, item) {
            $('#clublisting' + RegionID).append('<div class="regionlisting_club" rel="' + item.clubid + '"><div class="regionlisting_clubname">' + item.clubname + '</div><div class="regionlisting_clubammount">$ ' + item.clubtotal + '</div></div>');
        });
        ReconnectGotoClub(RegionID);
    });
}


function DisplayClubListingActivate() {
    var iloop = 1;
    while (iloop <= 20) {
        DisplayClubListing(iloop);
        iloop++;
    }
    //alert("Hello ReconnectGotoClub");
    //ReconnectGotoClub();
}


function ReconnectGotoClub(RegionID) {
    // Placing the functions as part of the JSON call back makes the new DOM items accessible
    //alert("Hello ReconnectGotoClub");
    $('#clublisting' + RegionID + ' > .regionlisting_club').click(function() {
        var clubtoshow = $(this).attr('rel');
        GoToClub(clubtoshow);
    });
}


// JQuery to execute after document loaded
$(document).ready(function() {

    // Show all comments on footer of dashboard
    $.getJSON("ws.aspx?Type=Comments&Count=50&Length=200", function(data) {
        $.each(data.items, function(i, item) {
            $('#latestcomments').append('<div class="latestcomment_cycle"><div class="latestcomment_comment">&quot;' + item.comment + '&quot;</div><div class="latestcomment_name">' + item.name + '</div></div>');
        });
        // Cycle through comments
        $('#latestcomments').cycle({
            fx: 'scrollVert',
            speed: 1000,
            timeout: 6000,
            pause: 1,
            prev: '#latestcomments_prev',
            next: '#latestcomments_next'

        });

    });




    // Display Club information via JSON
    $('.club_pointer').click(function() {
        var clubtoshow = $(this).children("a").attr('rel');
        GoToClub(clubtoshow);
    });
    $('.club_name').click(function() {
        var clubtoshow = $(this).attr('rel');
        GoToClub(clubtoshow);
    });

    // back to map from club info
    $("#clubinfo_backtomap").click(function() {
        hideallregions();
        $("#main_map").show('slow');
    });


    //Regions Accordian //
    //Hide all children in navigation menu
    $('#navigation div.menu_body').hide();

    //slides the element with class "menu_body" when paragraph with class "menu_head" is clicked 
    $("div.region_head").click(function() {
        $(this).next("div.region_body").slideToggle(300).siblings("div.region_body").slideUp(300);
        $(".expandcollapse").html('<img src="Images/ListingBox/expand.png" alt="Expand" />');
        $(this).children(".expandcollapse").html('<img src="Images/ListingBox/collapse.png" alt="Collapse" />');
    });

    //Populoate list with JSON
    // National Total
    $.getJSON("ws.aspx?Type=NationalTotal", function(json) {
        $("#listing_footer_total").text(json.nationaltotal);
    });

    // Regions Totals
    $.getJSON("ws.aspx?Type=Regions", function(json) {
        $("#region_head_1").text(json.Region1);
        $("#region_head_2").text(json.Region2);
        $("#region_head_3").text(json.Region3);
        $("#region_head_4").text(json.Region4);
        $("#region_head_5").text(json.Region5);
        $("#region_head_6").text(json.Region6);
        $("#region_head_7").text(json.Region7);
        $("#region_head_8").text(json.Region8);
        $("#region_head_9").text(json.Region9);
        $("#region_head_10").text(json.Region10);
        $("#region_head_11").text(json.Region11);
        $("#region_head_12").text(json.Region12);
        $("#region_head_13").text(json.Region13);
        $("#region_head_14").text(json.Region14);
        $("#region_head_15").text(json.Region15);
        $("#region_head_16").text(json.Region16);
        $("#region_head_17").text(json.Region17);
        $("#region_head_18").text(json.Region18);
        $("#region_head_19").text(json.Region19);

    });


    // List Clubs for Regions
   DisplayClubListingActivate();







}); // Document Ready END
