/**********************************************************************
	BioWare Javascript File
	
	File: mc_functions.js
	
	Description: Movable Content Functions - calls additional functions in layer_functions.js
	
	Created: Robin Mayne, April 11, 2002 Copyright 2002 BioWare Corp.
***********************************************************************	
	Version: 0.0.3
	
	Version History:
		0.0.1 / 11.04.02 / Created / RM
		0.0.2 / 16.04.02 / Updated / RM
						Calls setWidth() function	
		0.0.3 / 16.04.02 / Updated / RM
						Removed setNavPosition() - placed in header.inc	

***********************************************************************/

/******************************************************************************
* Netscape 4.0 has problems with resizing and then losing its CSS 			  *
* variables and stuff. This Fix is a common Net Fix for the problem.		  *
******************************************************************************/

function WM_netscapeCssFix() {
  if (document.WM.WM_netscapeCssFix.initWindowWidth != window.innerWidth ||
        document.WM.WM_netscapeCssFix.initWindowHeight != window.innerHeight) {
     document.location = document.location;
    }
}

function WM_netscapeCssFixCheckIn() {
  if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) { 
	if (typeof document.WM == 'undefined'){
       document.WM = new Object;
       }
    if (typeof document.WM.WM_scaleFont == 'undefined') {
       document.WM.WM_netscapeCssFix = new Object;
       document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
       document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
       }
    window.onresize = WM_netscapeCssFix;
  }
}

WM_netscapeCssFixCheckIn();

function fakeLoad() { //Need to set to window.onload, and need to add height support
	if (HM_Opera || HM_IE4M) {
	    //oldWidth = cbeInnerWidth();
		//myWindowWidth = getWindowWidth();
	    //oldHeight = cbeInnerHeight();
	    resizeHandler();
	}
}  
function resizeHandler()
{
  if (HM_Opera || HM_IE4M) {
    if (getWindowWidth() != windowwidth) {
      location.replace(location.href);
    }
    else {
      setTimeout("resizeHandler()",500);
    }  
  } 
} 

//set the cursor to focus on the community login form
// NOTE: focusOnLoad must be declared and set in header.inc, 
//           or else this function does nothing

//set focusOnLoad globally to avoid errors if it isn't defined in header.inc
if ( typeof(focusOnLoad) == "undefined" ) {
  var focusOnLoad = "";
} 

function setCommLoginFocus() 
{
  if ( ! HM_NS4 )
  {
    if ( focusOnLoad == "password" ) 
    {
      document.community_login.password.focus();    
    }
    else if ( focusOnLoad == "username" ) 
    {
      document.community_login.username.focus();        
    }
  }
}

function startItUp() {
	setNavPosition();
	fakeLoad();
	setCommLoginFocus();
}

// Global Vars
	var divspacex = 1;
	var divspacey = 10;
	var windowwidth, windowmiddle;
	// these get set in setNavPosition() depending on screensize
	var divstartx, divstarty; // divcols

function divDisplay(divcols) { // display divs where divcols equals number of columns
	for (i = 0; i < divarray.length; i++) {
	// write out div positioning of MC
		if (i == 0) { // first div
			eval("moveToShow('"+ divarray[i] +"', divstartx, divstarty);");
		}
		else if ((i % 2) == 1) { // additional odd divs - bottoms
			eval("moveToShow('"+ divarray[i] +"', findX('"+ divarray[i-1] +"image'), findY('"+ divarray[i-1] +"image'));");
		}
		else if ((i % 2) == 0) { // additional even divs - tops
			
			if (((i/2) % divcols) == 0) { // 1st column
				divtopx = divstartx;
			}
			else { // Any column but 1st column
				xprevious = eval("findX('"+ divarray[i-2] +"image');");
				divtopx = xprevious + 200 + divspacex;
			}
			
			if (i < (divcols*2)) { // 1st row
				divtopy = divstarty;
			}
			else { // Any row but 1st row
				divtopy = eval("findY('"+ divarray[i-((divcols*2)-1)] +"image');") + divspacey;
			}		
			eval("moveToShow('"+ divarray[i] +"', "+divtopx+", "+divtopy+");");
		}			
	}

}	
	
var move_timeout;
function timedMoveTo(startdiv, movedir) {	
	var islastdiv = 0;

	// find div position within the div array
	for (i = 0; i < divarray.length; i++) {
		if (startdiv == divarray[i]) {
			// check if startdiv is last div in array
			if ((i+1) == divarray.length) {
				var islastdiv = 1;
			}
			var divPosition = i+1;
			var divnext = divarray[i+1];
			break
		}
	}
	// repositioning code
	if (!islastdiv) { // if last div, repositioning code does not need to execute
		ndivnext = getLayer(divnext);
		
		if (movedir == "up") {
			moveamount = -20;
			startdivrefimage = divarray[i-1] + "image";
		}
		else {
			moveamount = 20;
			startdivrefimage = divarray[i] + "image";
		}
		var startdivrefimagey = findY(startdivrefimage);
		var finalpos = startdivrefimagey + divspacey;
		var nextpos = getTop(ndivnext)+moveamount;
		
		movecommands = "";	
		if (((movedir == "up") && (nextpos < finalpos)) || ((movedir == "down") && (nextpos > finalpos))) {
			movecommands = movecommands + "moveLayerTo(n"+ divnext +", divstartx, findY('"+ startdivrefimage +"') + divspacey);";
		}
		else {
			movecommands = movecommands + "moveLayerBy(n"+divnext+", 0, moveamount);";
		}
		
	
		// check each div after the altered one, and move appropriately
		for (i = (divPosition + 1); i < divarray.length; i++) { 				
		
			// check for first previous visible div, and set that as the reference div
			for (b = 1; b < divarray.length; b++) {
				myflag = eval(divarray[i-b] + "vis");	
				if (myflag) {
					var divref = divarray[i-b];
					break
				}
			}
			
			// now move div under reference div
			if ((i % 2) == 1) { // odd divs - bottoms
				movecommands = movecommands + "moveLayerTo(n"+ divarray[i] +", divstartx, findY('"+ divref +"image'));";
			}
			else if ((i % 2) == 0) { // even divs - tops
				movecommands = movecommands + "moveLayerTo(n"+ divarray[i] +", divstartx, findY('"+ divref +"image') + divspacey);";
			}
	
		}
		//alert(getTop(ndivnext));
		eval(movecommands);
		move_timeout = eval("setTimeout('timedMoveTo("+"\""+ startdiv + "\",\"" + movedir +"\"" +");', 1)");
		
		if (movedir == "up") {
			if ((getTop(ndivnext)) <= (findY(startdivrefimage)) + divspacey) {
				//alert(getTop(ndivnext)+" <= "+findY(startdivrefimage)+" + "+divspacey);
				clearTimeout(move_timeout);
			}
		}
		else {
			if ((getTop(ndivnext)) >= (findY(startdivrefimage)) + divspacey) {
				//alert(getTop(ndivnext)+"  >= "+findY(startdivrefimage)+" + "+divspacey);
				clearTimeout(move_timeout);
			}
		}	
	}
	
}

function MinMaxDiv(nlayer) { // Switch div visibility and reposition following divs
		
	// Reverse div visiblity
	flag = eval(nlayer + "vis");
	if (flag) { // div is visible
		eval("hideLayer(n" + nlayer + ")");
		eval(nlayer + "vis = 0");
		timedMoveTo(nlayer, 'up');
	}
	else { // div is hidden
		eval("showLayer(n" + nlayer + ")");
		eval(nlayer + "vis = 1");
		timedMoveTo(nlayer, 'down');
	}		
	
}

// Create Movable Content Div Array
var divarray = new Array();

var divindex = 0;
function loadDiv(divname) { // loads div names into array
    divarray[divindex] = divname;
    divindex++;
}

function listDivs() { // lists all values in array
	divList = "There are " + divarray.length + " divs in the array...\n";
	for (i = 0; i < divarray.length; i++) {
		divList = divList + "\n"+ divarray[i] +"\n";
	}
	alert(divList);
}

	