$(document).ready( function() {
    
	$('.main_fader').fadeOut(500);
	
	$('#post_14 a').addClass('nofollow');
	
	$('a:not(.nofollow)').live( 'click', function() {
		var thisHref = $(this).attr('href');
		window.location = thisHref;
		$('.main_fader').fadeIn(250);
		return false;
	});

	i = -1;
	$('title').animate({ opacity: '1' }, 500, function() {
		fadeInCase();
	});
	
	if($('body').is(':not(.msie)')) {
		$('.case .label').css('opacity', '0');
		$('.case').hover( function() {
			$(this).find('.label').stop().animate({ opacity: '1' });
		}, function() {
			$(this).find('.label').stop().animate({ opacity: '0' });
		});
	} else {
		$('.case .label').toggle();
		$('.case').hover( function() {
			$(this).find('.label').show();
		}, function() {
			$(this).find('.label').hide();
		});
	}
	
	$(window).scroll( function() {
		windowHeight = $(window).height();
		bodyHeight = $('body').height();
		bodyScroll = $('body').scrollTop();
		
		$('.cases .case:not(.initalized)').each( function() {
			if($(this).offset()['top']<(windowHeight+bodyScroll-200)) {
				$(this).addClass('initialized');
			}
		});
		
	});
		
	$('.slideshow .slide:gt(0)').css('display', 'none');
	totalSlides = $('.slide').size();
	currentSlide = 0;
	nextSlide = 0;
	$('.control:not(.current)').live( 'click', function() {
		currentSlide = $('.control').index(this);
		$('.slide:visible').fadeOut(1000);
		$('.slide:eq('+currentSlide+')').fadeIn(1000);
		$('.slideshow_controls .control').removeClass('current');
		$(this).addClass('current');
		$('.slides').animate({ height: $('.slideshow .slide:eq('+currentSlide+')').height() }, 1000);
		$('.op').css('visibility', 'visible');
		if($(this).is(':last-child')) {
			$('.op.next').css('visibility', 'hidden');
		}
		if($(this).is(':first-child')) {
			$('.op.prev').css('visibility', 'hidden');
		}
	});
	$('.slides').css({ height: $('.slideshow .slide:first').height() });
	if(totalSlides>1) {
		$('.slideshow').append('<div class="slideshow_controls"></div>');
		for(i=0;i<totalSlides;i++) {
			$('.slideshow_controls').append('<div class="control">'+(i+1)+'</div>');
		}
		$('.slideshow_controls .control:first').addClass('current');
		$('.slideshow').append('<div class="next op"></div><div class="prev op" style="visibility: hidden;"></div>');
		$('.slideshow .next').click( function() {
			$('.slideshow .slideshow_controls .current').next().click();
		});
		$('.slideshow .prev').click( function() {
			$('.slideshow .slideshow_controls .current').prev().click();
		});
		$('.op').click( function() {
			$('.op').toggle();
			setTimeout( function() {
				$('.op').fadeIn(1000);
			}, 1000);
		});
		$('.slideshow .op').toggle();
		$('.slideshow').hover( function() {
			$('.op').fadeIn();
		}, function() {
			$('.op').fadeOut();
		});
	}
	
	defaultMarginTop = $('.pdf_bar_wrapper').css('marginTop');
	
	$('.pdf_bar .custom_parts').load('http://www.neumeister.se/pdf/bar.php');

	$('.my_pdf_tab').click( function() {
		togglePdfBar();
	});

	$('a.pdf').click( function() {
		setTimeout( function() {
			showPdfBar();
			$('.pdf_bar .custom_parts').append('<div class="single new" rel="'+$('input[name=pdf_id]').val()+'"><div class="paper"></div><div class="label">'+$('input[name=pdf_label]').val()+'</div></div>');
			adjustPdfs(true);
			saveOrder();
			setTimeout( function() {
				$('.single.new').removeClass('new').find('.paper').css('backgroundColor', '#eaedef').animate({ 'backgroundColor': '#b5b8bf' }, 1250);
			}, 2000);
			return false;
		}, 250);
	});
	
	$('.custom_parts .single').live( 'click', function() {
		$(this).fadeOut( function() {
			$(this).remove();
			adjustPdfs();
			saveOrder();
		});
	});
	
	$('.generate_button').click( function() {
		window.location = 'http://www.neumeister.se/pdf/';	
	});
	
});

function togglePdfBar() {
	if($('.pdf_bar_wrapper').css('marginTop')!=='0px') {
		$('.pdf_bar_wrapper').animate({ marginTop: '0px' });
	} else {
		$('.pdf_bar_wrapper').animate({ marginTop: defaultMarginTop });
	}	
}

function showPdfBar() {
	$('.pdf_bar_wrapper').animate({ marginTop: '0' });
}

function saveOrder() {
	var order = '';
	$('.pdf_bar .custom_parts .single').each( function() {
		order = order + $(this).attr('rel') + ',';
	});
	$.get('http://www.neumeister.se/pdf/updatebar.php?order='+order);	
}

function adjustPdfs(animate) {
	if(animate==true) {
		var delay = 500;
	} else {
		var delay = 0;
	}
	setTimeout( function() {
		var pdfPages = $('.pdf_bar .single').size();
		limiterOffset = 76*(pdfPages-9);
		if(limiterOffset<0) {
			limiterOffset=0;
		}
		if(animate=true) {
			$('.pdf_bar .limiter').animate({ scrollLeft: limiterOffset });
		} else {
			$('.pdf_bar .limiter').css({ scrollLeft: limiterOffset });
		}
		if(pdfPages==3) {
			togglePdfBar();
		}
	}, delay);	
}

function fadeInCase() {
	i = i+1;
	$('.case:eq('+i+')').animate({ opacity: '1' }, 2000 );
	setTimeout( "fadeInCase()", 250);	
}