/* widget.js
 * Source: http://www.stefanwienert.de/blog/2013/10/15/intro-on-making-a-javascript-widget/
 */
(function() {

var jQuery;
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.11.1') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js");
    if (script_tag.readyState) {
      script_tag.onreadystatechange = function () { // For old versions of IE
          if (this.readyState == 'complete' || this.readyState == 'loaded') {
              scriptLoadHandler();
          }
      };
    } else {
      script_tag.onload = scriptLoadHandler;
    }
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
    jQuery = window.jQuery;
    main();
}

function scriptLoadHandler() {
    jQuery = window.jQuery.noConflict(true);
    main();
}

function main() {
  jQuery(document).ready(function($) {

    var ClassSearchWidget = {};
    ClassSearchWidget.format = "json";
    ClassSearchWidget.subject_code = $('.classsearch-widget').data('subject-code');

    if (typeof ClassSearchWidget.subject_code != 'undefined') {

          
      $.ajax({
        url: '//classsearch.jccc.edu/student/ClassSearch/class/subject',
        data: ClassSearchWidget,
        dataType: 'jsonp',
        jsonpCallback: 'jsonpCallback',
        success:   function(data) {
          $.each(data, function(i,d) {
            $('.classsearch-widget').append(d)
          })
        }
      });
    }    
  });
}

})();