Event.observe(window,"load",function(){
	
	if ($('synopsis')) {
		setup_box_toggle('synopsis');
		
		/* synopsis limitations */
		Event.observe($('synopsis'),"keydown",function(e){
			var field=Event.element(e);
			limit_textarea(field,4000,$('synopsis_count'));
		});
		Event.observe($('synopsis'),"keyup",function(e){
			var field=Event.element(e);
			limit_textarea(field,4000,$('synopsis_count'));
		});
	
		limit_textarea($('synopsis'),4000,$('synopsis_count'));
	}
	
});

document.observe("dom:loaded",function(){
	/* embed and link forms */
	$$(".cut-and-paste").each(function(item) 
	{
		Event.observe(item,"click",function(evt){var e=Event.element(evt); e.activate();});
	});
	
	/* handle the hash in the url */
	$$(".listing-info").each(function(item,count)
	{
		var hash=window.location.hash.substr(1);
		var aname=item.down().readAttribute('name');
		
		if(aname==hash)
			item.removeClassName('extra');
		return;		
	});
	
	/* cast and crew extras */
	$$(".listing-info").each(function(item,count) 
	{
		//handle the extras
		if(item.hasClassName('extra'))
			item.hide();
			
		//add a last class
		if(!item.hasClassName('extra') && (item.next() && item.next().hasClassName('extra')) || !item.next()) {
			item.addClassName('last');
		} else {
			item.removeClassName('last');
		}
	});
	/* show all for cast and crew */
	$$('.show-all').each(function(item){
		Event.observe(item,"click",function(evt){
			var e=Event.element(evt);
			var parent=e.up().identify();
			var extras=$$('#'+parent+' .extra');
			extras.each(function(extra,count)
			{
				if(count==0)
					extra.previous().removeClassName("last");
				item.hide();
				extra.show();
				if(extras.length-1==count)
					extra.addClassName('last');
			});
		});
	});
	/* page jump links */
	$$('.jump-link').each(function(item){
		var anchor=item.readAttribute("href");
		item.setAttribute("rel",anchor.substr(1,anchor.length));
		item.setAttribute("href","javascript:void(0);");
	    Event.observe(item,"click",function(evt){
			var e=Event.element(evt);
			new Effect.ScrollTo(e.readAttribute("rel"),{offset: 0});
		});		
	});
	
});

Event.observe(window,"load",function(){
	if($('embed_link') && $('embed_text') && !$('video_player'))
		$('embed_link','embed_text').invoke('hide');
	Page.watch(function(){ try {myLightbox.updateImageList();} catch(e) { } });
	//show/hide jumb links
	$$('div[class="main"] a[class="jump-link"]').each(function(item){
		if (item.cumulativeOffset()[1] > document.viewport.getHeight()) 
			item.show();
		else 
			item.hide();
	});
});



//cast-crew edits

function add_member(type,project_id)
{
	$('add_'+type).insert ({ 'before': '<div id="listing_"></div>'});
	$('add_'+type).hide();
	new Ajax.Request('/project/teammember/'+type+'/'+project_id);
}

function edit_member(type,project_id,id)
{
	if (!id) {
		id = '';
	}
	new Ajax.Request('/project/teammember/'+type+'/'+project_id+'/'+id);
}

var process_member_pause = false;
function process_member(id)
{
	if (!process_member_pause) {
		process_member_pause = true;
		setTimeout('process_member_pause = false', 5000);
		var item=$(id);
		new Ajax.Request(item.action,{
			method:item.method,parameters:item.serialize()
		});
	}
}

function cancel_member(type,project_id,id)
{
	if (id) {	//editing
		new Ajax.Request('/project/teammember/show'+type+'/'+project_id+'/'+id);	
	} else {	//adding new
		$('add_'+type).show();
		$('listing_').remove();
	}
}