/*  */ 
 
 
/*  */ 
$(document).ready(function(){
	
/*
 	Collapse long description text
*/	
	var about_txt = $('#about .description');
	
	if (about_txt.children('p').size() > 1) {
		
		about_txt.children().slice(2).wrapAll('<div class="hidden" />');
		$('.hidden').after('<a class="show_more"></a>');

		$('.description .show_more').bind("click", function(event) {
			about_txt.find('.hidden').slideToggle();
			$(this).toggleClass('show_less');
		});
	}


/* 
	Widen contributors block if there's no description text
*/
	if (!(about_txt.length)) {
		$('#production').addClass('wide');
	}

	
/*
 	Hide contributors, depending on what content there is
*/

	// If there are project thoughts...
	if ($('#project_thoughts').length) {
				
		// Just hide everything else		
		$('#contributors, .credits').wrapAll('<div class="hidden" />');
		$('#production').append('<a id="show_credits" class="show_more" />');
		
		$('#show_credits').bind("click", function(event) {
			$('#production').find('.hidden').slideToggle();
			$(this).toggleClass('show_less');
		});

	} else {
		
		$('#production').append('<div class="hidden"></div><a id="show_credits" class="show_more" style="display:none;" />');
		
		$('#show_credits').bind("click", function(event) {
			$('#production .hidden').slideToggle();
			$(this).toggleClass('show_less');
		});
		
		// If there are credits, add to hidden content!
		if ($('.credits').length) {
			var credits = $('.credits');
			$('#production .hidden').append(credits);
		}
		
		// If there's more than 3 contributors, slice	
		if ((about_txt.length) && ($('#contributors li').size() > 3)) {

			var contr = $('#contributors li').slice(3);
			$('#production .hidden').prepend('<ul id="contributors" class="grid compact people"></ul>');
			$('#production .hidden .people').prepend(contr);

		// If there's more than 4, slice 
		} else if (!(about_txt.length) && ($('#contributors li').size() > 5)) {	

			var contr = $('#contributors li').slice(5);
			$('#production .hidden').prepend('<ul id="contributors" class="grid compact people"></ul>');
			$('#production .hidden .people').prepend(contr);

		}
		
		// If there's any hidden content, show more button
		if ($('#production .hidden').children().length) {
			$('#show_credits').show();
		}

	}
	
});
$(document).ready(function() {	

	$(".dropdown").each(function(i){
		//create the dropdown
		var $defaultOption = $(this).children('li:first');
		var $activeOption = $(this).find('.active').parent('li');
	
		$(this).children().wrapAll('<li class="inactive"><ul class="options">');				
		$(this).find('.inactive').before('<li class="selected"><a href="#"><span class="value"></span></a></li>');
	
		if($activeOption.html() == null) {	
			$(this).find('.selected .value').html($defaultOption.find('a').html());	
			$defaultOption.remove();

		} else {	
			$(this).find(".selected .value").html($activeOption.find('a').html());
			$activeOption.hide();
		}

		if($(this).find('.options').children('li').length > 6) {
			$(this).addClass('long');	//denote long list for jscrollPane
		}
		
		$(this).parent().show();
		
		//set up the events
	    $(this).find(".selected a").click(function() {
			if($(this).parents('.dropdown').hasClass('long')) {
				var $options = $(this).parents('.dropdown').find(".inactive .options");
				var element =  $options.jScrollPane({
					verticalDragMinHeight: 50,
					verticalDragMaxHeight: 50
				});
				var api = element.data('jsp');
				$options.toggle('fast',function(){
					api.reinitialise();		//scroll only works if reinitialised after dropdown
				});
			} else {
				$(this).parents('.dropdown').find(".inactive .options").toggle('fast');
			}
			
			// Hide redundant items on tabbed content dropdowns
			$(this).parent().siblings('li').find('li:hidden').show();
			$(this).parent().siblings('li').find('a.active').parent().hide();
			
			return false;
	    });
	
		// trigger dropdown on heading click
		$(this).prev('h2').css('cursor', 'pointer').bind({
			mouseover: function(){
				$(this).next('.dropdown').find('.selected a').addClass('hover');
			}, 
			mouseout: function (){
				$(this).next('.dropdown').find('.selected a').removeClass('hover');
			},
			click: function(){
				if($(this).next('.dropdown').hasClass('long')) {
					var $options = $(this).next('.dropdown').find(".inactive .options");
					var element =  $options.jScrollPane({
						verticalDragMinHeight: 50,
						verticalDragMaxHeight: 50
					});
					var api = element.data('jsp');
					$options.toggle('fast',function(){
						api.reinitialise();		//scroll only works if reinitialised after dropdown
					});
				} else {
					$(this).next('.dropdown').find(".inactive .options").toggle('fast');
				}

				// Hide redundant items on tabbed content dropdowns
				$(this).next('.dropdown').children('li').find('li:hidden').show();
				$(this).next('.dropdown').children('li').find('a.active').parent().hide();

				return false;
			}
		});

	    $(this).find(".inactive .options li a").bind('click', function() {
	        var text = $(this).html();
	        $(this).parents('.dropdown').find(".selected .value").html(text);
	        $(this).parents('.dropdown').find(".inactive .options").hide();
	    });

	    $(document).bind('click', function(e) {
	        var $clicked = $(e.target);
	        if (! $clicked.parents().hasClass("dropdown"))
	            $(".dropdown .inactive .options").hide();
	    });

		// Fix z-index for multiple instances
		$(this).find('.options').css('z-index', 5000 - i);
	});
});
$(document).ready(function(){

	$('.stream_block').each(function(){
		
		if($(this).find('.form .textarea').children().length > 0) {
			$(this).find('.q_and_a textarea').autoResize({
			    onResize : function() {
			        $(this).css({opacity:0.8});
			    },
			    animateCallback : function() {
			        $(this).css({opacity:1});
			    },
			    animateDuration : 300
			});
		}

		if ($(this).find('.questions li').length > 8) {
			var item = $(this).find('.questions');

			item.children('li').slice(5).wrapAll('<div class="hidden" />');
			item.after('<a class="show_more"></a>');
			item.next('.show_more').bind("click", function(event) {
				item.find('.hidden').slideToggle();
				$(this).toggleClass('show_less');
			});
		}
	});
	
});
 
 

