var nm = {};
nm.backgroundTimer = null;
nm.backgroundKey = 0;
nm.backgroundPhotos = new Array('cloud_1_flt.jpg', 'cloud_2_flt.jpg', 'cloud_7_flt.jpg', 'cloud_8_flt.jpg');

nm.leftNavSelect = function(classToLoad)
{
	if ( '' === classToLoad ) { return false; };
	// select item in leftNav
	$('#leftNav li:not(.' + classToLoad + ')').removeClass('selected');
	$('#leftNav li.' + classToLoad).addClass('selected');
};




nm.previewOpen = function() 
{
	nm.pageClose();
	
	// get preview class
	var classToLoad = $(this).attr('title');

	// select item in leftNav
	nm.leftNavSelect(classToLoad);
	
	// get position of card, if it has one
	if ( $('#cards li.' + classToLoad + ' a').length > 0 )
	{
		var pos = $('#cards li.' + classToLoad + ' a').offset();
	}
	else
	{
		var pos = $('#cardInfo').offset();
	};

	// get the content to show
	var $preview = $('#preview .' + classToLoad);
	
	// set preview over card
	$('#preview .' + classToLoad)
	.removeClass('done')
	.addClass('card')
	.css({
		top: 	pos.top,
		left:	pos.left,
		height:	'182px',
		width:	'142px'
	})
	.show();
	
	// transition to big and centered
	$('#preview .' + classToLoad)
	.animate({
		 height: 	'482px',
		 width: 	'380px',
		 left: 		'350px',
		 top: 		'28px'
	}, 
	300, 
	function(){
		$(this).removeClass('card');
		
		movePhoto();
	});
	
	
	
	// slide photo
	function movePhoto()
	{
		setTimeout(function(){
			$('#preview .' + classToLoad + ' img.rnd10')
			.animate({
				left: '-426px'
			}, 
			510,
			function(){ clearInlineStye.call(this) }); 
		},
		10);
		
		setTimeout(function(){
			$('#preview .' + classToLoad)
			.addClass('done')
			.animate({
				 left: '582px'
			}, 
			500, 
			function(){ 
				clearInlineStye.call(this);
				
//				if ( $(this).hasClass('close') )
//				{
					$('#pageClose')
					.addClass('preview')
					.show();
//				};
			});
		},
		10);
	};
	
	// remove styles so it's reset for next time
	function clearInlineStye()
	{
		$(this)
		.attr('style', '')
		.show();
	};	
};



nm.previewClose = function() 
{
	var $preview = $('#preview > .block:visible');
	$preview.hide();
};



nm.pageLoad = function(url)
{
	$.ajax({
		type : 'POST',
		url : url,
		data : 'html=true',
		dataType: 'html',
		success : function(html)
		{
			$('#page').html(html);
			nm.pageOpen();
			
			/* Stunning Views Animation */
			var moveWidth = (parseInt($(".img-container img").width())-550);
			$(".img-container img.view").delay(1000).animate({right: '-='+moveWidth}, 5000, 'linear');
		}
	});
};



nm.pageOpen = function()
{
	nm.previewClose();
	$('#page, #pageClose')
	.removeClass('preview')
	.show();
};



nm.pageClose = function() 
{
	$('#leftNav li').removeClass('selected');
	
	$('#page, #pageClose')
	.removeClass('preview')
	.hide();
};



nm.backgroundRotate = function()
{
	nm.backgroundKey = nm.backgroundKey + 1;
	if ( nm.backgroundKey >= nm.backgroundPhotos.length )
	{
		nm.backgroundKey = 0;
	};
	
	$('<div id="backgroundFade"></div>')
	.css({
		backgroundImage: 'url(img/' + nm.backgroundPhotos[nm.backgroundKey] + ')',
		display: 'none'
	})
	.prependTo('body')
	.fadeIn(5000, function(){
		$('body').css({
			backgroundImage: 'url(img/' + nm.backgroundPhotos[nm.backgroundKey] + ')'
		});
		
		$('#backgroundFade').remove();
	});
	
};



// http://stackoverflow.com/questions/476679/preloading-images-with-jquery
function preload(arrayOfImages) {
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
        // Alternatively you could use:
        // (new Image()).src = this;
    });
};




$(document).ready(function(){
	
	$('.full a').click(function(){

		nm.previewClose();
		
		nm.previewOpen.call(this);
		return false;
	});
	
	$('#preview .block a').click(function()
	{
		// get preview class
		var classToLoad = $(this).attr('title');

		// select item in leftNav
		nm.leftNavSelect(classToLoad);
		
		var url = $(this).attr('href');
		nm.pageLoad(url);
		return false;
	});
	
	$('h1, #pageClose').click(function(){
		nm.previewClose();
		nm.pageClose();
		
		// force player to stop if still playing
		if(jwplayer('player')){
			jwplayer('player').stop();
		}
		
		return false;
	});
	
	$('li.page a').click(function(){
		// force player to stop if still playing
		if(jwplayer('player')){
			jwplayer('player').stop();
		}
		
		var url = $(this).attr('href');
		nm.pageLoad(url);
		
		var classToLoad = $(this).attr('title');
		nm.leftNavSelect(classToLoad);
		
		return false;

	});
	
	
	

 	// intro animation
	$('#loaderLogo').fadeIn(800, function(){
		$('#loaderProgress')
		.css({
			width: 0
		})
		.animate({
			width: '100%'
		}, 1000, function(){
			$('#loader').fadeOut(500, function(){
				$('#loader').remove();
				$('#leftNav ul').slideDown();
			});
		});
	});
	
	
	
	// load the card content that's dynamic
	$('#leftNav .load, #cards .load').click(function(){
		var url = $(this).attr('href');
		var classToLoad = $(this).attr('title');
		
		// force player to stop if still playing
		if(jwplayer('player')){
			jwplayer('player').stop();
		}
		
		$.ajax({
			type : 'POST',
			url : url,
			data : 'html=true',
			dataType: 'html',
			success : function(html)
			{
				$('#preview .' + classToLoad + ' div').html(html);
				$("#bf-wrapper div.bf-content").jScrollPane({showArrows:true});
			}
		});
	});
	
	preload(nm.backgroundPhotos);

	
	setInterval(nm.backgroundRotate, 10000);
});


