/* === shannon.js ============================== */
/*	@ddamico Nov. 2010
	site-specific js code for shannon mcglothin
	web portfolio
/* ============================================= */

// do a little setup on document ready
$(document).ready(
	function () {
		
		// handle the horizontal scrolling differently as
		// jquery.scrollTo is ineffective on iOS.
		if (ShannonPort.isiPad())
		{
			ShannonPort.navigateToProject = function (projectElementId) {
				if (projectElementId != '#' && $(projectElementId).length) {
					$(document).scrollLeft($(projectElementId).position().left);
					ShannonPort.refreshNavPosition();
				}
			}
		}
		else
		{
			ShannonPort.navigateToProject = function (projectElementId) {
				if (projectElementId != '#' && $(projectElementId).length) {
					$(document).scrollTo($(projectElementId), 800);
				}
			}
		}
		
		// attach fancybox events
		$("a.fBox").fancybox({
			'overlayShow':	false,
			'hideOnContentClick': true,
			'titleShow': false,
			'showNavArrows': false
		});
		
		// hide all projectBlocks and the other content blocks
		$('.projectBlock').hide();
		//$('.videoBlock').hide();
		if (!(ShannonPort.isiPad()))
		{
			$('#tvProjects').hide();
			$('#digitalProjects').hide();
			$('#ibcProjects').hide();
		}
		
	}
);


// once all the images have loaded, do some more stuff
$(window).load(
	function () {
		
		var totalPrintWidth = 0;
		var totalTvWidth = 0;
		var totalIbcWidth = 0;
		var totalContainerWidth = 0;
		/* reset widths of (print) project blocks based on 
			the total widths of the images they contain */
		$('.projectBlock').each(
			function ()
			{
				// console.log($(this).find('img'));
				var newWidth = 0;
				$(this).find('img').each(function (index, el) {
					// console.log(el.width + " +");
					newWidth += (el.width + 1);
				});
				$(this).width(newWidth);
				$(this).show();
				
				totalPrintWidth += (newWidth + 1);
			}   
		);
		
		/*
		$('.videoBlock').each(
			function ()
			{
				$(this).show();
				$(this).children().show();
			}
		)
		*/
		
		// turn the other content blocks back on,
		// hide the loader
		$('#tvProjects').show();
		$('#digitalProjects').show();
		$('#ibcProjects').show();
		$('#loadingIndicator').hide();
		
		// for IE's sake, we need to hard-set the widths of all
		// the horizontal containing blocks
		$('#printProjects').width(totalPrintWidth - 13);
		totalTvWidth = ($('#tvProjects .videoBlock').width() + 1) * $('#tvProjects .videoBlock').length;
		totalDigitalWidth = ($('#digitalProjects .videoBlock').width() + 1) * $('#digitalProjects .videoBlock').length
								+ ($('#digitalProjects .projectBlock').width() + 1) * $('#digitalProjects .projectBlock').length;
		totalIbcWidth = ($('#ibcProjects .videoBlock').width() + 1) * $('#ibcProjects .videoBlock').length;
		totalContainerWidth = (totalPrintWidth + 1) + (totalTvWidth + 1) + (totalDigitalWidth + 1) + (totalIbcWidth + 1) + $('#info').width();
		$('#tvProjects').width(totalTvWidth);
		$('#digitalProjects').width(totalDigitalWidth);
		$('#ibcProjects').width(totalIbcWidth);
		$('#innerContainer').width(totalContainerWidth + 1000);
		
		// fade in the nav, now that all the images have loaded
		$('#mainNav').fadeIn('slow');
		
		// as a failsafe to the media selector, if the window is
		// too short then turn off the project headings
		ShannonPort.refreshHeadingState();
		$(window).resize(ShannonPort.refreshHeadingState);
		
		// pin the footer to the bottom of the page if we're on an iPad/iPhone
		if (ShannonPort.isiPad())
		{
			$('#mainNav').css('position', 'absolute');
			$('#mainNav').css('padding', '5px');
			$('#mainNav').css('width', '100%');
			$('#mainNav').css('left', $(document).scrollLeft());
			$('#mainNav').css('bottom', '-1px');
			ShannonPort.refreshNavPosition();
			$(document).scroll(function () {
				ShannonPort.refreshNavPosition();
			});
		};
		
		if (window.location.hash) {
			// console.log(window.location.hash);
			ShannonPort.navigateToProject(window.location.hash);
		}
	}
);
