var Lights = IR.GetPage("Lighting");
var Holder = IR.GetPopup("RoomItemHolder").GetItem("Item 1");
var ListLight = Lights.GetItem("Rooms");
var Lightslist = Lights.GetItem("Item 8");
var popuppage = IR.GetPopup("ListRooms");
var click = popuppage.GetItem("delete");
var nameclick = popuppage.GetItem("Name");
var Listname = IR.GetPage("Rooms").GetItem("Rooms");

   var Listje = 0;
   var i =0;
   var top = 100;
   var left = 200;
   var clickcount = 0;
   var part1 = "Ruimte";
   var leftitems = 40;
   var clickds = 0;
   var clickds2 = 0; 
   var maxrooms = 10;
   var currrooms = 0;
   
function CreateItemsLists()
{    
    //IR.Log(part1);
    var part2 = i;
    var fulllist = part1 + part2;
    
    //IR.Log(testee);
   
    i++;
    
    if(currrooms == maxrooms)
    {
       return;
    }
    else if(currrooms <= maxrooms)
    {
    currrooms++;
    
         /*--elke streep geeft een aparte item groep met addlistener weer--*/
 
/*------------------------------------------------------------------------------------------------------------------------------------*/
    
    var testobject = IR.CreateItem(IR.ITEM_POPUP, fulllist, 30,10,500, 500);
    var lighttt = Lights.CreateItem(IR.ITEM_BUTTON, "open", 100, top, 105, 83);
    lighttt.CreateState(0);
    lighttt.GetState(0).Text = "Open " + i;
    lighttt.GetState(0).Image = "Ipad blauw.png";
    lighttt.GetState(0).FillColor = 0;
    
    testobject.CreateState(0);

    testobject.GetState(0).Image = "page0_item1_state0_image_07E302161003143D0.png";
    testobject.Drag = true;
    
/*------------------------------------------------------------------------------------------------------------------------------------*/
 
    var test = testobject.CreateItem(IR.ITEM_BUTTON, "test", 20,20,100,20);
    test.CreateState(0);
    test.GetState(0).Text = "Close";
    test.GetState(0).Font = "Arial";
    test.GetState(0).Image = "Ipad blauw.png";
    test.GetState(0).FillColor = 0;
    
    var dada = testobject.CreateItem(IR.ITEM_BUTTON, "dada", 20, 400, 100, 20);
    dada.CreateState(0);
    dada.GetState(0).Text = "Delete";
    dada.GetState(0).Font = "Arial";
    dada.GetState(0).Image = "Ipad blauw.png";
    dada.GetState(0).FillColor = 0;
    
    
    IR.AddListener(IR.EVENT_ITEM_PRESS, dada, function()
    {
      //IR.Log("test");
      IR.DeleteItem(testobject);
      IR.DeleteItem(lighttt);
    })
    
/*------------------------------------------------------------------------------------------------------------------------------------*/
 
    var btn1 = testobject.CreateItem(IR.ITEM_BUTTON, "btn1", leftitems, 150, 50, 50);
    btn1.CreateState(0);
    btn1.GetState(0).Image = "100x100_1.png";
    btn1.GetState(0).FillColor = 0; 
    btn1.Enable = true; 
    
    var btnLicht = testobject.CreateItem(IR.ITEM_BUTTON, "btnLicht", 125, 150, 50, 50);
    btnLicht.CreateState(0);
    btnLicht.GetState(0).Image = "Ipad grijs 3.png";
    btnLicht.GetState(0).FillColor = 0;
    btnLicht.GetState(0).Text = "Disable";
    
   IR.AddListener(IR.EVENT_ITEM_PRESS, btnLicht,function()
   {  
      if(btn1.Enable == true)
            {
               btn1.Enable = false;
               btnLicht.Text = "Enable";
            }
               
            else if(btn1.Enable == false)
            {
               btn1.Enable = true;
               btnLicht.Text = "Disable";
            }  
   })  
       
      IR.AddListener(IR.EVENT_ITEM_PRESS, btn1,function()
      {               
               if(clickds == 0)
               {
                  btn1.GetState(0).Image = "80x80_11.png"; 
                  clickds++;
               }
               else if(clickds == 1)
               {
                  btn1.GetState(0).Image = "100x100_1.png"; 
                  clickds--;
               }          
      })
         
/*------------------------------------------------------------------------------------------------------------------------------------*/
 
    var btn2 = testobject.CreateItem(IR.ITEM_BUTTON, "btn2", leftitems, 200, 50, 50);
    btn2.CreateState(0);
    btn2.GetState(0).Image = "popup8_item10_state0_image_07E304040B2B00186.png";
    btn2.GetState(0).FillColor = 0; 
    btn2.Enable = true;    
    
    var geluiden = testobject.CreateItem(IR.ITEM_BUTTON, "geluiden", 100, 200, 50, 50);
    geluiden.CreateState(0);
    geluiden.GetState(0).Image = "popup8_item11_state0_image_07E304040B2B00187.png";
    geluiden.GetState(0).FillColor = 0; 
    geluiden.Enable = false;
    geluiden.Visible = false; 
    
    var geluiduit = testobject.CreateItem(IR.ITEM_BUTTON, "geluiduit", 170, 200, 50, 50);
    geluiduit.CreateState(0);
    geluiduit.GetState(0).Image = "popup8_item12_state0_image_07E304040B2B00189.png";
    geluiduit.GetState(0).FillColor = 0; 
    geluiduit.Enable = false;
    geluiduit.Visible = false;
    
    var btnLicht2 = testobject.CreateItem(IR.ITEM_BUTTON, "btnLicht2", 250, 200, 50, 50);
    btnLicht2.CreateState(0);
    btnLicht2.GetState(0).Image = "Ipad grijs 3.png";
    btnLicht2.GetState(0).FillColor = 0;
    btnLicht2.GetState(0).Text = "Disable";
    
    IR.AddListener(IR.EVENT_ITEM_PRESS, btnLicht2,function()
   {  
            if(btn2.Enable == true)
            {
               btn2.Enable = false;
               geluiduit.Enable = false;
               geluiden.Enable = false;
               btnLicht2.Text = "Enable";
            }
               
            else if(btn2.Enable == false)
            {
               btn2.Enable = true;
               geluiden.Enable = true;
               geluiduit.Enable = true;
               btnLicht2.Text = "Disable";
            }  
   }) 
   
   
         IR.AddListener(IR.EVENT_ITEM_PRESS, btn2,function()
      {
         if(geluiden.Visible == true)
         {
            geluiden.Visible = false;
            geluiden.Enable = false;
            geluiduit.Visible = false;
            geluiduit.Enable = false;           
         }
         else if(geluiden.Visible == false)
         {
            geluiden.Visible = true;
            geluiden.Enable = true;
            geluiduit.Visible = true;
            geluiduit.Enable = true;    
         }      
      })  
    
   IR.AddListener(IR.EVENT_ITEM_PRESS, geluiden,function()
   {
      if(geluiden.Visible == true)
      {       
         //IR.Log("True");
         geluiden.GetState(0).Image = "64x64_3d_sound-on.png";
         geluiduit.GetState(0).Image = "popup8_item12_state0_image_07E304040B2B00189.png";        
      }
      else
      {
         return;
      }
   })
  
   IR.AddListener(IR.EVENT_ITEM_PRESS, geluiduit,function()
   {
      if(geluiduit.Visible == true)
      {   
         //IR.Log("False");
         geluiduit.GetState(0).Image = "64x64_3d_sound-off.png";
         geluiden.GetState(0).Image = "popup8_item11_state0_image_07E304040B2B00187.png";
      }
      else 
      {
         return;
      }
   })  
    
/*------------------------------------------------------------------------------------------------------------------------------------*/
    
    var btn3 = testobject.CreateItem(IR.ITEM_BUTTON, "btn3", leftitems, 250, 50, 50);
    btn3.CreateState(0);
    btn3.GetState(0).Image = "popup8_item7_state0_image_07E304040B2B00181.png";
    btn3.GetState(0).FillColor = 0; 
    btn3.Enable = true; 
    
    var geluiden2 = testobject.CreateItem(IR.ITEM_BUTTON, "geluiden2", 100, 250, 50, 50);
    geluiden2.CreateState(0);
    geluiden2.GetState(0).Image = "popup8_item11_state0_image_07E304040B2B00187.png";
    geluiden2.GetState(0).FillColor = 0; 
    geluiden2.Enable = false;
    geluiden2.Visible = false; 
    
    var geluiduit2 = testobject.CreateItem(IR.ITEM_BUTTON, "geluiduit2", 170, 250, 50, 50);
    geluiduit2.CreateState(0);
    geluiduit2.GetState(0).Image = "popup8_item12_state0_image_07E304040B2B00189.png";
    geluiduit2.GetState(0).FillColor = 0; 
    geluiduit2.Enable = false;
    geluiduit2.Visible = false;
       
    var btnLicht3 = testobject.CreateItem(IR.ITEM_BUTTON, "btnLicht3", 250, 250, 50, 50);
    btnLicht3.CreateState(0);
    btnLicht3.GetState(0).Image = "Ipad grijs 3.png";
    btnLicht3.GetState(0).FillColor = 0;
    btnLicht3.GetState(0).Text = "Disable";
    
    IR.AddListener(IR.EVENT_ITEM_PRESS, btnLicht3,function()
   {  
            if(btn3.Enable == true)
            {
               btn3.Enable = false;
               btnLicht3.Text = "Enable";
               geluiduit2.Enable = false;
               geluiden2.Enable = false;
            }
               
            else if(btn3.Enable == false)
            {
               btn3.Enable = true;
               btnLicht3.Text = "Disable";
               geluiden2.Enable = true;
               geluiduit2.Enable = true;
            }  
   })  
      
      
         IR.AddListener(IR.EVENT_ITEM_PRESS, btn3,function()
      {
         if(geluiden2.Visible == true)
         {
            geluiden2.Visible = false;
            geluiden2.Enable = false;
            geluiduit2.Visible = false;
            geluiduit2.Enable = false;           
         }
         else if(geluiden2.Visible == false)
         {
            geluiden2.Visible = true;
            geluiden2.Enable = true;
            geluiduit2.Visible = true;
            geluiduit2.Enable = true;    
         }      
      })  
    
   IR.AddListener(IR.EVENT_ITEM_PRESS, geluiden2,function()
   {
      if(geluiden2.Visible == true)
      {       
         //IR.Log("True");
         geluiden2.GetState(0).Image = "64x64_3d_sound-on.png";
         geluiduit2.GetState(0).Image = "popup8_item12_state0_image_07E304040B2B00189.png";        
      }
      else
      {
         return;
      }
   })
  
   IR.AddListener(IR.EVENT_ITEM_PRESS, geluiduit2,function()
   {
      if(geluiduit2.Visible == true)
      {   
         //IR.Log("False");
         geluiduit2.GetState(0).Image = "64x64_3d_sound-off.png";
         geluiden2.GetState(0).Image = "popup8_item11_state0_image_07E304040B2B00187.png";
      }
      else 
      {
         return;
      }
   })  
/*------------------------------------------------------------------------------------------------------------------------------------*/
 
    var btn4 = testobject.CreateItem(IR.ITEM_BUTTON, "btn4", leftitems, 300, 50, 50);
    btn4.CreateState(0);
    btn4.GetState(0).Image = "popup8_item9_state0_image_07E304040B2B00185.png";
    btn4.GetState(0).FillColor = 0; 
    btn4.Enable = true; 
    
    var btnLicht4 = testobject.CreateItem(IR.ITEM_BUTTON, "btnLicht4", 125, 300, 50, 50);
    btnLicht4.CreateState(0);
    btnLicht4.GetState(0).Image = "Ipad grijs 3.png";
    btnLicht4.GetState(0).FillColor = 0;
    btnLicht4.GetState(0).Text = "Disable";   
    
    IR.AddListener(IR.EVENT_ITEM_PRESS, btnLicht4,function()
   {  
            if(btn4.Enable == true)
            {
               btn4.Enable = false;
               btnLicht4.Text = "Enable";
            }
               
            else if(btn4.Enable == false)
            {
               btn4.Enable = true;
               btnLicht4.Text = "Disable";
            }  
   })  
      
      IR.AddListener(IR.EVENT_ITEM_PRESS, btn4,function()
      {         
            if(clickds2 == 0)
            {
               btn4.GetState(0).Image = "popup8_item8_state0_image_07E304040B2B00183.png"; 
               clickds2++;
            }
            else if(clickds2 == 1)
            {
               btn4.GetState(0).Image = "popup8_item9_state0_image_07E304040B2B00185.png"; 
               clickds2--;
            }             
      }) 
    
/*------------------------------------------------------------------------------------------------------------------------------------*/
     
      top += 100;

    //IR.GetItem("Lighting").CreateItem(IR.ITEM_BUTTON, fulllist,left,50,20,15);	

    IR.AddListener(IR.EVENT_ITEM_PRESS,lighttt,function()
   {   
         //IR.Log(fulllist);
         IR.ShowPopup(fulllist);   
         clickcount++;       
   })
    
    IR.AddListener(IR.EVENT_ITEM_PRESS,test,function()
   {		     
         IR.HidePopup(fulllist);
         clickcount--;
         //IR.Log(clickcount);
   })
      
    } 
}

IR.AddListener(IR.EVENT_ITEM_SELECT, ListLight, function(item, subitem)
{
      //IR.Log(ListLight.GetItem(item).GetItem(subitem).Text);
      //IR.Log(item);
      if(subitem == 3)
      {
         ListLight.DeleteItem(item);
         Listname.DeleteItem(item);
      }
      else if(subitem == 2)
      {    
          var test = ListLight.GetSelected(item);
          IR.Log(test);
          //IR.Log(test);        
          CreateItemsLists();          
      }  
});

IR.AddListener(IR.EVENT_ITEM_SELECT, Listname, function(item, subitem)
{
      //IR.Log(subitem);
      //IR.Log(item);
      if(subitem == 3)
      {
         ListLight.DeleteItem(item);
         Listname.DeleteItem(item);
      } 
});

