// global variables

function loginftp() 
{
	user = DOM.forms[0].elements[0].value;
	pass = DOM.forms[0].elements[1].value;
	window.open("ftp://" + user + ":" + pass + "@www.motiontheory.com","mtftplist","toolbar=1,menubar=1,location=1,status=1,scrollbars=1,RESIZABLE=1");
}


// image preloader
function cache(root, toLoad) {
    for(var i=0; i < toLoad.length; i++) // start preloading
	{ 
		var preloadedImageObject = new Image();
		preloadedImageObject.src = root+toLoad[i];
	} 
} 

var newsLoc = 0;
var newsDest = 0;
var doneMoving = true;

// get positions of all news posts
function getNewsPositions(newsID)
{
	marginOffset = 0; // margin between entries
	positions = Array();
	i=0;
	while(DOM.getElementById(newsID+(i+1)))
	{
		positions[i] = i == 0 ? 0 : (DOM.getElementById(newsID+i).offsetHeight+marginOffset) + positions[i-1];
		i++;
	}
	return positions;
}

function Sprite() { this.steps = 0; }
var newsSprite;
newsSprite = new Sprite(); // store

function newsNav(direction)
{		
	// objects
	newsPositions = getNewsPositions('newsentry');
	if(doneMoving == true )
	{
		newsDest = (newsLoc+direction) >= 0 && (newsLoc+direction) <= newsPositions.length-1 ? newsLoc+direction : newsDest;
		tween('newsbody', 'newsSprite', 0, -newsPositions[newsLoc], 0, -newsPositions[newsDest], 16); // move item
		newsLoc = (newsLoc+direction) >= 0 && (newsLoc+direction) <= newsPositions.length-1 ? newsLoc+direction : newsLoc;
	}
}

//physical move
function move(id, x, y)
{
	obj = DOM.getElementById(id);
	obj.style.left = x + 'px';
	obj.style.top = y + 'px';
}

// ease in
function mv_easein(t,b,c,d) 
{
	return (c*((t=t/d-1)*t*t*t*t + 1) + b);
}

// tween
function tween(objID, obj, sx, sy, x, y, duration)
{
	id  = eval(obj);

	if(duration == 0) // jump
	{
		doneMoving = true; // finished with the move
		move(objID, x, y);
	}
	else if(id.steps < duration) // has steps to go
	{	
		curpos_y = mv_easein(id.steps, sy, y-sy, duration);
		curpos_x = mv_easein(id.steps, sx, x-sx, duration);
		move(objID, curpos_x, curpos_y);
		setTimeout("tween('"+objID+"', '"+obj+"', "+sx+", "+sy+", "+x+", "+y+", "+duration+")", duration);
		id.steps++; // increment steps
		doneMoving = false; // still have some moving to do
	}
	else  // must be finished
	{       
		id.steps = 0; //reset step count
		move(objID, x, y);
		doneMoving = true; // finished with the move
	}
}





/* 

24-bit PNG support for Internet Explorer, as the browser does not support the ~10 year old format. 

Thanks to Drew and Aaron for making this very useful script open source, its contribution to the
web community does not go unnoticed. (Even if the results are transparent...)

*/

/**********************************************************
Sleight for Backgrounds
Original code (c) 2001 Aaron Boodman, http://www.youngpup.net
This version (c) 2003 Drew McLellan, http://www.allinthehead.com
**********************************************************/
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) 
{
	window.attachEvent("onload", alphaBackgrounds);
}

function alphaBackgrounds()
{
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (i=0; i<DOM.all.length; i++){
		var bg = DOM.all[i].currentStyle.backgroundImage;
		if (itsAllGood && bg)
		{
			if (bg.match(/\.png/i) != null)
			{
				var mypng = bg.substring(5,bg.length-2);
				DOM.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
				DOM.all[i].style.backgroundImage = "url('/assets/images/x.gif')";
			}
		}
	}
}












function stripPX(string)
{
	var value = new Array();
	value = string.split('px');
	return parseInt(value[0]);
}

/*
function toggleLogin(show)
{
	var nav, nav_block;

	//if(DOM.getElementById('nav_cube_login'))
	//{		
	
//		nav.style.display = show ? 'block' : 'none';
	//	nav_block.style.backgroundColor = show ? '#1E3041' : '#A9122A';
	//}

	nav = DOM.getElementById('nav_cube_login');
	nav_block = DOM.getElementById('nav_cube');
	
	var donefading = false;

	cfade = new Animator({
				duration: 800,
				onComplete: function() { donefading = true ;}
				});



	if(show){
		nav.style.display = "block";
		cfade.addSubject(new NumericalStyleSubject(nav, 'opacity', 0, 1.0, 4));
	} else {
		cfade.addSubject(new NumericalStyleSubject(nav, 'opacity', 1.0, 0, 4));
	}

	cfade.play();

	if(donefading){
		nav.style.display = show ? 'block' : 'none';
	}
}
*/




	
function toggleLogin(show) {
	var nav, nav_block;
	if(DOM.getElementById('nav_cube_login')){		
		nav = DOM.getElementById('nav_cube_login');
		nav_block = DOM.getElementById('nav_cube');
		nav.style.display = show ? 'block' : 'none';
		nav_block.style.backgroundColor = show ? '#1E3041' : '#A9122A';
	}
}









/*	Load image into main content area
 *	@param	string		img : image to display (with full web path)
 */
 
function v(n) 
{ 

	DOM.splash.style.visibility = "hidden";
	loadIM(n);
}


function loadIM(num){
	
	RAND_IMG = false;
	DOM.splash.style.width = "512px"; // resize

	if(DOM.images)
	{

		if (num != 'st' && DOM.splash.src)
		{
			DOM.splash.src = RAND_IMG ? r(image_root+images[num]) : image_root+images[num];
			current_image = num;
		}
		else if(DOM.splash.src && (images.length-1))
		{   
				// microsoft only
			 if (navigator.appName.indexOf("Microsoft")!=-1)
			 {
				//alert(current_image);
				if(current_image == 'unset' || current_image >= (images.length-2)) // first image or last image
				{ 
					current_image = 0; 
				}
				else // in between
				{
					current_image++;  
				}
			 }
			 else
			 {
				//alert(current_image);
				if(current_image == 'unset' || current_image >= (images.length-1)) // first image or last image
				{ 
					current_image = 0; 
				}
				else // in between
				{
					current_image++;  
				}
			 }

			//DOM.splash.style.visibility = "hidden"; // hide while setting source
			DOM.splash.src = RAND_IMG ? r(image_root+images[current_image]) : image_root+images[current_image];  
			//DOM.splash.style.visibility = "visible"; // show again

		}
		
	}
	
}

function doneLoading(){

	DOM.splash.style.visibility = "visible";

}

// visit the clicked item
function visit(obj)
{
	//obj.style.paddingBottom = '0';
	//obj.style.borderBottom = '2px solid #940000';
	//obj.style.marginBottom = '5px';
}		

// Launch quicktime window.
function vqt(qtID, size)
{
	url = '/global/qt.embed.php?qt='+qtID;
	windowID = 'qt_window';
	width = 520;
	height = 488;
	openWin(url, 'qt' + qtID, width, height, 'center', 'middle');
}

function launchDirectorReel(directorName, size, contents){
	url = '/global/qt.embed_director.php?name='+directorName+'&directorMedia='+contents;
	windowID = 'qt_window';
	width = 520;//512;
	height = 688;//668;//384;
	openWin(url, 'reel', width, height, 'center', 'middle');
}

function vqtDirect(url)
{
	windowID = 'qt_window';
	width = 512;
	height = 384;
	openWin(url, 'qt', width, height, 'center', 'middle');
}

function vqt_CLIENT(url)
{
	windowID = 'qt_window';
	width = 720;
	height = 600;
	openWin(url, 'qt', width, height, 'center', 'middle');	
}

function vprint(id)
{
	url = '/global/PrintMedia.php?id=' + id;
	windowID = 'print_window';
	width = 512;
	height = 384;
	openWin(url, 'print', width, height, 'center', 'middle');
}



function pvmedia(url)
{
	windowID = 'Preview';
	width = 740;
	height = 640;
	openWin(url, windowID, width, height, 'center', 'middle', false, true);
}

// very nice window opener from squidfingers.com
function openWin(url,name,width,height,xpos,ypos,chrome,scrolling){
  var x, y, w, h, moveX = 0, moveY = 0, features="";
  chrome = chrome ? "yes" : "no";
  scrolling = scrolling ? "yes" : "no";
  features += "toolbar="+chrome+",location="+chrome+",status="+chrome+",menubar="+chrome;
  features += ",scrollingbars="+scrolling+",resizable="+scrolling;
  if(width) features += ",width="+width;
  if(height) features += ",height="+height;
  if(xpos && window.screen)
  {
    w = window.screen.availWidth;
    width = parseInt(width);
    switch(xpos){
      case "left": x = 0; break;
      case "center": x = (w-width)/2; break;
      case "right": x = w-width; break;
      default: x = xpos;
    }
    features += ",screenX="+x+",left="+x;
    moveX = x;
  }
  if(ypos && window.screen){
    h = window.screen.availHeight;
    height = parseInt(height);
    switch(ypos){
      case "top": y = 0; break;
      case "middle": y = (h-height)/2; break;
      case "bottom": y = h-height; break;
      default: y = ypos;
    }
    features += ",screenY="+y+",top="+y;
    moveY = y;
  }
  openWinReference = window.open(url,name,features);
  if(moveX || moveY){
    // position the window for browsers that don't recognize screenX, screenY
    openWinReference.moveTo(moveX,moveY);
  }
}

function openOverlay(){
	
}

// refresh on reload
function refreshBrowser(sURL)
{
		re = false;
	if(browser.isIE && !re)
	{
		re = true;
		window.location.replace(sURL);
	}
}

// post-load directives
function postFunctions()
{
	//DOM.getElementById('subNavBlock').style.marginTop = (DOM.getElementById('subcontent').offsetTop)/2 + 'px';
	//alert();
	
	/*
	if(browser.isIE) { 
	
	//alert('yes');
	
	}*/
	
	
}

// get id
function $(id) {
	return document.getElementById(id);
}


// pinstripage
function ps(){

// left stripes
$("LP").style.width = $("outerpattern").offsetLeft + "px";

// right stripes
$("RP").style.width = eval(window.innerWidth - $("outerpattern").offsetWidth - $("outerpattern").offsetLeft) + "px";


}

// key handler
function keyPressHandler(e) {
	
		if (e.keyCode == 13) {
			return false;
		}
			
      var kC  = (window.event) ?    // MSIE or Firefox?
                 event.keyCode : e.keyCode;
      var Esc = (window.event) ?   
                27 : e.DOM_VK_ESCAPE // MSIE : Firefox
      if(kC==Esc)
	  {
		// esc pressed
		Search('-');
	  }
   }
