function positionFirstBgImg() {
	$('img#first-bg').each(function() {
		imgwidth = $(this)[0].width;
		imgheight = $(this)[0].height;
		viewportheight = $(window).height();
		viewportwidth = $(window).width();
		
		imgratio = imgwidth / imgheight;
		viewportratio = viewportwidth / viewportheight;

		if (imgratio == viewportratio) {
			imgwidth = viewportwidth;
			imgheight = viewportheight;
		} else {
			imgwidth = viewportwidth;
			imgheight = imgwidth / imgratio;
			if (imgheight < viewportheight) {
				imgheight = viewportheight;
				imgwidth = imgheight * imgratio;
			}
		}

		var top = (viewportheight - imgheight) / 2;
		if (top > 0) top = 0 - top;
		var left = (viewportwidth - imgwidth) / 2;
		if (left > 0) left = 0 - left;

		$("div#wrapper").css({'display': 'block'});
		
		$(this)
			.css({
				'width': parseInt(imgwidth) + 'px',
				'height': parseInt(imgheight) + 'px'
			});
	});
}	

function positionFirstInfoBox() {
	$('img#first-infobox').each(function() {
		imgheight = $(this)[0].height;
		viewportheight = $(window).height();
		
		bottommargin = 15;
		leftmargin = 22;

		$(this)
			.css({
				'top': (viewportheight - imgheight - bottommargin) + 'px',
				'left': leftmargin + 'px'
			});
	});
}

function positionCall() {
	viewportheight = $(window).height();
	viewportwidth = $(window).width();
	$('img#callbg').css({
		'width': viewportwidth + 'px',
		'height': viewportheight + 'px'
	});

	imgwidth = $('img#callinfo')[0].width;
	imgheight = $('img#callinfo')[0].height;
	var top = (viewportheight / 2) - (imgheight / 2);
	var left = (viewportwidth / 2) - (imgwidth / 2);
	$('img#callinfo').css({
		'top': parseInt(top) + 'px',
		'left': parseInt(left) + 'px'
	});
}

function positionPortfolio() {
	viewportheight = $(window).height();
	viewportwidth = $(window).width();
	$('img#portfoliobg').css({
		'width': viewportwidth + 'px',
		'height': viewportheight + 'px'
	});

	imgwidth = $('img#clients')[0].width;
	imgheight = $('img#clients')[0].height;
	clientstop = parseInt(viewportheight - imgheight - 25);
	clientsleft = parseInt((viewportwidth / 2) - (imgwidth / 2));

	var top = ((viewportheight - imgheight) / 2) - (480 / 2);
	var left = (viewportwidth / 2) - (755 / 2);
	if (clientstop < (top + 480 + 10)) {
		clientstop = top + 480 + 10;
	}
	
	$('img#clients').css({
		'top': clientstop + 'px',
		'left': clientsleft
	});
	$('div#portfolioscrollarea').css({
		'top': parseInt(top) + 'px',
		'left': parseInt(left) + 'px'
	});

}

function positionVideo() {
	// DIMMER-LAGRET
	viewportheight = $(window).height();
	viewportwidth = $(window).width();
	$('img#filmdimmer').css({
			'top': 0,
			'left': 0,
			'width': viewportwidth,
			'height': viewportheight
		});

	// FILMRUTAN
	$('img#filmbox').each(function() {
		imgwidth = $(this)[0].width;
		imgheight = $(this)[0].height;
		viewportheight = $(window).height();
		viewportwidth = $(window).width();

		var top = (viewportheight / 2) - (imgheight / 2);
		var left = (viewportwidth / 2) - (imgwidth / 2);

		$(this).css({
			'top': parseInt(top) + 'px',
			'left': parseInt(left) + 'px'
			});

		// FILM-STÄNGKNAPPEN
		topmargin = 8;
		rightmargin = 26;
		$('img#filmclose').css({
			'top': parseInt(top + topmargin) + 'px',
			'left': parseInt(left + imgwidth - $('img#filmclose')[0].width - rightmargin) + 'px'
		});

		// SJÄLVA FILMEN
		$('div#film').each(function() {
			$(this).css({
				'top': $('img#filmbox').css('top'),
				'left': $('img#filmbox').css('left')
			});
		});

	});
}

function playVideo() {
	positionVideo();

	$('div#film').each(function() {
		$(this).remove();
	});
	$('img#filmbox').after('<div id="film" style="display: none; position: absolute"></div>');

	margins = 26;
	width = $('img#filmbox')[0].width - (margins * 2);
	height = $('img#filmbox')[0].height - (margins * 2);
	var so = new SWFObject('player.swf','ply',width,height,'9','#000000');
	so.addParam('allowfullscreen','true');
	so.addParam('allowscriptaccess','always');
	so.addParam('wmode','opaque');
	// Bugg, funkar inte...
	//so.addVariable('icons','false');
	so.addVariable('file','video/video.flv');
	so.addVariable('image','video/preview.png');
	so.addVariable('controlbar','none');
	so.addVariable('dock','false');
	so.addVariable('autostart','false');
	so.addVariable('stretching','fill');
	so.addVariable('width', width);
	so.addVariable('height', height);
	so.write('film');
	$('div#film').css({
		'padding': margins + 'px 0px 0px ' + margins + 'px',
		'top': $('img#filmbox').css('top'),
		'left': $('img#filmbox').css('left')
	});
	
	$('img#filmdimmer').fadeIn(function() {
		$('img#filmbox').fadeIn(function() {
			$('div#film').fadeIn();
		});
		$('img#filmclose').fadeIn();
	});
}

function hideFilm() {
	$('img#filmbox').fadeOut();
	$('img#filmclose').fadeOut();
	$('img#filmdimmer').fadeOut();
	$('div#film').fadeOut(function() {
		$('div#film').remove();
	});
}
function hideCall() {
	$('img#callbg').fadeOut('fast');
	$('img#callinfo').fadeOut('fast');
}
function hidePortfolio() {
	$('img#portfoliobg').fadeOut('fast');
	$('div#portfolioscrollarea').fadeOut('fast', function() {
		$(this).html('');
	});
	$('img#clients').fadeOut();
}

$(window).load(function() {

	$("div#meny li").each(function() {
		$(this)
			.children('img.hover').css('margin-left', '-' + $(this).children('img.hover')[0].width);
		$(this).hover(function() {
			$(this).children('img.hover').fadeIn();
		}, function() {
			$(this).children('img.hover').fadeOut();
		});
	});

	$("img#first-bg").each(function() {
		positionFirstBgImg();
		$('div#wrapper').css('display', 'block');
		$(this).fadeIn(function() {
			$('img#first-infobox').each(function() {
				positionFirstInfoBox();
				$(this)
					.click(function() {
						playVideo();
					}).hover(function() {
						$(this).fadeTo('fast', 0.8);
					}, function() {
						$(this).fadeTo('fast', 1);
					})
					.fadeTo('slow', 1);
			})
		});
	});
	$('img#filmclose').click(function() {
		hideFilm();
	});
	$('li#hem').click(function() {
		hideFilm();
		hideCall();
		hidePortfolio();
	});
	$('li#blog').click(function() {
		document.location.href="http://snask.com/blog";
	});
	$('li#portfolio').click(function() {
		positionPortfolio();
		$('img#portfoliobg').fadeIn(function() {
			$('div#portfolioscrollarea')
				.css('display', 'block')
				.html('<iframe src="portfolio.php" id="portfolioiframe" width="755" height="480" scrolling="no" frameborder="0"></iframe>');
		});
		$('img#clients').fadeIn();
	});
	$('li#call').click(function() {
		positionCall();
		hideFilm();
		$('img#callbg').fadeIn('fast', function() {
			hidePortfolio();
			$('img#callinfo').fadeIn(100);
		});
	});
});
$(window).resize(function() {
	positionFirstBgImg();
	positionFirstInfoBox();
	positionVideo();
	positionCall();
	positionPortfolio();
});


