$posts = $("#posts");

// scrollpane
var scrollPane = $posts.find('article .scrollpane');
var scrollPaneSettings = {
    maintainPosition: false,
    showArrows: false,
    hideFocus: true
};

$(document).ready(function() {

	var h = $(document).height(),
		$projects = $('#projects'),
		$people = $('#people');
	
	/* Search */
	$('input[name="s"]').on('click', function() {
		$(this).val('');
	});
	$('#searchform').submit(function(){
		
		var $input = $('input[name="s"]').val();
		
		if ($input === '' || $input === 'Search' || $input === 'Enter a search query') {
			$('#searchform input[name="s"]').focus().val('Enter a search query');
			return false;
		} 
		
	});
	
	/* Nav */
	//$('#menu-item-148').find('a').after(wp_constants.client_list).on('click', navSlide);
	//$('#menu-item-149').find('a').after(wp_constants.sector_list).on('click', navSlide);
	if (wp_constants.is_team) $('#menu-item-19').addClass('current_page_item');
	
	/* News	 */	
	if (wp_constants.is_blog && !wp_constants.is_single) {
		paneHeight = h - 70;
		$posts.css({'height':h})
			  .scrollable({ circular: true, items: '.articles' })
			  .find('article .scrollpane').jScrollPane(scrollPaneSettings)
			  .find('.jspContainer').css({'height':paneHeight});
	}
	// toggle more/less content
	var scrollableAPI = $posts.data("scrollable");
	//var scrollPaneAPI = $posts.find('article').data('jsp'); // change to handle multiple
	$posts.find('.toggle-content a, .excerpt a').on('click', function(e) {		
		
		e.preventDefault();
		var $this = $(this),
		scrollPaneAPI = $this.parents('article .scrollpane').data('jsp');
		
		// scroll to the clicked panel
		var targetindex = $this.parents('article').index() - 1; // -1 to account for scrollable's 'article.cloned'
		scrollableAPI.seekTo(targetindex, 500);
		
		if ( $this.parents('article').hasClass('view-full') ) {
			$this.parents('.full-content').fadeOut();
			$this.parents('article').animate({'width':'200px'}, function(){
				$this.parents('article').find('.excerpt').fadeIn(function() {
					scrollPaneAPI.reinitialise();
				});
			});			
		} 
		else {
			$this.parents('.excerpt').fadeOut();
			$this.parents('article').animate({'width':'600px'}, function(){
				$this.parents('article').find('.full-content').fadeIn(function() {
					scrollPaneAPI.reinitialise();
				});
			});
		}
		
		$this.parents('article').toggleClass('view-full');
		
	});
	
	/* Work */
	$projects.isotope({itemSelector : 'article'});
	// filter
	$('.work-filter a').on('click', function() {
		var href = $(this).attr('href').replace( /^#/, '' ),
		option = $.deparam( href, true );
		$.bbq.pushState( option );
	});
	// bind hashchange
	$(window).bind('hashchange', function( event ){
	  var hashOptions = $.deparam.fragment();
	  $projects.isotope( hashOptions );
	})
	.trigger('hashchange');
	
	// set up ad-gallery
	var galleries = $('.ad-gallery').adGallery({loader_image: '/kn/wp-content/themes/knifedge/images/ad-gallery/loader.gif'});
	$('#switch-effect').change(
	  function() {
	    galleries[0].settings.effect = $(this).val();
	    return false;
	  }
	);
	$('#toggle-slideshow').click(
	  function() {
	    galleries[0].slideshow.toggle();
	    return false;
	  }
	);
	$('#toggle-description').click(
	  function() {
	    if(!galleries[0].settings.description_wrapper) {
	      galleries[0].settings.description_wrapper = $('#descriptions');
	    } else {
	      galleries[0].settings.description_wrapper = false;
	    }
	    return false;
	  }
	);

	/* Team */
	$people.find('li').on({
		mouseenter: function(){
			$(this).addClass("hover");
		},
		mouseleave: function(){
			$(this).removeClass("hover");
		}
	});
	
	$people.find("img[title]").tooltip({
		position: "bottom center", 
		opacity: 0.7
	});
	
	/* Footer */
	$('.twtr-timeline').jScrollPane();
	var scrollPaneAPI = $('.twtr-timeline').data('jsp');	
	$('#social').find('img').on('click', function() {
		$('#feed').slideToggle(function(){
			$(this).parent().toggleClass('open');
			if (scrollPaneAPI) scrollPaneAPI.reinitialise();
		});
	});

	
});



$(window).resize(function() {
	
	var h = $(window).height();
	
	/* News	 */	
	if (wp_constants.is_blog && !wp_constants.is_single) {
		paneHeight = h - 70;
		$posts.css({'height':h});
		scrollPane.jScrollPane(scrollPaneSettings).data('jsp').reinitialise();
		scrollPane.css({'height':paneHeight});
	}
	
});

/*
function navSlide(e) {
	$projects.isotope({ filter: '*' });
	if (wp_constants.is_work) {
		$(this).parent().find('ul').slideToggle();
		e.preventDefault();
	} else {
		
	}
}
*/
