// JavaScript Document

document.write('<link href="/motivetechnology/media/motive/css/javascript.css" rel="stylesheet" type="text/css" media="all" />');
Cufon.set('fontFamily', 'Futura').replace('.content_header, .content_header a, .mainmenu ul li a, h1,h2,h3,.quotesection p, .process li span.number', {hover:true });
Cufon.set('fontFamily', 'Helvetica Neue').replace('.pagefooter big', {hover:true});

var thisBrowser = navigator.userAgent;

if(navigator.appVersion.indexOf('MSIE 6') == -1) {
	Cufon.set('fontFamily', 'Futura').replace('.staff div.h3')
}

if(thisBrowser.indexOf('iPhone') > -1 || thisBrowser.indexOf('iPad') > -1) {
	document.write('<style type="text/css">#header, .pageheader, #main_content, #pagefooter, #footer { padding:0 10px; overflow:hidden; width:1003px; margin:0 auto; } .content_header br.break { display:none; }</style>');
}

var $j = jQuery.noConflict();
var currentLeft = -1966;
var xhr;
var myInterval;
var targetIndex;

$j(document).ready( function() {
	
	$j('.mainmenu').find('li').hover(function () { $j(this).addClass('hover');},function () { $j(this).removeClass('hover');})
	$j('.gallery').find('.gallery_contents').find('li').each( function(index) {
		$j(this).attr('index',index);
	});
	$j('.gallery').prepend('<div class="left_button"><div></div></div>');
	$j('.gallery').prepend('<div class="right_button"><div></div></div>');
	$j('.left_button').click(moveOneLeft);
	$j('.right_button').click(moveOneRight);
	
	$j('.popGalleryIndex').click(function() {
		//$j('.ui-tabs-nav').find('a[href='+$j(this).attr('href')+']').click();
		$j('#caseGallery').find('.ui-tabs-panel').hide().addClass('ui-tabs-hide');
		$j('#caseGallery').find('.ui-state-default').removeClass('ui-tabs-selected').removeClass('ui-state-active');
		$j('#caseGallery').find('a[href='+$j(this).attr('href')+']').parents('li').addClass('ui-tabs-selected').addClass('ui-state-active');
		$j($j(this).attr('href')).removeClass('ui-tabs-hide').show();
		displayCenterDiv('caseGallery');
		return false;
	});
	$j('.hiddenDiv').prepend('<div class="closeLink">Close <img src="/motivetechnology/media/motive/interface/btn_x.gif" alt="x" /></div>');
	$j('.closeLink, #backingDiv').click(function() {
		layerClose('caseGallery');
	});
	
	var newContents = "";
	
	$j('.thumbgallery a').click( function() {
		clearInterval(myInterval);
		var thisIndex = $j('.thumbgallery a').index($j(this));
		var contents = $j('.gallery').find('.gallery_contents');
		var lis = contents.find('li');
		var selectedIndex = parseInt(contents.find('li[class="active"]').attr('index'));
		if(thisIndex != selectedIndex) {
			targetIndex = thisIndex;
			gotoTargetIndex();
			//myInterval = setInterval( "gotoTargetIndex()",310);
		}
		return false;
	});
	
	var myHead = getFlashMovieObject("head_0");
	
	if(document.getElementById('head_0')) {
		if(myHead.nodeName != "IMG") {
			try {
				myHead.animateMovie();
			} catch(err) {
			}
		}
		myInterval = setInterval( "moveRight()", 5000);
	} 
	if($j('.staff').length > 0) {
		myInterval = setInterval( "moveRight()", 7000);
	}
	currentLeft = -$j('.gallery').find('.gallery_contents').width()/5;
	
	$j('.gallery').find('.gallery_contents').css('margin-left',currentLeft+"px");
	var lis = $j('.gallery').find('.gallery_contents').find('li');
	$j('.gallery').find('.gallery_contents').find('li[index="0"]').addClass('active');
	lis.parent().prepend($j('.gallery_contents').find('li:eq('+(lis.length-1)+')').detach());
	lis.parent().prepend($j('.gallery_contents').find('li:eq('+(lis.length-1)+')').detach());
	$j('.client_gallery').tabs( {fx: { opacity: 'toggle' } });
		
	var tallestBlogh3 = 25;
	$j('.blogPost h3').each(function() {
		if($j(this).height() > tallestBlogh3)
			tallestBlogh3 = $j(this).height();
	});
	$j('.blogPost h3').height(tallestBlogh3 + 'px');
	
	/*$j('#plc_lt_zoneContent_pageplaceholder_pageplaceholder_lt_zoneMain_BizForm_viewBiz_ctl00_YourName').blur(function() {
		if($j(this).val() == "Jenny Arnold") {
			$j('body').prepend('<div id="flashcover" style="position:fixed; _position:absolute; top:0; left:0; z-index:100000;"></div>');
			$j('#flashcover').width($j(window).width()+"px").height($j(window).height()+"px");
			var fo = new FlashObject('/MotiveTechnology/media/Motive/flash/panther.swf', 'panther', '100%', '100%', '7', '#FFFFFF'); 
			fo.addParam('wmode', 'transparent'); 
			fo.write('flashcover');
		}if($j(this).val() == "Geraint Martin") {
			$j('body').prepend('<div id="flashcover" style="position:fixed; _position:absolute; top:0; left:0; z-index:100000;"></div>');
			$j('#flashcover').width($j(window).width()+"px").height($j(window).height()+"px");
			var fo = new FlashObject('/MotiveTechnology/media/Motive/flash/sheep.swf', 'sheep', '100%', '100%', '7', '#FFFFFF'); 
			fo.addParam('wmode', 'transparent'); 
			fo.write('flashcover');
		}
	});*/
	$j('.NewsletterSubscription input.SubscriptionTextbox').focus(function(){
		if($j(this).val()=="Enter Email Address") {
			this.value = '';
			$j(this).css('color','#000');
		}
	}).blur( function() {
		if($j(this).val()=="") {
			this.value = 'Enter Email Address';
			$j(this).css('color','#6d6e71');
		}
	});
	//$j('.BlogCommentsTitle').append('&nbsp; <a class="blogCommentBtn" href="#blogAddComment"><img src="/motivetechnology/media/motive/interface/btn_addyourown.gif" alt="Add your own" /></a>');
	//$j('.BlogPanel').attr('id','blogAddComment');
});

function removeFlashCover() {
	$j('#flashcover').detach();
}

function gotoTargetIndex() {
	var contents = $j('.gallery').find('.gallery_contents');
	
	var contents = $j('.gallery').find('.gallery_contents');
	var lis = contents.find('li');
	var itemWidth = lis.attr('offsetWidth');
	var pageWidth = contents.parent().attr('offsetWidth');
	
	var selectedIndex = parseInt(contents.find('li[class="active"]').attr('index'));
	
	var myHead = getFlashMovieObject("head_"+selectedIndex);
	if(document.getElementById('head_'+selectedIndex)) {
	if(myHead.nodeName != "IMG") {
		try {
			myHead.pauseMovie();
		} catch(err) {
		}
	}}
	if(selectedIndex != targetIndex) { 
		var nextItems = contents.children('ul').find('li[index="'+targetIndex+'"] ~ li').detach();
		var activeItem = contents.children('ul').find('li[index="'+targetIndex+'"]').detach();
		var remaining = contents.children('ul').find('li').detach();
		
		contents.children('ul').prepend(nextItems);
		contents.children('ul').append(remaining);
		if(selectedIndex < targetIndex) {
			contents.children('ul').append(activeItem);
		} else {
			contents.children('ul').prepend(activeItem);
		}
		contents.css('margin-left',-contents.find('li[class="active"]').position().left);
	
	
	var thisItem = contents.find('li[index="'+targetIndex+'"]');
	lis.removeClass('active');
	thisItem.addClass('active');
	hightlightThumb(thisItem.attr('index'));
	
	currentLeft  = -thisItem.position().left;
	
	contents.stop(false, false).animate( { marginLeft: currentLeft +"px" }, 300, function() {
		var myUL = $j('.gallery').find('ul');
		if(myUL.children().index(myUL.children('.active')) != 0) {
			myUL.prepend(myUL.children('.active').detach());
		} 
		myUL.prepend(myUL.children('li:last').detach());
		myUL.prepend(myUL.children('li:last').detach());
		contents.css('margin-left',-contents.find('li[class="active"]').position().left);
		currentLeft = -contents.find('li[class="active"]').position().left;
		var myIndex = $j('.gallery').find('li.active').attr('index');

		myHead = getFlashMovieObject("head_"+myIndex);
		if(document.getElementById('head_'+selectedIndex)) {
		if(myHead.nodeName != "IMG") {
			try {
				myHead.animateMovie();
			} catch(err) {
			}
		}
		}
		targetIndex = null;
		Cufon.refresh();
	});
	} else {
		var myHead = getFlashMovieObject("head_"+myIndex);
		if(document.getElementById('head_'+selectedIndex)) {
		if(myHead.nodeName != "IMG") {
			try {
				myHead.animateMovie();
			} catch(err) {
			}
		}
		}
	}
	/*if(targetIndex != selectedIndex) {
		if(targetIndex < selectedIndex) {
			moveLeft();
		} else {
			moveRight();
		}
	} else {
		clearInterval(myInterval);
		targetIndex = null;
		myHead = getFlashMovieObject("head_"+selectedIndex);
		if(document.getElementById('head_'+selectedIndex)) { 
			try {
				myHead.animateMovie();
			} catch(err) {
			}
		}
	}*/
}

function hightlightThumb(thisIndex) {
	thumb = $j('.thumbgallery a:eq('+thisIndex+')');
	thumb.parent().siblings().removeClass("over");
	thumb.parent().addClass("over");
	myPosition = thumb.position();
}

function moveOneLeft() {
	clearInterval(myInterval);
	$j('.gallery_contents').stop(true,true);
	targetIndex = null;
	moveLeft();
}

function moveOneRight() {
	clearInterval(myInterval);
	$j('.gallery_contents').stop(true,true);
	targetIndex = null;
	moveRight();
}

function moveLeft() {
	if(typeof targetIndex != 'number') {
	var contents = $j('.gallery').find('.gallery_contents');
	var lis = contents.find('li');
	var itemWidth = lis.attr('offsetWidth');
	var pageWidth = contents.parent().attr('offsetWidth');
	
	var selectedIndex = parseInt(contents.find('li[class="active"]').attr('index'));
	
	var myHead = getFlashMovieObject("head_"+selectedIndex);
	if(document.getElementById('head_'+selectedIndex)) {
	if(myHead.nodeName != "IMG") {
		try {
			myHead.pauseMovie();
		} catch(err) {
		}
	}}
	
	var nextIndex = selectedIndex - 1;
	if(nextIndex < 0) 
		nextIndex = lis.length-1;
	var thisItem = contents.find('li[index="'+nextIndex+'"]');
	lis.removeClass('active');
	thisItem.addClass('active');
	hightlightThumb(thisItem.attr('index'));
	
	var leftElement = contents.find('li[index="'+selectedIndex+'"]');
	if(leftElement.position().left < thisItem.position().left) {
		contents.find('ul').prepend(contents.find('li:last').detach());
		contents.find('ul').find('li:first').html(contents.find('ul').find('li:first').html());
		contents.css('margin-left',(currentLeft - pageWidth) +"px");
	} else {
		currentLeft += pageWidth;
	}
	contents.stop(false, false).animate( { marginLeft: currentLeft +"px" }, 300, function() {
		var myIndex = $j('.gallery').find('li.active').attr('index');

		myHead = getFlashMovieObject("head_"+myIndex);
		if(document.getElementById('head_'+selectedIndex)) {
		if(myHead.nodeName != "IMG") {
			try {
				myHead.animateMovie();
			} catch(err) {
			}
		}
		}
		Cufon.refresh();
	});
	}
}

function moveRight() {
	if(typeof targetIndex != 'number') {
	var contents = $j('.gallery').find('.gallery_contents');
	var lis = contents.find('li');
	var itemWidth = lis.attr('offsetWidth');
	var pageWidth = contents.parent().attr('offsetWidth');
	var selectedIndex = parseInt(contents.find('li[class="active"]').attr('index'));
	
	var myHead = getFlashMovieObject("head_"+selectedIndex);
	if(document.getElementById('head_'+selectedIndex)) {
	if(myHead.nodeName != "IMG") {
		try {
			myHead.pauseMovie();
		} catch(err) {
		}
	}}
	
	var nextIndex = selectedIndex + 1;
	if(nextIndex > lis.length-1) 
		nextIndex = 0;
	var thisItem = contents.find('li[index="'+nextIndex+'"]');
	
	lis.removeClass('active');
	thisItem.addClass('active');
	
	hightlightThumb(thisItem.attr('index'));
	if(thisItem.position().left >= itemWidth)
		currentLeft -= itemWidth;
		contents.stop(false, false).animate( { marginLeft: currentLeft +"px" }, 300, function() {
		var activeElement = $j(this).find('li[class="active"]');
		var nextIndex = parseInt(activeElement.attr('index'))-1;
		if(nextIndex < 0)
			nextIndex += lis.length;
		var leftElement = $j(this).find('li[index="'+nextIndex+'"]');
		if(leftElement.position().left < activeElement.position().left) {
			$j(this).find('ul').append(contents.find('li:first').detach());
			$j(this).find('ul').find('li:last').html($j(this).find('ul').find('li:last').html());
			currentLeft += activeElement.attr('offsetWidth');
			$j(this).css('margin-left',currentLeft +"px");
		}
		var myIndex = $j('.gallery').find('li[class="active"]').attr('index');
		var myHead = getFlashMovieObject("head_"+myIndex);
		if(document.getElementById('head_'+selectedIndex)) {
		if(myHead.nodeName != "IMG") {
			try {
				myHead.animateMovie();
			} catch(err) {
			}
		}}
		Cufon.refresh();
	});
	}
}

function getFlashMovieObject(movieName){  
   if (window.document[movieName]){  
		if(window.document[movieName] == "[object NodeList]") {
			return window.document[movieName][1];  
		} else
			return window.document[movieName];  
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1 && document.embeds && document.embeds[movieName]){  
		return document.embeds[movieName];  
	} else {  
		return document.getElementById(movieName);  
	}  
} 


var msec=400;
var appearingDiv;
var windowHeight;
var thisBrowser = navigator.appVersion;
var centering;
var backdiv = true;

function displayDiv(hiddenDiv)
{
  $j('#'+hiddenDiv).hide();
  if($j('#backingDiv').length > 0 && backdiv) {
	  $j('#backingDiv').hide();
	  var currentHeight = $j('#form1').height();
	  var currentWidth = $j('#form1').width();
	  $j('#backingDiv').css('height',currentHeight + "px");
	  $j('#backingDiv').css('width',currentWidth + "px");
  }
  appearingDiv = hiddenDiv;
  //$j('#backingDiv').show().css('opacity', 0);
  $j('#backingDiv').show().css('opacity','0').stop(true,true).animate({ opacity:0.85 }, 'slow');
  //$j('#'+hiddenDiv).show().css('opacity', 0);
  $j('#'+hiddenDiv).stop(true,true).animate({ opacity:'show' }, 'slow');
}

function opacity(divID,mystart,myend,mymsec) {
	$j('#'+divID).css(opacity,mystart);
	$j('#'+divID).stop(true,true).animate({ opacity:myend/100 }, mymsec);
}

function setDisplay(nodeId, newvalue) {
	if(newvalue == "block") {
		$j('#'+nodeId).show();
	} else {
		$j('#'+nodeId).hide();
	}
}

function quickdisplayDiv(hiddenDiv)
{
  $j('#'+hiddenDiv).hide();
  $j('#backingDiv').hide();
  appearingDiv = hiddenDiv;
  $j('#'+hiddenDiv).stop(true,true).animate({ opacity:'show' }, 'slow');
}


function layerClose(hiddenDiv)
{
  $j('#backingDiv').stop(true,true).animate({ opacity:'hide' }, 'slow');
  $j('#'+hiddenDiv).stop(true,true).animate({ opacity:'hide' }, 'slow');
  clearInterval(centering);
}

var movedtotop = false;

function scrollingDIV(scrollingID) 
{
	var currentScrollY;
	var currentDiv = document.getElementById(scrollingID)
	var currentTop = currentDiv.style.top;
	currentTop.replace(/px/,"");
	currentTop = parseInt(currentTop);
	var currentLeft = currentDiv.style.left;
	currentLeft.replace(/px/,"");
	currentLeft = parseInt(currentLeft);
	var currentDivHeight = currentDiv.offsetHeight;
	var currentDivWidth = currentDiv.offsetWidth;
	var currentHeight;
	var currentWidth;
	if (typeof window.innerWidth != 'undefined')
	{
		currentWidth = window.innerWidth,
		currentHeight = window.innerHeight
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
 	{
		currentWidth = document.documentElement.clientWidth,
		currentHeight = document.documentElement.clientHeight
 	}
 	else
	{
		currentWidth = document.getElementsByTagName('body')[0].clientWidth,
		currentHeight = document.getElementsByTagName('body')[0].clientHeight
	}
	var centerY = (currentHeight-currentDivHeight)/2;
	var centerX = (currentWidth-currentDivWidth)/2;
	
	if(typeof(window.pageYOffset) == "undefined") 
	{
		currentScrollY = document.documentElement.scrollTop;
		currentScrollX = document.documentElement.scrollLeft;
	}
	else 
	{
		currentScrollY = window.pageYOffset;
		currentScrollX = window.pageXOffset;
	}
	
	centerX += currentScrollX;
	centerY += currentScrollY;
	
	if(isNaN(currentTop)) 
	{
		currentDiv.style.top = centerY + "px";
		currentTop = centerY;
		currentDiv.style.marginTop = "0px";
	}
	else 
	{
		var nextStep = (centerY - currentTop)/2;
		if(!isNaN(nextStep) && centerY != currentTop && currentHeight > currentDiv.offsetHeight) 
		{
			if(nextStep > 1 || nextStep < -1) 
			{
				currentDiv.style.top = currentTop + nextStep + "px";
			}
			else 
			{
				currentDiv.style.top = Math.round(centerY) + "px";
			}
			movedtotop = false;
		}
		else if(currentHeight < currentDiv.offsetHeight)
		{
			if(!movedtotop)
			{
				currentDiv.style.top = currentScrollY + "px";
				movedtotop = true;
			}
		}
	}
	if(isNaN(currentLeft)) 
	{
		currentDiv.style.left = centerX + "px";
		currentLeft = centerX;
		currentDiv.style.marginLeft = "0px";
	}
	else 
	{
		var nextStep = (centerX - currentLeft)/2;
		if(!isNaN(nextStep) && centerX != currentLeft && currentWidth > currentDiv.offsetWidth) 
		{
			if(nextStep > 1 || nextStep < -1) 
			{
				currentDiv.style.left = currentLeft + nextStep + "px";
			}
			else 
			{
				currentDiv.style.left = Math.round(centerX) + "px";
			}
		}
	}
	if(currentDiv.style.top == Math.round(centerY) + "px" && currentDiv.style.left == Math.round(centerX) + "px") {
		clearInterval(centering);
	}
}

function displayCenterDiv(hiddenDiv)
{
	var scrollingDiv = document.getElementById(hiddenDiv);
	scrollingDiv.style.marginTop = "0px";
	scrollingDiv.style.marginLeft = "0px";
	var currentScrollY;
	var currentScrollX;
	if(typeof(window.pageYOffset) == "undefined") 
	{
		currentScrollY = document.documentElement.scrollTop;
		currentScrollX = document.documentElement.scrollLeft;
	}
	else 
	{
		currentScrollY = window.pageYOffset;
		currentScrollX = window.pageXOffset;
	}
	
	var currentHeight;
	var currentWidth;
	if (typeof window.innerWidth != 'undefined')
	{
		currentWidth = window.innerWidth,
		currentHeight = window.innerHeight
	}
	else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
 	{
		currentWidth = document.documentElement.clientWidth,
		currentHeight = document.documentElement.clientHeight
 	}
 	else
	{
		currentWidth = document.getElementsByTagName('body')[0].clientWidth,
		currentHeight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	scrollingDiv.style.top = currentScrollY + "px";
	scrollingDiv.style.left = currentScrollX + "px";
	centering = setInterval("scrollingDIV('"+hiddenDiv+"')", 100);
	displayDiv(hiddenDiv);
}
