// Center img based on passed parent, with a vertical offset
(function ($) {
	$.fn.centerObject = function(parentObj, vOffset) {
		return this.each(function(i){
			var ah = $(this).height() + parseInt($(this).css('padding-top')) + parseInt($(this).css('padding-bottom'));
			var aw = $(this).width() + parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
			
			var ph = $(parentObj).height() - vOffset;
			var pw = $(parentObj).width();
			
			var mh = (ph - ah) / 2;
			var mw = (pw - aw) / 2;
			
			$(this).css('margin-top', mh);
			$(this).css('margin-left', mw);
		});
	};
})(jQuery);

function showFGContent() {
	switch(section) {
		case 'motion': case 'stills':
			$("#fg-content").centerObject("#container", 65);
			$("#fg-content").css('visibility', 'visible').animate({opacity:1}, 400);
			break;
		case 'news':
			$("#fg-content").centerObject("#container", 65);
			$('#fg-inner p').each(function(i){$(this).has('img').addClass('has-thumb');});
			$('#fg-inner h1:first').addClass('first');
			$('#fg-inner p:last').addClass('last');
			$('#fg-inner').jScrollPane({'scrollbarWidth': 11});
			$("#fg-content").css('visibility', 'visible').animate({opacity:1}, 400);
		default:
			break;
	}	
}

function showFGElements() {
	$('#paint-top').animate({backgroundPosition:"0 0"}, {duration:300});
	$('#paint-bot').animate({backgroundPosition:"0 0"}, {duration:300});

	$('#glow-top').delay(200).animate({opacity:1}, 400);
	$('#glow-bot').delay(200).animate({opacity:1}, 400);
	
	$('#grunge-tr').delay(500).animate({opacity:1}, 400);
	$('#grunge-bl').delay(500).animate({opacity:1}, 400);
	$('#grunge-br').delay(500).animate({opacity:1}, 400);
	
	$('#mm-name').delay(600).animate({opacity:1}, 400);
	$('#social-icons').delay(600).animate({opacity:1}, 400);
	$('#header-bg').delay(750).animate({opacity:1}, 400);
	$('#copyright').delay(750).animate({opacity:1}, 400, function(){fgReady = true; if (bgReady) {showBGImage();}});	
}

function showBGImage() {
	if (section == 'homepage') {
		$('#bg-img').animate({opacity:1}, 300);
		
		$('#nav-news').delay(520).animate({backgroundPosition:"0 0"}, 63);
		$('#nav-stills').delay(610).animate({backgroundPosition:"0 0"}, 63);
		$('#nav-motion').delay(700).animate({backgroundPosition:"0 0"}, 63);
		$('#nav-info').delay(790).animate({backgroundPosition:"0 0"}, 63);
	} else {
		$('#bg-img').delay(50).animate({opacity:1}, 300, function(){$("#fg-content").delay(200, showFGContent);});
	}
}

function resizeContent() {
	resizeMainImage();
	$("#fg-content").centerObject("#container", 65);
}

function changeState(elt, currElt, value, duration) {
	if (elt.id != $(currElt).attr('id')) {
		$(elt).animate({'opacity':value}, duration);
	}
}

$(document).ready(function() {
	$(window).bind('resize', resizeContent);
	host = 'http://' + window.location.hostname + '/' + (window.location.hostname == '10.0.1.99' || window.location.hostname == 'localhost' ? 'mmfoto2/' : '');	

	switch (section) {
		case 'motion': case 'stills':
			break;
		default:	
			$('#bg-img').css({opacity:0});
			loadBGImage();			
			break;
	}

	switch(section) {
		case 'homepage':
			totalLoadedObjects = 9;

			$('#header-bg').css({opacity:0}).image('assets/components/bugger_off.jpg', loadImgComplete, loadError);
			$('#social-icons').css({opacity:0});
			$('#copyright').css({opacity:0});
			$('#mm-name').css({opacity:0}).image('assets/components/mm_name.png', loadImgComplete, loadError);

			$("#nav a").each(function(i){
				$(this).css({backgroundPosition:"0 18px"});
			
				$(this).mouseover(function(){
					$(this).stop().animate({backgroundPosition:"0 -18px"}, {duration:150})
				})
				.mouseout(function(){
					if (!$(this).hasClass('navsel')) {
						$(this).stop().animate({backgroundPosition:"0 0"}, {duration:150, complete:function(){
							$(this).css({backgroundPosition: "0 0"})
						}})
					}
				})
			});
			
			$('#glow-top').css({opacity:0}).image('assets/components/glow-top.gif', loadImgComplete, loadError);
			$('#glow-bot').css({opacity:0}).image('assets/components/glow-bot.gif', loadImgComplete, loadError);
			
			$('#grunge-bl').css({opacity:0}).image('assets/components/grunge-bl.png', loadImgComplete, loadError);
			$('#grunge-br').css({opacity:0}).image('assets/components/grunge-br.png', loadImgComplete, loadError);
			$('#grunge-tr').css({opacity:0}).image('assets/components/grunge-tr.png', loadImgComplete, loadError);
						
			$('#paint-top').css({backgroundPosition:"0 -20px"}).image('assets/components/paint-top.png', loadImgComplete, loadError);
			$('#paint-bot').css({backgroundPosition:"0 20px"}).image('assets/components/paint-bot.png', loadImgComplete, loadError);
		
			break;
		default:
			totalLoadedObjects = 9;

			$('#nav-' + section).addClass('navsel');
		
			$("#nav a").each(function(i){
				if (!$(this).hasClass('navsel')) {$(this).css({backgroundPosition:"0 0"});}
				else {$(this).css({backgroundPosition:"0 -18px"});}
				
				$(this).mouseover(function(){
					$(this).stop().animate({backgroundPosition:"0 -18px"}, {duration:150})
				})
				.mouseout(function(){
					if (!$(this).hasClass('navsel')) {
						$(this).stop().animate({backgroundPosition:"0 0"}, {duration:150, complete:function(){
							$(this).css({backgroundPosition: "0 0"})
						}})
					}
				})
			});
		
			$('#fg-content').css({opacity:0});
			$('#nav-' + section).addClass('navsel');
			break;
	}
	
	$('#mm-name').click(function() {window.location = '.'});	
});
