jQuery(document).ready(function()
{
	$('.case').live('mouseenter', function () { $(this).find('img').fadeTo(200, 1.0); });
	$('.case').live('mouseleave', function () { $(this).find('img').fadeTo(500, 0.9); });
	
	var $window = $(window);
	var $menu = $('#menu');
	var $les = $('#larsersej');
	var $bg0 = $('#bg0');
	var $bg1 = $('#bg1');
	var $bg2 = $('#bg2');
	var $bg3 = $('#bg3');
	
	var $les_pos = 40;
	var $bg1_pos = 100;
	var $bg2_pos = 360;
	var $bg3_pos = 600;
	
	var $footer_height = $('#footer').height() + 50;
	var $menu_height = $('#menu').height();
	var $menu_stuck = false;
	
	$window.scroll(function() {
		$les.css('backgroundPosition', 'center '+ ($les_pos - $window.scrollTop() / 10) +'px');
		$bg1.css('backgroundPosition', 'center '+ ($bg1_pos - $window.scrollTop() / 6) +'px');
		$bg2.css('backgroundPosition', 'center '+ ($bg2_pos - $window.scrollTop() / 2) +'px');
		$bg3.css('backgroundPosition', 'center '+ ($bg3_pos - $window.scrollTop()) +'px');
		
		$menu.animate({ bottom: '10px' }, 300);
		$les.css('opacity', (1 / $window.scrollTop())* 100);
		$bg1.css('opacity', (1 / $window.scrollTop())* 200);
		
		if($window.scrollTop() < 40){
			$les.css('opacity', '1');
			$bg0.css('opacity', '1');
			$bg1.css('opacity', '1');
			$bg2.css('opacity', '1');
		}
		else if($window.scrollTop() < 700){
			$bg0.css('opacity', '1');
			$bg2.css('opacity', '1');
		}
		else{
			$les.css('opacity', '0');
			$bg0.css('opacity', '0');
			$bg1.css('opacity', '0');
			$bg2.css('opacity', '0');
		}
		
		if($menu_stuck == false){
			if(($('body').height() - $menu.offset().top) < $footer_height){
				$menu.css({
					'top'		: $('body').height() - $footer_height,
					'height'	: $menu_height,
					'position' : 'absolute'
				});
				$menu_stuck = true;
			}
		}
		else if(($menu.offset().top - $window.scrollTop()) - ($window.height()) > -50){
			$menu.css({
				'top'		: '',
				'position' : 'fixed'
			});
			$menu_stuck = false;
		}
	});
	
	$('#vis_cv').live('click', function() {
		$.scrollTo( $('#footer').offset().top, 700 );
	});
	$('#vis_det_hele').live('click', function() {
		vis($(this));
	});
	$('#vis_web').live('click', function() {
		vis($(this));
	});
	$('#vis_video').live('click', function() {
		vis($(this));
	});
	$('#vis_andet').live('click', function() {
		vis($(this));
	});
	
	$('.case').live('click', function() {
		getCase($(this));
	});
	
	$('#img').live('hover', function() {
		$('#dragimg').draggable({
			axis: 'y',
			scroll: false,
			stop: function(event, ui) {
				var helper = ui.helper, pos = ui.position;
				var h = -(helper.outerHeight() - $(helper).parent().outerHeight());
				if (pos.top >= 0) {
					helper.animate({ top: 0 }, 300, imgdrag);
				} else if (pos.top <= h) {
					helper.animate({ top: h }, 300, imgdrag);
				} else {
					imgdrag();
				}
			}
		});
	});
	$('.nopreview').live('click', function() {
		$('#imgpreview').remove();
		$('.haspreview').addClass('nopreview').removeClass('haspreview');
		$(this).removeClass('nopreview').addClass('haspreview').prepend('<div id="imgpreview"></div>');
		$('#imgpreview').draggable({
			containment: 'parent',
			scroll: false,
			drag: dragging
		});
		$('#img').html('<img id="dragimg" src="/img/cases/case'+ $(this).find('img').attr('rel') +'/'+ $(this).find('img').attr('alt') +'" alt="" />');
	});
});


var where = '';
var showcase_height_old;
var showcase_height_new;


function vis(e) {
	$.scrollTo( 100, 500 );
	$('.selected').removeClass('selected');
	e.addClass('selected');
	
	where = e.attr('rel');
	var t = setTimeout(getCases, 550);
}


function getCases() {
	$('#cases').load('get_cases.php?where='+where);
	$('#showcase').slideUp(300, function() { $(this).html(''); });
}


function getCase(e) {
	$.scrollTo( 400, 500 );
	var $case_id = e.attr('id').substr(4);
	
	$('#showcase').css({'height':'','display':'block'});
	$('#showcase').load('get_case.php?where='+$case_id, function() {
		$('#imgpreview').draggable({
			containment: 'parent',
			scroll: false,
			drag: dragging
		});
	});
}

function dragging() {
	$thumb_h = $('#imgpreview').parent().height();
	$preview_h = $('#imgpreview').height();
	$img_h = $('#img img').height();
	$img_view_h = $('#img').height();
	$preview_pos = $('#imgpreview').position().top;
	$percent = $preview_pos / ($thumb_h - $preview_h - 4) * 100;
	$('#img img').css('top','-'+ ((($img_h - $img_view_h) / 100) * $percent) +'px');
}
function imgdrag() {
	$thumb_h = $('#imgpreview').parent().height();
	$preview_h = $('#imgpreview').height();
	$img_h = $('#img img').height();
	$img_view_h = $('#img').height();
	$dragimg_pos = $('#dragimg').position().top;
	$percent = $dragimg_pos / ($img_h - $img_view_h) * 100;
	$('#imgpreview').css('top', Math.abs((($thumb_h - $preview_h - 4) / 100) * $percent) +'px');
}
