/*
 * General and specific DHTML/Ajax functions 
 */

// create a cross-browser XMLHTTPRequest
function createXMLHttpRequest() {
    try { return new ActiveXObject('Msxml2.XMLHTTP');    } catch(e) {}
    try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
    try { return new XMLHttpRequest();                   } catch(e) {}
    alert('XMLHttpRequest not supported');
    return null;
}
// fetch one module content asynchronously and fill it
function fetchPanel(panel,request){
    var ajaxhttp = createXMLHttpRequest();
    ajaxhttp.open('get', request);
    ajaxhttp.onreadystatechange = function(){ // async callback
      if (ajaxhttp.readyState == 4) {
        fillPanel(panel,ajaxhttp.responseText);
      }
    }
    ajaxhttp.send(null);
    return false;
}
// fill the container 'panel' with content
function fillPanel(panel,content){
  var wa=document.getElementById(panel);
  wa.innerHTML = content;
}

/**
 * Toggle the visibility of minimize/restore buttons
 * @param {} container The surrounding container
 */
function toggleModuleButtonsVisibility(container){
    // find the container wrapping the button
    var links=container.getElementsByTagName('div');	
    for (i=links.length-2; i<links.length; i++){
    	if ((links[i].className.indexOf('mod_minimize')>=0) ||
    	    (links[i].className.indexOf('mod_restore' )>=0)) 
    	{
    	   if (links[i].style.display=='block')
    	       links[i].style.display='none';
    	   else
    	       links[i].style.display='block';
    	}
    }
}
/*
 * Set module title from inside the module
 */
function setModuleTitle(containerID,title){
    // find parent container of the param container
    var childcontainer = document.getElementById(containerID);
    if (!childcontainer)
    	alert("No childcontainer found: "+containerID)
    var small_module_frame = childcontainer.parentNode;
    // find all divs in the parent
    var divs = small_module_frame.getElementsByTagName('div');
    // find the title div and set its content
    for (i=0; i<divs.length; i++){
        if (divs[i].className.indexOf("small_module_title") >=0 ){
            divs[i].innerHTML = title;
            break;
        }
    }
}

/*
 * Find the module container
 */
function getContentContainer(container){
    // get parent container two levels up from the button
    // which should be the module frame
    var headerContainer = container.parentNode;
    var moduleContainer= headerContainer.parentNode;
    // find all divs in the module frame
    var divs = moduleContainer.getElementsByTagName('div');
    // search the content div
    for (i=0; i<divs.length; i++){
        if (divs[i].className.indexOf("module_content")>=0){
            return divs[i];
        } else {
        	//alert(divs[i].className);
        }
    }
    // not found, return null
    return null;
}

/*
 * Called from the buttons in module_header
 * Hide or show the 'module_content' container inside
 * the module and swapping the visibility of the 
 * minimize/restore buttons
 */
function toggleModuleFromButtons(buttonDiv){
    // find content container 
    var contentContainer = getContentContainer(buttonDiv);
    // get header container
    var headerContainer = buttonDiv.parentNode;
    if (contentContainer){
        if (contentContainer.style.display != 'none'){
            contentContainer.style.display = 'none';
            //setModuleButtonsVisibility(headerContainer,'mod_restore');
        } else {
            contentContainer.style.display = 'block';
            //setModuleButtonsVisibility(headerContainer,'mod_minimize');            
        }
        toggleModuleButtonsVisibility(headerContainer);
    }
}

/*
 * General show or hide module
 */
function showHideModule(id,elementClicked){
  var el=document.getElementById(id);
  var ajaxhttpM=createXMLHttpRequest();
  var visvalue=0;
  var container = elementClicked.parentNode;
  if (el.style.display != 'none'){
    el.style.display='none';
    visvalue = 0;
    updateModuleState(container,'mod_restore');
  }else{
    el.style.display='inline';
    visvalue = 1;
    updateModuleState(container,'mod_minimize');
  }
  
  //ajaxhttpM.open('get','modvis.php?m='+id+'&v='+visvalue);
  ajaxhttpM.onreadystatechange = function(){
      if (ajaxhttpM.readyState == 4) {
           //
      }
    }
  //	ajaxhttpM.send(null);
  return false;
}
