// Lekker primitief ^^ maar helaas zorgt de funtie bij onChange voor problemen als het via een for loopje gaat :(
// Aanroepen als er boxen toegevoegd / verwijderd zijn uit de rijen
function CreateSortables()
{
  if(TotalRows >= 1)
  {
    Sortable.create("row_1",
      {
        tag:'div', dropOnEmpty:true, handle:'handle', containment:Rows, constraint:false, scroll:window,
        onUpdate:function(){ ReRowOrder() }
      }
    );
  }

  if(TotalRows >= 2)
  {
    Sortable.create("row_2",
      {
        tag:'div', dropOnEmpty:true, handle:'handle', containment:Rows, constraint:false, scroll:window,
        onUpdate:function(){ ReRowOrder(); }
      }
    );
  }

  if(TotalRows >= 3)
  {
    Sortable.create("row_3",
      {
        tag:'div', dropOnEmpty:true, handle:'handle', containment:Rows, constraint:false, scroll:window,
        onUpdate:function(){ ReRowOrder(); }
      }
    );
  }

  if(TotalRows >= 4)
  {
      Sortable.create("row_4",
      {
        tag:'div', dropOnEmpty:true, handle:'handle', containment:Rows, constraint:false, scroll:window,
        onUpdate:function(){ ReRowOrder(); }
      }
    );
  }

  if(TotalRows >= 5)
  {
    Sortable.create("row_5",
      {
        tag:'div', dropOnEmpty:true, handle:'handle', containment:Rows, constraint:false, scroll:window,
        onUpdate:function(){ ReRowOrder(); }
      }
    );
  }

  if(TotalRows >= 6)
  {
    Sortable.create("row_6",
      {
        tag:'div', dropOnEmpty:true, handle:'handle', containment:Rows, constraint:false, scroll:window,
        onUpdate:function(){ ReRowOrder(); }
      }
    );
  }
}

// Aanroepen VOOR het resizen.
function ReRowOrder()
{
  CreateSortables();
  var check = Array();

  // Gegevens ophalen
  for(i=1; i < (TotalRows+1); i++)
  {
    RowOrder[ i ] = Sortable.ReturnBoxIDs('row_' + i);
  }

  // Gegevens in de correcte rij zetten.
  NewRowOrder = Array();
  for(y=0; y<10; y++)
  {
    for(i=1; i < (TotalRows+1); i++)
    {
      if(RowOrder[i][y] == check[y]) {
      }else{
        NewRowOrder.push(RowOrder[i][y]);
} } } }


// Functie aanroepen NA het resizen!
function RePlaceRows()
{
  // Alles leeggooien...
  for(i=1; i < (MaxRows+1); i++)
  {
    document.getElementById('row_' + i).innerHTML = '';
  }

// Boxen opnieuw plaatsen.
  if(NewRowOrder.length == 0)
  {
    NewRowOrder = StartRowOrder;
  }

  i = 0;
  for(y=0; y < (NewRowOrder.length); y++)
  {
    PlaceBox(i, NewRowOrder[y]);
    i++;
    if(i >= TotalRows) { i = 0; }
  }
  CreateSortables();
}

// Aanroepen als de content uit een containor in een row replaatst moet worden.
function PlaceBox( rowID, boxID )
{
  document.getElementById( Rows[ rowID ] ).innerHTML += '<div id="box_'+boxID+'">'+document.getElementById("cover_box_" + boxID).innerHTML+'</div>'
}


// Aanroepen bij resizen van pagina / startup.
// Deze functie zal berekenen hoeveel rijen er geopend moeten worden.
function addRows()
{
  Width__ = (Width_ - 346);

  if(Math.floor( Width__/(RowWidth+SpacerWidth) ) > MaxRows)
  {
    rows_ = MaxRows;
  }else{
    rows_ = Math.floor(Width__/(RowWidth+SpacerWidth));
  }

  for(i=0; i <= (rows_-1); i++)
  {
    var Rows = Array();
    Rows[i] = 'rows_'+(i+1);
  }

  OldTotalRows = TotalRows
  if(StartingScript == 1)
  {
    OldTotalRows = -1;
    StartingScript = 0;
  }
  TotalRows = rows_;
}


// Aanroepen bij resizen van pagina / startup.
// deze functie opent de rijen, en sluit de rest.
function OpenRows()
{
  for (i=0; i <= (TotalRows - 1); i++)
  {
    document.getElementById( Rows[i]+'_spacer' ).className = 'spacerOpen';
    document.getElementById( Rows[i] ).className = 'containmentOpen';
  }

  for (i=TotalRows; i <= (MaxRows-1); i++)
  {
    document.getElementById( Rows[i]+'_spacer' ).className = 'spacerClose';
    document.getElementById( Rows[i] ).className = 'containmentClose';
  }
}


// Aanroepen bij het klikken op "Eerder / Later". Stopt de info in zowel de containor, als de box.
function GetBoxInfo( id_, time, do_)
{
  var ajaxRequest=null;
  try {
    // Firefox, Opera 8.0+, Safari, IE7+
    ajaxRequest = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      ajaxRequest=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Uw browser ondersteunt geen AJAX.");
        return false;
  } } }
  ajaxRequest.onreadystatechange = function() {
    if (ajaxRequest.readyState == 4) {
      try {
        if (ajaxRequest.status == 200) {
          document.getElementById("box_"+ id_).innerHTML = ajaxRequest.responseText;
          document.getElementById("cover_box_"+ id_).innerHTML = ajaxRequest.responseText;
          CreateSortables();
        }
      } catch (e) {
        document.getElementById("box_"+ id_).innerHTML = "Ajax error: " + e.description;
        document.getElementById("cover_box_"+ id_).innerHTML = "Ajax error: " + e.description;
        CreateSortables();
  } } }
  ajaxRequest.open("get", "GetBoxInfoAjax.php?id="+ id_ +"&time="+ time+"&do="+do_);
  ajaxRequest.send(null);
  CreateSortables();
}

// Aanroepen bij het openen van de popup.
function GetPopupInfo( id_ )
{
  var ajaxRequest=null;
  try {
    // Firefox, Opera 8.0+, Safari, IE7+
    ajaxRequest = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      ajaxRequest=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Uw browser ondersteunt geen AJAX.");
        return false;
  } } }
  ajaxRequest.onreadystatechange = function() {
    if (ajaxRequest.readyState == 4) {
      try {
        if (ajaxRequest.status == 200) {
          document.getElementById("PopupBox").innerHTML = ajaxRequest.responseText;
        }
      } catch (e) {
        document.getElementById("PopupBox").innerHTML = "Ajax error: " + e.description;
  } } }
  time = new Date();
  ajaxRequest.open("get", "GetPopupInfoAjax.php?id="+ id_ +"&md5="+time.getUTCSeconds());
  ajaxRequest.send(null);
  iBox.showURL('#PopupBox', '', '');
}


function StuurReminder(type, id)
{
  document.location = './?p=AddReminder&type='+type+'&id='+id;
}
