var loader = null;

$(function() {  
	$('.services .content li:last').css({'height' : 0, 'padding-bottom' : '11px'});
	$FullscreenrOptions = {  width: 1200, height: 800, bgID: '.bgimg' };
	jQuery.fn.fullscreenr($FullscreenrOptions);

	$container = $('#container');
	$loader = $('#loader');
	$bg1 = $('#bg1');
	$bg2 = $('#bg2');
	$content = $container.find('.content');
	$sub_content = $container.find('.sub-content');
	
	hash = location.hash ? location.hash.replace('#', '') : '';
	if (hash) {
		open_page(hash, 1);
	} else {
		open_page('about', 1);
	}

	$('.nav li a')
		.click(function() {
			$this = $(this);
		
			$li = $this.parent();
			if ($li.hasClass('current')) return false;

			page = $this.attr('href').replace('#', '');
			open_page(page, 0);
		})
	    .hover(function() {
			$(this).children('span').stop(true, true).fadeIn();
		}, function() {
			$(this).children('span').stop(true, true).fadeOut();
		});

    if (!navigator.userAgent.match(/(iphone|ipod|ipad)/i)) {
        $('.nav li a').each(function(i) {
			$this = $(this);
		    $(this).append($("<span>").css({width: $this.width() + 22, height: $this.height() + 10}).hide());
		});    	    
	}
});

function open_page(page, init) {
	$container.find('.nav .current').removeClass('current');
	$container.find('.nav li.' + page).addClass('current');

	$page = $('.page.' + page);
	$page_content = $page.find('.content');
	$page_sub_content = $page.find('.sub-content');
	
	if (init) {
		$bg1.attr('src', 'imgs/bg-' + page + '.jpg');
		$bg2.attr('src', 'imgs/bg-' + page + '.jpg');
		$content.parent().attr('class', 'sub-container ' + page);
		$content.width($page_content.css('width'));
		$content.height($page_content.css('height'));
		$sub_content.html($page_sub_content.html()).show();
		init_pages(page);
	} else {
		$content.css('height', $content.height() + 'px');
		$sub_content.fadeOut(200, function() {
			$content.parent().attr('class', 'sub-container ' + page);

			// Content
			$content.animate({
				backgroundColor : $page_content.css('background-color'),
				height			: $page_content.css('height'),
				width			: $page_content.css('width')
			}, 600, function() {
				$sub_content.html($page_sub_content.html()).fadeIn();
				init_pages(page);
				
				// Background image
				if ($bg1.attr('rel') == 'current') {
					$bgc = $bg1;
					$bgn = $bg2;
				} else {
					$bgc = $bg2;
					$bgn = $bg1;
				}
				
				img = 'imgs/bg-' + page + '.jpg';
//				if ($bgc.attr('src') != img) {
					$bgn.attr('src', img);
                    
					$loader.fadeIn(100);
                    
                    if ($.browser.msie) {
                        loader = setInterval(function() {
                            if ($bgn[0].complete) {
                                 image_loaded();
                                clearInterval(loader);
                            }
                        }, 200);
                    } else {
    					$bgn.load(function() {
    						image_loaded();
    					});
    				}
//				}
			});
		});
	}
}
function foo() {
     
}
function image_loaded() {
    $loader.fadeOut(100);

	$bgn.css('z-index', '-3').hide();
	$bgc.css('z-index', '-2').fadeOut(400, function() {
		$bgn.fadeIn(400);
		$bgn.css('z-index', '-2');
		$bgc.css('z-index', '-3').attr('src', '');
		$bgc.attr('rel', '');
		$bgn.attr('rel', 'current');
	});
}
	
function init_pages(page) {
	switch(page) {
		case 'team':
			$($('.team')[0]).find('.mem')
				.click(function(event) {
					$this = $(this);
					
					if ($this.hasClass('active')) return;
					
					$this.parents('.sub-content').find('.mem.active').parents('li').animate({
						width: '46px',
						height: '52px'
					}).removeClass('active');
					
					$this.parents('li').animate({
						width: '140px',
						height: '146px'
					}, function() {$this.parents('.sub-content').find('li').removeAttr('style');}).addClass('active');
					
					$this.parents('.sub-content').find('.mem.active').animate({
						width: '46px',
						height: '52px'
					}).removeClass('active');
					
					$this.animate({
						width: '140px',
						height: '146px'
					}).addClass('active');
					
					person = $this.attr('title');
					$profile = $container.find('.profile[rel=' + person + ']');
					
					$container.find('.profile.current')
						.css('position', 'absolute')
						.fadeOut(500, function() {
							$(this).removeClass('current').css('position', 'relative');
						});
					$profile
						.css('position', 'absolute')
						.fadeIn(500, function() {
							$(this).addClass('current').css('position', 'relative');
						});
					$content.animate({
						height: $profile.height() + 150
					});
				})
				.hover(function() {
					$(this).fadeTo(200, 0.8);
				}, function() {
					$(this).fadeTo(200, 1);
				});

			break;
		case 'services':
			$('#services-accordian').accordion(function() {
				$('#services-accordian').parents('.content').css({height : 'auto'});
			});
			$('#services-accordian').find('li').css({height : 'auto'});
			break;
	}
}

