var inhalt = window;

// helper func: get the document height
function documentHeight() {
	var frameWidth;
	var frameHeight;
	if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}

        if (inhalt.document.body.scrollHeight < frameHeight) {
        	return frameHeight;
        } else {
        	return inhalt.document.body.scrollHeight;
        }

	return inhalt.document.body.scrollHeight;
	
	/*
  var docHeight;
  if (typeof inhalt.document.height != 'undefined') {
    docHeight = inhalt.document.height;
  } else if (inhalt.document.compatMode && inhalt.document.compatMode != 'BackCompat') {
    docHeight = inhalt.document.documentElement.scrollHeight;
  } else if (inhalt.document.body && typeof inhalt.document.body.scrollHeight != 'undefined') {
    docHeight = inhalt.document.body.scrollHeight;
  }

  return docHeight;
  */
}

function documentScrollPos() {
	var scrollbarY;
  if (window.pageYOffset){
    scrollbarY=window.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){
    scrollbarY=document.documentElement.scrollTop;
  } else if (document.body){
    scrollbarY=document.body.scrollTop;
  }
  //alert('ScrollBarY:'+scrollbarY);
  return scrollbarY;
}


// msgBlobup: represents one kind of a window (f.e.warnings or infos)
function classMsgBlobup( uid) {
  
  if (!uid) {
    uid = 'blobupv2';
  }
  this.uid = uid;
  this.acontent = '';
  
  this.refresh = function( pageOnLoad) {
    if (pageOnLoad && this.bdiv) { delete this.bdiv };
    if (this.hasContent()) {
        if (! this.bdiv) {
          this.createBlobupElement();
        }
        this.show();
    } else {
      if (this.bdiv) {
        this.hide();
      }
    }
  };
  
  this.createBlobupElement = function() {
    if (!inhalt || !inhalt.document) {
      // seems we are to early, our frame is not yet available
      return;
    }
    var theBody = inhalt.document.getElementsByTagName('BODY')[0];
    if (!theBody) {
      // still to early, no body available
      return;
    }
    this.bdiv = inhalt.document.createElement( 'div');
    this.bdiv.id = uid;
    theBody.appendChild( this.bdiv);

    // create the background div
    this.backdiv = inhalt.document.createElement( 'div');
    this.backdiv.id = uid+'_back';
    theBody.appendChild( this.backdiv);
  };
  
  this.showLoading = function() {
    if (! this.bdiv) {
          this.createBlobupElement();
    }

    if (this.bdiv) {
      this.bdiv.innerHTML = '<div align="center"><div style="width:110px; height:110px; position:relative;"><div style="position:absolute; top:0px; left:0px; z-index:1;"><img src="/0_image/blobup-load-bg.png" width="110" height="110"></div><div style="position:absolute; top:15px; left:15px; z-index:2;"><img src="/0_image/blobup-load-fg.gif" width="80" height="80"></div></div>';
      this.bdiv.style.display = 'block';
      this.bdiv.style.visibility = 'visible';
      this.bdiv.style.top = documentScrollPos() + 'px';
    }
    if (this.backdiv) {
      this.backdiv.innerHTML = '&nbsp;';
      this.backdiv.style.height = documentHeight() + 'px';
      this.backdiv.style.display = 'block';
      this.backdiv.style.visibility = 'visible';
    }
  };

  this.show = function() {
    if (this.bdiv) {
      this.bdiv.innerHTML = this.getContent();
      this.bdiv.style.display = 'block';
      this.bdiv.style.visibility = 'visible';
      this.bdiv.style.top = documentScrollPos() + 'px';
    }
    if (this.backdiv) {
      this.backdiv.innerHTML = '&nbsp;';
      this.backdiv.style.height = documentHeight() + 'px';
      this.backdiv.style.display = 'block';
      this.backdiv.style.visibility = 'visible';
    }
  };
  
  this.hide = function() {
    if (this.bdiv) {
      this.bdiv.style.display = 'none';
      this.bdiv.style.visibility = 'hidden';
    }
    if (this.backdiv) {
      this.backdiv.style.display = 'none';
      this.backdiv.style.visibility = 'hidden';
    }
  };
  
  this.hasContent = function() {
    return (this.acontent != '');
  };
  
  this.getContent = function() {
    return this.acontent;
  };

  this.setContent = function( content) {
    this.acontent = content;
    this.refresh();
  };
  
  this.confirm = function() {
    this.acontent = '';
    this.refresh();
  };
  
  this.createIframe = function() {
    //this.setContent( "{#replace|{#js_escape|str|{#datei_laden|{#path_javascript#}msgIframe.htm#}#}|^|"+|°|+"#}");
  };

};

var msgBlobup = new classMsgBlobup( 'blobupv2');

function classMsgOrder(uid) {
  this.uid = uid;
  
  this.getElement = function() {
    if (!inhalt || !inhalt.document) {
      // seems we are to early, our frame is not yet available
      return;
    }
    var el = inhalt.document.getElementById( this.uid);
    return el;
  };

  this.show = function() {
    var el = this.getElement();
    if (el) { 
      el.style.top = '100px'; // TODO: make it position in the visible area
    }
  };
  
  this.hide = function() {
    var el = this.getElement();
    if (el) { 
      el.style.top = '-551px'; // TODO: make it position in the visible area
      msgBlobup.confirm();
    }
  };

}

var msgOrder =  new classMsgOrder( 'orderblobup');

function classMsgInfos( msgBlobup){

  this.msgBlobup = msgBlobup;
  this.msgs = new Object();
  
  this.refresh = function() {
    if (this.hasContent()) {
      this.msgBlobup.setContent( this.getHTML());
    }
  };
  
  this.hasContent = function() {
    return this.getCount() > 0;
  };
  
  this.getContent = function() {
    var t = '';
    for (uid in this.msgs) {      
      var msg = this.msgs[uid];
      t = t + msg.getContent();
    }
    return t;
  };

  this.add = function( msg)  {    
    //{#if|{#?|JS_DEBUG#}|DbgLog( 'Added msg "'+msg.uid+'": '+msg.text);#}
    this.msgs[msg.uid] = msg;
    this.refresh();
  };
  
  this.getCount = function()  {   
    var c = 0;
    for (msg in this.msgs) {      
      c++;
    }
    return c;
  };
  
  this.remove = function( uid) {    
    if (delete this.msgs[uid]) {
      this.refresh();
    }
  };

  this.removeGroup = function( gid) {
    var nmsgs = new Object();
    var changed = 0;
    
    for (uid in this.msgs) {      
      var msg = this.msgs[uid];
      if (msg.gid != gid) {
        nmsgs[uid] = msg;
      } else {
        changed++;
      }
    }
    if (changed > 0) {
      this.msgs = nmsgs;
      this.refresh();
    }
  };
  
  this.getHTML = function() {
    var blobupContent = this.getContent();
    //return"<div align=\"center\">\n	<div class=\"blobup color3\">\n	<div class=\"head color1\">Information</div>\n	\n	<div>\n	<div class=\"color8\">\n	<div class=\"color3\">"+blobupContent+"\n	<div class=\"btn\" align=\"center\"><a href=\"javascript:parent.msgInfos.confirm();\" class=\"no\"><img src=\"/0_banner/btn-ok.gif\"></a></div>\n	<div class=\"pizza-de\" align=\"right\"><span class=\"color3\" style=\"padding:0px 3px 0px 3px;\">pizza.de</span></div>\n	</div>\n	</div>\n	</div>\n	\n	</div> \n</div>\n";
    return blobupContent;
    //return "{#replace|{#js_escape|str|{#datei_laden|{#path_javascript#}msgInfo.htm#}#}|^|"+|°|+"#}";
  };
  
  this.confirm = function() {
    this.msgs = new Object();
    this.msgBlobup.confirm();
  };

}

var msgInfos = new classMsgInfos( msgBlobup);


// uid - unique id of the message
// gid - group id, messages of same group gets combined
function Msg( uid, gid, title, text) {
  
  this.getContent = function() {
    return '<h1>' + this.title + '</h1>' + this.text;
  };
  
  this.uid = uid;
  this.gid = gid;
  if (!text) {
    this.title = 'Hinweis';
    this.text = title;
  } else {
    this.title = title;
    this.text = text;
  }
  
};


  msgBlobup.loaded = function( xml, content, obj) {
    msgBlobup.acontent = processServerResponse( xml, content);
    msgBlobup.refresh();	
  };
  
  msgBlobup.loadUrl = function( URL) {
   if (!this.loader) {
      this.loader = new HTMLHttpRequest( 'msgBlobup.loader', msgBlobup.loaded);		
   }
   this.loader.load( URL);
   //this.showLoading();
  };

  msgBlobup.submit = function( formRef, event) {
   if (!this.loader) {
      this.loader = new HTMLHttpRequest( 'msgBlobup.loader', msgBlobup.loaded);		
   }
   var res=this.loader.submit( formRef, event);
   //if (!res) { this.showLoading(); }
   return res;
  };
