﻿//declare our window class
function DocstocWindow() 
{
	//create a reference to ourselves to access class variables and objects.  this.[var_name] doesn't work
	var me = this;
	//pointer to our outer window
	var _container = null;
	//pointer to our body div
	var _body = null;
	//pointer to the iframe
	var _iframe = null;
	var _imgPath ='/i/popup/';
	//var _imgPath ='/i/popup/';
	//set up collection of large table settings
	var _largeTableSettings = { imgTop:_imgPath + 'large/T_Main.png',
										imgBottom:_imgPath + 'large/B_Main.png',
										imgRight:_imgPath + 'large/R_Main.png',
										imgLeft:_imgPath + 'large/L_Main.png',
										imgTopLeft:_imgPath + 'large/TL_Main.png',
										imgTopRight:_imgPath + 'large/TR_Main.png',
										imgBottomLeft:_imgPath + 'large/BL_Main.png',
										imgBottomRight:_imgPath + 'large/BR_Main.png',
										imgCloseButton:_imgPath + 'large/btn_close.gif',
										widthTopLeft:'26px',
										widthTopRight:'30px',
										widthLeft:'26px',
										widthRight:'30px',
										widthBottomLeft:'26px',
										widthBottomRight:'30px',
										widthCloseButton:'13px',
										heightTopLeft:'43px',
										heightTop:'43px',
										heightTopRight:'43px',
										heightBottomLeft:'32px',
										heightBottom:'32px',
										heightBottomRight:'32px',
										heightCloseButton:'15px',
										topCloseButton:'10px',
										rightCloseButton:'20px',
										rightToolTip:'34px' };
	//set up collection of small table settings
	var _smallTableSettings = { imgTop:_imgPath + 'small/s_T_main2.png',
										imgBottom:_imgPath + 'small/s_B_main2.png',
										imgRight:_imgPath + 'small/s_R_main2.png',
										imgLeft:_imgPath + 'small/s_L_main2.png',
										imgTopLeft:_imgPath + 'small/s_TL_main2.png',
										imgTopRight:_imgPath + 'small/s_TR_main2.png',
										imgBottomLeft:_imgPath + 'small/s_BL_main2.png',
										imgBottomRight:_imgPath + 'small/s_BR_main2.png',
										imgCloseButton:_imgPath + 'small/btn_close2.gif',
										widthTopLeft:'18px',
										widthTopRight:'19px',
										widthLeft:'18px',
										widthRight:'19px',
										widthBottomLeft:'18px',
										widthBottomRight:'19px',
										widthCloseButton:'20px',
										heightTopLeft:'28px',
										heightTop:'28px',
										heightTopRight:'28px',
										heightBottomLeft:'19px',
										heightBottom:'19px',
										heightBottomRight:'19px',
										heightCloseButton:'20px',
										topCloseButton:'5px',
										rightCloseButton:'5px',
										rightToolTip:'34px' };
	//get browser name and version.  we use the javascript intrinsic navigator object to retrieve this information
	var _browserName = navigator.appName;
	var _browserVer = new String(navigator.appVersion);
	
	// safari, mozilla and ie treat "scrollTop" and "scrollLeft" differently.
	var _bodyScrollLeft = 0;
	var _bodyScrollTop = 0;
	var _bodyScrollWidth = 0;
	var _bodyScrollHeight = 0;
	
	//the true height represents the proper height of this control including the header and footer heights.  
	//the height specified by the user is used to set the height of the body of the window.
	var _trueHeight = '400px';
	//set up click messages
	var _smallClickMessage = 'esc or ';
	var _largeClickMessage = 'press escape or click button to close';
	
	//declare our options
	this.title='New Window'; 
	this.draggable=true; 
	this.width='400px'; 
	this.height='400px'; 
	this.top='20px'; 
	this.left='20px'; 
	this.zindex = '1000';
	this.centered = true;
	this.closeOnEsc = true;
	this.type = 1;	//type defines the, um, type of the popup:  0 - large, 1 - small
	this.formId = null;
	this.clearContentOnHide = true;
	this.preserveContent = false;
	this.scrollBars = false;
	this.instanceName = null;
	this.contentOwnerElement = null;
	this.divId = null;
	this.onClose = function(){}; //gets called when window closes
	this.displayCloseButton = true;
	//initialize our new window.  The divId is the id of the div that should be created as the container
	//of our new window.  Options is a collection of values utilized in the creation of our window
	this.init = function(divId)
	{
		me.divId = divId;

		me.setBodyScrolls();

		//create the div in the current window
		_container = document.createElement('div');
		_container.setAttribute('id', divId);
		_container.setAttribute('style', '');
		
		//set up styles this way cuz of IE. piece of shit doesn't take the setAttribute styles
		_container.style.position = 'absolute';
		_container.style.visibility = 'hidden';
		_container.style.width = me.width;
		_container.style.top = me.top;
		_container.style.left = me.left;
		_container.style.zIndex = me.zindex;
		
		//add the proper table structure with the correct styles to the div
		var table = "";
		
		switch (me.type)
		{
			case 0:
				table = me.getTable(divId, _largeTableSettings);
				break;
			case 1:
				table = me.getTable(divId, _smallTableSettings);
				break;
		}
		
		//set the html of our container
		_container.innerHTML = table;
		
		//if this is IE 6.0, place an iframe behind our div
		if ((_browserName == "Microsoft Internet Explorer") && (_browserVer.indexOf("MSIE 6.0") >= 0))
		{
			//"<iframe src='j avascript:'<html></html>';' style='position:absolute;top:50px;left:50px;width:200px;height:200px;overflow:hidden;border:none' allowtransparency='true'></iframe>";
			//'<iframe id="' + this.element.id + '_iefix" '+ 'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' + 'src="javascript:false;" frameborder="0" scrolling="no"></iframe>'
			
			var myIFrame = document.createElement('iframe');
			
			myIFrame.setAttribute('id', divId + '_iframe');
			myIFrame.setAttribute('src', "javascript:false;");
			myIFrame.setAttribute('frameborder', '0');
			myIFrame.setAttribute('style', '');
			
			myIFrame.style.position = 'absolute';
			myIFrame.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
			myIFrame.style.zIndex = me.zindex - 1;
			myIFrame.style.width = me.width;
			myIFrame.style.height = _trueHeight;
			myIFrame.style.top = me.top;
			myIFrame.style.left = me.left;
			
			//set the body style so the transparency works in ie (fucken hack!)
			//document.body.style.backgroundColor = 'transparent';
			//inject our iframe into the body of the document
			document.body.appendChild(myIFrame);
			
			//get reference to our iframe
			_iframe = document.getElementById(divId + '_iframe');
			_iframe.style.visibility = 'visible';
		}
		
		if (me.formId != null)
		{
			document.getElementById(me.formId).appendChild(_container);
		}
		else
		{	//inject our new div into the body of the document
			document.body.appendChild(_container);
		}
		
		//get our body element
		_body = document.getElementById(divId + '_docWinBody');

		//make our div draggable
		if (me.draggable) Drag.init(_container, null, 
											_bodyScrollLeft, _bodyScrollLeft + _bodyScrollWidth,
											_bodyScrollTop, _bodyScrollTop + _bodyScrollHeight);
		//center our div
		if (me.centered) me.center();
		//register for escape key capture
		if (me.closeOnEsc) me.registerAsKeyListener();
	},	//end init

	this.getTable = function(divId, settings) {
		//set true height to be used later
		_trueHeight = (parseInt(me.height) + parseInt(settings.heightTop) + parseInt(settings.heightBottom) + 10) + 'px';

		//get body width
		var bodyWidth = (parseInt(me.width) - parseInt(settings.widthLeft) - parseInt(settings.widthRight)) + 'px';

		//set up the cursor for the header
		var cursorStyle = '';
		if (me.draggable) cursorStyle = 'cursor:move;';

		//set up click message
		var clickMessage = '';
		if (parseInt(me.width) > 200) {
			clickMessage = _largeClickMessage;
		}
		else {
			clickMessage = _smallClickMessage;
		}



		//set up close button behavior
		var closeOnMouseOver = "javascript:document.getElementById(\"" + divId + "_docWinToolTip\").style.visibility = \"visible\";";
		var closeOnMouseOut = "javascript:document.getElementById(\"" + divId + "_docWinToolTip\").style.visibility = \"hidden\";";
		var closeOnClick = "document.getElementById(\"" + divId + "\").style.visibility = \"hidden\";";
		if (me.instanceName != null) closeOnClick = 'javascript:' + me.instanceName + '.hide();';

		closeTxt = 'Close';

		if (this.displayCloseButton == false) {
			settings.imgCloseButton = "";
			closeOnMouseOver = "";
			closeOnMouseOut = "";
			closeTxt = "";
		}

		var table = "<table border='0' cellpadding='0' cellspacing='0' style='width:" + me.width + ";'>" +
							"<tr>" +
								"<td style='width:" + settings.widthTopLeft + "; height: " + settings.heightTopLeft + "; " +
										"background:transparent " + me.getImageStyleString(settings.imgTopLeft) + "; " + cursorStyle + "'></td>" +
								"<td style='background:transparent " + me.getImageStyleString(settings.imgTop) + "; " +
										"height:" + settings.heightTop + "; " + cursorStyle + "' valign='bottom' align='center'>" +
									me.title +
									"<div id='" + divId + "_docWinToolTip' style='display:none; position:absolute; visibility:hidden; " +
											"top:" + settings.topCloseButton + "; right:" + settings.rightToolTip + "; background:transparent; color:#FFFFFF; '>" +
									clickMessage + "</div>" +
									"<div style='font-size:12px;padding:3px 37px 0 0;-padding-right:23px; font-weight:bold;position:absolute; top:" + settings.topCloseButton + "; right:" + settings.rightCloseButton + "; " +
											"width:" + settings.widthCloseButton + "; height:" + settings.heightCloseButton + "; " +
											"background-image:url(" + settings.imgCloseButton + "); background-position:right top; background-repeat: no-repeat; cursor:default;' " +
											"onclick='" + closeOnClick + "' " +
											"onmouseover='" + closeOnMouseOver + "' " +
											"onmouseout='" + closeOnMouseOut +
											"'>" + closeTxt + "</div>" +
								"</td>" +
								"<td style='width:" + settings.widthTopRight + "; height: " + settings.heightTopRight + "; " +
										"background:transparent " + me.getImageStyleString(settings.imgTopRight) + "; " + cursorStyle + "'></td>" +
							"</tr>" +
							"<tr>" +
								"<td style='width:" + settings.widthLeft + "; " +
										"background: transparent repeat-y " + me.getImageStyleString(settings.imgLeft) + ";'></td>" +
								"<td style='background-color:#FFFFFF;'>" +
									"<div id='" + divId + "_docWinBody' style='height:" + me.height + ";width:" + bodyWidth + "; background:#d5d5d5'></div>" +
								"</td>" +
								"<td style='width:" + settings.widthRight + "; " +
										"background:transparent repeat-y top right " + me.getImageStyleString(settings.imgRight) + ";'></td>" +
							"</tr>" +
							"<tr>" +
								"<td style='width:" + settings.widthBottomLeft + "; height: " + settings.heightBottomLeft + "; " +
										"background:transparent " + me.getImageStyleString(settings.imgBottomLeft) + ";'></td>" +
								"<td style='background:transparent " + me.getImageStyleString(settings.imgBottom) + "; " +
										"height:" + settings.heightBottom + ";' valign='bottom'></td>" +
								"<td style='width:" + settings.widthBottomRight + "; height: " + settings.heightBottomRight + "; " +
										"background:transparent " + me.getImageStyleString(settings.imgBottomRight) + ";'></td>" +
							"</tr>" +
						"</table>";
		return table;
	},
	
	this.getImageStyleString = function(imgSrc)
	{
		//return variable
		var imgStyleString = "";
		
		//ie 6 and 7 both return version 4.0 as the initial version in the variable but then say the full version of the browser later in the string.
		if ((_browserName == "Microsoft Internet Explorer") && (_browserVer.indexOf("MSIE 6.0") >= 0))
		{	//if ie6, return a filter that would load the png correctly
			imgStyleString = '; filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + imgSrc + '", sizingMethod="scale")';
		}
		else
		{	//if else, just return a url string
			imgStyleString = "url(" + imgSrc + ")";
		}
		
		return imgStyleString;
	},
	
	//remove the div from the page
	this.destroy = function()
	{
		document.body.removeChild(_container);
		if (_iframe != null) document.body.removeChild(_iframe);
	},
	
	this.registerAsKeyListener = function()
	{
		if (document.attachEvent)
		{
			document.attachEvent('onkeyup', me.onKeyPress);
		}
		else
		{
			document.addEventListener('keyup', me.onKeyPress, true);
		}
	},
	
	this.onKeyPress = function(e)
	{
		var keyPressed;
		
		if (window.event) // IE
		{
			keyPressed = event.keyCode;
		}
		else if(e.which) // Netscape/Firefox/Opera
		{
			keyPressed = e.which;
		}
		
		if (keyPressed == 27)
		{	//escape key was pressed
			me.hide();
		}
	},
	
	//set content
	this.setContent = function(content)
	{
		//fix for object not showing up properly under IE
		if (content.substr(0, 7) == "<OBJECT") content = "<div style='height:0px;width:0px;'>&nbsp;</div>" + content;
		_body.innerHTML = content;
	},
	//set the location where content was grabbed from
	this.setContentOwner = function(el)
	{
	    me.contentOwnerElement = el;
	},	
	
	//return the content
	this.getContent = function()
	{
		return _body.innerHTML;
	},
	
	//set url
	this.setUrl = function(url)
	{
		var scrollText = "scrolling='auto'";
		if (!me.scrollBars) scrollText = "scrolling='no'";
		
		var iframe = "<iframe width='100%' height='100%' src='" + url + "' border='0' frameborder='0' " + scrollText + "></iframe>";
		_body.innerHTML = iframe;
	},
	
	//set ajax content
	this.setAjaxContent = function(action, url, data)
	{
		//declare object
		var oAjax = new AjaxObject101();
		
		//set response type
		oAjax.returnXml = false;
		
		//set method delegates
		//oAjax.funcWait = Working;
		oAjax.funcDone = me.OnAjaxReturn;
		
		oAjax.sndReq(action, url, data);
	},
	
	this.OnAjaxReturn = function(data)
	{
		me.setContent(data);
	},
	
	//show our window
	this.show = function()
	{
		me.setBodyScrolls();
		if (me.centered) me.center();
		_container.style.visibility = 'visible';
		if (_iframe != null) _iframe.style.visibility = 'visible';


	},	//end show
	
	//hide our window
	this.hide = function()
	{
		_container.style.visibility = 'hidden';
		document.getElementById(me.divId + "_docWinToolTip").style.visibility = "hidden";
		if (_iframe != null) _iframe.style.visibility = 'hidden';
		if (me.preserveContent && me.contentOwnerElement!=null) {me.contentOwnerElement.innerHTML = me.getContent();}
		if (me.clearContentOnHide) me.setContent('');
		me.onClose();
	},	//end hide
	
	this.center = function()
	{
		_container.style.left = "50%";
		_container.style.top = "50%";

		var newLeft = _container.offsetLeft;
		//take into account the scrolling of the browser
		newLeft = (newLeft - _container.offsetWidth / 2) + _bodyScrollLeft;
		//make sure we're not in the negatives
		if (newLeft <= 0) newLeft = 1;
		//make sure we're in the scrollable area
		if (newLeft < _bodyScrollLeft) newLeft = _bodyScrollLeft;
		//set up the string
		newLeft = parseInt(newLeft);
		newLeft = newLeft.toString() + 'px';

		//set left
		_container.style.left = newLeft;
		if (_iframe != null) _iframe.style.left = newLeft;
		
		var newTop = _container.offsetTop;
		//take into account the scrolling of the browser
		newTop = (newTop - _container.offsetHeight / 2) + _bodyScrollTop;
		//check negatives
		if (newTop <= 0) newTop = 1;
		//make sure we're in the scrollable area
		if (newTop < _bodyScrollTop) newTop = _bodyScrollTop;
		//set up string
		newTop = parseInt(newTop);
		newTop = newTop.toString() + 'px';
		
		//set top
		_container.style.top = newTop;
		if (_iframe != null) _iframe.style.top = newTop;
	
	},
	
	this.setLocation = function(top, left)
	{
		_container.style.left = left;
		_container.style.top = top;
	},
	
	//position denotes on which side of the element this div should line up.  accepted values are top, bottom, left, right
	this.setLocationByElement = function(elementId, position)
	{
	
		//get the absolute top and left of the passed in document
		var top = elementId.offsetTop;
		var left = elementId.offsetLeft;
		var offsetParent = elementId.offsetParent;
		
		while (offsetParent !=null && offsetParent.tagName != 'BODY')
		{
			top += offsetParent.offsetTop;
			left += offsetParent.offsetLeft;
			
			offsetParent = offsetParent.offsetParent;
		}
		
		//take into account the user desired position
		switch (position)
		{
			case 'top':
				//left remains the same.  top should be decremented by the height of our control
				top -= parseInt(me.height);
				
				break;
			case 'bottom':
				//left remains the same.  top should be incremented by the height of the client control
				top += elementId.offsetHeight;
				
				break;
			case 'left':

				//top remains the same.  left should be decremented by the width of our control
				left -=parseInt(me.width);
				break;
			case 'right':
				//top remains the same. left should be incremented by the width of the client control
				left += elementId.offsetWidth;
				
				break;
		}
		
		//set the location
		me.setLocation(top + 'px', left + 'px');
	},
	
			
	this.setBodyScrolls = function()
	{
	    
		// safari, mozilla and ie treat "scrollTop" and "scrollLeft" differently.
		if(_browserVer.indexOf("Safari") != -1)
		{
			_bodyScrollLeft	= document.body.scrollLeft;
			_bodyScrollTop		= document.body.scrollTop;
			_bodyScrollWidth	= document.body.scrollWidth;
			_bodyScrollHeight	= document.body.scrollHeight;
			return;
		}
		_bodyScrollLeft	= document.documentElement.scrollLeft;
		_bodyScrollTop		= document.documentElement.scrollTop;
		_bodyScrollWidth	= document.documentElement.scrollWidth;
		_bodyScrollHeight	= document.documentElement.scrollHeight;

		return;
	};
		
	
	/**************************************************
	* dom-drag.js
	* 09.25.2001
	* www.youngpup.net
	* Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
	**************************************************
	* 10.28.2001 - fixed minor bug where events
	* sometimes fired off the handle, not the root.
	**************************************************/
	var Drag = {
		obj: null,

		init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
			o.onmousedown = Drag.start;
			o.hmode = bSwapHorzRef ? false : true;
			o.vmode = bSwapVertRef ? false : true;
			o.root = oRoot && oRoot != null ? oRoot : o;

			if (o.hmode && isNaN(parseInt(o.root.style.left))) o.root.style.left = "0px";
			if (o.vmode && isNaN(parseInt(o.root.style.top))) o.root.style.top = "0px";
			if (!o.hmode && isNaN(parseInt(o.root.style.right))) o.root.style.right = "0px";
			if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

			o.minX = typeof minX != 'undefined' ? minX : null;
			o.minY = typeof minY != 'undefined' ? minY : null;
			o.maxX = typeof maxX != 'undefined' ? maxX : null;
			o.maxY = typeof maxY != 'undefined' ? maxY : null;
			o.xMapper = fXMapper ? fXMapper : null;
			o.yMapper = fYMapper ? fYMapper : null;
			o.root.onDragStart = new Function();
			o.root.onDragEnd = new Function();
			o.root.onDrag = new Function();
		},

		start: function(e) {
			var o = Drag.obj = this;

			e = Drag.fixE(e);

			var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
			var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);

			o.root.onDragStart(x, y);
			o.lastMouseX = e.clientX;
			o.lastMouseY = e.clientY;

			if (o.hmode) {
				if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
				if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
			}
			else {
				if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
				if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
			}

			if (o.vmode) {
				if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
				if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
			}
			else {
				if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
				if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
			}

			document.onmousemove = Drag.drag;
			document.onmouseup = Drag.end;

			return false;
		},

		drag: function(e) {
			e = Drag.fixE(e);

			var o = Drag.obj;
			var ey = e.clientY;
			var ex = e.clientX;
			var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
			var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
			var nx, ny;

			if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
			if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
			if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
			if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

			nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
			ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

			if (o.xMapper) {
				nx = o.xMapper(y);
			} else if (o.yMapper) {
				ny = o.yMapper(x);
			}

			Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
			Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
			Drag.obj.lastMouseX = ex;
			Drag.obj.lastMouseY = ey;
			Drag.obj.root.onDrag(nx, ny);

			return false;
		},

		end: function() {
			document.onmousemove = null;
			document.onmouseup = null;

			Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
											 parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
			Drag.obj = null;
		},

		fixE: function(e) {
			if (typeof e == 'undefined') e = window.event;
			if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
			if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;

			return e;
		}
	};   	//end drag class
	

}	//end class
