(function( $ ) { $.fn.fit=function(width,height,verticalAlignment,horizontalAlignment) {
	if (!verticalAlignment) verticalAlignment='middle';
	if (!horizontalAlignment) horizontalAlignment='center';
	
	if (!width || width<1) width=this.width();
	if (!height || height<1) height=this.height();
	

	var vw=this.parent().width();
	var vh=this.parent().height();
	
	var ratio=width/height;
	if (vh<vw/ratio) {
		this.width(vw);
		this.height(vw/ratio);
		this.css('top',0);
		if (verticalAlignment=='middle') this.css('top',0-Math.round((vw/ratio)-vh)/2);
		if (verticalAlignment=='bottom') this.css('top',vh-Math.round(vw/ratio));
		this.css('left',0);
	} else {
		this.width(vh*ratio);
		this.height(vh);
		this.css('left',0);
		if (horizontalAlignment=='center') this.css('left',0-Math.round((vh*ratio)-vw)/2);
		if (horizontalAlignment=='right') this.css('left',vw-Math.round(vh*ratio));
		this.css('top',0);
	}
};})( jQuery );

function positionLogo() {
	var h=($(window).height()-$('.window').height())/4;
	h=h-($('#logo').height()-10)/2;
	$('#logo').css('top',h);
}

var menuActive=false;

function startvideo(id,t) {
	$(t).parent().prepend('<iframe src="http://player.vimeo.com/video/'+id+'?title=0&amp;byline=0&amp;portrait=0&amp;color=ed3300&amp;autoplay=1" width="296" height="166" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
	$(t).detach();
}

var currentSlide=0;
var gotoURL=false;

$(document).ready(function() {
	positionLogo();
	//$('#background IMG:eq(0)').bind("load",function() {
	//	$('#background IMG:eq(0)').fit();
	//});
	$(window).resize(function() {
		positionLogo();
		//$('#background IMG').fit();
	});
	
	$('.menu>A').hide();
	$('.menu>A').click(function(e) {
		gotoURL=this.href;
		$('.menu>A').fadeOut(function() {if (gotoURL) {document.location=gotoURL; gotoURL=false}});
		return false;
	});
	$('.menubutton').click(function(e) {
		if (menuActive) {
			menuActive=false;
			$('.menu>A').fadeOut(function() {$('.menu SPAN').fadeIn();});
		} else {
			menuActive=true;
			$('.menu SPAN').hide();
			$('.menu>A').each(function(n) {
				$(this).fadeIn((n+1)*300);
				//$(this).css('margin-top',-15);
				//$(this).show().animate({'margin-top':0,'opacity':1});
			});
		}
		return false;
	});
	$('.menu SPAN').hide().fadeIn();
	$('A.pages').each(function(n) {
		$(this).click(function(e) {
			$('DIV.projects').animate({scrollLeft:n*933});
			$('A.pages').removeClass('active');
			$(this).addClass('active');
			currentSlide=n;
			if (currentSlide==0) $('A.prevslide').css('visibility','hidden'); else $('A.prevslide').css('visibility','visible');
			if (currentSlide==$('A.pages').length-1) $('A.nextslide').css('visibility','hidden'); else $('A.nextslide').css('visibility','visible');
			return false;
		});
	});
	$('A.prevslide').css('visibility','hidden');
	$('A.nextslide').click(function() {
		$('A.pages:eq('+(currentSlide+1)+')').click();
	});
	$('A.prevslide').click(function() {
		$('A.pages:eq('+(currentSlide-1)+')').click();
	});

	$('DIV.projectdetails').each(function() {
		//$(this).parent().scrollLeft(315);
	});
	$('A.info').click(function(e) {
		if ($(this).hasClass('active')) {
			$('DIV.info').fadeOut();
			//$('DIV.projects').animate({scrollLeft:315});
			$(this).removeClass('active');
		} else {
			$('DIV.info').fadeIn();
			//$('DIV.info').css('margin-left',-315).animate({marginleft:0});
			$(this).addClass('active');
			//$('DIV.projects').animate({scrollLeft:0});
		}
	});

	$('DIV.projects').bind("mousewheel",function(ev,delta) {
		//$(this).scrollTop($(this).scrollTop()-ev.wheelDelta);
		//$(this).scrollLeft($(this).scrollLeft()-ev.wheelDelta);
	});

});

