/* 
 * Die folgenden Funktionen dienen dazu eine andere PHP oder HTML Datei ueber ein Icon in einer Art Popupbox aufzurufen.
 * 
 * Bei mehreren Popupboxen mit veraenderbarem Bild und Titel sollte darauf geachtet werden, dass "img_name" bei jedem Icon einen anderen Wert 
bekommt um zu verhindern, dass das falsche Bild veraendert wird.
 * 
 * simplePopupbox()
 * Wenn das Icon, ueber das die Popupbox aufgerufen wird, nicht veraendert werden soll, kann diese "vereinfachte" Popupbox verwendet werden.
 * 
 * popupbox()
 * Wenn das Icon, ueber das die Popupbox aufgerufen wird, beim Klicken veraendert werden soll, muss die normale Popupbox verwendet werden.
 * 
 * Benoetigte und optionale Uebergabewerte fuer simplePopupbox() und popupbox() s. var Deklarationen der Funktionen.
 * 
 * Script by: www.jtricks.com
 * Version: 20070301
 * Latest version:
 * www.jtricks.com/javascript/window/box_alone.html
 *
 * Changed by Benjamin Reichelt 05.2009, 11.2009
 */

// Declare variables for changing icon
var icon_nr = 0;

// Shows a popupbox if it wasn't shown yet or hides it if it is currently shown
function popupbox() { //Arguments see var below
  //Mandatory Arguments
	var element = arguments[0]; //The element (icon); usally 'this'
	var width = arguments[1]; //Width of the popupbox in px
  var height = arguments[2]; //Height of the popupbox in px
  
  //Optional Arguments
  var cleft = (arguments[3]) ? arguments[3] : 0; //Position of the popupbox in px: left
  var ctop = (arguments[4]) ? arguments[4] : 0; //Position of the popupbox in px: top   
  var img_name = (arguments[5]) ? arguments[5] : ""; //name of img
  var img_folder = (arguments[6]) ? arguments[6] : "../Images/icons_small/"; //Image folder for changing icon
  var img_1 = (arguments[7]) ? arguments[7] : "down_icon.png"; //Image 1 for changing icon
  var img_2 = (arguments[8]) ? arguments[8] : "up_icon.png"; //Image 2 for changing icon
  var title_1 = (arguments[9]) ? arguments[9] : ""; //Title 1 for changing title
  var title_2 = (arguments[10]) ? arguments[10] : ""; //Title 2 for changing title  
  var border_style = (arguments[11]) ? arguments[11] : "2px solid"; //CSS border-style popupbox
  var border_color = (arguments[12]) ? arguments[12] : "midnightblue"; //CSS border-color popupbox
  var background_color = (arguments[13]) ? arguments[13] : "#FFFFFF"; //CSS background-color popupbox	
  var close = (arguments[14]) ? arguments[14] : false; //Close X; true or false
  var hide_other = (arguments[15]) ? arguments[15] : false; //Hide all other Popupboxes; true or false
  var scrollbar = (arguments[16]) ? arguments[16] : true; //Allow scrolling; true or false  
  var text_align = (arguments[17]) ? arguments[17] : "right"; //CSS text-align popupbox
  var padding = (arguments[18]) ? arguments[18] : "4"; //CSS padding popupbox in px 
  var href = element.href;
  var boxdiv = document.getElementById(href);
  
  // Detect Browser
  var arr_browser = new Array();          
  var arr_browser = detectBrowser();
  var browser = arr_browser[0];
  var version = arr_browser[1];
  
  if (browser == 'msie' && version < 7) {  
    // alert("Browser: " + browser + ", Version: " + version);
  } else {
    //Change Icon
    if (img_name != "") {
      changeIcon (img_name, img_folder, img_1, img_2, title_1, title_2);
    }      
  }   
      
  //Show Popupbox and hide others
  if (boxdiv != null) {
      if (boxdiv.style.display=='none') {
        // hide all other boxes
        if (hide_other == true) {
          hide_other_alone(boxdiv);
        }
    
        // Show existing box, move it
        // if document changed layout
        movePopupbox(element, boxdiv, cleft, ctop);
        boxdiv.style.display='block';

        // Workaround for Konqueror/Safari
        if (!boxdiv.contents.contentWindow) {
          boxdiv.contents.src = href;
        }
      }
      else {
        // Hide currently shown box.
        boxdiv.style.display='none';
      }
      return false;
  }

  if (hide_other == true) {
    hide_other_alone(null);
  }

  // Create box object through DOM
  boxdiv = document.createElement('div');

  // Assign id equalling to the document it will show
  boxdiv.setAttribute('id', href);

  // Add object identification variable
  boxdiv.alonePopupBox = 1;

  boxdiv.style.display = 'block';
  boxdiv.style.position = 'absolute';
  boxdiv.style.width = width + 'px';
  boxdiv.style.height = height + 'px';
  boxdiv.style.border = border_style;
  boxdiv.style.borderColor = border_color;
  boxdiv.style.textAlign = text_align;
  boxdiv.style.padding = padding + 'px';
  boxdiv.style.background = background_color;
  document.body.appendChild(boxdiv);

  var offset = 0;

  //X for close
  if (close == true) {
    var close_href = document.createElement('a');
    close_href.href = 'javascript:void(0);';
    close_href.onclick = function(){ 
      popupbox(element, width, height, border_style);
    }
    close_href.appendChild(document.createTextNode('X'));
    boxdiv.appendChild(close_href);
    offset = close_href.offsetHeight;      
  } // End of 'Close' hyperlink code.
  
  var contents = document.createElement('iframe');
  if (scrollbar != true) {
    contents.scrolling = 'no';
    contents.overflowY = 'hidden';
  } else {
    contents.overflowY = 'scroll';
  }
  contents.overflowX = 'hidden';
  contents.frameBorder = '0';
  contents.style.width = width + 'px';
  contents.style.height = (height - offset) + 'px';

  boxdiv.contents = contents;
  boxdiv.appendChild(contents);

  movePopupbox(element, boxdiv, cleft, ctop);

  if (contents.contentWindow)
      contents.contentWindow.document.location.replace(
          href);
  else
      contents.src = href;

  // The script has successfully shown the box,
  // prevent hyperlink navigation.
  return false;
}

// Shows a popupbox if it wasn't shown yet or hides it if it is currently shown
// Without changing picture or title
function simplePopupbox() { //Arguments see var below
  //Mandatory Arguments
	var element = arguments[0]; //The element; usally 'this'
	var width = arguments[1]; //Width of the Popupbox
  var height = arguments[2]; //Height of the Popupbox
  
  //Optional Arguments
  var cleft = (arguments[3]) ? arguments[3] : 0; //Position of the popupbox in px: left
  var ctop = (arguments[4]) ? arguments[4] : 0; //Position of the popupbox in px: top
  var close = (arguments[5]) ? arguments[5] : false; //Close X; true or false
  var border_style = (arguments[6]) ? arguments[6] : "2px solid"; //CSS border-style popupbox
  var border_color = (arguments[7]) ? arguments[7] : "midnightblue"; //CSS border-color popupbox
  var background_color = (arguments[8]) ? arguments[8] : "#FFFFFF";	//CSS background-color popupbox 
  var hide_other = (arguments[9]) ? arguments[9] : false; //Hide all other Popupboxes; true or false
  var scrollbar = (arguments[10]) ? arguments[10] : true; //Allow scrolling; true or false  
  var text_align = (arguments[11]) ? arguments[11] : "right"; //CSS text-align popupbox
  var padding = (arguments[12]) ? arguments[12] : "4"; //CSS padding popupbox in px 
  
  var href = element.href;
  var boxdiv = document.getElementById(href);
    
    //Show Popupbox and hide others
    if (boxdiv != null) {
        if (boxdiv.style.display=='none') {
          // hide all other boxes
          if (hide_other == true) {
            hide_other_alone(boxdiv);
          }
			
          // Show existing box, move it
          // if document changed layout
          movePopupbox(element, boxdiv, cleft, ctop);
          boxdiv.style.display='block';

          // Workaround for Konqueror/Safari
          if (!boxdiv.contents.contentWindow) {
            boxdiv.contents.src = href;
          }
        }
        else {
          // Hide currently shown box.
          boxdiv.style.display='none';
        }
        return false;
    }

    if (hide_other == true) {
      hide_other_alone(null);
    }

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);

    // Add object identification variable
    boxdiv.alonePopupBox = 1;

    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = border_style;
    boxdiv.style.borderColor = border_color;
    boxdiv.style.textAlign = text_align;
    boxdiv.style.padding = padding + 'px';
    boxdiv.style.background = background_color;
    document.body.appendChild(boxdiv);

    var offset = 0;

    //X for close
    if (close == true) {
      var close_href = document.createElement('a');
      close_href.href = 'javascript:void(0);';
      close_href.onclick = function(){ 
        popupbox(element, width, height, border_style);
      }
      close_href.appendChild(document.createTextNode('X'));
      boxdiv.appendChild(close_href);
      offset = close_href.offsetHeight;      
    } // End of 'Close' hyperlink code.
    
    var contents = document.createElement('iframe');
    if (scrollbar != true) {
      contents.scrolling = 'no';
      contents.overflowY = 'hidden';
    } else {
      contents.overflowY = 'scroll';
    }
    contents.overflowX = 'hidden';
    contents.frameBorder = '0';
    contents.style.width = width + 'px';
    contents.style.height = (height - offset) + 'px';

    boxdiv.contents = contents;
    boxdiv.appendChild(contents);

    movePopupbox(element, boxdiv, cleft, ctop);

    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(
            href);
    else
        contents.src = href;

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false; 
}

// Shows a help popupbox if it wasn't shown yet or hides it if it is currently shown
function helpPopupbox() { //Arguments see var below
  //Mandatory Arguments
	var element = arguments[0]; //The element; usally 'this'
	var width = arguments[1]; //Width of the Popupbox
  var height = arguments[2]; //Height of the Popupbox
  
  //Optional Arguments
  var cleft = (arguments[3]) ? arguments[3] : 0; //Position of the popupbox in px: left
  var ctop = (arguments[4]) ? arguments[4] : 0; //Position of the popupbox in px: top
  var close = (arguments[5]) ? arguments[5] : true; //Close X; true or false
  var border_style = (arguments[6]) ? arguments[6] : "2px solid"; //CSS border-style popupbox
  var border_color = (arguments[7]) ? arguments[7] : "#555555"; //CSS border-color popupbox
  var background_color = (arguments[8]) ? arguments[8] : "#EEEEEE";	//CSS background-color popupbox 
  var hide_other = (arguments[9]) ? arguments[9] : false; //Hide all other Popupboxes; true or false
  var scrollbar = (arguments[10]) ? arguments[10] : true; //Allow scrolling; true or false  
  var text_align = (arguments[11]) ? arguments[11] : "right"; //CSS text-align popupbox
  var padding = (arguments[12]) ? arguments[12] : "4"; //CSS padding popupbox in px
 
  var href = element.href;
  var boxdiv = document.getElementById(href);
    
    //Show Popupbox and hide others
    if (boxdiv != null) {
        if (boxdiv.style.display=='none') {
          // hide all other boxes
          if (hide_other == true) {
            hide_other_alone(boxdiv);
          }
			
          // Show existing box, move it
          // if document changed layout
          movePopupbox(element, boxdiv, cleft, ctop);
          boxdiv.style.display='block';

          // Workaround for Konqueror/Safari
          if (!boxdiv.contents.contentWindow) {
            boxdiv.contents.src = href;
          }
        }
        else {
          // Hide currently shown box.
          boxdiv.style.display='none';
        }
        return false;
    }

    if (hide_other == true) {
      hide_other_alone(null);
    }

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);

    // Add object identification variable
    boxdiv.alonePopupBox = 1;

    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = border_style;
    boxdiv.style.borderColor = border_color;
    boxdiv.style.textAlign = text_align;
    boxdiv.style.padding = padding + 'px';
    boxdiv.style.background = background_color;
    document.body.appendChild(boxdiv);

    var offset = 0;

    //X for close
    if (close == true) {
      var close_href = document.createElement('a');
      close_href.href = 'javascript:void(0);';
      close_href.onclick = function(){ 
        popupbox(element, width, height, border_style);
      }
      close_href.appendChild(document.createTextNode('X'));
      boxdiv.appendChild(close_href);
      offset = close_href.offsetHeight;      
    } // End of 'Close' hyperlink code.
    
    var contents = document.createElement('iframe');
    if (scrollbar != true) {
      contents.scrolling = 'no';
      contents.overflowY = 'hidden';
    } else {
      contents.overflowY = 'scroll';
    }
    contents.overflowX = 'hidden';
    contents.frameBorder = '0';
    contents.style.width = width + 'px';
    contents.style.height = (height - offset) + 'px';

    boxdiv.contents = contents;
    boxdiv.appendChild(contents);

    movePopupbox(element, boxdiv, cleft, ctop);

    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(
            href);
    else
        contents.src = href;

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false; 
}


// Shows a information popupbox if it wasn't shown yet or hides it if it is currently shown
function informationPopupbox() { //Arguments see var below
  //Mandatory Arguments
	var element = arguments[0]; //The element; usally 'this'
	var width = arguments[1]; //Width of the Popupbox
  var height = arguments[2]; //Height of the Popupbox
  
  //Optional Arguments
  var cleft = (arguments[3]) ? arguments[3] : 0; //Position of the popupbox in px: left
  var ctop = (arguments[4]) ? arguments[4] : 0; //Position of the popupbox in px: top
  var close = (arguments[5]) ? arguments[5] : false; //Close X; true or false
  var border_style = (arguments[6]) ? arguments[6] : "2px solid"; //CSS border-style popupbox
  var border_color = (arguments[7]) ? arguments[7] : "#555555"; //CSS border-color popupbox
  var background_color = (arguments[8]) ? arguments[8] : "#EEEEEE";	//CSS background-color popupbox 
  var hide_other = (arguments[9]) ? arguments[9] : false; //Hide all other Popupboxes; true or false
  var scrollbar = (arguments[10]) ? arguments[10] : false; //Allow scrolling; true or false  
  var text_align = (arguments[11]) ? arguments[11] : "right"; //CSS text-align popupbox
  var padding = (arguments[12]) ? arguments[12] : "4"; //CSS padding popupbox in px
 
  var href = element.href;
  var boxdiv = document.getElementById(href);
    
    //Show Popupbox and hide others
    if (boxdiv != null) {
        if (boxdiv.style.display=='none') {
          // hide all other boxes
          if (hide_other == true) {
            hide_other_alone(boxdiv);
          }
			
          // Show existing box, move it
          // if document changed layout
          movePopupbox(element, boxdiv, cleft, ctop);
          boxdiv.style.display='block';

          // Workaround for Konqueror/Safari
          if (!boxdiv.contents.contentWindow) {
            boxdiv.contents.src = href;
          }
        }
        else {
          // Hide currently shown box.
          boxdiv.style.display='none';
        }
        return false;
    }

    if (hide_other == true) {
      hide_other_alone(null);
    }

    // Create box object through DOM
    boxdiv = document.createElement('div');

    // Assign id equalling to the document it will show
    boxdiv.setAttribute('id', href);

    // Add object identification variable
    boxdiv.alonePopupBox = 1;

    boxdiv.style.display = 'block';
    boxdiv.style.position = 'absolute';
    boxdiv.style.width = width + 'px';
    boxdiv.style.height = height + 'px';
    boxdiv.style.border = border_style;
    boxdiv.style.borderColor = border_color;
    boxdiv.style.textAlign = text_align;
    boxdiv.style.padding = padding + 'px';
    boxdiv.style.background = background_color;
    document.body.appendChild(boxdiv);

    var offset = 0;

    //X for close
    if (close == true) {
      var close_href = document.createElement('a');
      close_href.href = 'javascript:void(0);';
      close_href.onclick = function(){ 
        popupbox(element, width, height, border_style);
      }
      close_href.appendChild(document.createTextNode('X'));
      boxdiv.appendChild(close_href);
      offset = close_href.offsetHeight;      
    } // End of 'Close' hyperlink code.
    
    var contents = document.createElement('iframe');
    if (scrollbar != true) {
      contents.scrolling = 'no';
      contents.overflowY = 'hidden';
    } else {
      contents.overflowY = 'scroll';
    }
    contents.overflowX = 'hidden';
    contents.frameBorder = '0';
    contents.style.width = width + 'px';
    contents.style.height = (height - offset) + 'px';

    boxdiv.contents = contents;
    boxdiv.appendChild(contents);

    movePopupbox(element, boxdiv, cleft, ctop);

    if (contents.contentWindow)
        contents.contentWindow.document.location.replace(
            href);
    else
        contents.src = href;

    // The script has successfully shown the box,
    // prevent hyperlink navigation.
    return false; 
}


// Hides other alone popup boxes that might be displayed
function hide_other_alone(obj) {
    if (!document.getElementsByTagName) {
      return;
    }
        
    var all_divs = document.body.getElementsByTagName("DIV");

    for (i = 0; i < all_divs.length; i++) {
        if (all_divs.item(i).style.position != 'absolute' ||
            all_divs.item(i) == obj ||
            !all_divs.item(i).alonePopupBox) {
          continue;
        }

        all_divs.item(i).style.display = 'none';
    }
    return;
}

// Moves the popupbox object to be directly beneath an object.
function movePopupbox(element, box, cleft, ctop)
{
    // var cleft = mleft;
    // var ctop = mtop;
    var obj = element;

    while (obj.offsetParent)
    {
        cleft += obj.offsetLeft;
        ctop += obj.offsetTop;
        obj = obj.offsetParent;
    }

    box.style.left = cleft + 'px';

    ctop += element.offsetHeight + 8;

    // Handle Internet Explorer body margins,
    // which affect normal document, but not
    // absolute-positioned stuff.
    if (document.body.currentStyle &&
        document.body.currentStyle['marginTop'])
    {
        ctop += parseInt(
            document.body.currentStyle['marginTop']);
    }

    box.style.top = ctop + 'px';
}

//Change icon & title
function changeIcon(img_name, img_folder, img_1, img_2, title_1, title_2) {
  var dir = img_folder;
  var icons = Array(img_1, img_2);
  var title = Array(title_1, title_2);    
  
  icon_nr++;
  if (icon_nr >= icons.length) {
    icon_nr = 0;
  } else if (icon_nr < 0) {
    icon_nr = 0;
  }
  window.document.images[img_name].src = dir + icons[icon_nr]; //Change icon
  window.document.images[img_name].title = title[icon_nr]; //Change title
}
 
 
//Hide popupbox
function hidePopupbox(element) {
	var href = element.href;
  var boxdiv = document.getElementById(href);
	
  if (boxdiv != null) {
    boxdiv.style.display='none';
  }
}

// Browser detection ::WORKAROUND:: Have to be in an external file
// Credits: w3schools, http://www.javascriptkit.com/javatutors/navigator.shtml

function detectBrowser() {
  if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
    var browser = "msie";
    var version = new Number(RegExp.$1); // capture x.x portion and store as a number   
  } else if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
    var browser = "firefox";
    var version = new Number(RegExp.$1); // capture x.x portion and store as a number
  } else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Opera/x.x or Opera x.x (ignoring remaining decimal places);
    var browser = "opera";
    var version = new Number(RegExp.$1) // capture x.x portion and store as a number
  } else {
    var browser = "unknown";
    var version = "0";
  }
  
  
  var arr_browser = new Array();
  arr_browser[0] = browser;
  arr_browser[1] = version;
  
  return arr_browser;
}
