$(document).ready(function() {

	// external links
	$("a[href^='http:']:not([href*='" + window.location.host + "'])").live("click", function(){
		window.open($(this).attr('href'));
		return false;
	});



	// by industry drop down
	$('#by_industry .current ul').hide();
	$('#by_industry .current').hover(function() {
		$('#by_industry .current ul').show();
	}, function() {
		$('#by_industry .current ul').hide();
	});


	// #project_url only exists if linking directly to single project
	load_project($('#project_url').html());


	// project listing filter
	var $container = $('#project_listing ul');

	$container.isotope({
		itemSelector : 'li',
		layoutMode : 'fitRows'
	});


	$('#by_category .filter a').click(function(){
		// get href attr, remove leading #
		var href = $(this).attr('href').replace( /^#/, '' ),
		// convert href into object
		// i.e. 'filter=.inner-transition' -> { filter: '.inner-transition' }
		option = $.deparam( href, true );
		// set hash, triggers hashchange on window
		$.bbq.pushState( option );

		$('#project_detail').slideUp();
		return false;
	});

	$(window).bind( 'hashchange', function( event ){
		// get options object from hash
		var h = $.deparam.fragment();
		// apply options from hash
		// console.log(h)
		if ($.isEmptyObject(h)) {
			$container.isotope({ filter: '*' });
		} else {
			$container.isotope(h);
		}
		// styling the main nav
		f = $.param.fragment().replace( /^filter=/, '' );
		$('#by_category a').removeClass('selected');
		if (f =='' || f == '*') {
			$('#by_category a.all').addClass('selected');
		} else {
			$('#by_category a'+f+'').addClass('selected');
		}
	})
	// trigger hashchange to capture any hash data on init
	.trigger('hashchange');



	// $('#project_detail').hide();
	// show project details (hidden via CSS 'cause of FOC)
	$('#project_listing a').click(function() {

		$('#project_detail').slideDown(); // height is already set by #loading CSS

		p = $(this).attr('href');
		// load detail via ajax
		load_project(p);

		$('#project_listing a').removeClass('current');
		$(this).addClass('current'); // makes it easy to load next/prev project
		return false;
	});


	// prev/next project
	$("#ajax_content").on("click", "#project_nav .next a", function(event){
		var n = $('#project_listing a.current').parent().nextAll("li:not(.isotope-hidden):first").find('a');
		if (n.length == 1) { //making sure there is a next project
			var nlink = n.attr('href');

			$('#project_listing a').removeClass('current');
			n.addClass('current');
			load_project(nlink);
		}
		return false;
	});
	$("#ajax_content").on("click", "#project_nav .prev a", function(event){
		var p = $('#project_listing a.current').parent().prevAll("li:not(.isotope-hidden):first").find('a');
		if (p.length == 1) { //making sure there is a previous project
			var plink = p.attr('href');

			$('#project_listing a').removeClass('current');
			p.addClass('current');
			load_project(plink);
		}
		return false;
	});

	// load single project content and bind events
	function load_project(ajax_content) {

		// reset display
		$("#loading").show();
		$("#ajax_content").hide();

		$('#ajax_content').load(ajax_content, function() { // add all js after content is loaded (so binding works)

			$('#slider').waitForImages(function() { // plugin that makes sure all slides are loaded before fading content in
				$("#loading").hide();
				$("#ajax_content").fadeIn();

				$('#close a').click(function() {
					$('#project_detail').slideUp();
					return false;
				});

				// make sure there's a next/prev project and highlight project nav accordingly
				var n_proj = $('#project_listing a.current').parent().nextAll("li:not(.isotope-hidden):first").find('a');
				var p_proj = $('#project_listing a.current').parent().prevAll("li:not(.isotope-hidden):first").find('a');
				if (n_proj.length == 0) {
					$('#project_nav .next a').addClass('off');
				}
				if (p_proj.length == 0) {
					$('#project_nav .prev a').addClass('off');
				}

				// Slideshow
			    // var interval = 3000;
				var speed = 850;
				var total_slides = ($('#slides li').length-1);

				$('#slider').serialScroll({
					items: 'li',
					prev: '#local_nav .prev a',
					next: '#local_nav .next a',
					force: true,
					// interval: interval,
					duration: speed,
					cycle: false,
					easing: 'easeOutExpo',
					constant: false,
					onBefore: function(e, elem, $pane, $items, pos) {
						$('#slides li').removeClass('selected');
						$('#slides li:eq('+pos+')').addClass('selected');
						if (pos == 0) {
							$('#local_nav .prev a').addClass('off');
						} else {
							$('#local_nav .prev a').removeClass('off');
						}
						if (pos == total_slides) {
							$('#local_nav .next a').addClass('off');
						} else {
							$('#local_nav .next a').removeClass('off');
						}
						// alert(pos);
					},
					onAfter: function(e, elem, $pane, $items, pos) {
						if ($('#slides li:last-child').hasClass('selected')) { // we track it here because pos doesn't work in onAfter method for some reason...
						}
					}
				});
			});
		});
	}

});

// $(document).ajaxComplete(function(){ // make sure FB like button shows up in ajax content
//     try{
//         FB.XFBML.parse(); 
//     }catch(ex){}
// });
