/*
	(c) 2009 - Dominic Szablewski 
	www.phoboslab.org
*/

/* Page Panning 
----------------------------------------------------------------------- */
var PagePan = {
	element: null,
	last: {x: 0, y: 0},
	lastSample: {x: 0, y: 0},
	speed: {x:0, y: 0},
	sampleInterval: null,
	time: 0
};

PagePan.init = function( element, nopan ) {
	PagePan.element = $(element);
	PagePan.element.addClass( 'panElement' );
	PagePan.element.mousedown( PagePan.mousedown );
	PagePan.element.mouseup( PagePan.mouseup );
	$(nopan).mousedown( PagePan.noop );
	$(nopan).mouseup( PagePan.noop );
	
	if( !$.browser.msie ) {
		setInterval( PagePan.removePrints, 100 );
	}
}

PagePan.removePrints = function() {
	$('.handprint').each(function(){
		var op = $(this).css('opacity') - 0.05;
		if( op < 0 ) {
			$(this).remove();
		} else {
			$(this).css( 'opacity', op );
		}
	});
}

PagePan.noop = function( ev ) {
	ev.stopPropagation();
}

PagePan.mousedown = function( ev ) {
	PagePan.element.stop();
	PagePan.last = { x: ev.clientX, y: ev.clientY };
	PagePan.speed = { x:0, y:0 };
	PagePan.time = (new Date).getTime();
	
	PagePan.lastSample = { x: ev.clientX, y: ev.clientY };
	PagePan.sampleInterval = setInterval( PagePan.takeSample, 20 );
	
	$(this).addClass( 'panMouseDown' );
	$(this).bind( 'mousemove', PagePan.mousemove );
	
	if (document.selection) {
		document.selection.empty();
	}
	else if (window.getSelection) {
		window.getSelection().removeAllRanges();
	}
	
	return false;
}

PagePan.mouseup = function( ev ) {
	$(this).removeClass( 'panMouseDown' );
	$(this).unbind( 'mousemove', PagePan.mousemove );
	clearInterval( PagePan.sampleInterval );
	
	var cx = parseInt( PagePan.element.css('left') );
	var cy = parseInt( PagePan.element.css('top') );
	
	if( !$.browser.msie ) {
		PagePan.element.append( '<div class="handprint" style="top: '+(ev.clientY-cy)+'px; left:'+(ev.clientX-cx)+'px"/>' );
	}

	PagePan.element.animate({ 
		left:(cx + PagePan.speed.x + 'px'),
		top:(cy + PagePan.speed.y + 'px')
	}, 500, 'easeOutQuad' );
	
	return false;
}

PagePan.mousemove = function( ev ) {
	var cx = parseInt( PagePan.element.css('left') );
	var cy = parseInt( PagePan.element.css('top') );
	PagePan.element.css( 'left', cx + ev.clientX - PagePan.last.x + 'px' );
	PagePan.element.css( 'top', cy + ev.clientY - PagePan.last.y + 'px' );

	PagePan.last = { x: ev.clientX, y: ev.clientY };
	return false;
}

PagePan.takeSample = function( ev ) {
	var newTime = (new Date).getTime();
	var tdiff = Math.max(newTime - PagePan.time, 20 ) / 1000;
	PagePan.time = newTime;
	PagePan.speed.x = PagePan.speed.x * 0.2 + ((PagePan.last.x - PagePan.lastSample.x) / tdiff) * 0.1;
	PagePan.speed.y = PagePan.speed.y * 0.2 + ((PagePan.last.y - PagePan.lastSample.y) / tdiff) * 0.1;
	PagePan.lastSample = { x:PagePan.last.x, y:PagePan.last.y };
}



/* Link Scroll
----------------------------------------------------------------------- */
var LinkScroll = {
	offset:{ x:-240, y:120 }
};
LinkScroll.init = function() {
	$('a[name]').each(function(){
		$(this).attr('id', 'scroll_' + $(this).attr('name') );
		$(this).attr('name', '');
	});
	
	$('a[href^=#]').click(function() {
		LinkScroll.scrollTo( this.hash );
		this.blur();
		return false;
	});
	
	if( document.location.hash.length ) {
		$(document).scrollTop(0).scrollLeft(0);
		
		var pos = $('#scroll_' + document.location.hash.slice(1)).position();
		PagePan.element.css('left', (-pos.left + LinkScroll.offset.x + $(window).width()/2) + 'px');
		PagePan.element.css('top', (-pos.top + LinkScroll.offset.y) + 'px');
	} else {
		$(document).scrollTop(0).scrollLeft(0);
		
		var pos = $('#scroll_songfever').position();
		PagePan.element.css('left', (-pos.left + LinkScroll.offset.x + $(window).width()/2) + 'px');
		PagePan.element.css('top', (-pos.top + LinkScroll.offset.y) + 'px');
	}
}

LinkScroll.scrollTo = function( hash ) {
	document.location.hash = hash;
	$(document).scrollTop(0).scrollLeft(0);
	
	var pos = $('#scroll_' + hash.slice(1)).position();
	PagePan.element.stop();
	PagePan.element.animate({ 
		left:((-pos.left + LinkScroll.offset.x + $(window).width()/2) + 'px'),
		top:((-pos.top + LinkScroll.offset.y) + 'px')
	}, 500, 'easeOutQuad', function() {}  );
}



/* Document Ready
----------------------------------------------------------------------- */
$(function(){
	$('.email').each(function() {
		// convert email links
		var email = $(this).html().replace(/\s*{at}\s*/, '@').replace(/\s*{dot}\s*/, '.');
		$(this).html('<a href="mailto:' + email + '">' + email + '</a>' );
	});
	
	
	if( navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ) {
		// no Fancy stuff for the iPhone/iPod
		return;
	}

	$('body,html').css('overflow', 'hidden');
	$('div.column').css('float', 'left');
	$('#portfolio').addClass('portfolioWithJS');
	
	if( $.browser.msie ) {
		// fix some fucking IE fuckups
		$('body').prepend('<div class="ieFixFlicker">&nbsp;</div>');
		$('body').prepend('<div class="ieFixHeadBackground">&nbsp;</div>');
		$('.work').addClass('ieFixWorkBackground');
	}
	
	PagePan.init( '#portfolio', 'a' );
	LinkScroll.init();
});
