/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

function PopupDialog() {
  this.element = null;
  this.classPrefix = "popupDialog";
  this.top = 0;
  this.left = 0;
  this.title = "No Title";
  this.content = "No Content";
  this.width = null;  
  this.shown = false;
  this.contentBox = null;
  this.innerBox = null;
  this.headerBox = null;
  
  this.showPopup = function() {
    if (this.element == null) {
      this.element = document.createElement("div");
      this.element.className = this.classPrefix + "Element";
      var header = document.createElement("div");
      header.className = this.classPrefix + "Header";
      this.element.appendChild(header);
      var infoBox = document.createElement("span");
      infoBox.className = this.classPrefix + "InfoBox";
      header.appendChild(infoBox);
      var closeBox = document.createElement("a");
      closeBox.className = this.classPrefix + "CloseBox";
      var _self = this;
      closeBox.onclick = function() {
        _self.element.style.display = "none";
        _self.element.parentNode.removeChild(_self.element);
        _self.shown = false;
      }
      header.appendChild(closeBox);
      this.headerBox = document.createElement("div");
      this.headerBox.className = this.classPrefix + "HeaderBox";
      header.appendChild(this.headerBox);
      this.contentBox = document.createElement("div");
      this.contentBox.className = this.classPrefix + "ContentBox";
      this.element.appendChild(this.contentBox);
      this.innerBox = document.createElement("div");
      this.contentBox.appendChild(this.innerBox);
    }
    this.innerBox.innerHTML = this.content;
    this.headerBox.innerHTML = this.title;
    var scrollY = window.scrollY;
    if (!scrollY) {
      scrollY = document.documentElement.scrollTop;
    }
    this.element.style.top = (this.top + scrollY) + "px";
  	style = null;
  	if (document.defaultView) {
  	  style = document.defaultView.getComputedStyle(this.element, "");
  	}
  	else {
  	  style = this.element.currentStyle;  	  
  	}  
  	if (style != null) {
  	  var w = parseInt(style.width);
  	}
  	else {
  	  w = 250;
  	}  
  	if (this.left + w > window.innerWidth) {
      this.element.style.right = "5px";
  	}
  	else {
      var scrollX = window.scrollX;
      if (!scrollX) {
        scrollX = document.documentElement.scrollTop;
      }
      this.element.style.left = (this.left + scrollX) + "px";
  	}  
    var width = this.width;
    if (width != null) {
      width += "px";
    }
    this.element.style.width = width;
    this.element.style.display = "block";
    document.getElementsByTagName("body")[0].appendChild(this.element);
    this.shown = true;
  }
  
  this.closePopup = function() {
    this.element.style.display = "none";
    this.element.parentNode.removeChild(this.element);
    this.shown = false;    
  }
}

function parseHelpPopups(image, caption, defaultTitle, elementName, classPrefix) {
  if (elementName == null) {
    elementName = "HelpPopup";
  }
  if (defaultTitle == null) {
    defaultTitle = "Help";
  }
  var elements = document.getElementsByName(elementName);
  if (myBrowser.strictElementsByName) {
    var elements = new Array();
    var temp = document.getElementsByTagName("span");
    for (var i = 0; i < temp.length; i++) {
    	if (temp[i].getAttribute("name") == elementName) {
    	  
    	  elements[elements.length] = temp[i];
    	}
    }
    var temp = document.getElementsByTagName("div");
    for (var i = 0; i < temp.length; i++) {
    	if (temp[i].getAttribute("name") == elementName) {
    	  
    	  elements[elements.length] = temp[i];
    	}
    }
  }
  
  for (var i = 0; i < elements.length; i++) {
  	var element = elements[i];
  	var popup = new PopupDialog();
  	popup.content = element.innerHTML;
  	if (classPrefix != null) {
  	  popup.classPrefix = classPrefix;
  	}
  	popup.title = defaultTitle;
  	if (element.title != null && element.title.length > 0) {
  	  popup.title = element.title;
  	}
  	element.helpPopup = popup;
  	element.innerHTML = "";
  	var button = null;
  	if (image != null) {
  	  button = document.createElement("img");
  	  button.src = image;
  	  button.className = popup.classPrefix + "ButtonImage";
  	}
  	else {
  	  button = document.createElement("button");
  	  if (caption == null) {
  	    caption = "?";
  	  }
  	  button.innerHTML = caption;
  	}  
  	button.helpPopup = popup;
  	button.onclick = function(event) {
  	  if (window.event) {
  	    event = window.event;
  	  }  	  
  	  var popup = this.helpPopup;
  	  if (popup.shown) {
  	    popup.closePopup();
  	  }
  	  else {
  	    popup.left = event.clientX;
  	    popup.top = event.clientY;
  	    popup.showPopup()
  	  }  
  	};
  	element.appendChild(button);
  }
}


