Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is JSON code now...
- <script type="text/javascript">
- var doFades = true;
- var Stations = {
- Schedule : {}
- };
- $(document).ready(function(){
- Stations.Schedule.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
- Stations.Schedule.Show = function () {
- _s = null;
- _startDate = null;
- _endDate = null;
- this.days = Stations.Schedule.days;
- _selector = '';
- this.setShow = function(s) {
- this._s = s;
- this._startDate = new Date( parseInt(s.startminutes, 10) * 1000);
- this._endDate = new Date(parseInt(s.endminutes, 10) * 1000 );
- };
- this.getEndDate = function(){
- return this._endDate;
- }
- this.getStartDate = function(){
- return this._startDate;
- }
- this._getShowDay = function (){
- return this.days[this.getStartDate().getDay()];
- };
- this._getShowUnitsTaken = function(){
- // if it's the same day
- return this._getEndUnits() - this._getStartUnits();
- };
- this._getEndUnits = function(){
- if(this.getEndDate().getHours() == 0)
- {
- //console.log(this._s.longname +' ends at midnight');
- return 48;
- }
- return this.getEndDate().getMinutes() !== 0 ? (this.getEndDate().getHours() * 2) : (this.getEndDate().getHours() * 2);
- };
- this._getStartUnits = function(){
- if(this.getStartDate().getHours() == 0)
- {
- return 0;
- }
- return this.getStartDate().getMinutes() !== 0 ? (this.getStartDate().getHours() * 2) : (this.getStartDate().getHours() * 2);
- };
- this.getCellPositions = function() {
- return {
- 'start' : this.getStartDate(),
- 'end' : this.getEndDate(),
- 'colIndex' : this.getStartDate().getDay() + 2,
- 'startUnits' : this._getStartUnits(),
- 'endUnits' : this._getEndUnits(),
- 'unitsTaken' : this._getShowUnitsTaken()
- }
- };
- this.pad = function(number){
- return number < 10 ? '0'+number : number;
- };
- // return the table cell html.
- this.toHtml = function () {
- var d = new Date();
- var units = this._getStartUnits();
- var rowspan = this._getShowUnitsTaken();
- var desc = this._s.description;
- var name = this._s.longname;
- var starttime = this.pad(this.getStartDate().getHours()) + ':' + this.pad(this.getStartDate().getMinutes());
- var endtime = this.pad(this.getEndDate().getHours()) + ':' + this.pad(this.getEndDate().getMinutes());
- var site = this._s.websitelink;
- var cls = this.isActive() ? 'current-program' : '';
- var isToday = this.getStartDate().getDay() === d.getDay() ? 'active-program' : '';
- var html = '<td class="schedule-show ' + isToday + ' ' + cls + '" rowspan="' + rowspan + '" data-start="' + this.getStartDate() + '" data-end="' + this.getEndDate() + '">';
- html += '<div>';
- html += '<a href="' + site + '" title="' + desc + '">' + name + '</a>';
- html += '</div>';
- if(doFades)
- {
- html += '<div class="schedule_details clearfix" style="display:none;">';
- html += '<img width="105px" height="105px" alt="' + desc + '" src="' + this._s.showimage + '">';
- html += '<strong>' + name + '</strong>';
- html += '<p>' + desc + '</p>';
- html += '<span>' + starttime + ' - ' + endtime +'</span>';
- html += '</div>';
- }
- html += '</td>';
- return html;
- };
- this.setTableSelector = function(sel){
- this._selector = sel;
- };
- // check if we should add the active class.
- this.isActive = function(){
- var t = new Date();
- return t >= this.getStartDate() && t <= this.getEndDate();
- };
- };
- Stations.Schedule.ScheduleGen = function(){
- return {
- insertShow : function(show) {
- var p = show.getCellPositions();
- $('tr#units-' + p.startUnits).append(show.toHtml());
- },
- init : function (stationName){
- var self = this;
- // load the schedule.
- $.getJSON('http://localhost/myradio1.php?callback=?', {
- name: 'Minnesota FM'
- }, function(json){
- // loop each show and append to our giant table.
- // this is well sick.
- if(json.success === false)
- {
- $('.content-inner table').remove();
- $('<div>errors</div>').appendTo('.content-inner');
- }
- else
- {
- var currentDay = '';
- var day = 0;
- // highlight the current time..
- var d = new Date();
- var weekStart = new Date();
- weekStart.setDate(d.getDate()-6-(d.getDay()||7));
- $.each(json.data.schedule, function(i, broadcast){
- var dStart = new Date( parseInt(broadcast.startminutes, 10) * 1000);
- var dEnd = new Date(parseInt(broadcast.endminutes, 10) * 1000 );
- /*// transform to a show object defined above, if the show spans 2 days we create two show objects.
- // IF THE SHOW STARTS/ENDS AT MIDNIGHT, DON'T SPLIT IT.
- if(dStart.getHours() !== 0 && dEnd.getHours() !== 0 && dStart.getDate() != dEnd.getDate())
- {
- var showOne = new Stations.Schedule.Show();
- showOne.setShow(broadcast);
- // set to midnight
- showOne.getEndDate().setHours(0);
- showOne.getEndDate().setMinutes(dStart.getMinutes());
- // append first half of show.
- self.insertShow(showOne);
- // handle second half.
- var showTwo = new Stations.Schedule.Show();
- showTwo.setShow(broadcast);
- showTwo.getStartDate().setDate(showTwo.getStartDate().getDate() + 1);
- showTwo.getStartDate().setHours(0);
- showTwo.getStartDate().setMinutes(dEnd.getMinutes());
- //console.log('2nd Half Start: ' + showTwo.getStartDate());
- //console.log('2nd Half End: ' + showTwo.getEndDate());
- self.insertShow(showTwo);
- }
- else
- {*/
- var show = new Stations.Schedule.Show();
- show.setShow(broadcast);
- show.setTableSelector('table#schedule');
- // add the show to the table. Thankfully the order these come out the API means they get added
- // in the right place. So don't change the schedule builder code!
- self.insertShow(show);
- //}
- });
- var days = Stations.Schedule.days;
- // apply the current day / time classes
- $('th:contains('+ days[d.getDay()]+')').addClass('active');
- $('td.time').each(function(i, cell){
- // get the value, convert to int.
- var hours = $(cell).html().split(':')[0];
- // compare the hours with now, add class if matched.
- if(parseInt(hours, 10) === d.getHours())
- {
- $(cell).addClass('current_time');
- }
- });
- }
- if(doFades)
- {
- // apply events to show info fade in / out.
- $('td.schedule-show').hover(function(){
- $(this).find('.schedule_details').fadeIn('fast');
- }, function(){
- $(this).find('.schedule_details').fadeOut('fast');
- });
- }
- });
- }
- };
- }();
- Stations.Schedule.ScheduleGen.init();
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement