jQuery(function() {

	
	
	//search submit
	jQuery('.submit_search').click(function() {
		jQuery('#search').submit();
	});


	//
	// Feature story cycle:
	//    Fix title length caused, and show/hide summary as space permitted
	//
	jQuery('#fnav').find('li').each(function(index, element){
		if(jQuery(element).find('.atitle').height()>14){
			//jQuery(element).find('.excerpt').hide();
			if(jQuery(element).find('.atitle').height()>28){
				var i=0;
				while(jQuery(element).find('.atitle').height()>28){
					var excerpt = new String(jQuery(element).find('.atitle').html());
					var new_excerpt = excerpt.substr(0, excerpt.lastIndexOf(' '))+'...';
					jQuery(element).find('.atitle').html(new_excerpt)
					//alert(jQuery(element).find('.title').html());
					if(i>100) break;
					i++;
				}
			}
		}
	});

	//sidebar menu fix last item in the list
	jQuery('#menu-block').children('li').last().addClass('last');
	jQuery('#menu-block li').each(function(index, element){
		jQuery(element).find('ul li').last().addClass('last');
	});

	//block last paragraph fix
	jQuery('.block').each(function(){
		jQuery(this).find('p').last().addClass('last');
	});
	
	//program toggle
	jQuery('#program_head').click(function() {
		jQuery('#programs').slideToggle();
		if (jQuery('#program_head').hasClass('close')) {
		    jQuery('#program_head').removeClass('close');
		    jQuery('#program_head').addClass('open');
		} else if (jQuery('#program_head').hasClass('open')) {
		    jQuery('#program_head').removeClass('open');
		    jQuery('#program_head').addClass('close')
		}
	});

	//All external link, open in new window or tab
	jQuery("a[href^='http:']").not("[href*='reframecollection.org'],[href*='tribecafilminstitute.org']").attr('target','_blank');  
	
	//open links in a new window
	//jQuery('a[rel*=external]').click(function() {
	//	window.open(this.href);
	//	return false;
	//});
	
	//slideshow
	jQuery('#feature_content').cycle({
		fx: 'fade',
		timeout: 5000,
		pager: '#fnav',
		pagerEvent: 'mouseover',
		pauseOnPagerHover: true,
		pagerAnchorBuilder: function(idx, slide) {
			// return selector string for existing anchor
			return '#fnav li:eq(' + idx + ')';
		}
	});
	
	jQuery('#fnav li').click(function(){
		document.location = jQuery(this).find('a').attr('href');
		//alert('test');
	});
	
	//stop slideshow on mouseover
	jQuery('#featured_content').mouseover(function() {
		jQuery('#feature_content').cycle('pause');
	});
	
	jQuery('#feature_site_area').mouseover(function(){jQuery('.feature_site_description').show(200);});
	
	jQuery('.event_info').dialog({
		modal: true, 
		title: 'Event Info', 
		autoOpen: false 
	});
	
	jQuery('.post .reveal_div').hide();
	jQuery('.post .reveal_click a').click(function(){
		jQuery(this).parent().addClass('active');
		jQuery('.post .active + .reveal_div').toggle(200);
		jQuery(this).parent().removeClass('active');
		return false;
	});
	//jQuery('#youth_about .reveal_click').click(function(){
	//	jQuery(this).addClass('active');
	//	jQuery('#youth_about .active + .reveal_div').toggle(200);
	//	jQuery(this).removeClass('active');
	//});
	//jQuery('#faq .active + blockquote').show(200);

	/*
	jQuery('.event_click').click(function(){
		alert('im clicked');
		jQuery(this).addClass('event_active');
		jQuery('.event_active + .event_info').show();
		return false;
	});
	*/

	// FAQ hide and reveal: alternate between p and blockquote
	jQuery('.faq blockquote').hide();
	jQuery('.faq p').click(function(){
		jQuery('.faq p').removeClass('active');
		jQuery('.faq blockquote').hide();
		jQuery(this).addClass('active');
		jQuery('.faq .active + blockquote').show(200);
	}).hover(function(){
		jQuery(this).css('cursor', 'pointer');
	});

	//accordian
	jQuery('.accordian .fold').each(function(){
		jQuery(this).children().last().css('marginBottom', '0');
	});

	jQuery('.accordian .fold').first().addClass('active').find('.accordian_content').show();


	jQuery('.accordian .fold h2').click(function(){
		var current_fold = jQuery(this).parent();
		current_fold.parent().find('.active').removeClass('active').find('.accordian_content').slideUp('slow');
		current_fold.find('.accordian_content').slideDown('slow');
		current_fold.addClass('active');
	});
	
	// Filmmaker Index fix margin and height
	var filmmaker_index_max_height = 0;
	jQuery('.filmmaker_index').children('li').each(function(index, element){

			if(index%4 == 3) {
				jQuery(element).addClass('last');
			} else if(index%4 == 0){
				jQuery(element).addClass('first');
			}
			//alert();
			//alert(jQuery(element).height());
			//return;
			if(filmmaker_index_max_height < jQuery(element).height()){
				filmmaker_index_max_height = jQuery(element).height();
			}
			//alert(filmmaker_index_max_height);
	});

	//alert(filmmaker_index_max_height);

	jQuery('.filmmaker_index').children('li').height(filmmaker_index_max_height);

	jQuery('.filmmaker_index > li').last().addClass('last');


	// Filmmaker Index reveal animation
	jQuery('.filmmaker_index li > img, .filmmaker_index li > h4, ').click(function(){
		//alert();
		//alert(jQuery('.filmmaker_info').length);
		jQuery('.filmmaker_info').remove();
		var new_content = '<li class="filmmaker_info" style="display:none;">'+jQuery(this).parents('li').find('li').html()+'</li>';
		jQuery('.filmmaker_index > li').removeClass('active');
		jQuery(this).parents('li').addClass('active');
		if(jQuery(this).parents('li').hasClass('last')){
			jQuery(this).parents('li').after(new_content);
		} else {
			jQuery(this).parents('li').nextUntil('.first').last().after(new_content);
		}
		jQuery('.filmmaker_info').slideDown('fast');

		//jQuery('.filmmaker_index ul').hide();
		//jQuery(this).parents('li').find('ul').show(500);
		//alert('hi');
		return false;
	});




	//reframe masthead search
	jQuery('.nav_right_reframe #search').focus(function(){
	if(jQuery('#search').val()=='Search Reframe'){
		jQuery('#search').val('')
	}
	});
	//reframe masthead search
	jQuery('.nav_right_reframe #search').blur(function(){
		if(jQuery('#search').val()==''){
			jQuery('#search').val('Search Reframe')
		}
	});
	//reframe masthead search
	jQuery('.nav_right_reframe #hdrsubmit').click(function(){
		document.location = 'http://reframecollection.org/search/?'+jQuery('#search').serialize();
		//alert(jQuery('#search').serialize());
	});
	
	//reframe homepage search
	jQuery('#reframe_home_search').focus(function(){
		if(jQuery('#reframe_home_search').val()=='Search Reframe'){
		    jQuery('#reframe_home_search').val('')
		}
	});
    	//reframe homepage search
	jQuery('#reframe_home_search').blur(function(){
		if(jQuery('#reframe_home_search').val()==''){
		    jQuery('#reframe_home_search').val('Search Reframe')
		}
	});
    	//reframe homepage search
	jQuery('#reframe_home_hdrsubmit').click(function(){
		document.location = 'http://reframecollection.org/search/?'+jQuery('#reframe_home_search').serialize();
		//alert(jQuery('#search').serialize());
	});

	//reframe featured collection first/last in the row fix
	jQuery('.featured_collection li').each(function(index, element){
		if(index%3 == 2) {
			jQuery(element).addClass('last');
		} else if(index%3 == 0) {
			jQuery(element).addClass('first');
		}
	});

});

function twitter_parse(str){
	var result = str.replace(/([A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+)/g, "<a href=\"$1\">$1</a>");
        result = result.replace(/^TribecaFilmIns: |^TribecaAllAcces: |^TribecaYouth: |^TFISloanFund: /, '');
	result = result.replace(/(@)([A-Za-z0-9-_]+)/g, "<a href=\"http://twitter.com/$2\">$1$2</a>");
	result = result.replace(/(#)([A-Za-z0-9-_]+)/g, "<a href=\"http://search.twitter.com/search?q=%23$2\">$1$2</a>");
	return result;
}

