/* basic mouseover code */
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function changeHomepageSlide(atag)
{
	// make sure to extend the <a> element
	atag = $(atag);
	
	// grab the currently showing slide, the slide we want to show next, and a ref to the list
	var currentSlide = atag.up('dl').down('.currentSlide'),
		obj = $(atag).up().next(),
		list = $(atag).up('dl');

	// is the animating flag set? if so, quit. otherwise, the flag is set to false or just plain doesn't exist yet, so we're good to go
	try {
		if (list.animating)
			return false;
	}
	catch (err) {};

	
	// bail if the user clicked to see the slide we're already displaying.
	if (currentSlide == obj)
		return false;
	
	list.animating = true;
	
	// prep the slides for "crossfading"
	currentSlide.setStyle({'zIndex': 0});
	obj.setStyle({'zIndex': 1}).setOpacity(0).addClassName('currentSlide');
	
	// fade in the new slide and, when done, hide the old slide
	obj.appear({
		duration: .5,
		afterFinish: function () {
			currentSlide.removeClassName('currentSlide');
			list.animating = false;
		}
	});
	
	return false;
}


function changeService(atag)
{	
	atag = $(atag);
	var list = atag.up('ul');	
	try {
		if (list.animating)
			return false;
	}
	catch (err) {};
	
	list.animating = true;
	
	var	listitem = atag.up('li'),
		hideSectionDT = $$('dl.services dt.currentService')[0],
		hideSectionDD = $$('dl.services dd.currentService')[0],
		showSectionName = atag.href.replace(/^[^#]+#/, ''),
		showSectionDT = $$('dl.services a[name='+showSectionName+']').shift().up('dt'),
		showSectionDD = showSectionDT.next('dd'),
		sectionList = showSectionDT.up('dl');
	
	if (hideSectionDT == showSectionDT)
	{
		list.animating = false;
		return false;
	}
	
	list.select('li.currentService').invoke('removeClassName', 'currentService');
	listitem.addClassName('currentService');
	
	[showSectionDT, showSectionDD].invoke('setStyle', {'left':'30px'});


	[hideSectionDD,hideSectionDT].invoke('absolutize');
	
	[showSectionDT, showSectionDD].invoke('setOpacity', 0).invoke('addClassName', 'currentService');
	
	
	var effects = [
		new Effect.Fade(hideSectionDT, {sync:true}),
		new Effect.Fade(hideSectionDD, {sync:true}),
		new Effect.Appear(showSectionDT, {sync:true}),
		new Effect.Appear(showSectionDD, {sync:true})
	];
	
	new Effect.Parallel(effects, {
		duration: .5,
		afterFinish: function () {
			hideSectionDT.setStyle({'left':'-9000px'}).removeClassName('currentService');
			hideSectionDD.setStyle({'left':'-9000px'}).removeClassName('currentService');
			list.animating = false;
		}
	});
	
	return false;
}

function changeServiceOnLoad()
{
	var	regexp = /^[^#]*#/g,
		showService = location.href.replace(regexp, ''),
		services = $$('.browse-services a'),
		index = services.pluck('href').invoke('replace', regexp, '').indexOf(showService);
	
	if (showService.empty() || index < 0)
		return;
	
	services[index].onclick();
}

function changePortfolioScreenshot(atag)
{
	atag = $(atag);
	
	if (atag.hasClassName('current') || atag.up('p').previous('ul').animating)
		return false;
	
	atag.up('p').previous('ul').animating = true;
	
	$$('p a, li').invoke('absolutize').invoke('setStyle', {'zIndex':0, 'margin':0});
	
	$$('p a').invoke('removeClassName', 'current');
	$(atag).addClassName('current');
	
	var	currentScreenshot = $$('li').find(Element.visible).setStyle({'zIndex':10}),
		displayScreenshot = $$('li')[Number(atag.innerHTML)-1].setStyle({'zIndex':0, 'top':'20px', 'left':'20px', 'display':'block'}).setOpacity(1).show();

	new Effect.Fade(currentScreenshot, {queue:'start', duration:.25});
	new Effect.Appear(displayScreenshot, {queue:'end', duration:.25, afterFinish: function () {
			currentScreenshot.hide();
			atag.up('p').previous('ul').animating = false;
		}
	});

	
	return false;
}
