//object constructor for MB DHTML library
var MB = {

	//=========================================================================================================\\
	// SUPPORT FUNCTIONS (used by the higher-level functions to access the various browser's DOM)              \\
	//=========================================================================================================\\


				//Crossbrowser code to grab an id-based dom object
	getElement:		function(id) {
				  if(document.getElementById) return document.getElementById(id);	//DOM based browsers
				  else if(document.all) return document.all[id];				//Internet Explorer 4
				  else if(document.layers) return MB.find(id);				//Netscape 4
				  else return null;													//Other browsers
				},


				//Crossbrowser code to grab the styles for an id-based dom object
	getStyles:		function(id) {
				  if(document.getElementById) return document.getElementById(id).style;	//DOM based browsers
				  else if(document.all) return document.all[id].style;			//Internet Explorer 4
				  else if(document.layers) return MB.find(id);				//Netscape 4
				  else return null;							//Other browsers
				},


				//returns the x position of a layer
	x:			function(id) {
				  return parseInt(MB.getStyles(id).left);
				},

				//returns the y position of a layer
	y:			function(id) {
				  return parseInt(MB.getStyles(id).top);
				},

				//returns the width of a layer (as specified in the style-sheet or style-tag)
	w:			function(id) {
				  return parseInt(MB.getStyles(id).width);
				},

				//returns the height of a layer (as specified in the style-sheet or style-tag)
	h:			function(id) {
				  return parseInt(MB.getStyles(id).height);
				},



	//=========================================================================================================\\
	// BASIC FUNCTIONS (a set of simple functions to manipulate layers)                                        \\
	//=========================================================================================================\\

				//shows a layer (with the optional ability to fade between states)
	show:			function(id, fade) {
				  var ie55 = (navigator.appVersion.indexOf("MSIE 5.5")>-1 && document.getElementById && !navigator.userAgent.indexOf("Opera")>-1 )?1:0; 
				  var ie6 = (navigator.appVersion.indexOf("MSIE 6")>-1 && document.getElementById && !navigator.userAgent.indexOf("Opera")>-1 )?1:0; 

				  if((ie55 || ie6) && fade) {
				    MB.getStyles(id).filter = "progid:DXImageTransform.Microsoft.Fade(duration=" + fade + ")";
				    MB.getElement(id).filters[0].apply();
				    MB.getStyles(id).visibility = "visible";
				    MB.getElement(id).filters[0].play();
				  } else {
				    MB.getStyles(id).visibility = "visible";
				  }
				},

				//hides a layer
	hide:			function(id, fade) {
				  var ie55 = (navigator.appVersion.indexOf("MSIE 5.5")>-1 && document.getElementById && !navigator.userAgent.indexOf("Opera")>-1 )?1:0; 
				  var ie6 = (navigator.appVersion.indexOf("MSIE 6")>-1 && document.getElementById && !navigator.userAgent.indexOf("Opera")>-1 )?1:0; 

				  if((ie55 || ie6) && fade) {
				    MB.getStyles(id).filter = "progid:DXImageTransform.Microsoft.Fade(duration=" + fade + ")";
				    MB.getElement(id).filters[0].apply();
				    MB.getStyles(id).visibility = "hidden";
				    MB.getElement(id).filters[0].play();
				  } else {
				    MB.getStyles(id).visibility = "hidden";
				  }
				},


	//=========================================================================================================\\
	// COOKIES (Allows you to save and retrieve page settings)                                                 \\
	//=========================================================================================================\\

	getCookie:		function(name, d) {
				  if (!d) var d = null;
				  var arg = name + "=";
				  var alen = arg.length;
				  var clen = document.cookie.length;
				  var i = 0;
				  while (i < clen) {
				    var j = i + alen;
				    if (document.cookie.substring(i, j) == arg) {
				      var endstr = document.cookie.indexOf (";", j);
				      if (endstr == -1) endstr = document.cookie.length;
				      return unescape(document.cookie.substring(j, endstr));
 				    }
				    i = document.cookie.indexOf(" ", i) + 1;
				    if (i == 0) break;
				  }
				  return d;
				},

	setCookie:		function(name, value, expires) {
				  if (expires) {
				    var exp = new Date();
				    exp.setTime(exp.getTime() + (expires*60*60*1000));
				    expires = exp;
				  }
				  document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString()));
				},

	deleteCookie:		function(name) {
				  var exp = new Date();
				  exp.setTime (exp.getTime() - 1000);
				  var cval = GetCookie (name);
				  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
	},

	//=========================================================================================================\\
	// IMAGES (Functions for preloading images, and creating rollovers)                                        \\
	//=========================================================================================================\\


	images:			new Array(),		//used to store precached images
	rollovers:		new Array(),		//used to store rollovers

				//preloads image(s)
	preload:		function() {
				  for(var i=0; i<arguments.length; i++) {
				    var img = new Image();
				    img.src = arguments[i];
				    MB.images.push(img);
				  }
				},

				//creates a rollover, events are added to "caller" and affect "target"
	makeRollover:		function(target,caller,onImage) {
				  var offImage = MB.findImg(target).src;
				  MB.preload(onImage,offImage);
				  MB.rollovers[caller] = {target: target, on: onImage, off: offImage};
				  MB.getElement(caller).onmouseover = function() {
				    if(MB.rollovers[this.id]) {
				      MB.findImg(MB.rollovers[this.id].target).src = MB.rollovers[this.id].on;
				    }
				  }
				  MB.getElement(caller).onmouseout = function() {
				    if(MB.rollovers[this.id]) {
				      MB.findImg(MB.rollovers[this.id].target).src = MB.rollovers[this.id].off;
				    }
				  }

				  if(document.layers) {
				    MB.getElement(caller).captureEvents(Event.MOUSEOVER);
				    MB.getElement(caller).captureEvents(Event.MOUSEOUT);
				  }
				},
				
	imgSwap:		function(target,img) {
				  if(MB.findImg(target)) MB.findImg(target).src = img;				    
				},


	//=========================================================================================================\\
	// MISC (Random helper functions)                                                                          \\
	//=========================================================================================================\\

				//Creates a popup window with options to set the appearance
	popup:			function(filename, title, width, height, x, y,scrollbars,statusbar,resize,toolbar) {
				  if(x=='c') x = (screen.width / 2) - (width / 2);
				  if(y=='c') y = (screen.height / 2) - (height / 2);


				  if(scrollbars == null) scrollbars=1;
				  if(statusbar == null) statusbar=0;
				  if(resize == null) resize=0;
				  if(toolbar == null) toolbar=0;
	
				  NewWindow = window.open(filename, title, "status="+statusbar+",scrollbars="+scrollbars+",resizable="+resize+",toolbar="+toolbar+",menubar="+toolbar+",width="+width+",height="+height+",left="+x+",top="+y);
               
				  if(!NewWindow.opener) {
				    NewWindow.opener = self;
				  }	
				},


				// A function by: Michael van Ouwerkerk,  February 9 2002,  Updated for frames capability at March 19, 2002
				// find takes an id string as argument and returns a js reference to the image.
				// This method is used primarily for Netscape 4, to automatically find images that are nested in layers.
				// It can now also find an image in a frame, if the frame's name is given in the function call.
				// Example: MB.find('imgMinimalsm', 'mainFrame')
	find:			function(id, frame, d) {
				  var img = null;
				  if (!d) {
				    if (frame && top[frame]) d = top[frame].document;
				    else d = document;
				    if (!d) return null;
				  }
				  if (d.layers) { 
				    img = d.layers[id];
				    if(!img) img = d.links[id];
				    if(!img) img = d.images[id];
				  }
				  else if (d.getElementById) img = d.getElementById(id);
				  for (var i=0; !img && d.layers && i<d.layers.length; i++){
				    img = MB.find(id, null, d.layers[i].document);
				  }
				  return img;
				},

	findImg:		function(id, frame, d) {
				  var img = null;
				  if (!d) {
				    if (frame && top[frame]) d = top[frame].document;
				    else d = document;
				    if (!d) return null;
				  }
				  if (d.images) img = d.images[id];
				  else if (d.getElementById) img = d.getElementById(id);
				  for (var i=0; !img && d.layers && i<d.layers.length; i++){
				    img = MB.findImg(id, null, d.layers[i].document);
				  }
				  return img;
				},

	writeText:		function(id,text,w) {

						if (document.getElementById)
						{
							x = document.getElementById(id);
							x.innerHTML = text;
						}
						else if (document.all)
						{
							x = document.all[id];
							x.innerHTML = text;
						}
						else if (document.layers)
						{
							x = MB.find(id);
							
							text2 = "<table width=" + w + " border=0 cellpadding=0 cellspacing=0><tr><td><span class=standard1>" + text + "</span></td></tr></table>";
							x.document.open();
							x.document.write(text2);
							x.document.close();
						}
					},
					
		//move functionality
		
						//moves a layer to a specific screen coordinate

	moveTo:	function(id,x,y) {

				  if(document.getElementById) var unit = "px";
				  else var unit = 0;

				  MB.getStyles(id).left = x + unit;
				  MB.getStyles(id).top = y + unit;
				},

				//clips a layer to specific dimentions

	clipTo:	function(id,x,y,w,h) {

				  if(document.getElementById) var unit = "px";
				  else var unit = 0;

				  if(document.layers) {
				    MB.getStyles(id).clip.top = y;
				    MB.getStyles(id).clip.left = x;
				   MB.getStyles(id).clip.bottom = y+h;
				    MB.getStyles(id).clip.right = x+w;
				  } else
				    MB.getStyles(id).clip = "rect(" + x + unit + " " + (x+w) + unit + " " + (y+h) + unit + " " + y + unit + ")";
				},



				//moves a layer to by a specific number of units

	moveBy:		function(id,x,y) {

				  if(document.getElementById) var unit = "px";
				  else var unit = 0;

				  MB.getStyles(id).left = (MB.x(id) + x) + unit;
				  MB.getStyles(id).top = (MB.y(id) + y) + unit;

				},

	//=========================================================================================================\\
	// TIMESLIDE (Moves a layer between 2-points over a specified duration)                                    \\
	//=========================================================================================================\\

	moves:			new Array(),

	cancelMoves:		new Array(),

	moving:			new Array(),
	
	coord:			function (x,y) { if(!x) var x=0; if(!y) var y=0; return {x: x, y: y}; },

	B1:			function(t) { return (t*t*t); },

	B2:			function(t) { return (3*t*t*(1-t)); },

	B3:			function(t) { return (3*t*(1-t)*(1-t)); },

	B4:			function(t) { return ((1-t)*(1-t)*(1-t)); },
	
	timeSlide:		function(id,x,y,duration,acc,func,cancel,timeout) {

				  if(!cancel) var cancel = false;
				  if(!func) var func = "null";
				  if(!timeout) var timeout = 5;
				  if(!acc) var acc = 0;

				  if(!MB.moves[id]) MB.moves[id] = new Array();
				  if(!MB.cancelMoves[id]) MB.cancelMoves[id] = false;
				  if(!MB.moving[id]) MB.moving[id] = false;

				  var startTime = new Date().valueOf();
				  var endTime = startTime + duration;

				  if(!MB.moving[id]) {	//object isn't already moving so start animation
				    MB.moving[id] = true;
				    MB.timeSlideAux(id,MB.x(id),MB.y(id),x,y,acc,startTime,endTime,func,cancel,timeout);
				  } else {
				    if(cancel) {		//cancel other moves
				      MB.cancelMoves[id] = true;
				      MB.moves[id] = new Array();
				    }

				    //add this movement to array of movements
				    var strExec = "MB.timeSlide('" + id + "'," + x + "," + y + "," + duration + "," + acc + ",'" + func + "'," + cancel + "," + timeout + ")";
				    MB.moves[id].push(strExec);

				  }		

				},

	getBezier:		function (percent,startPos,endPos,control1,control2) {

				  if(!control2 && !control1) var control2 = new MB.coord(startPos.x + 3*(endPos.x-startPos.x)/4, startPos.y + 3*(endPos.y-startPos.y)/4);
				  if(!control2) var control2 = control1;
				  if(!control1) var control1 = new MB.coord(startPos.x + (endPos.x-startPos.x)/4, startPos.y + (endPos.y-startPos.y)/4);
				
				  var pos = new MB.coord();

				  pos.x = startPos.x * MB.B1(percent) + control1.x * MB.B2(percent) + control2.x * MB.B3(percent) + endPos.x * MB.B4(percent);
				  pos.y = startPos.y * MB.B1(percent) + control1.y * MB.B2(percent) + control2.y * MB.B3(percent) + endPos.y * MB.B4(percent);

				  return pos;

				},


	timeSlideAux:		function(id,startX,startY,endX,endY,acc,startTime,endTime,func,cancel,timeout) {
	
				  var curTime = new Date().valueOf();
				  if(MB.cancelMoves[id]) {
				    MB.moving[id] = false;
				    MB.cancelMoves[id] = false;
				    if(MB.moves[id][0]) eval(MB.moves[id].shift());		
				  } else if(curTime >= endTime) {
				    MB.moveTo(id,endX,endY);
				    MB.moving[id] = false;
				    if(func) eval(func);
				    if(MB.moves[id][0]) eval(MB.moves[id].shift());
				  } else {

				    var percent = (curTime - startTime) / (endTime - startTime); //percentage of way through animation
				    var startPos = new MB.coord(1,1);
				    var endPos = new MB.coord(0,0);	
							
				    if(acc!=0) var c1 = new MB.coord(0.5+(acc/2),0.5-(acc/2));
				    else c1 = null;
				    var pos = MB.getBezier(percent,startPos,endPos,c1);
				    var stage = pos.y;

				    MB.moveTo(id,((endX-startX)*stage)+startX,((endY-startY)*stage)+startY);

				    var strExec = "MB.timeSlideAux('" + id + "'," + startX + "," + startY + "," + endX + "," + endY + "," + acc + "," + startTime + "," + endTime + ",'" + func + "'," + cancel + "," + timeout + ")";
				    setTimeout(strExec,timeout);
				  }

				}
				

}
