window.addEvent('domready', function() {
	
	if($('moreProjects')){
		setupPortfolio();
		setupTooltips(".award", "awardTooltip");
	}
	
	if($('easteregg')){
		SqueezeBox.assign($('easteregg'));
		setupTooltips("#easteregg", "easterEggTip");
	}	
});

var amountToJump = 5;
var distance;
var intervalID;

function setupPortfolio(){
	
	$('up').addEvent('mousedown', function(event){
		distance = amountToJump;
		intervalID = scrollNav.periodical(20);
	});
	
	$('up').addEvent('mouseup', function(event){
		$clear(intervalID);
	});
	
	$('down').addEvent('mousedown', function(event){
		distance = -amountToJump;
		intervalID = scrollNav.periodical(20);
	});
	
	$('down').addEvent('mouseup', function(event){
		$clear(intervalID);
	});
	
	// Setup Read More / Back to Thumbnail
	if($('longDesc')){
		$('readMore').addEvent('click', function(event){		
			new Fx.Tween($('longDesc'), {property: 'opacity', duration: 'short'}).set(0);
			
			new Fx.Tween($('shortDesc'), {property: 'opacity', duration: 'short'}).start(0);
			new Fx.Tween($('projThumbnails'), {property: 'opacity', duration: 'short'}).start(0);
			
			if($('projAwards')){
				new Fx.Tween($('projAwards'), {property: 'opacity', duration: 'short'}).start(0);
			}
			
			if($('projMisc')){
				new Fx.Tween($('projMisc'), {property: 'opacity', duration: 'short'}).start(0);
			}
			
			new Fx.Tween($('longDesc'), {property: 'opacity', duration: 'short'}).start(1);
		});
		
		$('backToThumbnails').addEvent('click', function(event){
			new Fx.Tween($('shortDesc'), {property: 'opacity', duration: 'short'}).start(1);
			new Fx.Tween($('projThumbnails'), {property: 'opacity', duration: 'short'}).start(1);
			
			if($('projAwards')){
				new Fx.Tween($('projAwards'), {property: 'opacity', duration: 'short'}).start(1);
			}
			
			if($('projMisc')){
				new Fx.Tween($('projMisc'), {property: 'opacity', duration: 'short'}).start(1);
			}
			
			new Fx.Tween($('longDesc'), {property: 'opacity', duration: 'short'}).start(0);
		});
	}
	
	// check if there is an active
	activeNavItems = $$("#portfolioList .active");
	if(activeNavItems.length){
		var activeNav = activeNavItems[0];
		
		var navTop = $('portfolioList').getStyle('top').toInt();
		var elTop = activeNav.getPosition().y.toInt() - 58;
		var elHeight = activeNav.getSize().y.toInt();
		var scrollHeight = $('portfolios').getStyle('height').toInt();
		
		if(elTop > scrollHeight){
			var difference = elTop - scrollHeight + elHeight;
			var newTop = navTop - difference;
			
			//$('portfolioList').setStyle('top',newTop);
			new Fx.Tween($('portfolioList'), {property: 'top', duration: 'short'}).start(newTop);
		}
	}
}

function scrollNav(){
	var currentTop = $('portfolioList').getStyle('top').toInt();
	var newPos = currentTop + distance;
	
	var bottomPos = -1 * ($('portfolioList').getStyle('height').toInt() - 345);
	
	if(newPos > 0 || newPos < bottomPos){
		return;
	}
	
	$('portfolioList').setStyle("top", newPos);
}

function setupTooltips(el,classNameS){
	var myTips = new Tips(el,{className: classNameS, offsets: {'x': -50, 'y': 16}});
	
	myTips.addEvent('show', function(tip){
		tip.fade('in');
	});

	myTips.addEvent('hide', function(tip){
		tip.fade('out');
	});
}

var currentPhoto = 1;
var lastPhoto = null;

function swapPhotos(newPhoto){

	if(newPhoto != currentPhoto){
		// disable current thumb & photo
		$('thumb_'+currentPhoto).removeClass('active');
		var div = $('photo_'+currentPhoto);
		new Fx.Tween(div, {property: 'opacity', duration: 'short'}).start(0);
		
		currentPhoto = newPhoto;
		
		// turn on new current thumb & photo
		$('thumb_'+currentPhoto).addClass('active');
		var div = $('photo_'+currentPhoto).setStyles({
			opacity: 0,
			display:'block'
		});
		new Fx.Tween(div, {property: 'opacity', duration: 'short'}).start(1);
	}
}

function prevPhoto(){
	var prevPhoto = currentPhoto - 1;
	
	if(!prevPhoto){
		if(!lastPhoto){
		
			lastPhoto = 1;
			
			while($('thumb_'+lastPhoto)){
				lastPhoto++;
			}
			
			lastPhoto--;
		}
		
		prevPhoto = lastPhoto;
	}
	
	swapPhotos(prevPhoto);
}

function nextPhoto(){

	var nextPhoto = currentPhoto + 1;
		
	if(!$('thumb_' + nextPhoto)){
		nextPhoto = 1;
	}
		
	swapPhotos(nextPhoto);
}
