// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
var jq = jQuery.noConflict();
//jq.blockUI.defaults.css = {};

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

jQuery(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  // settings.data is a serialized string like "foo=bar&baz=boink" (or null)
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

jQuery.fn.submitWithAjax = function(){
    this.submit(function(){
        jq.post(jq(this).attr("action"), jq(this).serialize(), null, "script");
        return false;
    });
}

function setup_charting_page(){
    jq("#query_date").datepicker();
}

function render_pulse_chart(){
    var pulsechart = new FusionCharts("/fusioncharts/MSLine.swf", "PulseChart", "950", "610", "0", "0");
    pulsechart.setDataURL("/pulse/chart_data.xml");
    pulsechart.render("pulsechartdiv");
}

function render_dr_chart(){
    var drchart = new FusionCharts("/fusioncharts/MSColumn2D.swf", "DRChart", "950", "610", "0", "0");
    drchart.setDataURL("/districtrankings/chart_data.xml");
    drchart.render("drchartdiv");
}

function enableSortableResources(){
    jq('#scheduleDetail ul.scheduleDetail').sortable({
        axis: 'y',
        handle: '.rsrcHeader img',
        helper: 'clone',
        items: 'li.rsrcRow',
        opacity: .75,
        tolerance: 'pointer',
        update: function (event, ui) {
          jq.post('/schedules/sort_resources', '_method=put&'+jq(this).sortable('serialize'), null, "script");
        }
    });
}
function enableSortableBlocks(selector, resource_id) {
    jq(selector).sortable({
        connectWith: 'ul.rsrcDetail',
        opacity: .75,
        items: 'li',
        helper: 'clone',
        appendTo: '#schedule',
        tolerance: 'pointer',
        start: function(e, ui){
            jq(ui.helper).addClass("borderBlock");
        },
        update: function (event, ui) {
          jq.post('/schedules/sort_blocks/'+resource_id, '_method=put&'+jq(this).sortable('serialize'), null, "script");
        }
    });
}

function nocache() {
  return "?random=" + Math.random() * 99999;
}
function setupPopupForm(formId, formTitle){
  jq('body').append('<div id="' + formId + '"></div>');
  jq('#' + formId).dialog({
    autoOpen: false,
    draggable: false,
    modal: true,
    position: 'top',
    resizable: false,
    title: formTitle
  });
}
function showPopupForm(formId, formUrl) {
  jq('#' + formId).empty().unbind('dialogopen').bind('dialogopen', function(event, ui) {
    jq.get(formUrl + nocache(), function(data, textStatus) {
      jq('#' + formId).html(data);
    });
  });
  jq('#' + formId).dialog('open');
}
function bindScheduleForm(schedule_id) {
  var url = "/schedules/edit_schedule/" + schedule_id;
  jq('#scheduleFormLink').live('click', function(){
    showPopupForm('scheduleForm', url);
    return false;
  });
}
function bindResourceForm(resource_id) {
  var url = "/schedules/edit_resource/" + resource_id;
  jq('#rsrcFormLink' + resource_id).live('click', function(){
    showPopupForm('resourceForm', url);
    return false;
  });
}
function bindBlockForm(block_id) {
  var url = "/schedules/edit_block/" + block_id;
  jq('#blck_' + block_id).live('dblclick', function(){
    showPopupForm('blockForm', url);
    return false;
  });
}
function bindResourceActions(resource_id) {
  jq('#rsrcActionsLink' + resource_id).hover(
    function(){
      jq('#rsrcFlyout' + resource_id).show();
    },
    function(){
      jq('#rsrcFlyout' + resource_id).hide();
    }
  );
  jq('#rsrcFlyout' + resource_id).hover(
    function(){
      jq('#rsrcFlyout' + resource_id).show();
    },
    function(){
      jq('#rsrcFlyout' + resource_id).hide();
    }
  );
  jq('#rsrcActionsLink' + resource_id).live('click', function() {
    jq('#rsrcFlyout' + resource_id).show();
  });
  jq('#rsrcFormLink' + resource_id).live('click', function(){
    showPopupForm('resourceForm', "/schedules/edit_resource/" + resource_id);
    return false;
  });
//  jq('#rsrcCopyLink' + resource_id).live('click', function() {
//  });
//  jq('#rsrcDeleteLink' + resource_id).live('click', function() {
//  });
  jq('#blckAddLink' + resource_id).unbind('click').bind('click', function() {
    jq.get('/schedules/add_block/' + resource_id, null, null, "script");
  });
}
