// ====================================================
// = (c) 2009 Mickael Riga for CampbellHay Associates =
// ====================================================

function swap_background(file)
{
	$('#screen').hide();
	$('#spinner').show();
	//$('#background').css('opacity' , 0.5)
	var i = new Image();
	$(i).load(background_loaded).attr('src', file);
}
function background_loaded()
{
	
	var prev_src = $('#background').attr('src');
	$('#background').show()
	.attr('src', $(this).attr('src'));
	
	$('#spinner').hide();	
	if ($('#background').data('first_load')=='true')
	{
		resize_background();
		$('#background').data('first_load', 'false');
	}
	
	
	if (! $.browser.msie )
	{
		var prev_top = $('#background').position().top;
		var prev_left = $('#background').position().left;
		$('#background').clone().attr('id', 'clone')
		.attr('src', prev_src)
		.insertBefore($('#background'))
		.css({'position' : 'absolute', 'top' : prev_top+'px', 'left' : prev_left+'px'});
	
		if ($('#background').data('transition')=='vertical')
		{
			$('#background').css('top', $(window).height() + 'px');
			$('#background').animate({'top' : '-='+($(window).height()-prev_top)}, 'slow', 'swing', function() {
				$(this).css('top', Math.round(($(window).height() - $(this).height()) / 2) + 'px');
				if ($(this).attr('src')=='/images/interface/Background_Video.jpg')
				{
					$('#screen').fadeIn('slow');
				} else {
					//$('#screen').hide();
				}
			});
			$('#clone').animate({'top' : '-='+($(window).height()-prev_top)}, 'slow', 'swing', function() {
				$(this).remove();
			});
		} else {
			$('#background').css('left', $(window).width() + 'px');
			$('#background').animate({'left' : '-='+($(window).width()-prev_left)}, 'slow', 'swing', function() {
				$(this).css('left', Math.round(($(window).width() - $(this).width()) / 2) + 'px');
				if ($(this).attr('src')=='/images/interface/Background_Video.jpg')
				{
					$('#screen').fadeIn('slow');
				} else {
					//$('#screen').hide();
				}
			});
			$('#clone').animate({'left' : '-='+($(window).width()-prev_left)}, 'slow', 'swing', function() {
				$(this).remove();
			});
		}
	} 	else if ($.browser.msie && $(this).attr('src')=='/images/interface/Background_Video.jpg')
	{
		$('#screen').show();
	}
	//alert($('#background').attr('src'));
	
}

function swap_video(file)
{
	//$('#screen').fadeOut('fast', function() {
		$('#video').empty().flash({
			src: file,
			width: 640,
			height: 380,
			wmode: 'transparent'
		});
	//	$(this).fadeIn('fast');
	//});
	
}

function resize_background()
{
	
	// Sizing
	var ratio_w = ($(window).width()/1600*100);
	var ratio_h = ($(window).height()/1000*100);
	var ratio_to_use = Math.max(ratio_h,ratio_w);
	//
	$('#background').css({
		'width' : (16*ratio_to_use + "px"),
		'height' : (10*ratio_to_use + "px"),
		'left' : Math.round(($(window).width() - 16*ratio_to_use) / 2) + 'px',
		'top' : Math.round(($(window).height() - 10*ratio_to_use) / 2) + 'px'
	});
	// center screen
	$('#screen').center_on_screen();
	$('#spinner').center_on_screen();
}

function init_footer()
{
	var the_h3 = $('#portfolio-title a').html();
	$('#portfolio-title').hover(
		function () {
			$(this).find('a').html('Full Project');
			},
		function () {
			$(this).find('a').html(the_h3);
		}
	);
	
	$('#portfolio-title a.load-client').click_to_swap();
	
	$('#portfolio-info p').hide();
	$('#portfolio-info').hover(
		function () {
			$(this).children('p').slideDown(150);
			},
		function () {
			$(this).children('p').slideUp(150);
		}
	);
	
	$('.load-media').click_to_swap('horizontal');
	$('.next-media').click(function() {
		var next_media = $("a.load-media:not('.unactive')").prev();
		$(".load-media:first").click();
		//$(window).resize();
		return false
	});
}

// ========
// = Init =
// ========

$(function(){
	
	if (window.location.hash!='')
	{
		document.location.href = window.location.hash.replace('#', '');
	} else {
	
	// Extends ... but not a plugin
	$.fn.click_to_swap = function(transition) {
		$(this).click(function(e) {
			transition = (typeof transition == 'undefined' ? 'vertical' : transition);
			$('#background').data('transition', transition);
			var next_image = $(this).hasClass('interactive_media') ? '/images/interface/Background_Video.jpg' : $(this).attr('rel');
			if ($('#background').attr('src')!='' && $('#background').attr('src')!=(next_image))
			{
				swap_background(next_image);
			}
			if ($(this).hasClass('interactive_media'))
			{
				swap_video($(this).attr('rel'));
			}
			if (transition=='horizontal')
			{
				$('.load-media').addClass('unactive');
				$(this).removeClass('unactive');
			} else {
				$('#portfolio-footer').load($(this).attr('href'), {}, init_footer);
			}
			window.location.hash = $(this).attr('href');
			return false;
		});
		return this;
	}
	$.fn.center_on_screen = function() {
		this.css({
			'left' : Math.round(($(window).width() - $(this).width()) / 2) + 'px',
			'top' : Math.round(($(window).height() - $(this).height()) / 2) + 'px'
		});
		return this
	}
	
	// Menu links
	$('.load-client').click_to_swap();
	
	// Frist image
	$('#background').data('first_load', 'true');
	$('#background').data('transition', 'vertical');
	swap_background(first_image_url);
	
	// First video
	if (first_video!='')
	{
		$('#video').flash({
			src: first_video,
			width: 640,
			height: 380,
			wmode: 'transparent'
		});
		$('#screen').center_on_screen();
	}
	
	init_footer();
	
	$(window).resize(resize_background);
	}
});

