function popthing(popID,shadowID,objName){
	// default values
	var _self = this;
	this.shadowID = shadowID;
	this.popID = popID;	
	this.objName = objName;
	var shadowOpacity = 75;
	var shadowOpacityMax = 75;
	var popOpacity = 100;
	var scrollX_old = 0;
	var scrollY_old = 0;	
	var progressImg = new Image();
	progressImg.src="/graphics/loading/ajax-loader.gif";	
	var progressState = 0;	
	var timeOutVal = 60000;
	var popOffset = [0, 0];
	var flashPlayer = "/media/swf/player-320x180.swf";		
	var allowToggle = true;
	var highestZ = 0;
	var dragging = false;
	var oldmouseX = 0;
	var oldmouseY = 0;	
	var oldMouseMove = null; // used to store a documents current onmove event while popup is being dragged	
	_self.oldParent = null; // used with loadDiv to return innerhtml after use
	
	this.show = show;
	this.hide = hide;
	this.togglePop = togglePop;	
	this.loadDiv = loadDiv;
	this.loadImg = loadImg;
	this.loadSwf = loadSwf;
	this.loadFLV = loadFLV;
	this.loadMov = loadMov;
	this.ajaxLoad = ajaxLoad;	
	this.addPopOffset = addPopOffset;
	this.init = init;
	this.setOpacity = setOpacity;
	this.setupPop = setupPop;
	this.setCss = setCss;	
	this.addHtml = addHtml;
	this.toggleEnabled = toggleEnabled;
	this.showProgress = showProgress;	
	this.center = centerPopup;
	this.clear = clearPopup;	
		
	function show() {				
		window.onscroll = preventScroll;		
		var xy = getScrollXY();
		scrollX_old = xy[0];
		scrollY_old = xy[1];
		
		var div = document.getElementById(popID);
		var shadow = document.getElementById(shadowID);	
		if (!highestZ) {
			highestZ = getNextHighestZindex('div');
		}
		shadow.style.zIndex = highestZ;
		div.style.zIndex = highestZ + 1;
		
		div.style.display = 'block';
		shadow.style.display = 'block';		
		centerDiv(popID);
		centerDiv(shadowID);
		addPopOffset();	
	}
	
	function hide() {		
		if(!progressState) {		
			var div = document.getElementById(popID);
			var shadow = document.getElementById(shadowID);	
			div.style.display = 'none';
			shadow.style.display = 'none';
			window.onscroll = null;
			shadow.style.zIndex = div.style.zIndex = 0;
			if(_self.oldParent!=null) {
				document.getElementById(_self.oldParent).innerHTML = div.innerHTML;
				div.innerHTML = null;
				_self.oldParent = null;
			}
			_self.clear();
		}		
	}
	
	function ajaxLoad(url) {
		showProgress(1);
		xmlhttp = false; // ajax cache
		returnContent = null;
		//Clear our fetching variable
		xmlhttp = false;
		//Try to create active x object
		try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
		} 
		catch (e) {
			try {
				xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
			} 
			catch (E) {
				xmlhttp = false;
			}
		}
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			xmlhttp = new XMLHttpRequest();
		}
				
		xmlhttp.open('GET', url, true);
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 1) {
				
				//Check if it is ready to recieve data 
				}
				else 
					if (xmlhttp.readyState == 4) {
						//Make sure there is something in the content variable
						returnContent = xmlhttp.responseText;
						ajaxLoadContent(returnContent);
					}
			}
		xmlhttp.send(null);
				
		return;
		
	}
	
	function ajaxLoadContent(content) {
		showProgress(0);
		var div = document.getElementById(popID);
		div.innerHTML = "<div style='padding:10px;margin:0 auto'>"+content+"</div>";
		show();
	}
		
	function centerPopup() {
		centerDiv(popID);
	}		
	
	function getNextHighestZindex(obj){
		var highestIndex = 0;
		var currentIndex = 0;
		var elArray = Array();
		if (obj) {
			elArray = document.getElementsByTagName(obj);
		}
		else {
			elArray = document.getElementsByTagName('*');
		}
		for (var i = 0; i < elArray.length; i++) {
			if (elArray[i].currentStyle) {
				currentIndex = parseFloat(elArray[i].currentStyle['zIndex']);
			}
			else 
				if (window.getComputedStyle) {
					currentIndex = parseFloat(document.defaultView.getComputedStyle(elArray[i], null).getPropertyValue('z-index'));
				}
			if (!isNaN(currentIndex) && currentIndex > highestIndex) {
				highestIndex = currentIndex;
			}
		}
		return (highestIndex + 1);
	}
	
	function toggleEnabled(bool) {
		allowToggle = bool;
	}
	
	function addHtml(html) {
		document.getElementById(popID).innerHTML += html; 
	}
	
	function setStyle(objId, style, value) {
    	document.getElementById(objId).style[style] = value;
	}

	
	function getHeight(){
		var d = document.getElementById(popID);
		var divHeight;
		if (d.offsetHeight) {
			divHeight = d.offsetHeight;
		}
		else 
			if (d.style.pixelHeight) {
				divHeight = d.style.pixelHeight;
			}
		
		return divHeight;
	}
	
	function getWidth(){
		var d = document.getElementById(popID);
		var divWidth;
		if (d.offsetWidth) {
			divWidth = d.offsetWidth;
		}
		else 
			if (d.style.pixelWidth) {
				divWidth = d.style.pixelWidth;
			}
		
		return divWidth;
	}
	
	function getWindowHeight(){
		var windowHeight = 0;
		
		if (typeof(window.innerHeight) == 'number') {
			windowHeight = window.innerHeight;
		}
		
		else {
			if (document.documentElement && document.documentElement.clientHeight) {
				windowHeight = document.documentElement.clientHeight;
			}
			else {
				if (document.body && document.body.clientHeight) {
					windowHeight = document.body.clientHeight;
				}
			}
		}
		return windowHeight;
	}
	
	function getWindowWidth (){
		var windowWidth = 0;
		
		if (typeof(window.innerWidth) == 'number') {
			windowWidth = window.innerWidth;
		}
		
		else {
			if (document.documentElement && document.documentElement.clientWidth) {
				windowWidth = document.documentElement.clientWidth;
			}
			else {
				if (document.body && document.body.clientWidth) {
					windowWidth = document.body.clientWidth;
				}
			}
		}
		return windowWidth;
	}
	
	
	function centerDiv (id){
		var div = document.getElementById(id);
		var top = (getWindowHeight() - div.offsetHeight) / 2;
		if (id == shadowID) {
			var left = 0;
		}
		else 
			var left = (getWindowWidth() - div.offsetWidth) / 2; 
		var xy = getScrollXY();
		
		div.style.left = left + xy[0] + "px";
		div.style.top = top + xy[1] + "px";
	}	
	
	function getScrollXY (){
		var scrOfX = 0, scrOfY = 0;
		if (typeof(window.pageYOffset) == 'number') {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		}
		else 
			if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
				//DOM compliant
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			}
			else 
				if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
					//IE6 standards compliant mode
					scrOfY = document.documentElement.scrollTop;
					scrOfX = document.documentElement.scrollLeft;
				}
		return [scrOfX, scrOfY];
	}
	
	// prevents the user from scrolling while the popup is visible
	function preventScroll (){ 
	//	var h = getHeight() - getWindowHeight();
	//	if(h<0)
			window.scrollTo(scrollX_old, scrollY_old);
	}
	
	// fades in and out depending on what the global var 'fadeType' has been set to	
	
	//sets the opacity of the popup and shadow using the global opacity vars (popupOpacity & shadowOpacity)
	function setOpacity (){
		var div1 = document.getElementById(popID);
		var div2 = document.getElementById(shadowID);
		div1.style.opacity = popOpacity / 100;
		div1.style.filter = 'alpha(opacity=' + popOpacity + ')';
		div2.style.opacity = shadowOpacity / 100;
		div2.style.display = 'block'; // shouldn't really be here but couldn't get it to work otherwise
		div2.style.filter = 'alpha(opacity=' + shadowOpacity + ')';
		if ((div1.style.opacity == 0) || (div1.style.filter == "alpha(opacity=0)") || (div1.style.filter == "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)")) {
			div1.style.display = div2.style.display = 'none';
			div1.style.zIndex = div2.style.zIndex = 0;			
			clear();			
			window.onscroll = null;
		}
		
	}
	
	// hides the popup and removes all its content
	function clearPopup() {
		var div = document.getElementById(popID);
		var shadow = document.getElementById(shadowID);	
		div.style.display = 'none';
		shadow.style.display = 'none';
		div.innerHTML = '';
	}
	
	// toggles the popup on when the link is clicked and off when the popup is clicked

	function togglePop(){
		if(progressState) return 0;			
		
		window.onscroll = preventScroll;
		
		var xy = getScrollXY();
		scrollX_old = xy[0];
		scrollY_old = xy[1];
		
		var div = document.getElementById(popID);
		var shadow = document.getElementById(shadowID);	
		if (!highestZ) {
			highestZ = getNextHighestZindex('div');
		}
		shadow.style.zIndex = highestZ;
		div.style.zIndex = highestZ + 1;
		
		
		if (div.style.display == 'block') {					
				div.style.display = 'none';
				shadow.style.display = 'none';				
				window.onscroll = null;
				shadow.style.zIndex = div.style.zIndex = 0;
				if(_self.oldParent!=null) {
					document.getElementById(_self.oldParent).innerHTML = div.innerHTML;
					div.innerHTML = null;
					_self.oldParent = null;
				}
			}
		
		else {
			div.style.display = 'block';
			shadow.style.display = 'block';			
		}						
		centerDiv(popID);
		centerDiv(shadowID);
		addPopOffset();
	}
	
	// loads the content from another div into the popup
	// the div should be defined at the start of the html body and its display should be set to 'none'
	function loadDiv (contentID, title){
		var div1 = document.getElementById(popID);
		var div2 = document.getElementById(contentID);
		div1.innerHTML = "";		
		div1.innerHTML = div2.innerHTML;
		_self.oldParent = contentID;
		div2.innerHTML = null;				
		show();
	}
		
	function loadImg (mylink, title){		
		var div1 = document.getElementById(popID);
		div1.innerHTML = "";		
		div1.innerHTML += "<div align=\"center\"><img src=\"" + mylink + "\" border=\"0\"></div>";		
		show();
	}
	
	function loadFLV(url, title){
			var swfid = this.popID;			
			var swf = flashPlayer;
			var id = "FLV";
			var width = 320;
			var height = 180;
			var version = "8";
			var bgcolor = "0";
			
			var fo = new SWFObject(swf, id, width, height, version, bgcolor);
			fo.addVariable("fname", url);
			fo.addParam("wmode", "opaque");
			fo.write(swfid);						
			show();
			
			return false;
	}
	
	function loadSwf (width, height, mylink, title){		
		var div1 = document.getElementById(popID);
		div1.innerHTML = "";
		if(!width) width = 320;
		if(!height) height = 200;	
		div1.innerHTML += "<center><div id=\"flv_player\" style=\"margin:auto 0; margin-top:" + closeButtonHeight + "px; z-index:20; position:relative;width:"+width+"; height:"+height+";\" align=\"center\"><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" viewastext=\"\" id=\"Object1\" height=\"" + height + "\" width=\"" + width + "\"><param name=\"movie\" value=\"" + flashPlayer + "?src=" + mylink + "\"><param name=\"quality\" value=\"high\" /><embed src=\"" + flashPlayer + "?fname=" + mylink + "\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" height=\"" + height + "\" width=\"" + width + "\"></embed></object></div></center>";
		show();
	}
	
	function loadMov (width, height, mylink, title){
		var div1 = document.getElementById(popID);
		div1.innerHTML = "";
		if(!width) width = 320;
		if(!height) height = 200;			
		div1.innerHTML += "<center><div style=\"margin:auto 0; margin-top:" + closeButtonHeight + "px; z-index:20; position:relative;\" align=\"center\"><embed src=\"" + mylink + "\" width=\"" + width + "\" height=\"" + height + "\" align=\"absmiddle\"></embed></div></center>";		
		show();
	}
	
	// redefines the popup's width, height, background, border and padding so that it can be used with different content 
	// would be nice if the width and height could be set automatically in relation to the content dimensions
	function setupPop (w, h, bgCol, b, p){
		var div = document.getElementById(popID)
		div.style.width = w;
		div.style.height = h;
		div.style.background = bgCol;
		div.style.border = b;
		div.style.padding = p;
		div.style.overflow = 'hidden';
	}
	
	function setShadow (){
		var shadow = document.getElementById(shadowID);
		var height = '100%';
		var width = '100%';	
		shadow.onclick = hide;		
		shadow.style.opacity = shadowOpacityMax/100;
		shadow.style.filter = 'alpha(opacity='+shadowOpacityMax+')';		
	}
	
	function init() {		
		setShadow();
		var div1 = document.getElementById(popID);
		var div2 = document.getElementById(shadowID);		
			
		div1.onmousedown = dragPopup;	
		div1.onmouseup = function (){						
			document.getElementById(popID).onmousemove = null; 						
			dragging = false;
			document.onmousemove = oldMouseMove;
		}
	}
	
	function mouseDown(e) {
		var ctrlPressed=0;
		 		
		if (parseInt(navigator.appVersion)>3) {
		
			var evt = navigator.appName=="Netscape" ? e:event;
			
			if (navigator.appName=="Netscape" && parseInt(navigator.appVersion)==4) {
			   // NETSCAPE 4 CODE
			   var mString =(e.modifiers+32).toString(2).substring(3,6);		   
			   ctrlPressed =(mString.charAt(1)=="1");		   
			   self.status="modifiers="+e.modifiers+" ("+mString+")"
			}
			else {
			   // NEWER BROWSERS [CROSS-PLATFORM]  
			   ctrlPressed =evt.ctrlKey;
			   self.status=""+", ctrlKey=" +ctrlPressed 
			}
			if (ctrlPressed) {
				document.body.style.cursor = 'move';
				document.onmouseup = function () { document.body.style.cursor = 'default'; document.onmouseup = null; }
				return 1;
			}
			   				
			else {
				
				return 0;
			}
		
		}
	}
	
	function dragPopup(mouseEvent){		
		if(!mouseDown(mouseEvent)) return 0;
		if (!mouseEvent) var mouseEvent = window.event;
		var tg = (window.event) ? mouseEvent.srcElement : mouseEvent.target; 
		if(tg.className == 'nodrag') return false;	
		
		dragging = true;		
		
		oldmouseX = mouseEvent.clientX;
		oldmouseY = mouseEvent.clientY;
			
		//save the documents onmouseEvents if it has any
		oldMouseMove = document.onmousemove;		
		
		document.onmousemove = startDrag;
		// cancel out any text selections
        document.body.focus();

        // prevent text selection in IE
        document.onselectstart = function () { return false; };
        // prevent IE from trying to drag an image
        tg.ondragstart = function() { return false; };
        
        // prevent text selection (except IE)
        return false;
	   
	}
	
	function startDrag(e) {
		 if (dragging) {

				x = e.clientX;
				y = e.clientY;			
				
				var incX = x - oldmouseX;
				var incY = y - oldmouseY;			
				
				oldmouseX = x;
				oldmouseY = y;
				
				var newY = parseInt(document.getElementById(popID).style.top) + incY;
				var newX = parseInt(document.getElementById(popID).style.left) + incX;
				
				document.getElementById(popID).style.top = newY + 'px'; //
				document.getElementById(popID).style.left = newX + 'px'; //				
			}
	}
	
	function addPopOffset(){
		var div = document.getElementById(popID)
		div.style.left = div.offsetLeft + popOffset[0] + "px";
		div.style.top = div.offsetTop + popOffset[1] + "px";
	}
	
	function setPlayer (p){
		flashPlayer = p;
	}
	
	function setCss() {
		var e = document.getElementById(popID);	
		e.style.zIndex = 100;
		e.style.backgroundColor = 'white';
		e.style.position = 'absolute';
		e.style.top = '0px';
		e.style.left = '0px';
		e.style.display = 'none';
		e.style.fontFamily = 'Arial, san-serif';
		e.style.margin = '0 auto';
		e.style.overflow = 'hidden';
		e.style.border = '5px solid #999';
		e.style.padding = '7px';
		e.style.mozBorderRadius = '15px';	
		e.style.webkitBorderRadius = '15px';	
		e.style.OperaBorderRadius = '15px';
		e.style.KhtmlBorderRadius = '15px';
		e.style.borderRadius = '15px';
		e.style.behavior = 'url(border-radius.htc)'; 
		
		var s = document.getElementById(shadowID);	
		s.style.backgroundColor = 'black';
		s.style.width = '100%';
		s.style.height = '100%';
		s.style.position = 'absolute';
		s.style.zIndex = 50;
		s.style.display = 'none';
		s.style.top = '0px';
		s.style.left = '0px';		
	}	
	
	function showProgress(i){		
		if ((i)&&(progressState==0)) {
			progressState = Math.floor(Math.random()*250000);
			var e = document.getElementById(popID);	
			e.className= 'loading';		
			e.innerHTML = '<div id="progressWheel" style="padding:10px;"><img src="'+progressImg.src+'" align="center"></div>';
			show();
			setTimeout(objName+".timeOut("+progressState+")",timeOutVal);			
		}
		else if((progressState!=0)&&(!i)) {
			progressState = 0;
			var e = document.getElementById(popID);	
			e.className= 'popup';		
			hide();			
		}
	}
	
	this.timeOut = function(n) { 
		if(progressState==n) {
			progressState = 0;
			hide();
		}
	}
		
}

