/*jshint browser:true, jquery:true */

var jenkinsHost = 'http://alembic.local:7000';

var APIURL = jenkinsHost +
        '/api/json?' +
        '&jsonp=?';

var app = {};

app.status = {};

var jsonpHelper = function(data) {
    return data;
};

var publishItem = function(i, item){

    $('<li class="gridWidget ' + item.color + '">')
        .html('<p style="margin-left: 0;"' +
              '">' +
              '<a href="' +
              item.url +
              '" title="' +
              item.name +
              '">' +
              item.name +
             '</a>' +
              ' &nbsp; <a href="' + jenkinsHost + '/job/' + item.name + '/configure" class="conf">configure</a>' +
              '<p>')
        .click(function(){
            var buildURL = jenkinsHost +
                    '/job/' +
                    item.name +
                    '/build?delay=0sec';

            $.ajax({
                url: buildURL,
                dataType: "jsonp"
            });

            pollRemoteHost();

        })
        .appendTo( '#list' );

    return true;
};

var noisy_log = function(message){
    console.debug('[FILTER] ' + message);
};

var noisy_assert = function(expression, message){
    console.debug('[ASSERT] ' + message);
    console.assert(expression, message);
};

var updateWith = function(responseDocument){
    app.status.jenkinsAPIResponse = responseDocument;

    $('#list').html('');

    noisy_assert(_.isArray(responseDocument.jobs),
                 'I see a list of Jenkins jobs.');

    var rawList = _.
            reject(responseDocument.jobs,
                   function(item){
                       var input = item.color;
                       var result = (/^disabled$/).test(input);

                       noisy_log('Are any Jenkins jobs disabled?');

                       return result;
                   });

    $.each(rawList,
           publishItem);

    /* analysis */

    var widgets = $('#list li').length,
        passing = _.where(app.status.jenkinsAPIResponse.jobs, {color: "blue"}).length,
        failing = _.where(app.status.jenkinsAPIResponse.jobs, {color: "red"}).length;;


    $('p.description').html(widgets + ' jobs displayed.<br>' +
                            passing + ' succeeded,<br>' +
                            failing + ' falled.<br>');



};

var pollRemoteHost = function(){
    $.getJSON(APIURL, updateWith);
};

pollRemoteHost();

var eventLoop = setInterval(pollRemoteHost, 8 * 1000);

@@ -32,16 +32,634 @@
 rue */%0A%0A
+/*%0A*   Copyright 2014 Noah Sussman New Media, LLC%0A*%0A*   Licensed under the Apache License, Version 2.0 (the %22License%22);%0A*   you may not use this file except in compliance with the License.%0A*   You may obtain a copy of the License at%0A*%0A*       http://www.apache.org/licenses/LICENSE-2.0%0A*%0A*   Unless required by applicable law or agreed to in writing, software%0A*   distributed under the License is distributed on an %22AS IS%22 BASIS,%0A*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A*   See the License for the specific language governing permissions and%0A*   limitations under the License.%0A*/%0A%0A
 var jenk

update license