  // JavaScript Document

var animatePageActiveA5=true;
var noAnimation=false;
var currentDragDivA5=null;
var currentResizeDiv=null;
var currentResizeContent=null;
var resizeDivActiveA5=false;
var animateActive=false;
var tempXA5;
var tempYA5;
var offsetX;
var offsetY;
var animateBublicationDivOpening=true;
var currentBublicationDivId='';
var bublicationDivMaxHeight=180;
var bublicationDivMaxWidth=800;
var animateBubDivTimeId='';
var collapseA5Trigger = 40; // see app class bubbleStyle collapseA5Trigger
var bubDivIdArray= new Array();
if ( !isDockedArray ) {
	var isDockedArray= new Array('','','','','','','','','',''); // this is set by conducta
}
var bubbleBarContentsArray = new Array();
var contentDivIdArray = new Array();
var bubDivMaxHeightArray= new Array(); //225
var topOffset = 147;
var bottomOffset = 32;
var bottomOffsetOpenBub = 205;
var bottomOffsetClosedBub = bottomOffset;
var bubWinIsCollapsedImage='';
var bubWinIsOpenImage='';
var IE=false;
var bubWinIsOpenImage = "/lib/aria5/graphics/skin/default/bubbleCollapseButtonIsOpen.png";
var bubWinIsCollapsedImage = "/lib/aria5/graphics/skin/default/bubbleCollapseButtonIsCollapsed.png";
var bubbleAnchorOnImage = "/lib/aria5/graphics/skin/default/bubbleAnchorOn.png";
var bubbleAnchorOffImage = "/lib/aria5/graphics/skin/default/bubbleAnchorOff.png";
var mouseIsDown = false;
var bubbleAnchorArray = new Array();
// document.addEventListener('mousemove', getMouseXY, true);
document.addEventListener('mousedown', mouseIsDownSet, true);
document.addEventListener('mouseup', mouseIsUpSet, true);
function mouseIsDownSet() {
	mouseIsDown = true;
}
function mouseIsUpSet() {
	mouseIsDown = false;	
}
function getUrlUpdateInputObj(bubble) {
	for (var i=0; i<bubble.childNodes.length; i++) {
		if (bubble.childNodes[i].className == "urlUpdateInput") {
			return bubble.childNodes[i];
		}
	}
	return false;
}
function setIframeSource(bubbleID,inputID) {
	if (bubbleID && inputID) {
		var bubble = document.getElementById(bubbleID);
		var input = document.getElementById(inputID);
		if (bubble !=null && input !=null) {
			var url = input.value;
			if (url) {
				if (url.length>1 && url.substr(0,4)!='http') {
					url = 'http://'+url;
				}
				for (var i=0; i<bubble.childNodes.length; i++) {
					if (bubble.childNodes[i].className == "iframeA5") {
						bubble.childNodes[i].src=url;
						input.value=url;
						
						var urlUpdateInputObj=getUrlUpdateInputObj(bubble);
						if (urlUpdateInputObj) {
							// $urlUpdateString = 
							// <input type='hidden' id='urlUpdateInput' value='peopleUrl' />
							var str = dataSourcePathA5 + "?func=saveEns&newVariable=" + urlUpdateInputObj.value+"ab&newValue="+url;
							sendToServerAria5(str);
						}
						
						
					}
				}
			} else {
		
					// reset to default by reloading the bubble wrap.. sledgehammer style!.
					lastHash = "null";
					var str = "func="+window.location.hash;
					str = dataSourcePathA5 + "?" + str;
					sendToServerAria5(str);
					//popBubble(bubbleID+'Div');
					dropNode(bubbleID+'Div');
			}
		}
	}
}
function anchorThisBubble(obj,imgID) {
	var img = document.getElementById(imgID);
	if (obj != null) {
		if (!in_array(obj,bubbleAnchorArray, true)) {
			// register bubble state in an array for lookup when we flush or fade bubbles.
			bubbleAnchorArray.push(obj);
			recordBubPosition(obj);
			if (img !=null) {
				img.src = bubbleAnchorOnImage; // bubbleAnchorOffImage
			}
			// move the node into anchoredBubblesStore 
			// .. to prevent playback dissruption on DOM OBJ DELETE 
			var anchoredBubblesStore = document.getElementById('anchoredBubblesStore');
			if (anchoredBubblesStore != null) {
				anchoredBubblesStore.appendChild(obj);
			}
		} else {
			// de-register bubble.
			for (var i=0; i<bubbleAnchorArray.length; i++) {
				if (bubbleAnchorArray[i].id==obj.id){
			//		trace('splice:'+bubbleAnchorArray[i].id);
					bubbleAnchorArray.splice(i,1);
					
					break;
				}
			}
			
			recordBubPosition(obj);
			
			
			if (img !=null) {
				img.src = bubbleAnchorOffImage; 
			}
			var container = document.getElementById('container');
			if (container !=null) {
				container.appendChild(obj);
			}
		}
	}
}

function weightAnchorThisBubble(obj,imgID) {
	var img = document.getElementById(imgID);
	if (obj != null) {
		if (in_array(obj,bubbleAnchorArray)) {
			
		}
	}
}
function recordBubPosition(obj) {
	//	trace(obj.id);
	if (!conducta) {
		if (obj != null) {
			var bubbleName = obj.id;
			bubbleName = bubbleName.substr(0,bubbleName.length-3);
			var bubbleX = obj.offsetLeft;
			var bubbleY = obj.offsetTop;
			var bubbleZ = obj.style.zIndex;
			var bubbleW = obj.offsetWidth;
			var bubbleH = obj.offsetHeight;
			var bubbleState = 1;
			var ltwhz = '';
			if (bubbleH <= collapseA5Trigger) {
				var heightStoreObj = document.getElementById(bubbleName+"DivHeight");
				if (heightStoreObj != null) {
					bubbleH = heightStoreObj.value;
					bubbleState = 0;
				} else {
	//				trace('not found: '+bubbleName+"DivHeight");
				}
			}
			if (bubbleH <= collapseA5Trigger) {
				var widthStoreObj = document.getElementById(bubbleName+"DivWidth");
				if (widthStoreObj != null) {
					bubbleW = widthStoreObj.value;
					bubbleState = 0;
				} else {
	//				trace('not found: '+bubbleName+"DivWidth");
				}
			}
			// ltwhz
			// div restore object holds a string L,T,W,H,Z, save to bubPos.
			if (bubbleH > collapseA5Trigger) {
				var ltwhzStoreObj = document.getElementById(bubbleName+"DivRestore");
				if (ltwhzStoreObj != null) {
					ltwhz = ltwhzStoreObj.value;
				} else {
	//				trace('not found: '+bubbleName+"DivRestore");
				}
			}
			var params = "&bubName="+bubbleName;
			params += "&bubX="+bubbleX;
			params += "&bubY="+bubbleY;
			params += "&bubZ="+bubbleZ;
			params += "&bubW="+bubbleW;
			params += "&bubH="+bubbleH;
			params += "&bubState="+bubbleState;
			params += "&ltwhz="+ltwhz;
			var bubbleSplurge = getAllBubbles('record Bub Pos');
			zIndexList='';
			for (var i=0; i<bubbleSplurge.length; i++) {
				name = bubbleSplurge[i].id;
				name = name.substr(0,name.length-3);
				zIndexList += name + "," + bubbleSplurge[i].style.zIndex + ",";
			}
			params += "&zIndexes="+zIndexList;
	//		trace(params);
			var str = "func=recordBubPos"	+ params;
			str = dataSourcePathA5 + "?" + str;
	//trace('rec: '+str);
			sendToServerAria5(str);
		}
		lastBublicationDivId='';
	}
}
function bubCollapse(divId,collapseImgId) {
	lastBublicationDivId='';
	if (divId) {
		var obj = document.getElementById(divId);
		var str = divId.substr(0,divId.length-3);
		var contentDiv = document.getElementById(str);
		if (obj != null) {
			
			bringBubToFront(divId);
			
//			if (!isInA5Dock(divId)) {
			currentBublicationDivId=divId;
			
			if (obj.offsetHeight <= collapseA5Trigger+10) {
				
				// open it
				
				animateBublicationDivOpening=true;
				if (collapseImgId) {
					var imgObj=document.getElementById(collapseImgId);
					
					if (imgObj != null) {
						imgObj.src=bubWinIsOpenImage; 
					}
					
				}
				if (contentDiv != null) {
					
					contentDiv.style.height="auto";
					contentDiv.style.display="block";
					var scrl = getCurrentBubbleScrollBar();
					if (scrl != null) {
						scrl.style.display="block";
					}
				}
				animateBublicationDiv();
			
			} else {
				
				// close it.
				
				var dh = getCurrentBubbleDragHandle();
				if (dh != null) {
					dh.style.display="none";
				}
				
				
				animateBublicationDivOpening=false;
				animateBublicationDiv();
				if (contentDiv != null) {
//					contentDiv.style.height='1px';
					
				}
				var scrl = getCurrentBubbleScrollBar();
				if (scrl != null) {
					scrl.style.display="none";
				}
				var imgObj=document.getElementById(collapseImgId);
				if (imgObj != null) {
					imgObj.src=bubWinIsCollapsedImage; 
				}
			}
//			}
		}
	}
}
function bubClose(divId) {
	if (divId) {
		var obj = document.getElementById(divId);
		if (obj != null) {
			// see aria5/js/lib.js
			dropNode(divId);
		}
	}
}
function popBubble(divId) {
	currentDragHandle = null;
	if (divId) {
		var obj = document.getElementById(divId);
		if (obj != null) {
			// see aria5/js/lib.js
			dropNode(divId);
			if (in_array(obj,bubbleAnchorArray,true)) {
				for (var i=0; i<bubbleAnchorArray.length; i++) {
					if (bubbleAnchorArray[i].id==obj.id){
						//trace('splice:'+bubbleAnchorArray[i].id);
						bubbleAnchorArray.splice(i,1);
						
						break;
					}
				}
			}
			if ( !conducta ) {
			var str = "func=popBubble&name="	+ divId.substr(0,divId.length-3);
	//		trace('pop: '+str);
			str = dataSourcePathA5 + "?" + str;
			sendToServerAria5(str);
			}
			
			
		}
	}
}
function bubOpen(divId,collapseImgId) {
	lastBublicationDivId='';
	if (divId) {
		var obj = document.getElementById(divId);
		var str = divId.substr(0,divId.length-3);
		var contentDiv = document.getElementById(str);
		if (obj != null) {
			
			var scrl = getCurrentBubbleScrollBar();
			if (scrl != null) {
				scrl.style.display="block";
			}
			
			bringBubToFront(divId);
			currentBublicationDivId=divId;
	
	
		
				// the bubble is closed at the time we clicked restore.
				animateBublicationDivOpening=true;
				if (contentDiv != null) {
		//			contentDiv.style.height="85%";
					contentDiv.style.display="block";
	
					contentDiv.style.height="99%";
					for (var i=0; i<contentDiv.childNodes.length; i++) {
						//trace(contentDiv.childNodes[i].className);
						if (contentDiv.childNodes[i].className=='iframeA5') {
							contentDiv.childNodes[i].style.height="99%";
						}
					}
				}
				if (collapseImgId) {
					var imgObj=document.getElementById(collapseImgId);
					if (imgObj != null) {
						imgObj.src=bubWinIsOpenImage; 
					}
				}
				animateBublicationDiv();
			
		}
	}
}
function setCurrentDragHandle(id) {
	if ( !mouseIsDown ) {
		currentDragHandle = id;
	}
}
function getCurrentBubbleScrollBar() {
	//    currentBublicationDivId	
	var scrollBarObject = null;
	if (currentBublicationDivId) {
		var obj=document.getElementById(currentBublicationDivId);
		if (obj != null) {
			for (var i=0; i<obj.childNodes.length; i++) {
				if (obj.childNodes[i].className == "ergoScroll") {
					scrollBarObject = obj.childNodes[i];
				}
			}
		}
	}
	if (!scrollBarObject) { /*trace('no ergoScroll in '+currentBublicationDivId);*/ }
//	trace('currentBublicationDivId: '+currentBublicationDivId);
	return scrollBarObject;
}
function getCurrentBubbleDragHandle() {
	//    currentBublicationDivId	
	var dragHandleObj = null;
	var obj=getCurrentBubbleScrollBar();
	if (obj != null) {
		for (var i=0; i<obj.childNodes.length; i++) {
		
			if (obj.childNodes[i].className == "scrDrag") {
				dragHandleObj = obj.childNodes[i];
				//trace('dragHandleObj: ');
			}
			
		}
	}
	
	return dragHandleObj;
}

var lastBublicationDivId='';
function animateBublicationDiv() {
	if (currentBublicationDivId) {
		var obj=document.getElementById(currentBublicationDivId);
		if (obj != null) {
			//endAnimation=false;
			// Width
			var objWidthValue = obj.offsetWidth;
			var widthStoreObj = document.getElementById(currentBublicationDivId+"Width");
			if (widthStoreObj != null) {
				bublicationDivMaxWidth = widthStoreObj.value;
			} else {
				trace('cannot find '+currentBublicationDivId+"Width");	
			}
			// Height
			var objHeightValue = obj.offsetHeight;
			var heightStoreObj = document.getElementById(currentBublicationDivId+"Height");
			if (heightStoreObj != null) {
				bublicationDivMaxHeight = heightStoreObj.value;
			}
			// check width and height
			if (objWidthValue<1 || !objWidthValue ) {objWidthValue=1;}
			if (objHeightValue<1 || !objHeightValue ) {objHeightValue=1;}
			// generate the revised width and height
			// If the bubble in question is opening..
			if (animateBublicationDivOpening) {
				objWidthValue += ((bublicationDivMaxWidth - objWidthValue)/2);
				objHeightValue += ((bublicationDivMaxHeight - objHeightValue)/2);
			//	trace('propo: '+objHeightValue);
			//	if (objHeightValue<collapseA5Trigger) {
			//		objHeightValue=collapseA5Trigger+1;
			//	}
			} else {
			// the bubble is closing
				objHeightValue = Math.floor(objHeightValue/2);
			// if the bubble is not in the dock and the height is less than the collapse trigger then fix the height to thcollapse trigger
				if (!isInA5Dock(currentBublicationDivId) && objHeightValue<(collapseA5Trigger+5)) {
					// collapseA5Trigger = minimum height e.g. 44 pixels.
					//!animateBublicationDivOpening
					objHeightValue=collapseA5Trigger;
					//window.clearTimeout(animateBubDivTimeId);
					//trace('clear timeout: animateBubDivTimeId');
					//endAnimation=true;
				}
			}
			// ..having aquired the new sizes for the bubble
			// IF closing and the proposed height is less than the min
			// OR opening and the height will be greater than the max height
			// i.e. if the proposed sizes are out of bounds.. 
			//if (!endAnimation) {
	//trace('max: '+bublicationDivMaxHeight+' propo: '+objHeightValue+' now: '+obj.offsetHeight);
	
			
			
			if ((!animateBublicationDivOpening && objHeightValue<=collapseA5Trigger) 
				 || (animateBublicationDivOpening && objHeightValue>bublicationDivMaxHeight-1)) {
				// PROPOSED DIMS TOO BIG OR SMALL - FOR THE CURRENT OPERATION
				// - stop the animation
				window.clearTimeout(animateBubDivTimeId);
				//window.clearInterval(animateBubDivTimeId);
				
				// SET BUBBLE SIZE TO MAX OR MIN - halt animation
				if (animateBublicationDivOpening) {
					
				// opening - width & height set to max
					obj.style.width=bublicationDivMaxWidth+'px';
					obj.style.height=bublicationDivMaxHeight+'px';
					// make sure the scroll drag handle is visible ..
					var dh = getCurrentBubbleDragHandle();
					if (dh != null) {dh.style.display="block";}
					positionDragHandle();
					
					var str = currentBublicationDivId.substr(0,currentBublicationDivId.length-3);
					var contentDiv = document.getElementById(str);
					if (contentDiv !=null) {
						contentDiv.style.height="93%";
						contentDiv.style.paddingTop="28px";
						for (var i=0; i<contentDiv.childNodes.length; i++) {
							//trace(contentDiv.childNodes[i].className);
							if (contentDiv.childNodes[i].className=='iframeA5') {
								contentDiv.childNodes[i].style.height="99%";
							}
						}
						contentDiv.style.display="block";
					}

					
						
				} else {
					// closing - height will be set to 1px
					obj.style.height=(collapseA5Trigger-1)+'px';
					var str = currentBublicationDivId.substr(0,currentBublicationDivId.length-3);
					var contentDiv = document.getElementById(str);
					if (contentDiv !=null) {
						var dh = getCurrentBubbleDragHandle();
						if (dh != null) {dh.style.display="none";}
//						contentDiv.style.display="none";
						contentDiv.style.height="1px";
						contentDiv.style.paddingTop="15px";
					}
				}
				
				if (currentBublicationDivId !== lastBublicationDivId) {
					recordBubPosition(obj);
					lastBublicationDivId=currentBublicationDivId;
	//				trace('rec');
				}
				
			} else {
				// the bubble is higher than min but less than the max height
				// SET BUBBLE SIZE AS PROPOSED - continue animation
				obj.style.height=String(objHeightValue+'px');
				obj.style.width=String(objWidthValue+'px');
//trace('set: w: '+objWidthValue+' h: '+objHeightValue);
				// opening
				if (animateBublicationDivOpening && obj.offsetHeight < collapseA5Trigger) {
					// as th opening starts set the content to visible again
					var str = currentBublicationDivId.substr(0,currentBublicationDivId.length-3);
					var contentDiv = document.getElementById(str);
					if (contentDiv !=null) {
						contentDiv.style.display="block";
						contentDiv.style.height="95%";
						
						// doesn't happen !!
					}					
				}

					
				// if the height is still between collapse trigger and max
				if (animateBublicationDivOpening && (obj.offsetHeight < bublicationDivMaxHeight-1)) {
					// recall animation - opening
	//				animateBubDivTimeId = window.setTimeout('animateBublicationDiv()',100);
					//trace('set timeout: animateBubDivTimeId');
				}
				// closing - height at min
				if (!animateBublicationDivOpening && (obj.offsetHeight <= collapseA5Trigger)) {
					// hide contents
					var str = currentBublicationDivId.substr(0,currentBublicationDivId.length-3);
					var contentDiv = document.getElementById(str);
					if (contentDiv !=null) {
	//					contentDiv.style.display="none";
	//					contentDiv.style.height="1px";
	//					trace('hide content');
						// NEVER HAPPENS ??
					}
					// hide the scroll bar contents
					var scrl = getCurrentBubbleScrollBar();
					if (scrl != null) {scrl.style.display="none";}
					// stop animation
					window.clearTimeout(animateBubDivTimeId);
					//window.clearInterval(animateBubDivTimeId);
					//trace('clear timeout: animateBubDivTimeId');
					// record position
	// NEVER HAPPENS !!
					trace('rec pos '+obj.id);
					recordBubPosition(obj);
				} else {
					// closing with height still > min
					//animateBubDivTimeId = window.setTimeout('animateBublicationDiv()',100);
//					animateBubDivTimeId = window.setInterval('animateBublicationDiv()',100);
					if (currentBublicationDivId !== lastBublicationDivId) {
						//animateBubDivTimeId = window.setInterval('animateBublicationDiv()',100);
						window.clearTimeout(animateBubDivTimeId);
						animateBubDivTimeId = window.setTimeout('animateBublicationDiv()',100);
					}
				}
				
				if (animateBublicationDivOpening) {
					// opening 
					var dh = getCurrentBubbleDragHandle();
					if (dh != null) {dh.style.display="block";}				
					//animateBubDivTimeId = window.setTimeout('animateBublicationDiv()',100);
					if (currentBublicationDivId !== lastBublicationDivId) {
						//animateBubDivTimeId = window.setInterval('animateBublicationDiv()',100);
						window.clearTimeout(animateBubDivTimeId);
						animateBubDivTimeId = window.setTimeout('animateBublicationDiv()',100);
					}
					var str = currentBublicationDivId.substr(0,currentBublicationDivId.length-3);
					var contentDiv = document.getElementById(str);
					if (contentDiv !=null) {
						contentDiv.style.height="95%";
					}
					//trace('set timeout: animateBubDivTimeId');
				}
			}
			//}
			
		} else {
			//alert("Error! "+currentBublicationDivId+" Not Found");
			
			window.clearTimeout(animateBubDivTimeId);
			//window.clearInterval(animateBubDivTimeId);
			currentBublicationDivId='';
		}
	}
}


function bringBubToFront(id) {
	var currentZ = 0;
//	trace('bringBubToFront: '+id);
	if (id) {
		var obj=document.getElementById(id);
	//	var str = id; //+ 'Container'; //id.substr(0,id.length-3);
	//	trace(str);
	//	var contentDiv = document.getElementById(str);
		if (obj != null) {
			currentBublicationDivId = id;
			var container = document.getElementById(targetDivA5);
			var zArray = getAllDepthBubbles();
			
			for (var i=0; i<zArray.length; i++) {
				
				if (zArray[i].id == id) {
					
					zArray[i].style.zIndex = 1000;
					zArray[i].style.opacity = 1;
				} else {
					zArray[i].style.zIndex--;
					if (zArray[i].style.zIndex < 1) {
						zArray[i].style.zIndex = 1;	// crucial to prevent things getting unclickable behind container (but still visible!)
					}
					zArray[i].style.opacity = 1;
				}
		
			}
//			recordBubPosition(obj);
		} else {
	//		trace('bringBubToFront: '+id);
		}
	}
}
function removeFromDock(obj) {
	for (var i=0; i<isDockedArray.length; i++) {
		if (obj.id==isDockedArray[i]) {
			//isDockedArray[i]='';
			isDockedArray.splice(i,1);
			return;
		}
	}
}
function removeFromBubbleBar(obj) {
	for (var i=0; i<bubbleBarContentsArray.length; i++) {
		if (obj.id==bubbleBarContentsArray[i]) {
			bubbleBarContentsArray.splice(i,1);
//			trace('rem: '+obj.id);
			return;
		}
	}
}

var bubBarFrameLimit = 10;
var bubBarFrameCounter = 1;
var currentBubble = null;
var bubBarAnimTimeoutObj = null;
var bubBarTarget = new Array(4); // left, top, width, height, zIndex
var bubRestoreTarget = new Array(4); // left, top, width, height, zIndex
var currentBubblePos = new Array(4); // offsetLeft, offsetTop, width, height
var currDeflIncr = new Array(4); // current Deflate Increments - left, top, w, h

function deflateToBubbleBar(id) {
	var obj = document.getElementById(id);
	currentDragHandle=null;
	if (obj != null) {
//		addToDock(obj);	
		currentBubble = obj;
		
		if (!inBubbleBar(id)) {
			getTargetDeflateCoords();
			currentBubblePos = Array(obj.offsetLeft, obj.offsetTop, obj.offsetWidth, obj.offsetHeight);
//			monitorPosition(currentBubble);
			var a = (currentBubblePos[0]-bubBarTarget[0]) / bubBarFrameLimit;
			var b = (bubBarTarget[1]-currentBubblePos[1]) / bubBarFrameLimit;
			var c = (currentBubblePos[2]-bubBarTarget[2]) / bubBarFrameLimit;
			var d = (currentBubblePos[3]-bubBarTarget[3]) / bubBarFrameLimit;
			currDeflIncr = Array(a,b,c,d);
//			monitorIncrements();
			bubBarAnimTimeoutObj = window.setInterval(deflateStep,10);
		 } else {
			reflateFromBubbleBar(id);
		 }
	} else {
		trace('not found: '+id);	
	}
}
function reflateFromBubbleBar(id) {
	var obj = document.getElementById(id);
	
	if (obj != null) {
		removeFromBubbleBar(obj);
		currentBubble = obj;
		getTargetReflateCoords();
		currentBubblePos = Array(obj.offsetLeft, obj.offsetTop, obj.offsetWidth, obj.offsetHeight);
//		monitorPosition(currentBubble);
		var a = (currentBubblePos[0]-bubRestoreTarget[0]) / bubBarFrameLimit;
		var b = (bubRestoreTarget[1]-currentBubblePos[1]) / bubBarFrameLimit;
		var c = (currentBubblePos[2]-bubRestoreTarget[2]) / bubBarFrameLimit;
		var d = (currentBubblePos[3]-bubRestoreTarget[3]) / bubBarFrameLimit;
		currDeflIncr = Array(a,b,c,d);
//			monitorIncrements();
		bubBarAnimTimeoutObj = window.setInterval(reflateStep,10);
	}
}
function deflateStep() {
	// Deflate repeat function
	if (bubBarFrameCounter < bubBarFrameLimit) {
		var obj = document.getElementById(currentBubble.id+"Restore");// currentBubble.id+"Restore"
		setRestoreValues(obj);// record this before a deflate occurs (should already be there tho '
		
//		if (restoreStoreObj != null) {
			// record the value for bubPos.ltwhz
//			var str = currentBubble.offsetLeft+','+currentBubble.offsetTop+','+currentBubble.offsetWidth+','+currentBubble.offsetHeight+','+currentBubble.style.zIndex; 
//			restoreStoreObj.value = str;
//		}
		
		setBubblePosition(1);
		var n = ( 1- (bubBarFrameCounter / bubBarFrameLimit ))*100;
		setBubbleOpacity(n);
		bubBarFrameCounter++;
	} else {
		setBubblePosition(2);
		addToBubbleBar();
		window.clearInterval(bubBarAnimTimeoutObj);
		bubBarFrameCounter = 1;
		// record Bubble Position
		recordBubPosition(currentBubble);
		
	}
}
function reflateStep() {
	// Reflate repeat function
	if (bubBarFrameCounter < bubBarFrameLimit) {
		setBubblePosition(1);
		var n = (bubBarFrameLimit / bubBarFrameCounter)*100;
//		setBubbleOpacity(n);
		bubBarFrameCounter++;
	} else {
		setBubblePosition(0);
		window.clearInterval(bubBarAnimTimeoutObj);
		bubBarFrameCounter = 1;
	
		var id = currentBubble.id;
		// this is a hack
		var str = id.substr(0,id.length-3); // as in take 'Div'
	
		bubOpen(id,str+'WinCollapseImg');
		
		// record Bubble Position
		recordBubPosition(currentBubble);
		
	}
}
function setBubbleOpacity(n) {
	if (!in_array(currentBubble,bubbleAnchorArray,true)) {
	 currentBubble.style.opacity = (n / 100);
	}
}
function setBubblePosition(n) {
	switch(n) {
		case 0:
			// start
	currentBubble.style.left=bubRestoreTarget[0]+'px';
	currentBubble.style.top=bubRestoreTarget[1]+'px';
	currentBubble.style.width=bubRestoreTarget[2]+'px';
	currentBubble.style.height=bubRestoreTarget[3]+'px';
	currentBubble.style.zIndex=bubRestoreTarget[4];	
//	monitorRestoreTarget();
		break;
		case 1:
			// in progress
	currentBubble.style.left=(currentBubblePos[0]-currDeflIncr[0])+'px';
	currentBubble.style.top=(currentBubblePos[1]+currDeflIncr[1])+'px';
	currentBubble.style.width=(currentBubblePos[2]-currDeflIncr[2])+'px';
	currentBubble.style.height=(currentBubblePos[3]-currDeflIncr[3])+'px';
//	monitorPlacement();
	currentBubblePos = Array(currentBubble.offsetLeft, currentBubble.offsetTop, currentBubble.offsetWidth, currentBubble.offsetHeight);
	//monitorPosition(currentBubble);
		break;
		case 2:
			// final
	currentBubble.style.left=bubBarTarget[0]+'px';
	currentBubble.style.top=bubBarTarget[1]+'px';
	currentBubble.style.width=bubBarTarget[2]+'px';
	currentBubble.style.height=bubBarTarget[3]+'px';
	currentBubble.style.zIndex=bubBarTarget[4];	
		break;
	}
}
function monitorRestoreTarget() {
	var str = "rest: L: "+bubRestoreTarget[0]+'px';
	str += ", T: "+bubRestoreTarget[1]+'px';
	str += ", W: "+bubRestoreTarget[2]+'px';
	str += ", H: "+bubRestoreTarget[3]+'px';
	str += ", Z: "+bubRestoreTarget[4]+'px';
	trace(str);
}
function monitorIncrements() {
	var str = "inc: L: "+currDeflIncr[0];
	str += ", T: "+currDeflIncr[1];
	str += ", W: "+currDeflIncr[2];
	str += ", H: "+currDeflIncr[3];
	trace(str);
}
function monitorPlacement() {
	var str = "place: L: "+(currentBubblePos[0]-currDeflIncr[0])+'px';
			str += ", T: "+(currentBubblePos[1]+currDeflIncr[1])+'px';
			str += ", W: "+(currentBubblePos[2]-currDeflIncr[2])+'px';
			str += ", H: "+(currentBubblePos[3]-currDeflIncr[3])+'px';
	trace(str);
}
function monitorPosition(obj) {
	var str = 'pos: L: '+obj.offsetLeft+', T: '+obj.offsetTop+', W: '+obj.offsetWidth+', H: '+obj.offsetHeight;
	trace(str);
}
function getTargetDeflateCoords() {
	// look up the next 'slot' in the bubbleBar
	// x leftOffset, bottomOffset, z index (x is the important one)
	var n = bubbleBarContentsArray.length;
//	trace('n: '+n);
	var bubBar = document.getElementById('bubbleBarContainerBox');
	if (bubBar != null) {
		bubBarTarget = Array(n*160,bubBar.offsetTop-20,160,45,100);  // left, top, width, height, zIndex
	} else {
		trace('no bubble bar !');	
	}
}
function getTargetReflateCoords() {
	var restoreStoreObj = document.getElementById(currentBubble.id+"Restore");
	if (restoreStoreObj != null) {
		var str = restoreStoreObj.value;
//		trace('rest: '+str);
		bubRestoreTarget = str.split(',');
	} else {
		trace('not found: '+currentBubble.id+"Restore");	
	}
}
function addToBubbleBar() {
	bubbleBarContentsArray.push(currentBubble.id);
	currentBubble.style.opacity=1;
}
function addToDock(obj) {
	if (obj != null) {
		isDockedArray.push(obj.id);
		obj.style.bottom="0px";
		obj.style.top="auto";
	}
}
function inBubbleBar(id) {
	for (var i=0; i<bubbleBarContentsArray.length; i++) {
		if (bubbleBarContentsArray[i]==id) {
			return true;
		}
	}
	return false;
}
function isInA5Dock(id) {
	for (var i=0; i<isDockedArray.length; i++) {
		if (isDockedArray[i]==id) {
			return true;
		}
	}
	return false;
}
function dragDivA5(obj,e) {
	// drag a bubble around
	//trace('dragDiv');
	if (obj != null) {
		switch(e) {
			case "start":
				animatePageActiveA5=true;
				resizeDivActiveA5=false;
				currentDragDivA5=obj;
				var dh = getCurrentBubbleDragHandle();
				if (dh != null) {dh.style.display="none";}
	//			trace(currentDragDivA5.id);
	//			removeFromDock(currentDragDivA5);
				bringBubToFront(currentDragDivA5.id);
				currentDragDivA5.style.position="absolute";
				dragDivStartPositionX = parseInt(currentDragDivA5.style.left);
				dragDivStartPositionY = parseInt(currentDragDivA5.style.top);		
				offsetX = tempXA5-dragDivStartPositionX;
				offsetY = tempYA5-dragDivStartPositionY;

				var str = currentDragDivA5.id.substr(0,currentDragDivA5.id.length-3);
				var contentDiv = document.getElementById(str);
				if (!in_array(currentDragDivA5,bubbleAnchorArray,true) && contentDiv != null && currentDragDivA5.offsetHeight > collapseA5Trigger) { contentDiv.style.display='none'; }


				removeFromBubbleBar(obj);
				animateA5Page();
			break;
			case "stop":
				animatePageActiveA5=false;
				resizeDivActiveA5=false;
				currentDragDivA5=obj;
				recordBubPosition(obj);
				var dh = getCurrentBubbleDragHandle();
				if (dh != null) {dh.style.display="block";}
			//	var str = currentDragDivA5.style.top;
			//	str=parseInt(str.substr(0,str.length-2));
			//	str = currentDragDivA5.offsetTop;
			//	str=tempYA5-offsetY;
				if ((tempYA5-currentDragDivA5.offsetTop) > -220) {
					// magnetic attract to the dock.
	//				currentDragDivA5.style.top=null;
//					addToDock(currentDragDivA5);
				}

				var str = currentDragDivA5.id.substr(0,currentDragDivA5.id.length-3);
				var contentDiv = document.getElementById(str);
				if (!in_array(currentDragDivA5,bubbleAnchorArray,true) && contentDiv != null && currentDragDivA5.offsetHeight > collapseA5Trigger ) { contentDiv.style.display='block'; }

				setRestoreValues(obj);
				currentDragDivA5=null;
			break;
		}
	}
}

function resizeBubDiv(obj,e,cont) {
	//trace('obj: '+obj+', e: '+e+', cont: '+cont);
	if (obj != null) {
		switch(e) {
			case "start":
				var anch=document.getElementById(targetDivA5);
			// right of the div x coord
				var divRightX = obj.offsetWidth + obj.offsetLeft;
//				trace('divRightX: '+divRightX);
//				trace('anch.offsetLeft: '+anch.offsetLeft);
			// bottom of the div y coord
				var divBotY = obj.offsetHeight + obj.offsetTop;
				clickOffsetWidth = (divRightX-tempXA5+anch.offsetLeft);
				clickOffsetHeight = (divBotY-tempYA5);
				resizeDivActiveA5=true;
				currentResizeDiv=obj;
				currentResizeContent=cont;
				bringBubToFront(currentResizeDiv.id);
				var handle = document.getElementById(obj.id+"ResizeHandle");
				if (handle != null) {
	//				handle.style.border="1px Solid Red";
				}
				// BottomRight
				/*
				var obj1 = document.getElementById(obj.id+'BottomRight');
				if (obj1 !=null) {
					obj1.style.width='100%';
					obj1.style.height='100%';
				} */
			//	animatePageActiveA5=true;
			break;
			case "stop":
				currentResizeDiv=obj;
			/*	for(var i=0; i<bubDivIdArray.length;i++) {
				if (bubDivIdArray[i]==currentResizeDiv.id) {
				bubDivMaxHeightArray[i]=currentResizeDiv.offsetHeight;
				}
				}*/
				var str = currentResizeDiv.id;
				var contentID = str.substr(0,str.length-3);
				var contentObject = document.getElementById(contentID); 
				if (contentObject != null) {
					// very heavy to drag around in Mozzilla and other browsers
				//	trace('here stop');
					contentObject.style.display='block';
					contentObject.style.width = (currentResizeDiv.offsetWidth-14) + 'px';
					contentObject.style.height = (currentResizeDiv.offsetHeight-45) + 'px';
					positionDragHandle();
					
					
				} else{
					//trace('contentObject not found ' + contentID);
				}
				setRestoreValues(obj);
				resizeDivActiveA5=false;
				currentResizeDiv=null;
				currentResizeContent=null;
				
				recordBubPosition(obj);
				
				var heightStoreObj = document.getElementById(obj.id+"Height");
				if (heightStoreObj != null && obj.offsetHeight > collapseA5Trigger) {
					heightStoreObj.value = obj.offsetHeight;
				} else {
					trace('height not saved: '+obj.id+"Height");	
				}
				var widthStoreObj = document.getElementById(obj.id+"Width");
				if (widthStoreObj != null) {
					widthStoreObj.value = obj.offsetWidth;
		//			trace('width: '+widthStoreObj.value);
				} else {
					trace('width not saved: '+obj.id+"Width");	
				}
				
				var handle = document.getElementById(obj.id+"ResizeHandle");
				if (handle != null) {
//					handle.style.border="none";
				}/*
				var obj1 = document.getElementById(obj.id+'BottomRight');
				if (obj1 !=null) {
					obj1.style.width='120px';
					obj1.style.height='32px';
				} */
			//	animatePageActiveA5=false;
				
			break;
		}
	} else {
		trace('currentResizeDiv / currentResizeDiv not found!');
	}
}
function setRestoreValues(obj) {
	var restoreStoreObj = document.getElementById(obj.id+"Restore");// currentBubble.id+"Restore"
	if ( restoreStoreObj != null && !inBubbleBar(obj.id) ) {
		// record the value for bubPos.ltwhz
		var str = obj.offsetLeft+','+obj.offsetTop+','+obj.offsetWidth+','+obj.offsetHeight+','+obj.style.zIndex; 
		restoreStoreObj.value = str;
	}
}
function stopBubResize() {
	
	if (!mouseIsDown) {
		//var contentObject = document.getElementById(currentResizeDivID); 
		//trace('stopResize '+currentResizeDiv);
		if (currentResizeDiv != null) {
			// very heavy to drag around in Mizzilla and other browsers
			currentResizeDiv.style.width = currentResizeDiv.offsetWidth-54 + 'px';
			currentResizeDiv.style.height = currentResizeDiv.offsetHeight-64 + 'px';
			//currentResizeDiv.style.padding = "28px 20px 20px 5px";
			// we ain't using this !!
	//		alert('stopBubResize: '+currentResizeDiv.id);
			positionDragHandle();
			currentResizeDiv.style.display='block';
			currentDragDivA5=null;
			currentResizeDiv=null;
			/*
			
			Resize the iFrame if there is one inside the bubble.
			
			*/
		} else{
			//trace('contentObject not found ' + contentID);
		}
		resizeDivActiveA5=false;
	//	setRestoreValues(obj);
		
		
		recordBubPosition();
	}
	
}

function getMouseXYA5(e) {
	if (IE) { 
		tempXA5 = event.clientX + document.body.scrollLeft;
		tempYA5 = event.clientY + document.body.scrollTop;
	} else { 
	// IE 8  seems to pick this up.. and doesn't like it.
	// problem is that we're not checking the browser out !!
	// ie doesn't do the quirksmode test .. Argh.
		tempXA5 = e.pageX;
		tempYA5 = e.pageY;
	}  
	if (tempXA5 < 0){ 
		tempXA5 = 0;
		}
	if (tempYA5 < 0){ 
		tempYA5 = 0;
	}  
	//trace('tempXA5: '+tempXA5+' tempYA5: '+tempYA5);
	if (animatePageActiveA5) {
		animateA5Page();
	}
	if (resizeDivActiveA5) {
		animateA5Page();
	}
	return true;
}

var clickOffsetWidth = 0;
var clickOffsetHeight =0;

function animateA5Page() {
	
	if (!noAnimation && (currentDragDivA5!=null || currentResizeDiv!=null)) {
		if (animatePageActiveA5) {

			if (!resizeDivActiveA5) {
			
				if (isNaN(tempYA5-offsetY)) {
					//offsetY=tempYA5*1.47;
					//offsetY=tempYA5+200;
					if (currentDragDivA5) {
						var divHeight = currentDragDivA5.offsetHeight;
					//	divHeight=parseInt(divHeight.substr(0,divHeight.length-2));
						offsetY=tempYA5+divHeight+20;
					} else {
						trace('no currentDragDivA5');
					}
				}
				if (isNaN(tempXA5-offsetX)) {
					//offsetX=tempXA5*1.47;
					offsetX=100;
				}
				if (tempXA5-offsetX) {
					currentDragDivA5.style.left = (tempXA5-offsetX)+'px';
//					trace(currentDragDivA5.style.left);
				}
				if (tempYA5-offsetY) {
					currentDragDivA5.style.top = (tempYA5-offsetY)+'px';
				}
				
				var str = "";
				for (var i=0; i<isDockedArray.length; i++) {
					str += "<li>"+isDockedArray[i]+"</li>";
				}
				str="<ul>"+str+"</ul>";
				//trace('is docked: '+str);
			}
			
		}
		if (resizeDivActiveA5) {
			var anch=document.getElementById(targetDivA5);
			var anchorLeft=anch.offsetLeft;//+containerLeftSpace;
			var anchorTop=anch.offsetTop;
			var farInWidth=currentResizeDiv.offsetwidth+currentResizeDiv.offsetLeft-tempXA5;
			var farInHeight=currentResizeDiv.offsetHeight+currentResizeDiv.offsetTop-tempYA5;
			var newWidth=tempXA5-currentResizeDiv.offsetLeft-anchorLeft;
			var newHeight=tempYA5-currentResizeDiv.offsetTop-anchorTop;
			var changeWidth=newWidth-currentResizeDiv.offsetWidth;
			var changeHeight=newHeight-currentResizeDiv.offsetHeight;

			// so the 'padding'  is:
			newWidth += clickOffsetWidth;
			newHeight += clickOffsetHeight;
			// now resize the contents & scrolll bar
			if (currentResizeContent) {

				if (newWidth < 200) { newWidth=200; }
				if (newHeight < 250) { newHeight=250; }
				
				var str = currentResizeDiv.id;
				var contentID = str.substr(0,str.length-3);
				var contentObject = document.getElementById(contentID); 
				if (contentObject != null) {
					// very heavy to drag around in Mizzilla and other browsers
					//contentObject.style.width = newWidth-54 + 'px';
					//contentObject.style.height = newHeight-64 + 'px';
					//positionDragHandle();
					// so try
					contentObject.style.display='none';
				}
				
				if (newWidth < 1400) {
					currentResizeDiv.style.width=newWidth+'px';
				} else {
					currentResizeDiv.style.width='1400px';
				}
				if (!isInA5Dock(currentResizeDiv.id)){
					if (newHeight < 800) {
						currentResizeDiv.style.height=newHeight+'px';
					} else {
						currentResizeDiv.style.height='800px';
					}
				}
				
				
			}
		}
	}
}

function bubOpenClose(id,com) {
//	trace('bubOpenClose: id: '+id);
	if (!id) {
		return;
	}
	var obj = document.getElementById(id);
	if (obj != null) {
		
//		trace('found: '+id + ' is in dock: '+isInA5Dock(id));
		bringBubToFront(id);
		
		if (isInA5Dock(id)) {
			// run through the bubDivIdArray hiding all others
			for (var i=0; i<bubDivIdArray.length; i++) {
		//		trace(bubDivIdArray[i]);
				if (isInA5Dock(bubDivIdArray[i])) {
					var eachObj = document.getElementById(bubDivIdArray[i]);
					if (eachObj != null && id != bubDivIdArray[i]) {
						eachObj=eachObj.style;
						eachObj.display='none';
						eachObj.height='0px';
					}	
				}
			}
			currentBublicationDivId=id;
			
			obj=obj.style;
			if (obj.display != 'block') {
				animateBublicationDivOpening=true;
				animateBublicationDiv();
				obj.display='block';
			} else {
				animateBublicationDivOpening=false;
				animateBublicationDiv();
			}
			
			if (!com) return;
			
			switch(com) {
				case "showBasket":
					viewBasket(); // see shop.js
				break;
				case "showOrders":
					viewOrders();// see shop.js
				break;
				case "showProfile":
					getInterface(profileDisplayPath,'profile');
				break;
				case "showMessages":
					getInterface(msgFormPath,'messages');
					setMsgCheckCycle();
					//msgCheckCommand;
				break;
				case "showRelationships":
				
				break;
				case "showMediaPlayer":
				
				break;
				case "showMediaLibrary":
					getInterface(mediaLibraryPath,'mediaLibrary');
				break;
				case "showFavorites":
				
				break;
			}
			
		} else {
			// not in dock
			obj.style.top=null;
			addToDock(obj);
			bubOpenClose(obj.id,'');
	//		trace('not in dock: '+id);
		}
	} else {
//		trace('not found id: ' + id);
		return;
	}
}

/* Quirksmode browser Detect */
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};



