/**
* 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(caption,mypage,w,h,gui){
    //set defaults
    w = w ? w : 200;
    h = h ? h : 50;

	//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 = 2;
	    popwindiv.style.display = 'none';
		popwindiv.style.border = '2px outset #666';
		popwindiv.style.backgroundColor = '#FFF';
		
        document.getElementsByTagName('body')[0].appendChild(popwindiv);

		//add gui
		if(gui !== false){
			popwingui = document.createElement('DIV');
			popwingui.id = 'popwingui';
			popwingui.style.textAlign = 'left';
			popwingui.style.backgroundColor = '#0D276C';
			popwingui.style.color = '#FFF';
			popwingui.style.padding = '2px 2px 2px 5px';
			popwingui.style.fontSize = '14px';
			popwingui.style.fontFamily = 'Tahoma';
			popwingui.style.fontWeight = 'bold';
			popwingui.style.height = '20px';
			popwingui.innerHTML = '<span style="float:left;">' + caption + "</span><a href=\"javascript:void(0);\" onclick=\"javascript:document.getElementById('popwindiv').style.display='none';\" style=\"border: 2px outset rgb(255, 255, 255); margin: 3px 0px 0px; padding: 0px 2px; font-size: 10px; font-family: verdana; background-color: rgb(212, 208, 200); color: rgb(0, 0, 0); text-align: center; text-decoration: none;float:right;\">X</a>";
			popwindiv.appendChild(popwingui);
		}

	    //add iframe
	    popwiniframe = document.createElement('IFRAME');
		popwiniframe.id = 'popwiniframe';
		popwiniframe.name = 'popwin';
		popwiniframe.scrolling = 'no';
	
		popwiniframe.style.width = w + "px";
		popwiniframe.style.height = h + "px";
		
		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');
		if (popwingui)
			popwingui.innerHTML = '<span style="float:left;">' + caption + "</span><a href=\"javascript:void(0);\" onclick=\"javascript:document.getElementById('popwindiv').style.display='none';\" style=\"border: 2px outset rgb(255, 255, 255); margin: 3px 0px 0px; padding: 0px 2px; font-size: 10px; font-family: verdana; background-color: rgb(212, 208, 200); color: rgb(0, 0, 0); text-align: center; text-decoration: none;float:right;\">X</a>";
	    popwiniframe = document.getElementById('popwiniframe');
    }

    //resize element...
    popwindiv.style.width = w + 'px';
	popwiniframe.style.width = w + 'px';
	if (popwingui) {
	    popwindiv.style.height = (h + parseInt(popwingui.offsetHeight)) + 'px';
		popwiniframe.style.height = (h + parseInt(popwingui.offsetHeight)) + 'px';
	} else {
	    popwindiv.style.height = (h) + 'px';
		popwiniframe.style.height = (h) + 'px';
	}
	//popwiniframe.style.height = h + 'px';

    //position the popup
    if(document.all){
		//patch position for MSIE
		popwindiv.style.top = _lastPopWindowTop = (document.documentElement.scrollTop + 10) + '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 = (document.documentElement.scrollTop + 10) + '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.display = 'block';
}

function guiHTML(title) {
		return '<span>' +
				title + 
				'</span><a href="#"' +
				 'onclick=\"javascript:document.getElementById(\'popwindiv\').style.display=\'none\';">X</a>';
}

/**
* _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;
}

