var AIRTIME = (function(AIRTIME){
var mod,
oSchedTable,
SB_SELECTED_CLASS = "sb-selected",
$sbContent,
$sbTable,
$toolbar,
$lib;
if (AIRTIME.showbuilder === undefined) {
AIRTIME.showbuilder = {};
}
mod = AIRTIME.showbuilder;
function checkError(json) {
if (json.error !== undefined) {
alert(json.error);
}
}
mod.timeout = undefined;
mod.resetTimestamp = function() {
var timestamp = $("#sb_timestamp");
//reset timestamp value since input values could have changed.
timestamp.val(-1);
};
mod.setTimestamp = function(timestamp) {
$("#sb_timestamp").val(timestamp);
};
mod.getTimestamp = function() {
var timestamp = $("#sb_timestamp"),
val;
//if the timestamp field is on the page return it, or give the default of -1
//to ensure a page refresh.
if (timestamp.length === 1) {
val = timestamp.val();
}
else {
val = -1;
}
return val;
};
mod.getSelectedData = function() {
var $selected = $sbTable.find("tbody").find("input:checkbox").filter(":checked").parents("tr"),
aData = [],
i, length,
$item;
for (i = 0, length = $selected.length; i < length; i++) {
$item = $($selected.get(i));
aData.push($item.data('aData'));
}
return aData.reverse();
};
mod.selectAll = function () {
$sbTable.find("input:checkbox").attr("checked", true);
};
mod.selectNone = function () {
$sbTable.find("input:checkbox").attr("checked", false);
};
mod.fnAdd = function(aMediaIds, aSchedIds) {
$.post("/showbuilder/schedule-add",
{"format": "json", "mediaIds": aMediaIds, "schedIds": aSchedIds},
function(json){
checkError(json);
oSchedTable.fnDraw();
AIRTIME.library.selectNone();
});
};
mod.fnMove = function(aSelect, aAfter) {
$.post("/showbuilder/schedule-move",
{"format": "json", "selectedItem": aSelect, "afterItem": aAfter},
function(json){
checkError(json);
oSchedTable.fnDraw();
});
};
mod.fnRemove = function(aItems) {
$.post( "/showbuilder/schedule-remove",
{"items": aItems, "format": "json"},
function(json) {
checkError(json);
oSchedTable.fnDraw();
});
};
mod.fnRemoveSelectedItems = function() {
var aData = mod.getSelectedData(),
i,
length,
temp,
aItems = [];
for (i=0, length = aData.length; i < length; i++) {
temp = aData[i];
aItems.push({"id": temp.id, "instance": temp.instance, "timestamp": temp.timestamp});
}
mod.fnRemove(aItems);
};
mod.fnServerData = function ( sSource, aoData, fnCallback ) {
aoData.push( { name: "timestamp", value: AIRTIME.showbuilder.getTimestamp()} );
aoData.push( { name: "format", value: "json"} );
if (mod.fnServerData.hasOwnProperty("start")) {
aoData.push( { name: "start", value: mod.fnServerData.start} );
}
if (mod.fnServerData.hasOwnProperty("end")) {
aoData.push( { name: "end", value: mod.fnServerData.end} );
}
if (mod.fnServerData.hasOwnProperty("ops")) {
aoData.push( { name: "myShows", value: mod.fnServerData.ops.myShows} );
aoData.push( { name: "showFilter", value: mod.fnServerData.ops.showFilter} );
}
$.ajax( {
"dataType": "json",
"type": "POST",
"url": sSource,
"data": aoData,
"success": function(json) {
AIRTIME.showbuilder.setTimestamp(json.timestamp);
fnCallback(json);
}
} );
};
mod.builderDataTable = function() {
$sbContent = $('#show_builder');
$lib = $("#library_content"),
$sbTable = $sbContent.find('table');
oSchedTable = $sbTable.dataTable( {
"aoColumns": [
/* checkbox */ {"mDataProp": "allowed", "sTitle": "", "sWidth": "15px", "sClass": "sb-checkbox"},
/* Type */ {"mDataProp": "image", "sTitle": "", "sClass": "library_image sb-image", "sWidth": "16px"},
/* starts */ {"mDataProp": "starts", "sTitle": "Start", "sClass": "sb-starts", "sWidth": "60px"},
/* ends */ {"mDataProp": "ends", "sTitle": "End", "sClass": "sb-ends", "sWidth": "60px"},
/* runtime */ {"mDataProp": "runtime", "sTitle": "Duration", "sClass": "library_length sb-length", "sWidth": "65px"},
/* title */ {"mDataProp": "title", "sTitle": "Title", "sClass": "sb-title"},
/* creator */ {"mDataProp": "creator", "sTitle": "Creator", "sClass": "sb-creator"},
/* album */ {"mDataProp": "album", "sTitle": "Album", "sClass": "sb-album"},
/* cue in */ {"mDataProp": "cuein", "sTitle": "Cue In", "bVisible": false, "sClass": "sb-cue-in"},
/* cue out */ {"mDataProp": "cueout", "sTitle": "Cue Out", "bVisible": false, "sClass": "sb-cue-out"},
/* fade in */ {"mDataProp": "fadein", "sTitle": "Fade In", "bVisible": false, "sClass": "sb-fade-in"},
/* fade out */ {"mDataProp": "fadeout", "sTitle": "Fade Out", "bVisible": false, "sClass": "sb-fade-out"}
],
"bJQueryUI": true,
"bSort": false,
"bFilter": false,
"bProcessing": true,
"bServerSide": true,
"bInfo": false,
"bAutoWidth": false,
"bStateSave": true,
"fnStateSaveParams": function (oSettings, oData) {
//remove oData components we don't want to save.
delete oData.oSearch;
delete oData.aoSearchCols;
},
"fnStateSave": function fnStateSave(oSettings, oData) {
localStorage.setItem('datatables-timeline', JSON.stringify(oData));
$.ajax({
url: "/usersettings/set-timeline-datatable",
type: "POST",
data: {settings : oData, format: "json"},
dataType: "json"
});
},
"fnStateLoad": function fnBuilderStateLoad(oSettings) {
var settings = localStorage.getItem('datatables-timeline');
if (settings !== "") {
return JSON.parse(settings);
}
},
"fnStateLoadParams": function (oSettings, oData) {
var i,
length,
a = oData.abVisCols;
//putting serialized data back into the correct js type to make
//sure everything works properly.
for (i = 0, length = a.length; i < length; i++) {
if (typeof(a[i]) === "string") {
a[i] = (a[i] === "true") ? true : false;
}
}
a = oData.ColReorder;
for (i = 0, length = a.length; i < length; i++) {
if (typeof(a[i]) === "string") {
a[i] = parseInt(a[i], 10);
}
}
oData.iCreate = parseInt(oData.iCreate, 10);
},
"fnServerData": AIRTIME.showbuilder.fnServerData,
"fnRowCallback": function fnRowCallback( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var i,
sSeparatorHTML,
fnPrepareSeparatorRow,
node,
cl="",
//background-color to imitate calendar color.
r,g,b,a,
$nRow = $(nRow),
$image;
fnPrepareSeparatorRow = function fnPrepareSeparatorRow(sRowContent, sClass, iNodeIndex) {
node = nRow.children[iNodeIndex];
node.innerHTML = sRowContent;
node.setAttribute('colspan',100);
for (i = iNodeIndex + 1; i < nRow.children.length; i = i+1) {
node = nRow.children[i];
node.innerHTML = "";
node.setAttribute("style", "display : none");
}
$(nRow).addClass(sClass);
};
if (aData.header === true) {
//remove the column classes from all tds.
$(nRow).find('td').removeClass();
node = nRow.children[0];
node.innerHTML = '';
cl = 'sb-header';
sSeparatorHTML = ''+aData.title+'';
sSeparatorHTML += '';
if (aData.startDate === aData.endDate) {
sSeparatorHTML += ''+aData.startDate+''+aData.startTime+'';
sSeparatorHTML +='-'+aData.endTime+'';
}
else {
sSeparatorHTML += ''+aData.startDate+''+aData.startTime+'';
sSeparatorHTML +='-'+aData.endDate+''+aData.endTime+'';
}
sSeparatorHTML += '';
fnPrepareSeparatorRow(sSeparatorHTML, cl, 1);
}
else if (aData.footer === true) {
//remove the column classes from all tds.
$(nRow).find('td').removeClass();
node = nRow.children[0];
cl = 'sb-footer';
//check the show's content status.
if (aData.runtime > 0) {
node.innerHTML = '';
cl = cl + ' ui-state-highlight';
}
else {
node.innerHTML = '';
cl = cl + ' ui-state-error';
}
sSeparatorHTML = ''+aData.fRuntime+'';
fnPrepareSeparatorRow(sSeparatorHTML, cl, 1);
}
else if (aData.empty === true) {
//remove the column classes from all tds.
$(nRow).find('td').removeClass();
node = nRow.children[0];
node.innerHTML = '';
sSeparatorHTML = 'Show Empty';
cl = cl + " sb-empty odd";
fnPrepareSeparatorRow(sSeparatorHTML, cl, 1);
}
else if (aData.record === true) {
//remove the column classes from all tds.
$(nRow).find('td').removeClass();
node = nRow.children[0];
node.innerHTML = '';
sSeparatorHTML = 'Recording From Line In';
cl = cl + " sb-record odd";
fnPrepareSeparatorRow(sSeparatorHTML, cl, 1);
}
else {
//add the play function if the file exists on disk.
$image = $(nRow).find('td.sb-image');
//check if the file exists.
if (aData.image === true) {
$image.html('')
.click(function() {
open_show_preview(aData.instance, aData.pos);
return false;
});
}
else {
$image.html('');
}
node = nRow.children[0];
if (aData.allowed === true && aData.scheduled >= 1) {
node.innerHTML = '';
}
else {
node.innerHTML = '';
}
}
//add the show colour to the leftmost td
if (aData.footer !== true) {
if ($nRow.hasClass('sb-header')) {
a = 1;
}
else if ($nRow.hasClass('odd')) {
a = 0.3;
}
else if ($nRow.hasClass('even')) {
a = 0.4;
}
//convert from hex to rgb.
r = parseInt((aData.backgroundColor).substring(0,2), 16);
g = parseInt((aData.backgroundColor).substring(2,4), 16);
b = parseInt((aData.backgroundColor).substring(4,6), 16);
$nRow.find('td:first').css('background', 'rgba('+r+', '+g+', '+b+', '+a+')');
}
//save some info for reordering purposes.
$(nRow).data({"aData": aData});
if (aData.scheduled === 1) {
$(nRow).addClass("sb-now-playing");
}
else if (aData.scheduled === 0) {
$(nRow).addClass("sb-past");
}
else {
$(nRow).addClass("sb-future");
}
if (aData.allowed !== true) {
$(nRow).addClass("sb-not-allowed");
}
else {
$(nRow).addClass("sb-allowed");
}
//status used to colour tracks.
if (aData.status === 2) {
$(nRow).addClass("sb-boundry");
}
else if (aData.status === 0) {
$(nRow).addClass("sb-over");
}
if (aData.currentShow === true) {
$(nRow).addClass("sb-current-show");
}
//call the context menu so we can prevent the event from propagating.
$(nRow).find('td:gt(1)').click(function(e){
$(this).contextMenu({x: e.pageX, y: e.pageY});
return false;
});
},
//remove any selected nodes before the draw.
"fnPreDrawCallback": function( oSettings ) {
//disable jump to current button.
AIRTIME.button.disableButton("sb-button-delete");
//disable jump to current button.
AIRTIME.button.disableButton("sb-button-current");
//disable deleting of overbooked tracks.
AIRTIME.button.disableButton("sb-button-trim");
//disable cancelling current show.
AIRTIME.button.disableButton("sb-button-cancel");
},
"fnDrawCallback": function fnBuilderDrawCallback(oSettings, json) {
var wrapperDiv,
markerDiv,
$td,
$tr,
aData,
elements,
i, length, temp,
$cursorRows,
$table = $(this),
$parent = $table.parent(),
//use this array to cache DOM heights then we can detach the table to manipulate it to increase speed.
heights = [];
clearTimeout(AIRTIME.showbuilder.timeout);
//only create the cursor arrows if the library is on the page.
if ($lib.length > 0 && $lib.filter(":visible").length > 0) {
$cursorRows = $sbTable.find("tbody tr.sb-future.sb-allowed:not(.sb-header, .sb-empty)");
//need to get heights of tds while elements are still in the DOM.
for (i = 0, length = $cursorRows.length; i < length; i++) {
$td = $($cursorRows.get(i)).find("td:first");
heights.push($td.height());
}
//detach the table to increase speed.
$table.detach();
for (i = 0, length = $cursorRows.length; i < length; i++) {
$td = $($cursorRows.get(i)).find("td:first");
if ($td.hasClass("dataTables_empty")) {
$parent.append($table);
return false;
}
wrapperDiv = $("