/**
 * Forms the basis of a panel display
 * 
 * @ingroup mainapi
 */
class panels_display {
  var $args = array();
  var $content = array();
  var $panels = array();
  var $incoming_content = NULL;
  var $css_id = NULL;
  var $context = array();
  
  function add_pane($pane, $location = FALSE) {
    $pane->pid = $this->next_new_pid();
    if (!$location || !isset($this->panels[$location])) {
      foreach ($this->panels as $panel_name => $panel) {
        if (array_key_exists($pid, $panel)) {
          $this->panels[$panel_name][] = $pid;
        }
      }
    }
    else {
      $this->panels[$location][] = $pane->pid;
    }
  }
  
  function duplicate_pane($pid, $location = FALSE) {
    $pane = $this->clone_pane($pid);
    $this->add_pane($pane, $location);
  }
  
  function clone_pane($pid) {
    $pane = drupal_clone($this->content[$pid]);
    foreach (array_keys($this->content) as $pidcheck)
    unset($pane->position); // necessary?
    return $pane;
  }
  
  function next_new_pid() {
    // necessary if/until we use this method and ONLY this method for adding temporary pids.
    // then we can do it with a nice static var.
    foreach (array_keys($this->content) as $pid) {
      if (!is_numeric($pid)) {
        $id[] = substr($pid, 4);
      }
    }
    return ++end($id);
  }
}

function panels_export_pane_across_displays($source_display, &$target_display, $pid, $location = FALSE) {
  $pane = $source_display->clone_pane($pid);
  $target_display->add_pane($pane, $location);
}
