//***** GLOBAL JS ******

//---------
//PRE DOCUMENT-LOAD METHODS
getBills();

//DOCUMENT READY METHODS
$(document).ready(function() {
    get_upcoming_bills();

});

//***** FUNCTIONS *****
function getUrl(bill_id) {
    return 'https://congress.api.sunlightfoundation.com/bills?bill_id=' + bill_id + '&apikey=838cd938cfb244a7a5728083f9191152';
};

function getCommitteeUrl(committee_id) {
    return 'https://congress.api.sunlightfoundation.com/committees?committee_id=' + committee_id + '&apikey=838cd938cfb244a7a5728083f9191152';
}

function getSessionUrl(congressional_session) {
    return 'https://congress.api.sunlightfoundation.com/bills?congress=' + congressional_session + '&apikey=838cd938cfb244a7a5728083f9191152';
}

function getUpcomingUrl(){
    return 'https://congress.api.sunlightfoundation.com/upcoming_bills?apikey=838cd938cfb244a7a5728083f9191152';
}

function get_upcoming_bills() {
    $.ajax({
        type: 'GET',
        url: getUpcomingUrl()
    }).done(function(response){
        console.log(response)
    }).fail(function(error){
        console.log(error)
    });
};

function get_session_bills(session){
    $.ajax({
        type: 'GET',
        url: getSessionUrl(session)
    }).done(function(response){
        console.log(response)
    }).fail(function(error){
        console.log(error)
    });
};

function getBills() {
    var bill_ids = [];

    $.ajax({
        type: 'GET',
        url: getUpcomingUrl()
    }).done(function(response) {
        console.log(response)

        var bills = response.results

        $.each(bills, function(){
            bill_ids.push(this.bill_id)
        });

        billCommittees(bill_ids)
    }).fail(function(error){
        console.log(error)
    })
};

function billCommittees(upcoming_bills) {
    $.each(upcoming_bills, function(){
        var request = getUrl(this)

        $.ajax({
            type: 'get',
            url: request
        }).done(function(response){
            if(response.results && response.results.length > 0){
                var committee_list = response.results[0].committee_ids
                setCommitteeKeywords(response, committee_list);
            } else {
                console.log('Bill not found', request);
            }
        }).fail(function(error){
            console.log(error)
        })
    });
};


function setCommitteeKeywords(bill_obj, committee_ids) {

    $.each(committee_ids, function(){
        var request = getCommitteeUrl(this)

        $.ajax({
            type: 'get',
            url: request
        }).done(function(response){
            var bill = {}
            var extractable_bill_obj = bill_obj.results[0];
            var committee_id = response.results[0].committee_id
            var committee_name = response.results[0].name

            bill['bill_id'] = extractable_bill_obj.bill_id;
            bill['committee_id'] = committee_id;
            bill['committee_name'] = committee_name;
            bill['congress'] = extractable_bill_obj.congress;
            bill['chamber'] = extractable_bill_obj.chamber;
            bill['url_congress'] = extractable_bill_obj.urls.congress;
            bill['url_govtrack'] = extractable_bill_obj.urls.govtrack;
            bill['url_opencongress'] = extractable_bill_obj.urls.opencongress;
            bill['intro_date'] = extractable_bill_obj.introduced_on;
            bill['last_action_date'] = extractable_bill_obj.last_action_at;
            bill['last_version_date'] = extractable_bill_obj.last_version_on;
            bill['history'] = extractable_bill_obj.history;

            addBill(bill);
        }).fail(function(error){
            console.log(error)
        });
    });
}

function addBill(bill) {
    $.ajax({
        type: 'POST',
        data: bill,
        url: '/bills/add_bill',
        dataType: 'JSON'
    }).done(function(response){
        console.log(response)
    }).fail(function(error){
        console.log(error)
    })
};










@@ -131,35 +131,33 @@
 () %7B%0A    get
-_u
+U
 pcoming
-_b
+B
 ills();%0A%0A%7D);
@@ -190,24 +190,28 @@
 function get
+Bill
 Url(bill_id)
@@ -885,19 +885,17 @@
  get
-_u
+U
 pcoming
-_b
+B
 ills
@@ -1109,18 +1109,16 @@
  get
-_s
+S
 ession
-_b
+B
 ills
@@ -1475,39 +1475,8 @@
 ) %7B%0A
-        console.log(response)%0A%0A
     
@@ -1508,17 +1508,16 @@
 results%0A
-%0A
         
@@ -1594,24 +1594,26 @@
     %7D);%0A
-%0A
+ 
        
+//
  billCom
@@ -1619,16 +1619,100 @@
 mmittees
+ is in the .done due to the asynchronous nature of Javascript%0A        getBillDetails
 (bill_id
@@ -1794,32 +1794,23 @@
 ion 
-billCommittees(upcoming_
+getBillDetails(
 bill
@@ -1825,25 +1825,16 @@
  $.each(
-upcoming_
 bills, f
@@ -1848,44 +1848,8 @@
 ()%7B%0A
-        var request = getUrl(this)%0A%0A
     
@@ -1895,39 +1895,48 @@
            url: 
-request
+getBillUrl(this)
 %0A        %7D).done
@@ -1948,32 +1948,66 @@
 tion(response)%7B%0A
+            console.log(response)%0A
             if(r
@@ -2154,24 +2154,16 @@
 ommittee
-Keywords
 (respons
@@ -2386,16 +2386,8 @@
 ttee
-Keywords
 (bil

Clean up functions to be single responsibility