$(document).ready(function(){
	var itemToDrag = false;
	var offsetX, offsetY;

$('.portfolio-container a').click(function(){
		var image = $(this).children('img').attr('src');
		image = image.replace(/thumbnail/, 'medium');
		var project = $(this).attr('class');
		var link = $('#project-info .' + project + ' .project-url').text();
		var div = $(this).attr('class');
		$('#project-info div.hidden').hide();
		$('#project-info div.'+div).show().css('z-index', '999');
		
		$('#project-info #image img').attr('src', image);
		$('#project-info #url').attr('href', link);
		$('#project-info').fadeIn('fast');
		return false;
	}).hover(
		function(){
			$(this).children('.name').animate({
				top: '80px'
			}, {queue: false})
		},
		function(){
			$(this).children('.name').animate({
				top: '55px'
			}, {queue: false})
		}
	);
	
	$('#close, #close-icon').click(function(){
		$('#project-info').fadeOut('slow');
		removeSelection();
		return false;
	});
	
	$('#project-info #drag').mousedown(function(e){
		var pos = $('#project-info').offset();
		//$('#drag').html(pos.left + ':' + pos.top + '|||' + e.pageX + ':' + e.pageY + '|||' + e.clientX + ':' + e.clientY);
		itemToDrag = true;
		offsetX = e.pageX - pos.left;
		offsetY = e.pageY - pos.top;
		$('body').bind('mousemove', function(e){
			if (itemToDrag) {
				$('#project-info').css({
					left: e.clientX-offsetX,
					top: e.clientY-offsetY
				})
			}}).addClass('unselectable')
		$(document).bind('mousemove', function(){
			removeSelection();
		})
	});
	
	$('#project-info #drag, body').mouseup(function(){
		itemToDrag = false;
		$('body').unbind('mousemove').removeClass('unselectable');
		$(document).unbind('mousemove');
	});	
})

function removeSelection(){
	if (window.getSelection) { 
		window.getSelection().removeAllRanges(); 
	} 
	else if (document.selection && document.selection.clear)
	document.selection.empty();
}
