

function moon()
{
	this.slideIndex = 0;
	this.oldSlide = undefined;
	this.projectA = $('content').select('div.project');
	this.navA = $('nav').select('li a');
	this.slideMax = this.projectA.length;
	
	this.assetIndexA = [];
	
	this.assetIndex = 0;
	this.assetMax = 0;
	
	this.oldMouseTime = 0;
	this.prepInterval = 0;
	this.changeInterval = 0;
	
	//set up initial click handlers on projects - Event.observe targets nested elements instead of parent
	var scope = this;
	for(var i=0;i<this.slideMax;i++){
		this.projectA[i].onclick = function()
		{
			scope.viewSlideByIndex(this);
			return false;
		}
		var t = this.projectA[i].select('div.credit')[0];
		if(t){
			t.update('Created at Pentagram');
		}
				
		this.assetIndexA.push({assetIndex:0});
	}
	
	for(var i=0;i<this.slideMax;i++){
		Event.observe(this.navA[i], 'click', this.viewSlideIndex.bindAsEventListener(this));
	}
	
	$('logo_0').onclick = function()
	{
		scope.viewSlideByIndex(this);
		return false;
	}
	
	this.decrypt();
	this.viewSlide();
		
	Event.observe(document, 'keydown', this.handleKeyPress.bindAsEventListener(this));
	Event.observe(document, 'mousewheel', this.handleMouseWheel.bindAsEventListener(this));
	Event.observe(document, 'DOMMouseScroll', this.handleMouseWheel.bindAsEventListener(this));
	
}

moon.prototype.decrypt = function()
{
	var tA = $('body').select('a[class="em_encrypted_native"]');
	var iMax = tA.length;
	for(var i=0;i<iMax;i++){
		var obj = tA[i];
		var encA = obj.rev.split('_');
		var str = des(_em_key,hexToString('0x' + encA[2]),0,1,hexToString('0x' + encA[3]),0).substring(0,Number(encA[1]));
		obj.update(str);
		Event.observe(obj,'click',function()
		{
			window.location = 'mailto:' + this.innerHTML;
			Event.stop(arguments[0]);
		});
	}
}

moon.prototype.handleMouseWheel = function(event)
{
	//http://andrewdupont.net/2007/11/07/pseudo-custom-events-in-prototype-16/
	var realDelta;	
	if(event.wheelDelta){ // IE & Opera
		realDelta = event.wheelDelta / 120;
	}else if(event.detail){ // W3C
		realDelta = -event.detail / 3;
	}
	
	if (!realDelta) return;
	
	var td = new Date();
	if((td.getTime() - 250) > this.oldMouseTime){
		if(realDelta > 0){
			this.advanceSlide(-1);
		}else{
			this.advanceSlide(1);
		}
	}
	
	this.oldMouseTime = td.getTime();
	Event.stop(event);	

}

moon.prototype.handleKeyPress = function(event)
{
	var k = event.keyCode;
	switch(k)
	{
		case 37:
			this.advanceAsset(-1);
		break;
		
		case 38:
			this.advanceSlide(-1);
		break;
		
		case 39:
			this.advanceAsset(1);
		break;
		
		case 40:
			this.advanceSlide(1);
		break;
		
	}
	
	Event.stop(event);
}

moon.prototype.advanceSlide = function(dir)
{
			
	switch(true)
	{
		case this.slideIndex + dir > this.slideMax - 1:
			this.slideIndex = 0;
		break;
		
		case this.slideIndex + dir < 0:
			this.slideIndex = this.slideMax - 1;
		break;
		
		default:
			this.slideIndex += dir;
		break;
		
	}
	this.viewSlide();
	
}

moon.prototype.advanceAsset = function(dir)
{
			
	switch(true)
	{
		case this.assetIndex + dir > this.assetMax - 1:
			this.assetIndex = 0;
			if(arguments[1] == true){
				this.viewAsset();
				this.advanceSlide(1);
			}
		break;
		
		case this.assetIndex + dir < 0:
			this.assetIndex = this.assetMax - 1;
		break;
		
		default:
			this.assetIndex += dir;
		break;
	}
	
	this.viewAsset();
}

moon.prototype.viewSlideByIndex = function(elem)
{
	var tA = elem.id.split('_');
	this.slideIndex = Number(tA[1]);
	this.viewSlide();
}

moon.prototype.viewSlideIndex = function(event)
{
	var elem = Event.element(event);
	var tA = elem.id.split('_');
	this.slideIndex = Number(tA[1]);
	this.viewSlide();
	Event.stop(event);
}

moon.prototype.viewSlide = function()
{	
	this.slideSlide();
	
	var deet = $('p_'+this.slideIndex).select('div.info')[0].innerHTML;
	var nav = $('p_'+this.slideIndex).select('ul.nav_slides')[0].innerHTML;
		
	$('detail').update(deet + '<ul>' + nav + '</ul>');
	
	var tSlides = $('detail').select('a.slide_link');
	this.assetMax = tSlides.length;
	this.assetIndex = this.assetIndexA[this.slideIndex].assetIndex;
	
	this.viewAsset();
	
	for(var j=0;j<this.assetMax;j++){
		Event.observe(tSlides[j], 'click', this.viewAssetByIndex.bindAsEventListener(this));
	}
	
	if($('detail').select('li').length == 1){
		$('detail').select('ul')[0].hide();
	}
	
	this.oldSlide = this.slideIndex;
	
}

moon.prototype.viewAssetByIndex = function(event)
{
	var elem = Event.element(event);
	var tA = elem.id.split('_');
	
	this.assetIndex = Number(tA[1]);
	this.viewAsset();
	
	Event.stop(event);
	
}

moon.prototype.viewAsset = function()
{
	this.assetIndexA[this.slideIndex].assetIndex = this.assetIndex;
		
	new Effect.Move($('p_'+this.slideIndex).select('div.slides')[0],
	{
		x: -(720*this.assetIndex),
		y: 0,
		duration: 0.8,
		transition: Effect.Transitions.sinoidal,
		mode: 'absolute'
	});
	
	var tA = $('detail').select('li a');
	var iMax = tA.length;
	for(var i=0;i<iMax;i++){
		//alert(tA[i].id);
		if(i==this.assetIndex){
			tA[i].addClassName('active');
		}else{
			tA[i].removeClassName('active');
		}
	}
	
}

moon.prototype.prepSlide = function(elem)
{
	clearInterval(this.prepInterval);
	this.projectA[this.slideIndex].select('div.cover')[0].hide();
}

moon.prototype.slideSlide = function()
{
	
	for(var i=0;i<this.slideMax;i++){
		var obj = this.projectA[i].select('div.cover')[0];
		var scope = this;
		if(i == this.slideIndex){
			this.projectA[i].removeClassName('after');
			this.projectA[i].removeClassName('before');
			new Effect.Opacity(obj, {duration:0.5, to:0.0});			
			this.projectA[i].select('div.slides')[0].show();
			//load the next 2 images
			if(this.projectA[i].next('div',0)){
				this.projectA[i].next('div',0).select('div.slides')[0].show();
			}
			/*
			if(this.projectA[i].next('div',1)){
				this.projectA[i].next('div',1).select('div.slides')[0].show();
			}
			*/
			this.projectA[i].addClassName('active');
			this.navA[i].addClassName('active');
			
			//set up the click handler son
			if(this.projectA[i].select('object').length == 0){
				this.projectA[i].onclick = function()
				{
					scope.advanceAsset(1,true);
					return false;
				}
			}			
			
		}else{
			obj.show();
			new Effect.Opacity(obj, {duration:0.5, to:0.75});
			this.projectA[i].removeClassName('active');
			this.navA[i].removeClassName('active');
			
			//click
			this.projectA[i].onclick = function()
			{
				scope.viewSlideByIndex(this);
				return false;
			}
									
		}
		if(i<this.slideIndex){
			this.projectA[i].addClassName('before');
			this.projectA[i].removeClassName('after');
		}
		if(i>this.slideIndex){
			this.projectA[i].addClassName('after');
			this.projectA[i].removeClassName('before');
		}
	}
	
	new Effect.Move($('strip'),
	{
		x: 0,
		y: -this.slideIndex * (470+20) + 50,
		duration: 0.8,
		transition: Effect.Transitions.sinoidal,
		mode: 'absolute'
	});
	
	clearInterval(this.prepInterval);
	this.prepInterval = setInterval(this.prepSlide.bind(this), 600);	
	
}