// SCHEDULE

// PAGE LOAD 

$(document).ready(function()
	{
	   //alert("page ready");
	   init();
	}
);

function init()
{
    enableAccordion();
    updateUIHeight(800)
}

var selectedPanelId;
var lastPanelId;

var myIDSelected = 'thisweek';

var previousStr = "thisWeek";

function enableAccordion()
{
	 $('#accordion').accordion({ autoHeight: false });
	//$('#accordion').accordion({ icons: { 'header': 'ui-icon-accordion-plus', 'headerSelected': 'ui-icon-accordion-minus' } });

	$('#accordion').bind('accordionchange', function(event, ui) {
         //ui.newHeader // jQuery object, activated header
         //ui.oldHeader // jQuery object, previous header
         //ui.newContent // jQuery object, activated content
         //ui.oldContent // jQuery object, previous content
         
         // alert("new accordion week: " + $('#thisweek').height() + " month: " + $(selectedPanelId).height() + " year: " + $('#thisyear').height() );
         //476 month: 2065 year: 20769
         
         var selectedPanelIdS = $(ui.newContent).attr("id");
         var selectedPanelIdA = selectedPanelIdS.split("_");
         selectedPanelId = '#'+ String(selectedPanelIdA[0]);
         
         //clearMonth();
         
         lastPanelId = selectedPanelId;
         //alert("panel : " + selectedPanelIdA);
         
        // if ( selectedPanelIdA[0] == "march")
         //{
           // makeJsonRequest(2010, 3);         
         //}
         myIDSelected = selectedPanelIdA[0];
         var thismonthStr = selectedPanelIdA[0];
         
         var thisdate = new Date();
         
         var thisyear = thisdate.getFullYear();
         var thismonthNum = thisdate.getMonth() + 1;
         var nextMonthNum = thisdate.getMonth() + 2;
         
         //alert("thismonthNum: " + thismonthNum);
         
         // gold = f9d200
         // blue = 58d3ff 
           
         document.getElementById("thisweek_plus").innerHTML = "+ ";
         $("#thisweek_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("thismonth_plus").innerHTML = "+ ";
         $("#thismonth_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("nextmonth_plus").innerHTML = "+ ";
         $("#nextmonth_plus").css( { "color" :  "#58d3ff" } );
         
         /*
         document.getElementById("january_plus").innerHTML = "+ ";
         $("#january_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("february_plus").innerHTML = "+ ";
         $("#february_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("march_plus").innerHTML = "+ ";
         $("#march_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("april_plus").innerHTML = "+ ";
         $("#april_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("may_plus").innerHTML = "+ ";
         $("#may_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("june_plus").innerHTML = "+ ";
         $("#june_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("july_plus").innerHTML = "+ ";
         $("#july_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("august_plus").innerHTML = "+ ";
         $("#august_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("september_plus").innerHTML = "+ ";
         $("#september_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("october_plus").innerHTML = "+ ";
         $("#october_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("november_plus").innerHTML = "+ ";
         $("#november_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("december_plus").innerHTML = "+ ";
         $("#december_plus").css( { "color" :  "#58d3ff" } );
        */
         
         switch(thismonthStr)
         {
         case "thisweek" :
            makeJsonRequestThisWeek();    
            document.getElementById("thisweek_plus").innerHTML = "- ";  
             $("#thisweek_plus").css( { "color" :  "#f9d200" } ); 
             //
             clearPrevious();
              previousStr = "thisWeek";
             // 
            break;
         case "thismonth" :
            makeJsonRequest(thisyear, thismonthNum);   
            document.getElementById("thismonth_plus").innerHTML = "- ";  
            $("#thismonth_plus").css( { "color" :  "#f9d200" } );  
            //
            clearPrevious();
             previousStr = "thisMonth";
            //   
            break;
         case "nextmonth" :
            makeJsonRequest(thisyear, nextMonthNum);   
            document.getElementById("nextmonth_plus").innerHTML = "- ";  
            $("#nextmonth_plus").css( { "color" :  "#f9d200" } );  
            //
            clearPrevious();
            previousStr = "nextMonth";
            //   
            break;      
            /* 
         case "january" :
            makeJsonRequest(thisyear, 1);  
            document.getElementById("january_plus").innerHTML = "- ";  
            $("#january_plus").css( { "color" :  "#f9d200" } );      
            break;
         case "february" :
            makeJsonRequest(thisyear, 2); 
            document.getElementById("february_plus").innerHTML = "- "; 
            $("#february_plus").css( { "color" :  "#f9d200" } );        
            break;
        case "march" :
            makeJsonRequest(thisyear, 3); 
            document.getElementById("march_plus").innerHTML = "- ";  
            $("#march_plus").css( { "color" :  "#f9d200" } );       
            break;
        case "april" :
             makeJsonRequest(thisyear, 4); 
             document.getElementById("april_plus").innerHTML = "- "; 
             $("#april_plus").css( { "color" :  "#58d3ff" } );     
            break;
         case "may" :
             makeJsonRequest(thisyear, 5); 
             document.getElementById("may_plus").innerHTML = "- ";
             $("#may_plus").css( { "color" :  "#f9d200" } );   
            break;
         case "june" :
            makeJsonRequest(thisyear, 6);   
            document.getElementById("june_plus").innerHTML = "- "; 
            $("#june_plus").css( { "color" :  "#f9d200" } );
            break;
      case "july" :
            makeJsonRequest(thisyear, 7);  
            document.getElementById("july_plus").innerHTML = "- "; 
            $("#july_plus").css( { "color" :  "#f9d200" } ); 
            break;
       case "august" :
            makeJsonRequest(thisyear, 8);   
            document.getElementById("august_plus").innerHTML = "- "; 
            $("#august_plus").css( { "color" :  "#f9d200" } );
            break; 
        case "september" :
             makeJsonRequest(thisyear, 9); 
             document.getElementById("september_plus").innerHTML = "- "; 
             $("#september_plus").css( { "color" :  "#f9d200" } );  
            break; 
        case "october" :
            makeJsonRequest(thisyear, 10);  
            document.getElementById("october_plus").innerHTML = "- "; 
            $("#october_plus").css( { "color" :  "#f9d200" } ); 
            break; 
        case "november" :
             makeJsonRequest(thisyear, 11);
             document.getElementById("november_plus").innerHTML = "- ";    
             $("#november_plus").css( { "color" :  "#f9d200" } );
            break; 
        case "december" :
            makeJsonRequest(thisyear, 12);   
            document.getElementById("december_plus").innerHTML = "- "; 
            $("#december_plus").css( { "color" :  "#f9d200" } );
            break;
            */
        }    
     
    });
    
      makeJsonRequestThisWeek();      
      
      // set the first open one gold at the beginning
       document.getElementById("thisweek_plus").innerHTML = "- ";
         $("#thisweek_plus").css( { "color" :  "#f9d200" } );
      // the rest are blue
         document.getElementById("thismonth_plus").innerHTML = "+ ";
         $("#thismonth_plus").css( { "color" :  "#58d3ff" } );
         //
          document.getElementById("nextmonth_plus").innerHTML = "+ ";
         $("#nextmonth_plus").css( { "color" :  "#58d3ff" } );
         /*
         document.getElementById("january_plus").innerHTML = "+ ";
         $("#january_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("february_plus").innerHTML = "+ ";
         $("#february_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("march_plus").innerHTML = "+ ";
         $("#march_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("april_plus").innerHTML = "+ ";
         $("#april_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("may_plus").innerHTML = "+ ";
         $("#may_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("june_plus").innerHTML = "+ ";
         $("#june_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("july_plus").innerHTML = "+ ";
         $("#july_plus").css( { "color" :  "#58d3ff" } );
                
         document.getElementById("august_plus").innerHTML = "+ ";
         $("#august_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("september_plus").innerHTML = "+ ";
         $("#september_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("october_plus").innerHTML = "+ ";
         $("#october_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("november_plus").innerHTML = "+ ";
         $("#november_plus").css( { "color" :  "#58d3ff" } );
         
         document.getElementById("december_plus").innerHTML = "+ ";
         $("#december_plus").css( { "color" :  "#58d3ff" } ); 
        */
}



function makeJsonRequestThisWeek(thisYear, monthNum)
{
   var requestPath = "http://www.theroyal.to/schedule/?RANGE=WEEK";
 
  $.getJSON(requestPath, 
        function(data, textStatus){ populateMonth(data)});
 }

function makeJsonRequest(thisYear, monthNum)
{
    //var requestPath = "http://roydev.audt.ca/schedule/?RANGE=WEEK"; 

    //(instead of WEEK, you can also use MONTH or YEAR)
    var requestPath = "http://www.theroyal.to/schedule/?YEAR=" + thisYear + "&MONTH=" + monthNum;
    
    /*
    It’s currently a list of ‘day’ lists, one for each. Each ‘day’ list has two elements.
    The first element is the date. 
    The second is a list of showtimes. 
    Each showtime is an object with the following attributes and value types:

    * admission_discounted : float
    * admission_regular: float
    * showtime: string
    * film_running_time: int
    * film_year: int
    * film_rating: string
    * film_title: string
    * theatre_event_id: int
    * id: int

    */
    
    // the data looks like a month object with 31 day properties: 
    // 2010-03-01,[object Object],[object Object]  
    // 2010-03-02,[object Object],[object Object]
    // 2010-03-03,[object Object],[object Object]
    // etc
    
    $.getJSON(requestPath, 
        function(data, textStatus){ populateMonth(data)});

}

function clearPrevious()
{
    switch(previousStr)
    {
    case "thisWeek" :
        clearWeek();
        break;
    case "thisMonth" :
        clearMonth();
        break;
    case "nextMonth" :
        clearNextMonth();
        break;        
    }
}

function clearWeek()
{
    var filmCounter =  $("#thisweek_ul li").length - 1;
    var totalfilms = $("#thisweek_ul li").length;
    
    //alert("clear films total on week: " + totalfilms);
    
    if ( totalfilms > 1 )
    {
        $("#thisweek_ul li").each(function(index) {
           // alert(index + ': ' + $(this).text());
           $(this).remove();
        });

    }
        

}

function clearMonth()
{
    var filmCounter =  $("#thismonth_ul li").length - 1;
    var totalfilms = $("#thismonth_ul li").length;
    
    //alert("clear films total on month: " + totalfilms);
    
    if ( totalfilms > 1 )
    {
       $("#thismonth_ul li").each(function(index) {
           // alert(index + ': ' + $(this).text());
           $(this).remove();
        });
    }
        

}

function clearNextMonth()
{
    var filmCounter =  $("#nextmonth_ul li").length - 1;
    var totalfilms = $("#nextmonth_ul li").length;
    
    //alert("clear films total on month: " + totalfilms);
    
    if ( totalfilms > 1 )
    {
       $("#nextmonth_ul li").each(function(index) {
           // alert(index + ': ' + $(this).text());
           $(this).remove();
        });
    }
        

}


function populateMonth(data)
{
      //clearMonth();
      
      // alert("data.length: " + data.length);
        //alert("data: " + data);
      
      var recordHeight = 90;
      var totalPanelHeight = 0;
      
      // first add all the days         
       for (var dataCounter = 0; dataCounter < data.length; dataCounter++)
       {
            var dataArray = new Array();
            
            var filmsArray = new Array()
            var dayArray = new Array();
            
            dataArray = data[dataCounter];
               
            daysArray = dataArray[0]; // 0 for the date, 1 for the films 
            filmsArray = dataArray[1];   
           
            var liID = "liday_id_" + dataCounter;
             var ulID = "ulfilm_id_" + dataCounter;
                   
             $('#' + myIDSelected + "_ul").append("<li id=" + liID + "><span class='schedule_date'>" + dataArray[0] + "</span></li>");
             $("#"+liID).append("<ul id=" + ulID + " class='schedule_movies_per_day_ul'></ul>");
            
            if ( filmsArray.length > 0 ) 
            {
                                                 
                 for( var filmsCounter = 0; filmsCounter < filmsArray.length; filmsCounter++)
                 {
                                              
                    var lifilmID = "lifilm_id_" + filmsArray[filmsCounter].film_title + "_" + filmsCounter;
                    
                      if ( filmsArray[filmsCounter].title != null || filmsArray[filmsCounter].title != undefined || filmsArray[filmsCounter].title != "" )
                       {
                          
                          
                          var specialEventStr;
                          if ( filmsArray[filmsCounter].event_type == "film" ) specialEventStr = "";
                            else specialEventStr = " (special event) ";
                          
                         
                          var filmrating;
                           if ( filmsArray[filmsCounter].rating == "" ) filmrating = "";
                            else filmrating = "(" + filmsArray[filmsCounter].rating + ")";
                             
                             
                             var extraInfo;
                             if ( filmsArray[filmsCounter].extra_info == "" )  extraInfo = "";
                                else extraInfo = "<tr><td width='60px'></td><td><font color='#FFFFFF'>&ndash; " + filmsArray[filmsCounter].extra_info + "</font></td></tr>";
                             
                          
                           $("#" + ulID).append("<li id=" + lifilmID + "><table>" + 
                                    "<tr>"+
                                        "<td width='60px'><span class='film_showtime'>" +  filmsArray[filmsCounter].showtime + "</span> </td>" +
                                        "<td width='550px'><span class='film_title'><a href='" + filmsArray[filmsCounter].url+ "'>" + filmsArray[filmsCounter].title + specialEventStr + "</a></span> <span class='film_rating'>" + filmrating +  "</span></td>" +
                                        //"<td> <span class='film_year'>" + filmsArray[filmsCounter].year + "</span></td>" + 
                                        //"<td> <span class='film_country'>" + filmsArray[filmsCounter].event_type + "</span></td>" +
                                        //"<td width='50'><span class='film_length'>" +  filmsArray[filmsCounter].running_time + "mins</span></td>" + 
                                        //"<td> <span class='film_price'>$" + filmsArray[filmsCounter].admission_regular + "  /  $" + filmsArray[filmsCounter].admission_discounted + "*</span></td>"+
                                   "</tr>" + extraInfo + 
                                   /*
                                   "<tr>" + 
                                        "<td><hr class='scheduleline'></td>" +
                                        "<td><hr class='scheduleline'></td>" +
                                        "<td><hr class='scheduleline'></td>" +
                                        "<td><hr class='scheduleline'></td>" +
                                        "<td><hr class='scheduleline'></td>" +
                                        "<td><hr class='scheduleline'></td>" +
                                   "</tr>" + */ 
                                    "</table></li>");
                        } 
               
                  }
                  
                 totalPanelHeight = totalPanelHeight + recordHeight;  
             }    
             else
             {
                //alert("no film today"); <-- this gets here but for some reason, it doesn't add this next line until a count of 8?!
                // I may be trying to do too much here... I could add these items after I add all the hooks
                   $("#" + ulID).append("<li>Check back for more details</li>");
             }
            
         }     
  
     $("#" + ulID).append("<p><span class='discountprice'><i>Please click on each film title to see more information</i></span></p>");
     
     var mainContentHeight =  $('#' + myIDSelected).height();
     
     //alert("main height: " + mainContentHeight + " selectedPanelId: " + '#' + myIDSelected);
     //alert("totalPanelHeight: " + totalPanelHeight);
     updateUIHeight(mainContentHeight);
}


function updateUIHeight(mainContentHeight)
{
            var bufferheight = 200;
            
           // SLICE
           
            var sliceNewTop = mainContentHeight + bufferheight;
            $('.main_slice_bg').css( { "top" :  String(sliceNewTop) + "px" } );
           
            // SLICE BENEATH
                
            var sliceBoxNewTop = sliceNewTop +  $('.main_slice_bg').height() - 20;
            $('.main_whitebox_beneathslice').css( { "top" :  String(sliceBoxNewTop) + "px" } );
       
           
           // COMING SOON
           $('#comingsoon').css( { "top" :  String( sliceNewTop + 75 ) + "px" } );  
                             
           // FOOTER
           var newFooterTop = sliceBoxNewTop + $('.main_whitebox_beneathslice').height() - 2;
           $('#footer').css( { "top" :  String(newFooterTop) + "px" } );
           
           // SIDEBAR
            var sidebarNewHeight = mainContentHeight + $('.main_slice_bg').height() +  $('.main_whitebox_beneathslice').height() + bufferheight - $('.sidebar_top').height() - 27;
           $('.sidebar_block').height(sidebarNewHeight);
           
           // MAIN INNNER
           var mainInnerNewHeight = mainContentHeight + bufferheight +   $('.main_whitebox_beneathslice').height() +   $('.main_slice_bg').height() + $('#footer').height() - 50;
           $('#main_inner').height(mainInnerNewHeight);
           
           // MAIN HEIGHT
           
            var mainNewHeight = mainInnerNewHeight + 8;
           
           $('#main').height(mainNewHeight);
           
           // scroll to top of open tab
           
           //alert("selected id: " + '#' + myIDSelected);
           
          // $(...).scrollTo( $('#' + myIDSelected),  {duration:800} );
           
           
}


