var highZ = 100;
var lowZ = 99;

var floatingWins;

function floatingWinSetup(){
  //xMoveTo('d2', 200, 350);
  
  floatingWins = getElementsByClassName(document, "*", "floatingWin");
  
  for(i=0; i < floatingWins.length; i++){
    if(isClass(floatingWins[i], 'draggable')){

      	var handleElm = byId(floatingWins[i].getAttribute('id').replace('Win', 'FloatingWinHandle'));//getElementsByClassName(floatingWins[i], "*", "handle")[0];

      	handleElm.style.cursor = 'move';
  		xEnableDrag(handleElm, floatingWinOnDragStart, floatingWinOnDrag, null);
	}
  }
  //xShow('d2');
}

function showFloatingWindow(id){
  var elm = byId(id+'Win');
  
  elm.style.display = '';
  
  //move the current window above any other windows
  moveWinToTop(id+'Win');
 
  if(isClass(elm, 'display_centered')){
	centerFloatingWindow(id);
  }
  
  if(isClass(elm, 'modal')){
	addFloatingWinModalBGDiv(id);
  }
}

function hideFloatingWindow(id){
  byId(id+'Win').style.display = 'none';

  //remove the modal background that prevents the user clicking on page elements (if it exists)	
  var modalBG = byId('floatingWinModalBG'); 
  if(modalBG){
  	modalBG.parentNode.removeChild(modalBG);
  }
}

function hideAllFloatingWindows(){
  for(i=0; i < floatingWins.length; i++){
 	hideFloatingWindow(floatingWins[i].getAttribute('id').replace('Win', ''));
  }
}

function floatingWinOnDragStart(ele, mx, my){
  var winId = ele.getAttribute('id').replace('FloatingWinHandle', 'Win');

  moveWinToTop(winId);
}

function moveWinToTop(winId){
  //adjust zIndex of all windows so the newly selected window is on top
  for(i=0; i < floatingWins.length; i++){
    var curWinId = floatingWins[i].getAttribute('id');
    
  	if(curWinId == winId){
  		xZIndex(curWinId, highZ);
  	}
  	else{
  	 	xZIndex(curWinId, lowZ);
	}
  } 
  //xZIndex('calendarWin', highZ++);
}

function floatingWinOnDrag(ele, mdx, mdy){
  var winId = ele.getAttribute('id').replace('FloatingWinHandle', 'Win');
  
  xMoveTo(winId, xLeft(winId) + mdx, xTop(winId) + mdy);
}

function addFloatingWinModalBGDiv(id){
	var modalBG = document.createElement('div');

	modalBG.id = 'floatingWinModalBG';
	modalBG.className = 'floatingWinModalBG';
	
	var h = Math.max(
			document.documentElement.scrollHeight || document.body.scrollHeight,
			getViewportHeight());
	var w = getViewportWidth();
	
	with(modalBG.style) {
		width = w + 'px';
		height = h + 'px';
	}
	
	document.getElementsByTagName('body')[0].appendChild(modalBG);
	
	//var modalBG = byId('floatingWinModalBG');
	//modalBGIframe = new dojo.html.BackgroundIframe(modalBG);
	//addEvent(modalBG, 'click', function(){ pulsateFloatingWindow(id); }, false);
	modalBG.onclick = function(e){ 
		pulsateFloatingWindow(id); 
	};
	
	if(isIE){
		var bgIframe = addBgIframe(modalBG);
		//iframe.onclick = modalBG.onclick;

		bgIframe.contentWindow.document.onclick = function() { pulsateFloatingWindow(id); };
		//addEvent(bgIframe, 'click', function(){ alert('ian'); }, true);
	}
		
	//if(modalBGIframe.iframe){
	//	modalBGIframe.iframe.contentWindow.document.onclick = modalBG.onclick;
	//}
}

function pulsateFloatingWindow(id){
  	var handleElm = byId(id+'FloatingWinHandle');//getElementsByClassName(byId(id+'Win'), "*", "handle")[0];
	handleElm.className = 'handleHighlight';
    
    setTimeout(
		function(){ 
	  		handleElm.className = 'handle';
			setTimeout( 
				function(){ 
			  		handleElm.className = 'handleHighlight';
				    setTimeout( 
						function(){ 
					  		handleElm.className = 'handle';
						}, 100);
				}, 100);
		}, 100);
 
};

function centerFloatingWindow(id){
	var elm = byId(id+'Win');

	var winWidth = getBrowserWindowWidth();
	var winHeight = getBrowserWindowHeight(); 
	
	var elmWidth = 0;
	var elmHeight = 0;
	
	if(elm.style.width && elm.style.width.indexOf('px') != -1){
	  	elmWidth = elm.style.width.replace('px', '');
	}
	else if(elm.offsetWidth){
	  	elmWidth = elm.offsetWidth;
	}
	
	if(elm.style.height && elm.style.height.indexOf('px') != -1){
	  	elmheight = elm.style.height.replace('px', '');
	}
	else if(elm.offsetHeight){
	  	elmHeight = elm.offsetHeight;
	}
	
	var xpos = Math.floor((winWidth - elmWidth) / 2);
	var ypos = Math.floor((winHeight - elmHeight) / 2);		
	
	if(xpos < 0){
	  xpos = 0;
	}
	
	if(ypos < 0){
	  ypos = 0;
	}
	
	moveDivTo(id+'Win', xpos, ypos);
		
};
