Advertisement
Guest User

JSON error

a guest
Apr 4th, 2011
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is JSON code now...
  2. <script type="text/javascript">
  3.  
  4. var doFades = true;
  5.  
  6. var Stations = {
  7.     Schedule : {}
  8. };
  9.  
  10. $(document).ready(function(){
  11.    
  12.     Stations.Schedule.days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
  13.    
  14.     Stations.Schedule.Show = function () {
  15.         _s = null;
  16.         _startDate = null;
  17.         _endDate = null;
  18.         this.days = Stations.Schedule.days;
  19.         _selector = '';
  20.        
  21.         this.setShow = function(s) {
  22.             this._s = s;
  23.            
  24.             this._startDate = new Date( parseInt(s.startminutes, 10) * 1000);
  25.             this._endDate = new Date(parseInt(s.endminutes, 10) * 1000 );
  26.            
  27.         };
  28.        
  29.         this.getEndDate = function(){
  30.             return this._endDate;
  31.         }
  32.        
  33.         this.getStartDate = function(){
  34.             return this._startDate;
  35.         }
  36.        
  37.         this._getShowDay = function (){
  38.             return this.days[this.getStartDate().getDay()];
  39.         };
  40.        
  41.         this._getShowUnitsTaken = function(){
  42.             // if it's the same day
  43.             return this._getEndUnits() - this._getStartUnits();
  44.         };
  45.        
  46.         this._getEndUnits = function(){    
  47.             if(this.getEndDate().getHours() == 0)
  48.             {
  49.                 //console.log(this._s.longname  +' ends at midnight');
  50.                 return 48;
  51.             }
  52.            
  53.             return this.getEndDate().getMinutes() !== 0 ? (this.getEndDate().getHours() * 2) : (this.getEndDate().getHours() * 2);
  54.         };
  55.        
  56.         this._getStartUnits = function(){
  57.             if(this.getStartDate().getHours() == 0)
  58.             {
  59.                 return 0;
  60.             }
  61.            
  62.             return this.getStartDate().getMinutes() !== 0 ? (this.getStartDate().getHours() * 2) : (this.getStartDate().getHours() * 2);
  63.         };
  64.        
  65.         this.getCellPositions = function() {
  66.             return {
  67.                 'start' : this.getStartDate(),
  68.                 'end'   : this.getEndDate(),
  69.                 'colIndex' : this.getStartDate().getDay() + 2,
  70.                 'startUnits' : this._getStartUnits(),
  71.                 'endUnits' : this._getEndUnits(),
  72.                 'unitsTaken' : this._getShowUnitsTaken()
  73.             }
  74.         };
  75.        
  76.         this.pad = function(number){
  77.             return number < 10 ? '0'+number : number;
  78.         };
  79.        
  80.         // return the table cell html.
  81.         this.toHtml = function () {
  82.            
  83.             var d = new Date();
  84.             var units = this._getStartUnits();
  85.                    
  86.             var rowspan = this._getShowUnitsTaken();       
  87.             var desc = this._s.description;
  88.             var name = this._s.longname;
  89.            
  90.             var starttime   = this.pad(this.getStartDate().getHours()) + ':' + this.pad(this.getStartDate().getMinutes());
  91.             var endtime     = this.pad(this.getEndDate().getHours()) + ':' + this.pad(this.getEndDate().getMinutes());
  92.            
  93.             var site = this._s.websitelink;
  94.            
  95.             var cls = this.isActive() ? 'current-program' : '';
  96.            
  97.             var isToday = this.getStartDate().getDay() === d.getDay() ? 'active-program' : '';
  98.            
  99.             var html = '<td class="schedule-show ' + isToday + ' ' + cls + '" rowspan="' + rowspan + '" data-start="' + this.getStartDate() + '" data-end="' + this.getEndDate() + '">';
  100.                 html += '<div>';
  101.                 html += '<a href="' + site + '" title="' + desc + '">' + name + '</a>';
  102.                 html += '</div>';
  103.                 if(doFades)
  104.                 {
  105.                     html += '<div class="schedule_details clearfix" style="display:none;">';
  106.                     html += '<img width="105px" height="105px" alt="' + desc + '" src="' + this._s.showimage + '">';
  107.                     html += '<strong>' + name + '</strong>';
  108.                     html += '<p>' + desc + '</p>';
  109.                     html += '<span>' + starttime + ' - ' + endtime +'</span>';
  110.                     html += '</div>';
  111.                 }
  112.                 html += '</td>';           
  113.            
  114.             return html;
  115.         };
  116.        
  117.         this.setTableSelector = function(sel){
  118.             this._selector = sel;
  119.         };
  120.        
  121.         // check if we should add the active class.
  122.         this.isActive = function(){
  123.             var t = new Date();
  124.            
  125.             return  t >= this.getStartDate() && t <= this.getEndDate();
  126.         };
  127.     };
  128.    
  129.     Stations.Schedule.ScheduleGen = function(){
  130.  
  131.         return {
  132.             insertShow : function(show) {
  133.                 var p = show.getCellPositions();
  134.                
  135.                 $('tr#units-' + p.startUnits).append(show.toHtml());
  136.             },
  137.        
  138.             init : function (stationName){
  139.                 var self = this;
  140.                
  141.                 // load the schedule.
  142.                 $.getJSON('http://localhost/myradio1.php?callback=?', {
  143.                     name: 'Minnesota FM'
  144.                 }, function(json){
  145.                     // loop each show and append to our giant table.
  146.                     // this is well sick.
  147.                    
  148.                     if(json.success === false)
  149.                     {
  150.                         $('.content-inner table').remove();
  151.                        
  152.                         $('<div>errors</div>').appendTo('.content-inner');
  153.                     }
  154.                     else
  155.                     {
  156.                         var currentDay = '';
  157.                         var day = 0;
  158.                         // highlight the current time..
  159.                         var d = new Date();
  160.                         var weekStart = new Date();
  161.  
  162.                         weekStart.setDate(d.getDate()-6-(d.getDay()||7));
  163.                        
  164.                         $.each(json.data.schedule, function(i, broadcast){
  165.                            
  166.                            
  167.                             var dStart = new Date( parseInt(broadcast.startminutes, 10) * 1000);
  168.                             var dEnd = new Date(parseInt(broadcast.endminutes, 10) * 1000 );
  169.  
  170.                    
  171.                             /*// transform to a show object defined above, if the show spans 2 days we create two show objects.
  172.                             // IF THE SHOW STARTS/ENDS AT MIDNIGHT, DON'T SPLIT IT.
  173.                             if(dStart.getHours() !== 0 && dEnd.getHours() !== 0 && dStart.getDate() != dEnd.getDate())
  174.                             {
  175.                                 var showOne = new Stations.Schedule.Show();
  176.                                 showOne.setShow(broadcast);
  177.                                
  178.                                 // set to midnight
  179.                                 showOne.getEndDate().setHours(0);
  180.                                 showOne.getEndDate().setMinutes(dStart.getMinutes());
  181.    
  182.    
  183.                                 // append first half of show.
  184.                                 self.insertShow(showOne);
  185.                                
  186.                                 // handle second half.
  187.                                 var showTwo = new Stations.Schedule.Show();
  188.                                 showTwo.setShow(broadcast);
  189.                                
  190.                                 showTwo.getStartDate().setDate(showTwo.getStartDate().getDate() + 1);
  191.                                 showTwo.getStartDate().setHours(0);
  192.                                 showTwo.getStartDate().setMinutes(dEnd.getMinutes());
  193.                                
  194.                                 //console.log('2nd Half Start: ' + showTwo.getStartDate());
  195.                                 //console.log('2nd Half End: ' + showTwo.getEndDate());
  196.                                
  197.                                 self.insertShow(showTwo);
  198.                                
  199.                                
  200.                             }
  201.                             else
  202.                             {*/
  203.                                 var show = new Stations.Schedule.Show();
  204.                                
  205.                                 show.setShow(broadcast);
  206.                                 show.setTableSelector('table#schedule');
  207.  
  208.                                 // add the show to the table. Thankfully the order these come out the API means they get added
  209.                                 // in the right place. So don't change the schedule builder code!
  210.                                
  211.                                 self.insertShow(show);
  212.                             //}
  213.            
  214.                            
  215.                         });
  216.                        
  217.                         var days = Stations.Schedule.days;     
  218.                        
  219.                         // apply the current day / time classes
  220.                         $('th:contains('+ days[d.getDay()]+')').addClass('active');
  221.                        
  222.                         $('td.time').each(function(i, cell){
  223.                             // get the value, convert to int.
  224.                             var hours = $(cell).html().split(':')[0];
  225.                            
  226.                             // compare the hours with now, add class if matched.
  227.                             if(parseInt(hours, 10) === d.getHours())
  228.                             {
  229.                                 $(cell).addClass('current_time');
  230.                             }
  231.                         });
  232.                     }
  233.                    
  234.                    
  235.                     if(doFades)
  236.                     {
  237.                         // apply events to show info fade in / out.
  238.                         $('td.schedule-show').hover(function(){
  239.                             $(this).find('.schedule_details').fadeIn('fast');
  240.                         }, function(){
  241.                             $(this).find('.schedule_details').fadeOut('fast');
  242.                         });
  243.                     }
  244.                 });
  245.                
  246.                
  247.             }
  248.         };
  249.     }();
  250.    
  251.  
  252.     Stations.Schedule.ScheduleGen.init();
  253.    
  254. });
  255. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement