if (!this.SPRTZ) {
SPRTZ = {};
}
(function ($) {

var debugEnabled = true;
var exposed;
var overlayed;
var confirmDialog;
if (typeof SPRTZ.debug !== 'function') {
SPRTZ.debug = function (msg) {
if (debugEnabled && typeof console != 'undefined') {
console.log(msg);
}
};
}
if (typeof SPRTZ._initSetup !== 'function') {
SPRTZ._initSetup = function(){
SPRTZ.debug("Initializing Setup");
$('dl.sb-toggle dt').live('click',function(){
$(this).siblings('dd').slideToggle("fast");  }).disableSelection();
$(".showFilters").live('click',function(){
$(this).siblings(".pullOut").toggle();
});
$(".slidePanel .tab").live("click",function(){
$(this).parent().siblings("dd").slideToggle("slow");
$(".pullOut").hide();
$(this).toggleClass("active"); return false;
});
}
}
if (typeof SPRTZ._initGlobal !== 'function') {
SPRTZ._initGlobal = function () {
SPRTZ.debug("Initializing Global");
  
 $("a[href='#']").not("[onclick]").css("color","#ccc");
 $('.sb-btn').hover(
function() { $(this).addClass('ui-state-hover'); },
function() { $(this).removeClass('ui-state-hover'); }
);
};
}
if (typeof SPRTZ._initAccordion !== 'function') {
SPRTZ._initAccordion = function () {
SPRTZ.debug("Initializing Accordion");
 $("#generalAccordion").accordion({ header: "h3", collapsible: true, autoHeight: false });
$("#roleAccordion").accordion({ header: "h3", collapsible: true, autoHeight: false, active: 0 });
$("#helpAccordion").accordion({ header: "h3", collapsible: true, autoHeight: false });
};
}
if (typeof SPRTZ._initDataSelector !== 'function') {
SPRTZ._initDataSelector = function () {
SPRTZ.debug("Initializing DataSelector");
$("#comboAccordion").accordion({
header: "h3",
collapsible: true,
autoHeight: false,
changestart: function(event, ui){
$(".pullOut").hide();
}
});
};
}
if (typeof SPRTZ._initSortable !== 'function') {
SPRTZ._initSortable = function () {
SPRTZ.debug("Initializing Sortable");
var sortable = $(".sortable").sortable({
placeholder: 'ui-state-highlight ht1'
});
$(".sortable").disableSelection();
};
}
if (typeof SPRTZ._initSortableY !== 'function') {
SPRTZ._initSortableY = function () {
SPRTZ.debug("Initializing SortableY");
$(".sortable").sortable({
placeholder: 'ui-state-highlight', axis: 'y'
});
$(".sortable").disableSelection();
};
}
if (typeof SPRTZ._initPortlets !== 'function') {
SPRTZ._initPortlets = function () {
SPRTZ.debug("Initializing Portlets");
$(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
.find(".portlet-header")
.addClass("ui-widget-header ui-corner-all")
.end()
.find(".portlet-content");
$(".portlet-header .ui-icon").click(function() {
$(this).toggleClass("ui-icon-minusthick");
$(this).parents(".portlet:first").find(".portlet-content").toggle();
});
 $(".column").sortable({
items: 'div.portlet:not(.nosort)',
connectWith: '.column'
});
$(".column").disableSelection();
};
}
if (typeof SPRTZ._initDualcol !== 'function') {
SPRTZ._initDualcol = function () {
SPRTZ.debug("Initializing Dualcol");
$("form .dualcol").sortable({
connectWith: 'form .dualcol'
});
$("form .dualcol").disableSelection();
};
}
if (typeof SPRTZ._initSelectAll !== 'function') {
SPRTZ._initSelectAll = function () {
SPRTZ.debug("Initializing Select All");
$('.selectAll').click(function(){
var checked = this.checked;
$(this).siblings().find('input[type=checkbox]').each(function() {
this.checked=checked;
});
});
};
}
if (typeof SPRTZ._initSlider !== 'function') {
SPRTZ._initSlider = function () {
SPRTZ.debug("Initializing Slider");
$(".sliderInfo").each(function() {
var $this = $(this);
var start = parseInt($this.find(".start").val());
var stop = parseInt($this.find(".stop").val());
var vals = $this.find(".values").val();
eval("var values = " + vals);
$this.next("div").slider({
range: true,
min: start,
max: stop,
values: [start,stop],
step: 1,
slide: function(event, ui) {
$this.find(".start").val(ui.values[0]);
$this.find(".stop").val(ui.values[1]);
$this.find(".startText").text(values[ui.values[0]]);
$this.find(".stopText").text(values[ui.values[1]]);
}
});
});
};
}
if (typeof SPRTZ._initDOB !== 'function') {
SPRTZ._initDOB = function () {
SPRTZ.debug("Initializing DOB");
var $ = jQuery;
var $dob = $('.dob');
if ($dob.length > 0) {
var dob = $dob.val().split('-', 3);
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var html = '<select name="dob-month" id="dob-month" class="dob-date">';
for(var i = 1; i <= 12; i++){
html += '<option value="' + i + '"';
if (dob[1] == i)
html += ' selected="selected"';
html += '>' + months[i - 1] + '</option>';
}
html += '</select> ';
html += '<select name="dob-day" id="dob-day" class="dob-date">';
for(var i = 1; i <= 31; i++){
html += '<option value="' + i + '"';
if (dob[2] == i)
html += ' selected="selected"';
html += '>' + i + '</option>';
}
html += '</select> ';
var thisYear = new Date().getFullYear();
html += '<select name="dob-year" id="dob-year" class="dob-date">';
for(var i = (thisYear); i >= (thisYear - 110); i--){
html += '<option value="' + i + '"';
if (dob[0] == i)
html += ' selected="selected"';
html += '>' + i + '</option>';
}
html += '</select> ';
$dob.after(html).css('display','none');
$('select.dob-date').change(function(){
$dob.val($('select#dob-year').val() + '-' + $('select#dob-month').val() + '-' + $('select#dob-day').val());
$dob.change();
});
}
};
}
if (typeof SPRTZ._initCalendars !== 'function') {
SPRTZ._initCalendars = function () {
SPRTZ.debug("Initializing Calendars");
var assignmentSource = "/services/events/assignments";
var availabilitySource = "/services/events/availabilities";
var seasonSource = "/services/events/seasons";

var holidaySource = $.fullCalendar.gcalFeed(
'http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic',
{editable: false, className: 'holiday'});
 
var calendar = $('#calendar');
calendar.fullCalendar({
eventSources: [
holidaySource,
assignmentSource,
availabilitySource,
seasonSource
],
theme: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
timeFormat: {
agenda: 'h(:mm)t{ - h(:mm)t}',
'': 'h(:mm)t{-h(:mm)t }'
},
editable: false,
disableDragging: true,
disableResizing: true,
eventClick: function(calEvent, jsEvent, view) {
return false;
},
eventRender: function(event, element) {
element.children('a').css('cursor','default');
}
});
 

 $.fullCalendar.memberFeeds('/services/calendars',calendar);
};
}
if (typeof SPRTZ._initAvailability !== 'function') {
SPRTZ._initAvailability = function () {
SPRTZ.debug("Initializing Availability");
var assignmentSource = "/services/events/assignments";
var availabilitySource = "/services/events/availabilities";
var seasonSource = "/services/events/seasons";
var holidaySource = $.fullCalendar.gcalFeed(
'http://www.google.com/calendar/feeds/usa__en%40holiday.calendar.google.com/public/basic',
{editable: false, className: 'holiday'});
 
var sources = [];
sources.push(assignmentSource);
sources.push(availabilitySource);
sources.push(seasonSource);
sources.push(holidaySource);
 
var calendar = $('#avail-calendar');
calendar.fullCalendar({
eventSources: sources,
 theme: true,
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
timeFormat: {
agenda: 'h(:mm)t{ - h(:mm)t}',
'': 'h(:mm)t{-h(:mm)t }'
},
editable: false,
eventClick: function(calEvent, jsEvent, view) {
return false;
},
eventRender: function(event, element) {
if (!event.editable) {
element.children('a').css('cursor','default');
}
if (event.source == undefined || event.source == availabilitySource) {
var img = $('<img src="/icon/delete.png"/>');
element.children('a').prepend(img);
img.click(function() {
SPRTZ.debug("Delete Event: "+event.id);
$.ajax({
type: "DELETE",
url: availabilitySource+"/"+event.id,
contentType: "application/json",
dataType: "json",
success: function(result) {
calendar.fullCalendar('removeEvents', event.id);
},
error: function(result, status, err) {
alert("An error has occurred.\n");
}
});
});
}
},
dayClick: function(date, allDay, jsEvent, view) {
 if (view.name == "month" && view.start.getMonth() == date.getMonth()) {
 if ($(this).data('date') != undefined) {
 $(this).css('background-color', $(this).data('bgcolor'));
if ($(this).data('today')) {
$(this).addClass('ui-state-highlight');
}
$(this).removeData('date');
$(this).removeData('bgcolor');
$(this).removeData('today');
}
else {
 $(this).data('date',date);
$(this).data('bgcolor', $(this).css('background-color'));
if ($(this).hasClass('ui-state-highlight')) {
$(this).data('today', true);
$(this).removeClass('ui-state-highlight');
}
$(this).css('background-color', '#7EB0C8');
}
}
},
viewDisplay: function(view) {
 $('tbody td').each(function() {
if ($(this).data('date') != undefined) {
$(this).css('background-color', $(this).data('bgcolor'));
if ($(this).data('today')) {
$(this).addClass('ui-state-highlight');
}
$(this).removeData('date');
$(this).removeData('bgcolor');
$(this).removeData('today');
}
});
},
eventDrop: function(event,dayDelta,minuteDelta,allDay,revert) {
if (allDay) {
revert();
return;
}
var e = {
id: event.id,
start: event.start,
end: event.end
};
$.ajax({
type: "PUT",
url: availabilitySource+"/"+e.id,
data: JSON.stringify(e),
contentType: "application/json",
dataType: "json",
success: function(result) {
 },
error: function(result, status, err) {
revert();
alert("An error has occurred.\n");
}
});
},
eventResize: function(event,dayDelta,minuteDelta,allDay,revert) {
var e = {
id: event.id,
start: event.start,
end: event.end
};
$.ajax({
type: "PUT",
url: availabilitySource+"/"+e.id,
data: JSON.stringify(e),
contentType: "application/json",
dataType: "json",
success: function(result) {
 },
error: function(result, status, err) {
revert();
alert("An error has occurred.\n");
}
});
}
});
 $.fullCalendar.memberFeeds('/services/calendars',calendar);
$('select#start,select#end').selectToUISlider({
labels: 12,
labelSrc: "value",
sliderOptions: {
animate: true
}
});
$('input#addavail').click(function() {
var start = parseTime($('#start').val());
var end = parseTime($('#end').val());
var allDay = false;
if (end.getHours() == 0 && end.getMinutes() == 0) {
end.setDate(end.getDate()+1);
if (start.getHours() == 0 && start.getMinutes() == 0) {
allDay = true;
}
}
      
var sIdx = document.getElementById('start').selectedIndex;
var eIdx = document.getElementById('end').selectedIndex;
if (sIdx >= eIdx) {
alert("A valid time range must be specified.");
return;
}
var selections = [];
$('tbody td').each(function() {
var date = $(this).data('date');
if (date != undefined) {
var s = date;
var e = new Date(date.getTime());
s.setHours(start.getHours());
s.setMinutes(start.getMinutes());
e.setHours(end.getHours());
e.setMinutes(end.getMinutes());
if (e.getHours() == 0 && e.getMinutes() == 0) {
e.setDate(e.getDate()+1);
}
var event = {
title: 'Available',
start: s,
end: e
};
selections.push(event);
$(this).css('background-color', $(this).data('bgcolor'));
if ($(this).data('today')) {
$(this).addClass('ui-state-highlight');
}
$(this).removeData('date');
$(this).removeData('bgcolor');
$(this).removeData('today');
}
});
if (selections.length == 0) {
alert("You must select at least one date on the calendar.");
return;
}
$.ajax({
type: "POST",
url: availabilitySource,
data: JSON.stringify(selections),
contentType: "application/json",
dataType: "json",
success: function(result) {
$(result).each(function() {
calendar.fullCalendar('renderEvent',this);
})
},
error: function(result, status, err) {
alert("An error has occurred.\n");
}
});
});
};
}
var memberFeeds = function(feedUrl,calendar) {
var feeds = [];
$.getJSON(feedUrl,  function(data){
$.each(data, function(i,item){
var type = item.type.toLowerCase();
if (type == "gcal" || type == "holiday") {
var feed = $.fullCalendar.gcalFeed(item.value, {
draggable: false,
className: type
});
feeds.push(feed);
alert("Added feed");
calendar.fullCalendar('addEventSource',feed);
}
else if (type == "ical") {
 }
else {
 }
});
});
alert("Num Feeds:"+feeds.length);
return feeds;
}
var parseTime = function(timeString) { 
if (timeString == '') return null;
var d = new Date();
var time = timeString.match(/(\d+)(:(\d\d))?\s*(p?)/i);
if (time == null) return null;
var hours = parseInt(time[1],10); 
if (hours == 12 && !time[4]) {
hours = 0;
}
else {
hours += (hours < 12 && time[4])? 12 : 0;
} 
d.setHours(hours);
d.setMinutes(parseInt(time[3],10) || 0);
d.setSeconds(0, 0); 
return d;
}

if (typeof SPRTZ.setupCalendars !== 'function') {
SPRTZ.setupCalendars = function () {
SPRTZ.debug("Setup Calendars");
SPRTZ._initGlobal();
 SPRTZ._initSlider();
SPRTZ._initCalendars();
};
}
if (typeof SPRTZ.setupAvailability !== 'function') {
SPRTZ.setupAvailability = function () {
SPRTZ.debug("Setup Availability");
SPRTZ._initGlobal();
 SPRTZ._initSlider();
SPRTZ._initAvailability();
};
}
if (typeof SPRTZ.setupReferees !== 'function') {
SPRTZ.setupReferees = function () {
SPRTZ.debug("Setup Referees");
SPRTZ._initGlobal();
 SPRTZ._initSlider();
SPRTZ._initSelectAll();
SPRTZ._initDataSelector();
};
}
if (typeof SPRTZ.setupSchedules !== 'function') {
SPRTZ.setupSchedules = function () {
SPRTZ.debug("Setup Schedules");
SPRTZ._initGlobal();
 SPRTZ._initSlider();
SPRTZ._initSelectAll();
SPRTZ._initDataSelector();
};
}
if (typeof SPRTZ.setupVenues !== 'function') {
SPRTZ.setupVenues = function () {
SPRTZ.debug("Setup Venues");
SPRTZ._initGlobal();
 SPRTZ._initSlider();
SPRTZ._initSelectAll();
SPRTZ._initDataSelector();
};
}
if (typeof SPRTZ.setupLeagues !== 'function') {
SPRTZ.setupLeagues = function () {
SPRTZ.debug("Setup Leagues");
SPRTZ._initGlobal();
 SPRTZ._initSlider();
SPRTZ._initSelectAll();
};
}
if (typeof SPRTZ.setupSports !== 'function') {
SPRTZ.setupSports = function () {
SPRTZ.debug("Setup Sports");
SPRTZ._initGlobal();
SPRTZ._initSortableY();
SPRTZ._initSlider();
SPRTZ._initSelectAll();
};
}
if (typeof SPRTZ.setupNetworks !== 'function') {
SPRTZ.setupSports = function () {
SPRTZ.debug("Setup Networks");
SPRTZ._initGlobal();
SPRTZ._initSortableY();
SPRTZ._initSlider();
SPRTZ._initSelectAll();
};
}
if (typeof SPRTZ.setupRegister !== 'function') {
SPRTZ.setupRegister = function () {
SPRTZ.debug("Setup Register");
SPRTZ._initDOB();
};
}
if (typeof SPRTZ.setupProfile !== 'function') {
SPRTZ.setupProfile = function () {
SPRTZ.debug("Setup Profile");
SPRTZ._initGlobal();
};
}
if (typeof SPRTZ.setupProfileView !== 'function') {
SPRTZ.setupProfileView = function () {
SPRTZ.debug("Setup ProfileView");
SPRTZ._initGlobal();
SPRTZ._initPortlets();
};
}
if (typeof SPRTZ.setupDashboard !== 'function') {
SPRTZ.setupDashboard = function () {
SPRTZ.debug("Setup Dashboard");
SPRTZ._initGlobal();
};
}
if (typeof SPRTZ.setupDashboardOverview !== 'function') {
SPRTZ.setupDashboardOverview = function () {
SPRTZ.debug("Setup DashboardOverview");
SPRTZ._initGlobal();
SPRTZ._initPortlets();
};
}
if (typeof SPRTZ.setupSiteBase !== 'function') {
SPRTZ.setupSiteBase = function () {
SPRTZ.debug("Setup SiteBase");
SPRTZ._initSetup();
SPRTZ._initGlobal();
SPRTZ._initAccordion();
};
}
if (typeof SPRTZ.setupSettings !== 'function') {
SPRTZ.setupSettings = function () {
SPRTZ.debug("Setup Settings");
SPRTZ._initGlobal();
SPRTZ._initDualcol();
SPRTZ._initSortable();
SPRTZ._initDOB();
};
}
if (typeof SPRTZ.exposeLoad !== 'function') {
SPRTZ.exposeLoad = function (obj) {
SPRTZ.debug("Loading Expose");
if (typeof exposed != 'undefined') {
exposed.close();
}
exposed = $(obj).expose({api:true,closeOnClick:false,closeOnEsc:false}).load(); 
};
}
if (typeof SPRTZ.exposeClose !== 'function') {
SPRTZ.exposeClose = function (obj) {
SPRTZ.debug("Closing Expose");
if (typeof exposed != 'undefined') {
exposed.close();
exposed = undefined;
}
};
}
}(jQuery));
