// JavaScript Document
// conf/js/appWin.js
// Application Window Control (Only)
function maskPage() {
	/*
	var obj=document.getElementById('maskPageDiv');
	if (obj !=null) {
		obj=obj.style;
		var str = "url(/lib/conducta/graphics/frost.png)";
		obj.backgroundImage=str;
		obj.position="fixed";
		obj.left="0px";
		obj.top="0px";
		obj.width="100%";
		obj.height="100%";
		obj.zIndex="80";
		obj.display="block";
	}
	*/
	// appMask
	/*
	var obj=document.getElementById('appMask');
	if (obj !=null) {
		obj=obj.style;
		var str = "url(/lib/conducta/graphics/frost.png)";
		obj.backgroundImage=str;
		obj.position="absolute";
		obj.left="0px";
		obj.top="0px";
		obj.width="100%";
		obj.height="100%";
		obj.zIndex="10";
		obj.display="block";
	}
	*/
}
function unMaskPage() {
	var obj=document.getElementById('maskPageDiv');
	if (obj !=null) {
		obj=obj.style;
		obj.display="none";
	}
	/*
	var obj=document.getElementById('appMask');
	if (obj !=null) {
		obj=obj.style;
		obj.display="none";
	}
	*/
}
function bringAppToFront(id) {
	for (var i=0; i<appDivIdArray.length; i++) {
		var obj=document.getElementById(id);
		
		if (obj!=null) {
			if (appDivIdArray[i]==id) {		
				obj.style.zIndex=10;
			//	alert('bringAppToFront('+id+')');
			} else {
				var obj2=document.getElementById(appDivIdArray[i]);
				if (obj2 != null) {
					obj2.style.zIndex--;
				}
			}
		}
	}
}
function dragAppDiv(obj,e) {
	switch(e) {
		case "start":
			animatePageActive=true;
			currentDragDiv=obj;
			removeFromDock(currentDragDiv);
			bringAppToFront(currentDragDiv.id);
			currentDragDiv.style.position="absolute";
			dragDivStartPositionX = parseInt(currentDragDiv.style.left);
			dragDivStartPositionY = parseInt(currentDragDiv.style.top);		
			offsetX = tempX-dragDivStartPositionX;
			offsetY = tempY-dragDivStartPositionY;
		break;
		case "stop":
			animatePageActive=false;
			currentDragDiv=obj;
			var str = currentDragDiv.style.top;
			str=parseInt(str.substr(0,str.length-2));
			str=tempY-offsetY;
			if (str > -220) {
				// magnetic attract to the dock.
	//			currentDragDiv.style.top=null;
	//			addToDock(currentDragDiv);
			}
		break;
	}
}
/*
function resizeAppDiv(obj,e,cont,scr) {
	switch(e) {
		case "start":
			resizeDivActive=true;
			currentResizeDiv=obj;
			currentResizeContent=cont;
			currentResizeScrollbar=scr;
			bringAppToFront(currentResizeDiv.id);
			var ssObj=document.getElementById("ss");
			if (ssObj != null) {
				ssObj.focus();
			}
			
		break;
		case "stop":
			if (currentResizeDiv != null) {
				for(var i=0; i<appDivIdArray.length;i++) {
					if (appDivIdArray[i]==currentResizeDiv.id) {
						appDivMaxHeightArray[i]=currentResizeDiv.offsetHeight;
					}
				}
			}
			resizeDivActive=false;
			currentResizeDiv=null;
			currentResizeContent='';
			currentResizeScrollbar='';
			var ssObj=document.getElementById("ss");
			if (ssObj != null) {
				ssObj.focus();
			}
		break;
	}
}
*/
function resizeAppDiv(obj,e,cont,scr) {
	//trace('obj: '+obj+', e: '+e+', cont: '+cont);
	if (obj != null) {
		switch(e) {
			case "start":
				var anch=document.getElementById('container');
			// right of the div x coord
				var divRightX = obj.offsetWidth + obj.offsetLeft;
			// bottom of the div y coord
				var divBotY = obj.offsetHeight + obj.offsetTop;
				clickOffsetWidth = (divRightX-tempX+anch.offsetLeft);
				clickOffsetHeight = (divBotY-tempY);
				resizeDivActive=true;
				currentResizeDiv=obj;
				currentResizeContent=cont;
				bringAppToFront(currentResizeDiv.id);
				
			break;
			case "stop":
				currentResizeDiv=obj;
				setRestoreValues(obj);
				resizeDivActive=false;
				currentResizeDiv=null;
				currentResizeContent=null;
				var heightStoreObj = document.getElementById(obj.id+"Height");
				if (heightStoreObj != null && obj.offsetHeight > collapseTrigger) {
					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");	
				}
				
			break;
		}
	} else {
		trace('currentResizeDiv / currentResizeDiv not found!');
	}
}





function stopResize() {
	if (currentResizeDiv) {
	for(var i=0; i<appDivIdArray.length;i++) {
		if (appDivIdArray[i]==currentResizeDiv.id) {
			appDivMaxHeightArray[i]=currentResizeDiv.offsetHeight;
		}
	}
	}
	resizeDivActive=false;
	currentResizeDiv=null;
	var ssObj=document.getElementById("ss");
	if (ssObj != null) {
		ssObj.focus();
	}
}

function appOpen(id) {

	if (!id) return;
	var obj = document.getElementById(id);
	if (obj != null) {
		bringAppToFront(id);
		currentApplicationDivId=id;
		obj=obj.style;
		if (obj.display != 'block') {
			animateApplicationDivOpening=true;
			animateApplicationDiv();
			obj.display='block';
			bottomOffset=bottomOffsetOpenApp;
//			pageBodyResize();
		}
	}
}
function appClose(id) {
	if (!id) return;
	currentDragHandle = null;
	var obj = document.getElementById(id);
	if (obj != null) {
		bringAppToFront(id);
		currentApplicationDivId=id;
		obj=obj.style;
		if (obj.display != 'none') {
			animateApplicationDivOpening=true;
			animateApplicationDiv();
			obj.display='none';
			bottomOffset=bottomOffsetOpenApp;
			pageBodyResize();
		}
	}
}

function appOpenClose(id,com) {
	if (!id) return;
	currentDragHandle = null;
	var obj = document.getElementById(id);



	if (obj != null) {
		bringAppToFront(id);
//		trace('appOpenClose showBasket');
		if (isInDock(id)) {
			
			// run through the appDivIdArray hiding all others
			for (var i=0; i<appDivIdArray.length; i++) {
				if (isInDock(appDivIdArray[i])) {
					var eachObj = document.getElementById(appDivIdArray[i]);
					if (eachObj != null && id != appDivIdArray[i]) {
						eachObj=eachObj.style;
						eachObj.display='none';
						eachObj.height='0px';
					}	
				}
			}
			currentApplicationDivId=id;
			
			obj=obj.style;
			if (obj.display != 'block') {
				animateApplicationDivOpening=true;
				animateApplicationDiv();
				obj.display='block';
			} else {
				animateApplicationDivOpening=false;
				animateApplicationDiv();
			}
			
			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);
//			appOpenClose(obj.id,'');
		}
	} else {
		trace('not found: '+id);
		return;
	}
}

// 																											Why this not Work ???

function appCollapse(divId,collapseImgId) {
	if (!divId) return;
	var obj = document.getElementById(divId);
	if (obj != null) {
		if (!isInDock(divId)) {
			currentApplicationDivId=divId;
			obj=obj.style;
			if (obj.height == '60px') {
				animateApplicationDivOpening=true;
				var imgObj=document.getElementById(collapseImgId);
				if (imgObj != null) {
					imgObj.src=appWinIsOpenImage; 
				}
				animateApplicationDiv();
			} else {
				animateApplicationDivOpening=false;
				animateApplicationDiv();
				var imgObj=document.getElementById(collapseImgId);
				if (imgObj != null) {
					imgObj.src=appWinIsCollapsedImage; 
				}
			}
		} else {
	//		trace('in dock: '+divId);
		}
	} else {
//	trace('missing: '+divId);
	}
}

function animateApplicationDiv() {

	var obj=document.getElementById(currentApplicationDivId);
	if (obj != null) {
		var objHeightValue=obj.offsetHeight; 
		
		for(var i=0; i<appDivIdArray.length;i++) {
			if (appDivIdArray[i]==currentApplicationDivId) {
				applicationDivMaxHeight=appDivMaxHeightArray[i];
			}
		}
	
		if (animateApplicationDivOpening) {
			objHeightValue += ((applicationDivMaxHeight - objHeightValue)/2);
		} else {
			objHeightValue = Math.floor(objHeightValue/2);
			if (!isInDock(currentApplicationDivId) && objHeightValue<55) {
				objHeightValue=60;
				obj.style.display='block';
				window.clearTimeout(animateAppDivTimeId);
			}
		}

		if ((!animateApplicationDivOpening && objHeightValue<5) 
			 || (animateApplicationDivOpening && objHeightValue>applicationDivMaxHeight-1)) {
			
			window.clearTimeout(animateAppDivTimeId);
			if (!animateApplicationDivOpening) {
				obj.style.display='none';
				obj.style.height='0px';
			} else {
				obj.style.height=applicationDivMaxHeight+'px';
			}
		} else {
			obj.style.height=String(objHeightValue+'px');
			animateAppDivTimeId = window.setTimeout('animateApplicationDiv()',100);
		}
	} else {
		trace("Error! "+currentApplicationDivId+" Not Found");
	}
}
