// load content areas - one link for all areas
// uses add_component
function load(status_loading, status_complete, menu_url, menu_args, content_url, content_args) {
  var quantity = 0;
  if (menu_url != '')
    quantity += 1;
  if (content_url != '')
    quantity += 1;
    
  if (menu_url != '') {
    add_load_component(null, quantity, 'menu', menu_url, menu_args, status_loading, status_complete);
    quantity = 0;
  }
  if (content_url != '') {
    add_load_component(null, quantity, 'content', content_url, content_args, status_loading, status_complete);
    quantity = 0;
  }
}

function loadcontent(menuid, status_loading, url, param) {
  load_component(1, 'content', url, param, status_loading, '');
  // reload menu with id for highlighting and reset status_content
  if (menuid != null) {
    status_content('');
    load_component(1, 'menu', 'menu.php', 'id=' + menuid, null, null);
  }
}

// add or load one or more components (add=append content at bottom of div, 
// load=replace div with content) 
// first call gives the quantity of components to load, following
// calls set the first argument (compontents) to 0
function add_load_component(insertmode, components, updatediv, url, params, status_loading, status_complete) {
  // use a global var to count
  if (components != 0) {
    add_component_quantity = components
    add_component_counter = 0;
    if (status_loading != null)
      $('status_page').innerHTML = status_loading;
    begin_wait();
  } 
  // A synchronous call would be nice using Ajax.Updater...
  // therefor "updater.updateContent();" would be necessary, but
  // that does not work with safari
  var updater = new Ajax.Updater(updatediv, url, { insertion:insertmode, evalScripts:true, method:'get', asynchronous:true, parameters:params, onComplete:function() { add_load_component_done(status_complete); } });
}

// use this method to add several components
function add_component(components, updatediv, url, params, status_loading, status_complete) {
  add_load_component(Insertion.Bottom, components, updatediv, url, params, status_loading, status_complete);
}

// use this method to load one component
function load_component(components, updatediv, url, params, status_loading, status_complete) {
  add_load_component(null, components, updatediv, url, params, status_loading, status_complete);
}

// this method is called by add_load_component on complete
function add_load_component_done(status_complete) {
  add_component_counter += 1;
  if (add_component_counter >= add_component_quantity) {
    if (status_complete != null)
      $('status_page').innerHTML = status_complete;
    end_wait();
  }
}

// submit a form and replace a div or use insertmode to append result to 
// a div. Set insertmode null to replace a divs content.
// formname may contain more than one 
function submit(formname, targeturl, updatediv, insertmode, status_sending, status_done) {
  begin_wait();
  var params = Form.serialize($(formname));
  var updater = new Ajax.Updater(updatediv, targeturl, { insertion:insertmode, method:'post', evalScripts:true, asynchronous:true, parameters:params, onLoading:function() { $('status_page').innerHTML = status_sending; }, onComplete:function() { $('status_page').innerHTML = status_done; end_wait();} });
}

// submit a form and load another page when done
function submit2(formname, targeturl, newpage, status_sending) {
  begin_wait();
  var params = Form.serialize($(formname));
  var request =
    new Ajax.Request(targeturl, { 
      evalScripts:true, 
      asynchronous:true, 
      method:'post',
      parameters:params, 
      onLoading:function() { $('status_page').innerHTML = status_sending; }, 
      onComplete:function() { document.location=newpage; }
      }
    ); 
}

// Slide in and out of elements (divs)
function my_toggle(element) {
  Effect.toggle(element, 'slide');
}

// Show the hourglass as an addition feedback (to the status bar) 
function begin_wait() {
  document.documentElement.className = 'cursor_wait'; 
}

// Show the hourglass as an addition feedback (to the status bar)
function end_wait() { 
  document.documentElement.className = ''; 
}

function confirm_delete(url) {
  var answer = confirm("Eintrag wirklich löschen?")
	if (answer) {
		window.location = url;
	}
}

function picup(imgurl) {
  window.open("picup.html?" + imgurl, "", "resizable=1,HEIGHT=200,WIDTH=200");
}

function status_content(content) {
  $('status_content').innerHTML = content;
}
