//////////////////////////////////////////////////////////////// Fun
// Start
function Calendar(Page, Label_Month, Label_Year, Button_Next, Button_Prev, Localisation, DefaultColorDay, DefaultColorCuurentDay, Month_List, Year_List, bToday)
{
   // Localisation
   var Month_ENG = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];     
   var Day_ENG   = ["SU", "MO", "TU", "WE", "TH", "FR", "SA"]; 
   
   // Data           
   var Month_Day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];  
   var Today = new Date();  
   var Year_now = Today.getFullYear(); 
   var Month_now = Today.getMonth(); 
   var Year = Year_now;
   var Month_i = Month_now;
   var Month;
   var Day; 

   // Get Items Days
   var Label_Day = []; 
   for(var i = 1; i <= 42; i++)
   {
      Label_Day[i] = Page.GetItem("d" + i);     
   }  
   // Set Localisation
   if(Localisation == "RUS")
   { 
      Month = Month_ENG;
      Day = Day_ENG;
   }
   else
   { 
      Month = Month_ENG;
      Day = Day_ENG;
   }
   
   // Print Localisation
   for(var i = 1; i < 8; i++)
   {
      Page.GetItem("l"+[i]).GetState(0).Text = Day[i-1];
   }
   
   // Year Leap
   function Year_Change()
   {
      if ((Year % 4 == 0) && ((Year % 100 != 0) || (Year % 400 == 0)))
      {
         Month_Day[1] = 29;   
      }             
      else
      {
         Month_Day[1] = 28;    
      }  
      Today.setFullYear(Year); 
      
      Year_List.CreateItem(0,1,{Text:"", Value: 0});
      Year_List.CreateItem(1,1,{Text:"", Value: 0});
      Year_List.CreateItem(2,1,{Text:"", Value: 0});
      for(var i = 3; i < 31; i++)
      {
         Year_List.CreateItem(i,1,{Text:(i + 2000).toString(), Value: 0});
      }
      Year_List.CreateItem(31,1,{Text:"", Value: 0});
      Year_List.CreateItem(32,1,{Text:"", Value: 0});  
      Year_List.CreateItem(33,1,{Text:"", Value: 0});
          
      Year_List.CreateItem(Year - 2000,1,{Value: 1});
      
      Year_List.SetPosition(Year - 2003);
   }

   // Month Change
   function Month_Change()
   {
      // Get count Days in Prev Months
      var PrevMonth = Month_i - 1;      
      if(PrevMonth < 0)
      {
        PrevMonth =  11;
      }                       
      var nDays = Month_Day[PrevMonth]; 
      
      // Set Month   
      StartDay = Today.getDay();
      //Today.setMonth(Month_i);
      
      if (StartDay == 0)
      {
         StartDay = 7;   
      }
      else if (StartDay == 1)
      {
         StartDay = 8;  
      }
     
      // Prev Month Prewiew
      for(var i = nDays, j = StartDay - 1; j >= 1; i--, j--)
      {
         Label_Day[j].GetState(0).Text = i + 1; 
         Label_Day[j].GetState(0).Opacity = 100; 
         Label_Day[i].GetState(0).FillColor = DefaultColorDay; 
      } 
     
      // Get other Data
      var nDays = Month_Day[Month_i];
      var ThisDay = Today.getDate();
      
      // Print Month & Year
      Label_Month.GetState(0).Text = Month[Month_i]; 
      Label_Year.GetState(0).Text = Year.toString();
      
      // Print Days
      for(var i = StartDay; i < 44; i++)
      {           
         Label_Day[i - 1].GetState(0).Text = i - StartDay + 1;
         Label_Day[i - 1].GetState(0).FillColor = DefaultColorDay;  
         Label_Day[i - 1].GetState(0).Opacity = 255;
         
         // Next Month Prewiew
         if(i > nDays + StartDay - 1)
         {                              
            Label_Day[i - 1].GetState(0).Text = i - nDays - StartDay + 1; 
            Label_Day[i - 1].GetState(0).Opacity = 100;    
         }
      }
      
      // Select Current Day
      
      if((Month_now == Month_i) && (Year_now == Year))
      {
         Label_Day[StartDay + ThisDay - 2].GetState(0).FillColor = DefaultColorCuurentDay;    
      }
      
      Month_List.CreateItem(0,1,{Text: "", Value: 0});
      Month_List.CreateItem(1,1,{Text: "", Value: 0});   
      Month_List.CreateItem(2,1,{Text: "", Value: 0}); 
      
      for(var i = 3; i<=14; i++)
      {
         Month_List.CreateItem(i,1,{Text: Month[i-3], Value: 0});
      }    
      
      Month_List.CreateItem(15,1,{Text: "", Value: 0});
      Month_List.CreateItem(16,1,{Text: "", Value: 0});   
      Month_List.CreateItem(17,1,{Text: "", Value: 0}); 
      
      Month_List.CreateItem(Month_i+3,1,{Value: 1});    
      Month_List.SetPosition(Month_i);     
   }
   
   // Button Next
   IR.AddListener(IR.EVENT_ITEM_RELEASE, Button_Next, function()
   {
      if(Month_i < 11)
      {
         Month_i++;
        
      }
      else
      {
         Month_i = 0;
         Year++; 
         Year_Change(); 
      }
      Today.setMonth(Month_i);
      Month_Change();
   });
   
   // Button bToday
   IR.AddListener(IR.EVENT_ITEM_RELEASE, bToday, function()
   {
         Month_i = Month_now;
         Year = Year_now; 
         Year_Change();
         Today.setMonth(Month_i);
         Month_Change();
   });
   
   // Button Prev
   IR.AddListener(IR.EVENT_ITEM_RELEASE, Button_Prev, function()
   {
      if(Month_i >= 1)
      {
         Month_i--;
      }
      else
      {
         Month_i = 11;
         Year--;
         Year_Change();   
      }
      Today.setMonth(Month_i);
      Month_Change();
   });    
   
   IR.AddListener(IR.EVENT_ITEM_SELECT, Month_List, function(item, subitem)
   {
      if((item < 15) && (item > 2))
      {
         Month_i = item -3;         
         
         Month_List.CreateItem(0,1,{Text: "", Value: 0});
         Month_List.CreateItem(1,1,{Text: "", Value: 0});   
         Month_List.CreateItem(2,1,{Text: "", Value: 0}); 
         
         for(var i = 3; i<=14; i++)
         {
            Month_List.CreateItem(i,1,{Text: Month[i-3], Value: 0});
         }    
         
         Month_List.CreateItem(15,1,{Text: "", Value: 0});
         Month_List.CreateItem(16,1,{Text: "", Value: 0});   
         Month_List.CreateItem(17,1,{Text: "", Value: 0}); 
         
         Month_List.CreateItem(item,1,{Value: 1});    
         Month_List.SetPosition(item - 3);  
         Today.setMonth(Month_i);
         Month_Change(); 
      }     
   });
     
   IR.AddListener(IR.EVENT_ITEM_SELECT, Year_List, function(item, subitem)
   {
      if((item > 2) && (item < 31))
      {
         for(var i = 0; i < 31; i++)
         {
            Year_List.CreateItem(i,1,{Text:(i + 2000).toString(), Value: 0});
         }      
         
         Year_List.CreateItem(item,1,{Value: 1});
         Year = item + 2000;               
         Today.setFullYear(Year);
         Year_Change();
         Today.setMonth(Month_i);
         Month_Change(); 
      }     
   });
        
   //IR.AddListener(IR.EVENT_START, 0, Calendar);
   IR.AddListener(IR.EVENT_START, 0, Year_Change);
   IR.AddListener(IR.EVENT_START, 0, Month_Change);

};                  
Calendar(IR.GetPopup("Calendar"), IR.GetPopup("Calendar").GetItem("Item 50"), IR.GetPopup("Calendar").GetItem("Item 1"), IR.GetPopup("Calendar").GetItem("Item 52"), IR.GetPopup("Calendar").GetItem("Item 51"), "ENG", 0x5E6471FF, 0x00B4FFFF, IR.GetItem("Calendar_Select_Month").GetItem("Item 1"), IR.GetItem("Calendar_Select_Year").GetItem("Item 1"), IR.GetItem("Calendar").GetItem("Item 2"));