	/* NOTE: This file REQUIRES the presence of Globals.js */	
	/**** General PopUp functions / data ***/	    
    __popup_count              = 0;
	function __registerNewPopUp() { var id  = 'popup_'+ (++__popup_count); return id; }
	__window_instances = []; //this object keeps track of all windows created for interval-polling reasons

	var __eventLoopTime = 250;
	function __processIntCB() { $.each(__window_instances,function(i,win) {if( win ){ win.IntervalListener(); } }); }
	setInterval("__processIntCB()",__eventLoopTime);
		
	function __hideAllPopUps() { $('.ui_puw').fadeOut('fast'); }	
	/** end general popup functions / data ***/
	
	var zIndexCounter = 200; //start above other page elements
	function PopUpWindow() {
		/* constructor for a flexible PopUpWindow */
		this.id = __registerNewPopUp();
		__window_instances[__window_instances.length] = this;

		this.display_id = 'display-'+this.id;		
		this.auto_hide = false;
		this.visible = false;
		this.mode = 'div'; /* either DIV or New Window */
		this.url = ''; /* having a URL will cause an Ajax load in DIV mode, or popup a new window in window mode */
		this.content = ''; /* this.content will be displayed inside of a DIV style window */
		this.width = 300;
		this.height = 300;
		this.x = ( ($(window).width()-this.width)/2); /* the horizontal position; left->right */
		this.y = ( ($(window).height()-this.height)/2); /* the vertical position top->bottom */
		this.rendered = false;
		this.background_color = 'white';
		this.font_color = 'black';
		this.auto_center = false;
		this.eventLoopTime = 250; //how often to poll for changes
		this.load_content = false;
		this.draggable = false;
		this.max_size = false;
		this.margin = '0px';
		this.padding = '0px';
		this.closeable = true;
		this.zIndex = ++zIndexCounter;
		this.border = 2;
		this.borderStyle = 'solid';
		this.borderColor = '#63657B';
		this.title = '';
		this.modal = false;
		this.modal_id = '';
	}

	PopUpWindow.prototype.containerStyles = [];
	PopUpWindow.prototype.SetContainerStyle = function( Key, Value) { this.containerStyles[this.containerStyles.length] = {'key': Key, 'value': Value }; }
	//if you want to do work inside the intervalListener attach a function to this
	PopUpWindow.prototype.IntervalCallback = null; 
	PopUpWindow.prototype.SetTitle = function(t) { this.title = t; }	
	PopUpWindow.prototype.SetCloseable = function(c) { this.closeable = c; }	
    PopUpWindow.prototype.SetMaxSize = function(m) { this.max_size = m; }    
    PopUpWindow.prototype.CenterOnScreen = function(){ /* the horizontal position; left->right */ if( isNaN(this.width) && this.width.indexOf('%') > 0 ) { this.x = ( ( 100 - parseInt(this.width) )/2);  this.x += '%'; } else 	this.x = ( ($(window).width()-parseInt(this.width))/2); /* the vertical position top->bottom */ if( isNaN(this.height) && this.height.indexOf('%') > 0 ) { this.y = ( ( 100 - parseInt(this.height) )/2);  this.y += '%'; } else 	this.y = ( ($(window).height()-parseInt(this.height))/2); this.Refresh(); }
	PopUpWindow.prototype.SetContent = function( c ){ this.content = c;  if(this.rendered) this.Refresh(); };
	PopUpWindow.prototype.StartListening = function() { /* set up the listeners */  };
	PopUpWindow.prototype.StopListening = function() {   }
	/* general methods */
	PopUpWindow.prototype.SetProperties	= function(p) { /* this lets you pass in a URL-style string of arguments that get added to the window's properties.  */ if ( p ) { var Pairs = p.split(/[;&]/); for ( var i = 0; i < Pairs.length; i++ ) { var KeyVal = Pairs[i].split('='); if ( ! KeyVal || KeyVal.length != 2 ) continue; var key = unescape( KeyVal[0] ); var val = unescape( KeyVal[1] ); val = val.replace(/\+/g, ' '); this[key] = val; } } this.Refresh();	 };	
	PopUpWindow.prototype.EventLoopTime	= function(t){  /* unused */  };
	PopUpWindow.prototype.AutoCenter = function(a){ this.auto_center = a;  };
	
	PopUpWindow.prototype.Refresh = function(){

		if( !this.rendered ) return;		
		var _this = this;

		var hPf = 'px';
		var wPf = 'px';
		if(this.auto_center ) { this.draggable = false; }

		if(this.max_size )
		{
			$('#'+this.display_id).css({ 'left': 0, 'top': 0, 'width': '100%', 'height': '100%' });
            hPf = '%'; wPf = '%';
		} else
		{
			var _w = ( (this.closeable || this.draggable ) && this.width < 120 ? 120 : this.width); ///when the grab/close bar is active this shouldn't go below 120px
			$('#'+this.display_id).css({ 'left': this.x, 'top': this.y, 'width': _w, 'height': this.height })
		}
		$('#'+this.display_id).css({ 'backgroundColor': this.background_color, 'zIndex': this.zIndex, 'color': this.font_color, 'margin': this.margin+'px', 'padding':this.padding+'px', 'backgroundColor':this.background_color, 'border': this.border, 'borderColor': this.borderColor });
		if( this.border > 0 ) { $('#'+this.display_id).css('borderStyle','solid'); }
		$('#'+this.display_id).html(' ');

		if( this.draggable  || this.closeable  )
		{			
			var width = ( (this.closeable || this.draggable ) && this.width < 120 ? 120 : this.width);
			var closelink = ""; var grablink ="";
			if(this.draggable ) {	 
				grablink = "<div class='ui_puw_grabDiv' id='DragHandle_"+this.id+"'><img src='/new_images/PopUpWindow/PUW_DragBar.gif' width='50' height='29' border='0'/></div>"; 
			}
			if(this.closeable ) {  closelink = "<div class='ui_puw_closeDiv' ><img src='/new_images/PopUpWindow/PUW_CloseButton.gif' style='border: 0;margin-top: 5px; margin-right: 5px;' id='CloseButton_"+this.id+"'  /></div>";  }			
			$('#'+this.display_id).append("<div style='"+(this.draggable ? "cursor: move;" : "")+" ' class='ui_puw_titleBarDiv' id='TitleBar_"+this.id+"'>"+grablink+" "+closelink+"</div>");
			if( this.draggable ){ $('#'+this.display_id).draggable( { delay: 10, handle: "#TitleBar_"+this.id }); }						
			$("#CloseButton_"+this.id).click( function(){  _this.Hide(); });
		}
		if( this.title != "" ) { $('#'+this.display_id).append("<h3 style='font-size: 14px; margin: 2px;font-family: Trebuchet, Verdana'>"+this.title+"</h3>"); }

		var _contentHeight = this.height;
		if( !$.browser.msie )  { _contentHeight = parseInt(this.height)-30; }

		var containerStyleString = "";
		if( this.containerStyles.length > 0 )
		{
			for( var i =0;i<this.containerStyles.length;i++)
			{
				containerStyleString += this.containerStyles[i].key+": " + this.containerStyles[i].value + "; ";
			}
		}		
	
		$('#'+this.display_id).append("<div id='div_"+this.id+"_content' class='ui_puw_contentDiv' style='"+containerStyleString+"width:"+( $.browser.msie ? parseInt(this.width)-5 : this.width)+"; height: "+(_contentHeight)+";'></div>");

		if( this.url != "" )
		{
            try
            {
				if( this.JSONHandler != null )
				{ 
					this.JSONData.target_id = 'div_'+this.id+'_content'; $.getJSON(this.url,this.JSONData, this.JSONHandler );
				} else { 
					$('#div_'+this.id+'_content').load(this.url); 
				}
            } catch(e){ __debug('http error: '+e); }			
		} else { $('#div_'+this.id+'_content').html( this.content );
		}	
	};
	
	PopUpWindow.prototype.JSONData = {};
	PopUpWindow.prototype.SetJSONData = function(Data) { this.JSONData = Data; }	
	PopUpWindow.prototype.JSONHandler = null;
	PopUpWindow.prototype.SetJSONHandler = function(Handler) { this.JSONHandler = Handler; }
	PopUpWindow.prototype.Destroy = function() { $.each(__window_instances,function(i,val){ if( val == this ){ __window_instances[i] = null; }}); document.body.removeChild( __getElement(this.display_id) ); }
	
	PopUpWindow.prototype.Render = function() {	
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id',this.display_id);				
		newDiv.className = 'ui_puw';
		newDiv.style.position = 'absolute';
		newDiv.style.display = 'none';
		
		document.body.appendChild(newDiv);
		
		if(this.modal) { newDiv = document.createElement('div'); this.modal_id = this.id+'_modal'; newDiv.setAttribute('id',this.modal_id); newDiv.style.display = 'none'; document.body.appendChild(newDiv); $('#'+ this.modal_id).css({ position: 'absolute', left: 0, top: 0, zIndex: this.zIndex-1, width: $(window).width(), height: $(window).height(), backgroundColor: 'White', opacity: .65, MozOpacity: .65 }); }
		this.rendered = true;
		this.StartListening(); this.Refresh(); this.Show();
	};
	PopUpWindow.prototype.SetPositionRelativeToObject	= function (id,x,y) { /* this get the relative positions of an object on a page */ var objectPosition = __findPosById(id); var newX = objectPosition[0]+x; var newY = objectPosition[1]+y; this.SetPosition(newX,newY); };
	PopUpWindow.prototype.Move = function(dX,dY) { this.SetPosition( this.x+dX,this.y+dY ); };	
	PopUpWindow.prototype.SetPosition = function(x,y) { this.x = x; this.y = y; if( this.rendered ) { $('#'+this.display_id).css({ 'left': this.x, 'top': this.y}); } };	
	/* last_url allows us to determine if we are going to refresh the entire  page load or just properties */
	PopUpWindow.prototype.SetURL = function( u ) {  this.url = u;  };
	PopUpWindow.prototype.SetUrl = function( u ) { this.SetURL(u); };
	PopUpWindow.prototype.SetMode = function(m) { this.mode = m; };
	PopUpWindow.prototype.Hide = function() { 
		$('#'+this.display_id).fadeOut('fast');
		 this.visible = false; 
		if( this.modal ){  $('#'+this.modal_id).fadeOut('fast');  $('body').css('overflow','auto'); }
	};
	PopUpWindow.prototype.Show = function() { 
		if( !this.rendered ) { this.Render(); } 
		if( this.modal ){  $('#'+this.modal_id).css( 'top', $(window).scrollTop() ); $( '#'+this.modal_id).fadeIn("fast"); $('body').css( 'overflow','hidden');}
		$('#'+this.display_id).fadeIn('fast'); this.visible = true; 
	};
	PopUpWindow.prototype.Toggle = function() { if( this.visible ){ this.Hide(); } else { this.Show(); } };

    /* we need to wrap the internal methods in functions that reference _this so the global call can reach down into the instance */
	PopUpWindow.prototype.IntervalListener = function() {
		if( this.auto_center && this.rendered ) { var newX = 0; var newY = 0; newX = Math.round( ($(window).width()/2)-($('#'+this.display_id).width()/2)+$(document).scrollLeft() ); newY = Math.round( ($(window).height()/2)-($('#'+this.display_id).height()/2)+$(document).scrollTop() ); this.SetPosition( newX, newY); }	
		if( this.IntervalCallback != null ) this.IntervalCallback();
		
		return true;
	};