function pageWidth() {
	return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;
}

function pageHeight() {
	return window.innerHeight != null ? window.innerHeight
			: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight
					: document.body != null ? document.body.clientHeight : null;
}

function doResize() {
	$('div.slide').css('padding-left', (pageWidth()-660)/2).
		css('padding-right', (pageWidth()-660)/2);
	var margin_top = (pageHeight()-440)/2-120;
	var margin_bottom = (pageHeight()-440)/2-220;
	if(margin_top < 0)
		margin_top = 0;
	if(margin_bottom < 0)
		margin_bottom = 0;
	$('#header').css('margin-top', margin_top);
	$('#footer').css('margin-bottom', margin_bottom);
}

jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
};

$(document).ready(function() {
	$(window).resize(function() {
		doResize();
	});
	doResize();
	$('#page').serialScroll({
		target:'#content',
		items:'div.slide',
		axis:'xy',
		navigation:'#footer #nav a',
		duration:1200,
		force:true,
		start: 1
	});
	$('#showroom').cycle({
		fx: 'fade',
		speed: 100,
		timeout: 0,
		pager: '#showroom-nav',
		startingSlide: 6,
		pagerAnchorBuilder: function(idx, slide) {
	        return '<a href="#"><img src="' + slide.src.replace('originals','thumbs') + 
	        	'" width="16" height="16" border="0" alt="' + 
	        	slide.src + '" /></a>'; 
	    },
	    before: function (curr,next,opts) {
	        var $slide = $(next);
	        var w = $slide.outerWidth();
	        var h = $slide.outerHeight();
	        $slide.css({
	            marginTop: (440 - h) / 2,
	            marginLeft: (pageWidth() - w) / 2
	        });
	    }
	});
	{
		var src = $('#showroom-nav a.activeSlide').children('img').attr('src');
		$('#bg-img').children('img').attr('src', src.replace('thumbs', 'bgs'));
	}
	$('#showroom-nav a').click(function() {
		$('#content').trigger( 'goto', [ 1 ] );
		var src = $(this).children('img').attr('src');
		//$('#bg-img').children('img').fadeOut(250);
		$('#bg-img').children('img').attr('src', src.replace('thumbs', 'bgs'));
		//$('#bg-img').children('img').fadeIn(1000);
		$('#nav a').removeClass('activeSlide').filter('[title=Showroom]').
			addClass('activeSlide').children('img').attr('src', './img/btn_untypisch.png');
	});
	$('#nav a').click(function() {
		$('#nav a').removeClass('activeSlide');
		$(this).addClass('activeSlide');
		if($(this).attr('title') == 'Showroom') {
			$(this).children('img').attr('src', './img/btn_untypisch.png');
			var src = $('#showroom-nav a.activeSlide').children('img').attr('src');
			//$('#bg-img').children('img').css('display', 'none');
			$('#bg-img').children('img').attr('src', src.replace('thumbs', 'bgs'));
			//$('#bg-img').children('img').fadeIn(1000);
		}
		else {
			$('#nav a[title=Showroom] img').attr('src', './img/btn_showroom.png');
			//$('#bg-img').children('img').fadeOut(1000);
			$('#bg-img').children('img').attr('src', './img/spacer.gif');
		}
	});
	$('.email').defuscate();
	$('img:visible').load(function () {
		$('#loading').fadeOut(1200);
		$.preloadImages($('#showroom-nav img').attr('src').replace('thumbs', 'bgs'));
	});
});


