window.addEvent('domready', function(){

	// set up portfolio
	var portfolioimage = $('portfolio-image');
	if (portfolioimage) {
		var portfoliothumbs = $('portfolio-thumbs');
		var portfoliotitle  = $('portfolio-title');
		var portfoliocount  = $('portfolio-count');
		var portfoliothumbtitle = $('portfolio-thumbnail-title');

		var thumblist = portfoliothumbs.getElement('ul').set('tween', {
			duration: 100,
			link: 'cancel'
		});

		var thumbcount = thumblist.getElements('li').length;
		var thumblistwidth = thumbcount * 157 - 5;
		var thumbwindowwidth = portfoliothumbs.getStyle('width').toInt();
		var thumbwindowxpos = portfoliothumbs.getPosition().x;

		var moveThumbs;
		var moveSpeed = 0;

		thumblist.setStyle('left', 0);

		var scrollThumbs = function(){
			if (moveSpeed != 0) {
				var newPos = Math.min(0, Math.max(-(thumblistwidth - thumbwindowwidth), thumblist.getStyle('left').toInt() + moveSpeed));
				thumblist.setStyle('left', newPos);
			}
		};

		portfoliothumbs.addEvents({
			mouseenter: function(event){
				moveSpeed = (-(event.page.x - thumbwindowxpos - thumbwindowwidth / 2) / 20).toInt();
				portfoliothumbs.addEvent('mousemove', function(event){
					moveSpeed = (-(event.page.x - thumbwindowxpos - thumbwindowwidth / 2) / 20).toInt();
				});
				moveThumbs = scrollThumbs.periodical(20);
			},
			mouseleave: function(){
				$clear(moveThumbs);
				portfoliothumbs.removeEvents('mousemove');
			}
		});

		var thumblinks = portfoliothumbs.getElements('a');
		var linkfx = new Fx.Elements(portfoliothumbs.getElements('img'));
		var linkfxobj = {};
		var newimage;

		thumblinks.each(function(link, i){
			linkfxobj[i] = {
				opacity: (link.getParent().hasClass('current') ? 1 : 0.3)
			}

			link.addEvent('click', function(event){
				event.stop();
				var linkfxobj = {};

				if (portfolioimage.getStyle('overflow') != 'hidden') {
					var portfolioimagesize = portfolioimage.getSize();

					portfolioimage.setStyles({
						width: portfolioimagesize.x,
						height: portfolioimagesize.y,
						overflow: 'hidden'
					}).getElement('img').setStyles({
						position: 'absolute',
						top: 0,
						left: 0
					});

					$('portfolio-count').tween('top', portfolioimagesize.y - 16);
				}

				thumblinks.each(function(thumblink, j){
					linkfxobj[j] = {
						opacity: (j == i ? 1 : 0.3)
					}
				});

				linkfx.start(linkfxobj);

				portfolioimage.getElements('img').each(function(largeimage){
					largeimage.set('tween', {
						onComplete: function(){
							this.subject.destroy();
						}
					}).tween('opacity', 0);
				});

				portfoliotitle.set('tween', {
					onComplete: function(){
						portfoliotitle.set({
							html:  link.get('title'),
							tween: { onComplete: $empty }
						}).tween('opacity', 1);
					}
				}).tween('opacity', 0);

				portfoliocount.set('tween', {
					onComplete: function(){
						portfoliocount.getElement('em').set('html', (i < 9 ? '0' : '') + (i+1));
						portfoliocount.set('tween', {onComplete: $empty}).tween('opacity', 1);
					}
				}).tween('opacity', 0);

				newimage = new Asset.image(link.get('href'), {
					onload: function(){
						this.setStyles({
							position: 'absolute',
							opacity: 0,
							top: 0,
							left: 0
						});
						portfolioimage.adopt(this);
						var size = this.getSize();
						portfolioimage.tween('height',   size.y);
						portfoliocount.tween('top',      Math.max(250, size.y - 16));
						portfoliothumbtitle.tween('top', Math.max(363, size.y + 97));
						this.setStyle('left', Math.floor((780 - size.x) / 2));
						this.tween('opacity', 1);
					}
				});
			});
		});

		linkfx.set(linkfxobj);
	}

});