/* Author:
	Richard Trott
*/


$.ajaxSetup({
  cache: false
});



















// Let's get this party started!
$(document).ready(function () {


	$('#contactMe').prependTo('#pageHeader');

	$('#titleLink').click(function (e) {
		e.preventDefault();

		var $panel = $('#contactMe'),
			$this = $(this);

		if (!$panel.is(':animated')) {
			$panel.slideToggle(150, 'easeInCirc');
		}

	});



	/* Portfolio filters */
	var $filterLinks = $('#portfolioFilter input');

	$filterLinks.change(function () {
		var $this = $(this);

		updateCheckboxes($this);

		filterPortfolio();
	});


	function filterPortfolio() {
		var $items = $('#portfolio li');

		$items.fadeTo(0, .3);

		$filterLinks.filter(':checked').each(function () {
			var $this = $(this),
				val = $this.val();

			$items.each(function () {
				if ($(this).hasClass(val)) {
					$(this).fadeTo(0, 1);
				}
			});
		});
	};

	function updateCheckboxes($checkbox) {
		if ($checkbox.is(':checked')) {
			$checkbox.parent('label').addClass('checked').find('.icon').remove();
			$checkbox.after('<span class="icon check"></span>');
		} else {
			$checkbox.parent('label').removeClass('checked').find('.icon').remove();
			$checkbox.after('<span class="icon cross"></span>');
		}
	}


	// Jquery masonry to handle grid reflow
	var $container = $('#portfolio');

	$container.imagesLoaded(function () {
		$container.masonry({
			itemSelector: '#portfolio > li',
			columnWidth: 10,
			isFitWidth: true
		});
	});






	// log('inside coolFunc', this, arguments);

	$("#portfolio > li > a").fancybox({
		//maxWidth	: 800,
		//maxHeight	: 600,
		fitToView: false,
		width: 'auto',
		height: 'auto',
		autoSize: false,
		closeClick: false,
		openEffect: 'fade',
		scrolling: 'no',
		closeEffect: 'fade',
		padding: 1,
		helpers: {
			overlay: {
				opacity: 0.95,
				css: {
					'background-color': '#000'
				}
			}
		},
		beforeShow: function () {
			window.scrollTo(0, 0);
		},
		afterShow: function () {
			wireUpWaypoints();
			$('.fancybox-close').html('<a href="#" class="closeInner"><span class="icon cross"></span>CLOSE</a>').slideDown(300, 'swing');
			$.fancybox.update();
		},
		afterClose: function () {
			$('.itemNavWrap, .waypoint').waypoint('destroy');
		},
		onUpdate: function () {
		}

	});





	function wireUpWaypoints() {
		$.fancybox.reposition();

		$.waypoints.settings.scrollThrottle = 30;

		$('.waypoint').waypoint(function (event, direction) {
			var id = $(this).attr('id'),
				$targetLink = $('a[href=#' + id + ']');

			$('.navModule li').removeClass('current');

			if (direction === 'down') {
				// do this on the way down
				$targetLink.parent('li').addClass('current');
			}
			else {
				// do this on the way back up through the waypoint
				$targetLink.parent('li').prev().addClass('current');
			}

			event.stopPropagation();
		}, {
			offset: '20px'
		});


		$('.itemNavWrap').waypoint(function (event, direction) {
			var $module = $(this).find('.navModule'),
				$actions = $(this).find('.actions'),
				origWidth = $module.width();

			if (direction === 'down') {
				$module.addClass('sticky'/*, direction === "down"*/).css('width', origWidth);
				$actions.slideDown(150, 'swing').css('width', origWidth); ;
			} else {
				$module.removeClass('sticky'/*, direction === "up"*/).css('width', 'auto');
				$actions.slideUp(150, 'swing');
			}
			event.stopPropagation();
		}, {
			//offset: '-100%'
		});



		var scrollElement = 'html, body';

		/*
		// Credit for below to
		// http://www.zachstronaut.com/posts/2009/01/18/jquery-smooth-scroll-bugs.html
		$('html, body').each(function () {
			var initScrollTop = $(this).attr('scrollTop');

			$(this).attr('scrollTop', initScrollTop + 1);

			if ($(this).attr('scrollTop') == initScrollTop + 1) {
				scrollElement = this.nodeName.toLowerCase();
				$(this).attr('scrollTop', initScrollTop);
				return false;
			}
		});
		*/


		// Credit for below to http://imakewebthings.github.com/jquery-waypoints/   
		// Smooth scrolling for internal links
		$("a[href^='#']").click(function (event) {
			event.preventDefault();
			var $this = $(this),
				target = this.hash,
				$target = $(target);

			if (target != '') {
				$(scrollElement).stop().animate({
					'scrollTop': $target.offset().top - 40
				}, 500, 'swing', function () {
					log('trying to scroll to a nav item');

					/* 
					if (target != 'container') {
					//window.location.hash = target;
					}
					*/
				});
			}
		});

		$('.closeModal').click(function (e) {
			e.preventDefault();
			$.fancybox.close();
		});



		/* End of Ajax loaded content wireup */
	}






});










