// VERSION  2.3  18/08/2007
// Added closeAll()
// VERSION  2.2  18/08/2007
// Added allowScrollTop
// VERSION  2.1  25/07/2007
// Added dragable divs
// VERSION  2.2  25/07/2007
// Added pop positioning for dragable divs
// VERSION 2.3 21/10/2008
// Fixed XHTML DocType declaration compatability
// Fixed Bounding of dragable divs
// Add the dragable option

//############################
function layers(divs)
{
	this.popDiv = popDiv;
	this.moveDiv = moveDiv;
	this.load_layer = load_layer;
	this.closeDiv = closeDiv;
	this.dragable = dragable;
	this.divWidth = 450;
	this.horizontalAdj = 10;
	this.top = 175;
	this.left = 10;
	this.autoClose = true;
	this.hideMain = true;
	this.centered = true;
	this.allowScrollTop = false;
	this.dragit = false;
	// Load the associated divs to this object instance
	if(!divs){
		alert("Please supply the DIVs associated with the layer object.     ");
		return;
	}
	this.arrDivs = divs.split(",");
}

//####  Enable dragable DIVs  #####
function dragable()
{
	this.stacking = 25;
	this.stackOrderIndex = 100;
	this.dragit = true;
	document.body.onmousedown=drags;
	document.body.onmouseup=new Function("dragapproved=false");
}
	
//#############################
function load_layer()
{
	var qs = new Querystring();
	var div = qs.get("div");
	if(div != null){
		if(!document.getElementById(div)){
			alert("DIV id: '"+div+"' called via query string does not exist!     ");
			return; 
		}else{
			this.popDiv(div);
		}
	}
}

//############################
function popDiv(div)
{ 
	if(this.copyright != "Southern Web Associates :: www.sw-associates.biz")
		alert("\nThis page is running a DHTML script owned by:\n\nSouthern Web Associates :: www.sw-associates.biz     ");
	if(this.autoClose){
		for (x=1; x<=10; x++){ 
			if (document.getElementById("pop"+x))
					this.closeDiv("pop"+x);
		}
	}
	if(this.allowScrollTop)
		window.scroll(0,0);
	var liveScreenHeight=screen.availHeight;
	var liveScreenWidth=screen.availWidth;
	livePageHeight=findLivePageHeight( );
	livePageWidth=findLivePageWidth( );
	//alert ('Visible page width is ' + livePageWidth + 'px; Visible page height is ' + livePageHeight + 'px');
	var layer=document.getElementById(div);
 if(this.hideMain){
		var content=document.getElementById("mainContent");
		if(!content){
			alert("HTML container ID 'mainContent' not found!     ");
			return;
		}
	}
	//alert(layer+" "+livePageWidth+" - "+this.divWidth+" "+this.horizAdj);
	// Find what DIV we are opening and position accordingly
	var stack;
	if(this.dragit){
		var arrM = div.match(/(\d)/);
		stack = (arrM[0]-1) * this.stacking;
		layer.style.zIndex = this.stackOrderIndex;
		this.stackOrderIndex++;
	}else{
		stack = 0;
	}
	if(this.centered)
		layer.style.left=((((livePageWidth-this.divWidth)/2)-this.horizAdj)+stack)+"px";
	else
		layer.style.left = (this.left+stack)+"px";
	layer.style.top=(this.top+stack)+"px";
	layer.style.visibility="visible";
	if(this.hideMain)
		content.style.visibility="hidden";
}

//###############################
function moveDiv( )
{ 
	var layer;
	for (x=1; x<=10; x++)
		{ if (layer=document.getElementById("pop"+x))
				if (layer.style.visibility=="visible")
					for(i in this.arrDivs){
						if(this.arrDivs[i] == "pop"+x)
							this.popDiv("pop"+x);
					}
		}
}

// ##############################
function closeAll()
{
	for (x=1; x<=10; x++){ 
		if (document.getElementById("pop"+x))
			this.closeDiv("pop"+x);
	}
}

//###############################
function closeDiv(div)
{ var layer=document.getElementById(div);
	var content=document.getElementById("mainContent");
	layer.style.visibility="hidden";
	if(content)
		content.style.visibility="visible";
}

//###############################
function findLivePageHeight( )
{ if (window.innerHeight!=null && window.innerHeight!='undifined')
		return window.innerHeight;
	if (document.body.clientHeight!=null)
		return document.body.clientHeight;
	return (null);
}

//################################
function findLivePageWidth( )
{ if (window.innerWidth!=null)
		return window.innerWidth;
	if (document.body.clientWidth!=null)
		return document.body.clientWidth;
	return (null);
}

//############################################################
//#################   QUERY STRING CLASS  ###################

/* Client-side access to querystring name=value pairs
	Version 1.2.3
	22 Jun 2005
	Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object()
	this.get=Querystring_get
	
	if (qs == null)
		qs=location.search.substring(1,location.search.length)

	if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ')
	var args = qs.split('&') // parse out name/value pairs separated via &
	
// split out each name=value pair
	for (var i=0;i<args.length;i++) {
		var value;
		var pair = args[i].split('=')
		var name = unescape(pair[0])

		if (pair.length == 2)
			value = unescape(pair[1])
		else
			value = name
		
		this.params[name] = value
	}
}

function Querystring_get(key, default_) {
	// This silly looking line changes UNDEFINED to NULL
	if (default_ == null) default_ = null;
	
	var value=this.params[key]
	if (value==null) value=default_;
	
	return value
}

//#######################################################
// Enable draggable DIV!
	//browser detection
	var agt=navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
	var is_minor = parseFloat(navigator.appVersion);

	var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
		&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
		&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	var is_nav4 = (is_nav && (is_major == 4));
	var is_nav6 = (is_nav && (is_major == 5));
	var is_nav6up = (is_nav && (is_major >= 5));
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
 var dragapproved=false
 var z,x,y
 var maxleft,maxtop,maxright,maxbottom;
 

//##############################
 function setdragBounds(divObj){
	 maxleft = 10;
	 maxtop = 10;
	 maxright = findLivePageWidth( ) - (parseInt(divObj.offsetWidth) + 20);
	 maxbottom = findLivePageHeight( ) - parseInt(divObj.offsetHeight +10);
 }

//##############################
function drags(e){
	 if (!(is_ie)&&!(!is_ie))
	 return
	 var firedobj=(!is_ie)? e.target : event.srcElement;
	 var topelement=(!is_ie)? "HTML" : "BODY"
	// alert(document.getElementById("pop1").style.top);
	 
	 while (firedobj.tagName!=topelement && firedobj.className!="drag" && firedobj.tagName!='SELECT' && firedobj.tagName!='TEXTAREA' && firedobj.tagName!='INPUT' && firedobj.tagName!='IMG'){
		//you can add the elements that cannot be used for drag here. using their class name or id or tag names
	 firedobj=(!is_ie)? firedobj.parentNode : firedobj.parentElement
	 if (firedobj.className=="drag"){ 
			setdragBounds(firedobj);
			dragapproved=true
			z=firedobj
			var tmpheight = z.style.height.split("px")

			temp1=parseInt(z.style.left+0)
			temp2=parseInt(z.style.top+0)
			x=(!is_ie)? e.clientX: event.clientX
			y=(!is_ie)? e.clientY: event.clientY
			document.onmousemove=move
			return false
	 }
	}
}
				
//#########################################
 function move(e){
		var tmpXpos = (!is_ie)? temp1+e.clientX-x: temp1+event.clientX-x;
		var tmpYpos = (!is_ie)? temp2+e.clientY-y : temp2+event.clientY-y;
		if (dragapproved){
				z.style.left = tmpXpos+"px";
				z.style.top = tmpYpos+"px";

				if(tmpXpos < maxleft)
					z.style.left = maxleft+"px";
				if(tmpXpos > maxright)
					z.style.left = maxright+"px";

				if(tmpYpos < maxtop)
					z.style.top = maxtop+"px";
				if(tmpYpos > maxbottom)
					z.style.top = maxbottom+"px";
				return false
		}
 }