/**
* dhtmlPopWindow v0.1
*	By: me@daantje.nl
*	Mon Jun 26 14:33:04 CEST 2006
*
*	DESCRIPTION:
*		Wrote this script, cause I needed something to fool the popup killers.
*		This script pops a DHTML div with an iframe with the given page.
*		The popped window appears always in the center of the window/frame.
*		But it's draggable across the window/frame. All the layout can be done
*		with a CSS style sheet. This is what I needed till now, but this script
*		could be somewhat better. Check the todo list...
*
*	DONATE:
*		When you like my script, please do a big or smal PayPal donation to me@daantje.nl
*
*	SUPPORT:
*		Check http://www.daantje.nl
*
*	BUGS AND PATCHES:
*		Please mail me any bugs and/or patches!
*
*	TODO LIST:
*		When you fiel the need to help...
*		- Add scale window funtionality.
*		- Make close button configurable.
*		- Add configurable window placement.
*		- Add configurable window target name.
*		- Add multiple window support.
*
*	LICENSE:
*		This script is GPL. (http://www.gnu.org/copyleft/gpl.txt)
*/

//init globals (DON'T TOUCH!)
_movePopWindowTrigger = false;
_popWindowOffsetX = 0;
_popWindowOffsetY = 0;
_lastPopWindowLeft = 0;
_lastPopWindowTop = 0;


/**
* popWindow(STRING mypage [, INT w] [, INT h] [,BOOL gui]);
*	pop a navigator window with my own specs
*		mypage	: STRING	URL to display
*		w		: INT		window width in pixels
*		h		: INT		window heigth in pixels
*		gui		: BOOL		show window title bar, default true
*/
function popWindow(mypage,w,h,gui){
    //set defaults
    w = w ? w : 200;
    h = h ? h : 150;

	//get or create element...
    if(!document.getElementById('popwindiv')){
        //create div
	    popwindiv = document.createElement('DIV');
	    popwindiv.id = 'popwindiv';
    	popwindiv.style.position = document.all ? 'absolute' : 'fixed'; //fixed position for MSIE is patched!
	    popwindiv.style.zIndex = 1000000;
	    popwindiv.style.visibility = 'hidden';
        popwindiv.style.backgroundColor = '#FFFFFF';
        document.getElementsByTagName('body')[0].appendChild(popwindiv);

		//add gui
		if(gui !== false){
			popwingui = document.createElement('DIV');
			popwingui.id = 'popwingui';
			popwingui.style.textAlign = 'right';
			popwingui.innerHTML = "<a href=\"javascript:void(0);\" onclick=\"javascript:document.getElementById('popwindiv').style.visibility='hidden'; pg.hide();\" style=\"text-decoration:none;color:#dddddd;font-size:10px;font-family:arial;\"><img src=\"images/close.jpg\" border=\"0\"></a>";
			popwindiv.appendChild(popwingui);
		}

	    //add iframe
	    popwiniframe = document.createElement('IFRAME');
		popwiniframe.id = 'popwiniframe';
		popwiniframe.name = 'popwin';
		popwiniframe.frameBorder = 0;
		if(document.all){
		    popwiniframe.onmouseover = function(){_movePopWindowTrigger=false;};
		}else{
			popwiniframe.setAttribute('onmouseover','_movePopWindowTrigger=false;');
		}
		popwindiv.appendChild(popwiniframe);
    }else{
    	//get elements
	    popwindiv = document.getElementById('popwindiv');
	    popwingui = document.getElementById('popwingui');
	    popwiniframe = document.getElementById('popwiniframe');
    }

	//when mypage is an imgage file, kill margins. (this doesn't seem to work on Mozilla based browsers??)
	ext = mypage.substring(mypage.lastIndexOf('.') + 1);
	if(ext == 'gif' || ext == 'png' || ext == 'jpg' || ext == 'jpeg'){
		popwiniframe.setAttribute('marginWidth','0');
		popwiniframe.setAttribute('marginHeight','0');
	}else{
		popwiniframe.setAttribute('marginWidth','10');
		popwiniframe.setAttribute('marginHeight','10');
	}

    //resize element...
    popwindiv.style.width = w + 'px';
    popwindiv.style.height = (h + parseInt(popwingui.offsetHeight)) + 'px';
	popwiniframe.style.width = w + 'px';
	popwiniframe.style.height = h + 'px';

    //position the popup
    if(document.all){
		//patch position for MSIE
		popwindiv.style.top = _lastPopWindowTop = (parseInt((document.body.clientHeight / 2) - (parseInt(popwindiv.style.height) / 2)) + document.body.scrollTop) + 'px';
		popwindiv.style.left = _lastPopWindowLeft = (parseInt((document.body.clientWidth / 2) - (parseInt(popwindiv.style.width) / 2)) + document.body.scrollLeft) + 'px';
    	//emulate fixed position.
        window.onscroll = window.onresize = _MSIEpositionPopWindow;
    }else{
		//set position other browsers
		popwindiv.style.top = ((window.innerHeight / 2) - (h / 2)) + 'px';
		popwindiv.style.left = ((window.innerWidth / 2) - (w / 2)) + 'px';
    }

	//get page
	popwiniframe.src = mypage;

	//add drag and drop functionality.
	document.onmousedown = _movePopWindow;
	document.onmouseup = new Function('_movePopWindowTrigger=false;');

	//show popup
	popwindiv.style.visibility = '';
}

/**
* _MSIEpositionPopWindow()
*	nasty patch for fixed position for MSIE
*	(This can be done better, only not without a CSS style. And because I want this script to be fool proof...)
*/
function _MSIEpositionPopWindow(){
	popwindiv = document.getElementById('popwindiv');
	popwindiv.style.top = (parseInt(_lastPopWindowTop) + document.body.scrollTop) + 'px';
	popwindiv.style.left = (parseInt(_lastPopWindowLeft) + document.body.scrollLeft) + 'px';
}

/**
* _movePopWindow()
*	Init the drag of the popup window...
*/
function _movePopWindow(e){
	bar = document.all ? event.srcElement.id : e.target.id;
	if(bar == 'popwingui'){
		//get start mouse position
		_popWindowOffsetX = document.all ? event.clientX : e.clientX;
		_popWindowOffsetY = document.all ? event.clientY : e.clientY;

		//get window position
		popwindiv = document.getElementById('popwindiv');
		tempx = parseInt(popwindiv.style.left);
		tempy = parseInt(popwindiv.style.top);

		//do movement
		_movePopWindowTrigger = true;
		document.onmousemove = _dragPopWindow;
	}
}

/**
* _dragPopWindow()
*	Do movement of window....
*/
function _dragPopWindow(e){
	if(_movePopWindowTrigger == true){
		//push to new position
		popwindiv = document.getElementById('popwindiv');
		if(document.all){
			popwindiv.style.left = _lastPopWindowLeft = (tempx + (event.clientX - _popWindowOffsetX)) + 'px';
			popwindiv.style.top = _lastPopWindowTop = (tempy + (event.clientY - _popWindowOffsetY)) + 'px';
		}else{
			popwindiv.style.left = _lastPopWindowLeft = (tempx + (e.clientX - _popWindowOffsetX)) + 'px';
			popwindiv.style.top = _lastPopWindowTop = (tempy + (e.clientY - _popWindowOffsetY)) + 'px';
		}
	}
	return false;
}



/**
* Grey Screen
*/

window.onload = function()
{
};
window.onunload = function() // assigning an unload listener disables bfcache (fastback) in Opera and Firefox
{
};
var pg=null;
xAddEventListener(window, 'load',
  function() {
    if (document.getElementById && document.createElement && document.body.appendChild) {
      pg = new xPageGrey('clsPageGreyDiv', '', 'clsPageGreyImg', '', 'clsPageGreyMsg');
      xGetElementById('demoBtn').onclick = function() {
        pg.show();
              };
      xGetElementById('demoForm').onsubmit = function() {
        pg.show();
        return true;
      };
    }
  },
  false
);
function xPageGrey(sDivClass, sImgUrl, sImgClass, sMsg, sMsgClass)
{
  /*@cc_on
  @if (@_jscript_version < 5.5) // opacity not supported in IE until v5.5
  this.ele = null;
  @else @*/
  this.ele = document.createElement('div');
  this.ele.className = sDivClass;
  if (sImgUrl) {
    var img = document.createElement('img');
    img.src = sImgUrl;
    img.className = sImgClass;
    this.msg = document.createElement('p');
    this.msg.className = sMsgClass;
    this.msg.appendChild(img);
    this.msg.appendChild(document.createTextNode(sMsg));
    document.body.appendChild(this.msg);
  }
  document.body.appendChild(this.ele);
  /*@end @*/
  this.show = function()
  {
    if (this.ele) {
      var ds = xDocSize();
      xMoveTo(this.ele, 0, 0);
      xResizeTo(this.ele, ds.w, ds.h);
      if (this.msg) {
        xMoveTo(this.msg, xScrollLeft()+(xClientWidth()-xWidth(this.msg))/2, xScrollTop()+(xClientHeight()-xHeight(this.msg))/2);
      }
    }
  };
  this.hide = function()
  {
    if (this.ele) {
      xResizeTo(this.ele, 10, 10);
      xMoveTo(this.ele, -10, -10);
      if (this.msg) {
        xMoveTo(this.msg, -xWidth(this.msg), 0);
      }
    }
  };
}

/* Compiled from X 4.09 with XC 1.02 on 02Mar07 */
function xAddEventListener(e,eT,eL,cap)
{
  if(!(e=xGetElementById(e)))return;
  eT=eT.toLowerCase();
  if(e.addEventListener)e.addEventListener(eT,eL,cap||false);
  else if(e.attachEvent)e.attachEvent('on'+eT,eL);
  else e['on'+eT]=eL;
}
function xClientHeight()
{
  var v=0,d=document,w=window;
  if(d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientHeight)
    {v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
    {v=d.body.clientHeight;}
  else if(xDef(w.innerWidth,w.innerHeight,d.width)) {
    v=w.innerHeight;
    if(d.width>w.innerWidth) v-=16;
  }
  return v;
}
function xClientWidth()
{
  var v=0,d=document,w=window;
  if(d.compatMode == 'CSS1Compat' && !w.opera && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientWidth;}
  else if(d.body && d.body.clientWidth)
    {v=d.body.clientWidth;}
  else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerWidth;
    if(d.height>w.innerHeight) v-=16;
  }
  return v;
}
function xDef()
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}
function xDocSize()
{
  var b=document.body, e=document.documentElement;
  var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
  if (e) {
    esw = e.scrollWidth;
    eow = e.offsetWidth;
    esh = e.scrollHeight;
    eoh = e.offsetHeight;
  }
  if (b) {
    bsw = b.scrollWidth;
    bow = b.offsetWidth;
    bsh = b.scrollHeight;
    boh = b.offsetHeight;
  }
  return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
}
function xGetComputedStyle(oEle, sProp, bInt)
{
  var s, p = 'undefined';
  var dv = document.defaultView;
  if(dv && dv.getComputedStyle){
    s = dv.getComputedStyle(oEle,'');
    if (s) p = s.getPropertyValue(sProp);
  }
  else if(oEle.currentStyle) {
    var i, c, a = sProp.split('-');
    sProp = a[0];
    for (i=1; i<a.length; ++i) {
      c = a[i].charAt(0);
      sProp += a[i].replace(c, c.toUpperCase());
    }
    p = oEle.currentStyle[sProp];
  }
  else return null;
  return bInt ? (parseInt(p) || 0) : p;
}
function xGetElementById(e)
{
  if(typeof(e)=='string') {
    if(document.getElementById) e=document.getElementById(e);
    else if(document.all) e=document.all[e];
    else e=null;
  }
  return e;
}
function xHeight(e,h)
{
  if(!(e=xGetElementById(e))) return 0;
  if (xNum(h)) {
    if (h<0) h = 0;
    else h=Math.round(h);
  }
  else h=-1;
  var css=xDef(e.style);
  if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    h = xClientHeight();
  }
  else if(css && xDef(e.offsetHeight) && xStr(e.style.height)) {
    if(h>=0) {
      var pt=0,pb=0,bt=0,bb=0;
      if (document.compatMode=='CSS1Compat') {
        var gcs = xGetComputedStyle;
        pt=gcs(e,'padding-top',1);
        if (pt !== null) {
          pb=gcs(e,'padding-bottom',1);
          bt=gcs(e,'border-top-width',1);
          bb=gcs(e,'border-bottom-width',1);
        }
        else if(xDef(e.offsetHeight,e.style.height)){
          e.style.height=h+'px';
          pt=e.offsetHeight-h;
        }
      }
      h-=(pt+pb+bt+bb);
      if(isNaN(h)||h<0) return;
      else e.style.height=h+'px';
    }
    h=e.offsetHeight;
  }
  else if(css && xDef(e.style.pixelHeight)) {
    if(h>=0) e.style.pixelHeight=h;
    h=e.style.pixelHeight;
  }
  return h;
}
function xLeft(e, iX)
{
  if(!(e=xGetElementById(e))) return 0;
  var css=xDef(e.style);
  if (css && xStr(e.style.left)) {
    if(xNum(iX)) e.style.left=iX+'px';
    else {
      iX=parseInt(e.style.left);
      if(isNaN(iX)) iX=xGetComputedStyle(e,'left',1);
      if(isNaN(iX)) iX=0;
    }
  }
  else if(css && xDef(e.style.pixelLeft)) {
    if(xNum(iX)) e.style.pixelLeft=iX;
    else iX=e.style.pixelLeft;
  }
  return iX;
}
xLibrary={version:'4.09',license:'GNU LGPL',url:'http://cross-browser.com/'};
function xMoveTo(e,x,y)
{
  xLeft(e,x);
  xTop(e,y);
}
function xNum()
{
  for(var i=0; i<arguments.length; ++i){if(isNaN(arguments[i]) || typeof(arguments[i])!='number') return false;}
  return true;
}
function xResizeTo(e,w,h)
{
  xWidth(e,w);
  xHeight(e,h);
}
function xScrollLeft(e, bWin)
{
  var offset=0;
  if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && e) w = e;
    if(w.document.documentElement && w.document.documentElement.scrollLeft) offset=w.document.documentElement.scrollLeft;
    else if(w.document.body && xDef(w.document.body.scrollLeft)) offset=w.document.body.scrollLeft;
  }
  else {
    e = xGetElementById(e);
    if (e && xNum(e.scrollLeft)) offset = e.scrollLeft;
  }
  return offset;
}
function xScrollTop(e, bWin)
{
  var offset=0;
  if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && e) w = e;
    if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
    else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
  }
  else {
    e = xGetElementById(e);
    if (e && xNum(e.scrollTop)) offset = e.scrollTop;
  }
  return offset;
}
function xStr(s)
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])!='string') return false;}
  return true;
}
function xTop(e, iY)
{
  if(!(e=xGetElementById(e))) return 0;
  var css=xDef(e.style);
  if(css && xStr(e.style.top)) {
    if(xNum(iY)) e.style.top=iY+'px';
    else {
      iY=parseInt(e.style.top);
      if(isNaN(iY)) iY=xGetComputedStyle(e,'top',1);
      if(isNaN(iY)) iY=0;
    }
  }
  else if(css && xDef(e.style.pixelTop)) {
    if(xNum(iY)) e.style.pixelTop=iY;
    else iY=e.style.pixelTop;
  }
  return iY;
}
function xWidth(e,w)
{
  if(!(e=xGetElementById(e))) return 0;
  if (xNum(w)) {
    if (w<0) w = 0;
    else w=Math.round(w);
  }
  else w=-1;
  var css=xDef(e.style);
  if (e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    w = xClientWidth();
  }
  else if(css && xDef(e.offsetWidth) && xStr(e.style.width)) {
    if(w>=0) {
      var pl=0,pr=0,bl=0,br=0;
      if (document.compatMode=='CSS1Compat') {
        var gcs = xGetComputedStyle;
        pl=gcs(e,'padding-left',1);
        if (pl !== null) {
          pr=gcs(e,'padding-right',1);
          bl=gcs(e,'border-left-width',1);
          br=gcs(e,'border-right-width',1);
        }
        else if(xDef(e.offsetWidth,e.style.width)){
          e.style.width=w+'px';
          pl=e.offsetWidth-w;
        }
      }
      w-=(pl+pr+bl+br);
      if(isNaN(w)||w<0) return;
      else e.style.width=w+'px';
    }
    w=e.offsetWidth;
  }
  else if(css && xDef(e.style.pixelWidth)) {
    if(w>=0) e.style.pixelWidth=w;
    w=e.style.pixelWidth;
  }
  return w;
}

