(function($){$.fn.autocomplete=function(data,$output,options){var local=data instanceof Array;var output=$output.addClass('autocomplete')[0];options=$.extend({minLength:2,spinner:_('Loading')},options);return this.each(function(){this.setAttribute('autocomplete','off');var KEYCODES={TAB:9,RETURN:13,ESC:27,UP:38,DOWN:40};var CLASSES={HOVER:'hover',FOCUS:'focus'};function select(step){if($suggestions.length){selected+=step;if(selected<0){selected=-1;$output.hide();}
else if(selected>=$suggestions.length)
selected=$suggestions.length-1;if(selected>=0){if($output.is(':hidden'))
$output.show();$suggestions.removeClass(CLASSES.FOCUS);var $li=$suggestions.eq(selected),li=$li[0];if(li.offsetTop+li.offsetHeight>output.offsetHeight+output.scrollTop)
scrollIntoView(li.offsetHeight,function(){$li.addClass(CLASSES.FOCUS);});else if(output.scrollTop&&li.offsetTop<output.scrollTop)
scrollIntoView(-1*li.offsetHeight,function(){$li.addClass(CLASSES.FOCUS);});else
$li.addClass(CLASSES.FOCUS);}}}
function scrollIntoView(by,callback){var toScroll=Math.abs(by),delta=6;var scrolling=setInterval(function(){if(toScroll>0){output.scrollTop+=((toScroll<delta?toScroll:delta)*Math.abs(by)/by);toScroll-=delta;}
else{clearInterval(scrolling);callback();}},25);}
function showSuggestions(){if($suggestions.length)
$output.show();}
function hideSuggestions(){$output.hide();selected=-1;$suggestions.removeClass(CLASSES.FOCUS);}
var selected=-1,loading,request,hiding;var show=true;var $input=$(this),$suggestions=$('li',$output);var results=function(){$suggestions.bind('click',function(e){if(!$(this).hasClass('not-found')){selected=$suggestions.index(this);$input.trigger('selection',[this,$output]);}
else{$input.trigger('not-found');e.preventDefault();}
$output.hide();$suggestions.length=0;}).hover(function(){$(this).addClass(CLASSES.HOVER);},function(){$(this).removeClass(CLASSES.HOVER);});if(!$suggestions.length)
$output.hide();else if(show)
$output.show();};var search=local?function(needle){var re=new RegExp('('+needle+')','i');var found=data.filter(function(item){return needle&&re.test(item);}).map(function(item){return['<li>',item.replace(re,'<b class="match">$1</b>'),'</li>'].join('');}).join('');output.innerHTML=['<ul>',found,'</ul>'].join('');$suggestions=$('li',$output);results();}:function(){if(request){request.abort();request=null;}
request=$.ajax({url:data,data:$input.serialize(),success:function(r){$output.html(r);$suggestions=$('li',$output);results();},error:function(){$output.hide();}})};$input.bind('keypress',function(e){if(e.keyCode==KEYCODES.RETURN){if($output.is(':visible')){$suggestions.filter('.'+CLASSES.FOCUS).trigger('click');e.preventDefault();if(selected==-1)
$input.trigger('enter');}
else{e.preventDefault();$input.trigger('enter');}}}).bind('keyup',function(e){switch(e.keyCode){case KEYCODES.UP:e.preventDefault();select(-1);break;case KEYCODES.DOWN:e.preventDefault();select(1);break;case KEYCODES.TAB:if($output.is(':visible')){hideSuggestions();clearTimeout(hiding);e.preventDefault();}
break;case KEYCODES.RETURN:break;case KEYCODES.ESC:hideSuggestions();break;default:var typed=$.trim(this.value);if(typed&&typed.length>=options.minLength&&data.length){clearTimeout(loading);$output.html('<em class="processing">'+options.spinner+'<span>&#8230;</span></em>').show();show=true;selected=-1;loading=setTimeout(function(){search(typed);},300);}else{$output.hide();}}}).bind('focus click',function(e){if(!this.readOnly)
showSuggestions();}).bind('blur',function(){hiding=setTimeout(hideSuggestions,200);show=false;});$output.bind('click',function(){clearTimeout(hiding);});});};})(jQuery);(function(){var $who=$('#who');if($who.length){if($('#share').length)
$('#do-share, #who legend a').bind('click',function(){$('#share').toggle();this.blur();return false;});else
$('#do-who a, #who legend a').bind('click',function(){$($('#do-who strong a').length?'#do-who, #who':'#who').toggle();this.blur();return false;});if(/who|share/i.test(location.hash))
$('#do-who a, #do-share a').click();var $email=$('#who input[type=text]').one('focus',function(){this.value='';});$('<button type="button"><b><b>'+$email.attr('title')+'</b></b></button>').insertAfter($email).bind('click',function(){$email.trigger('enter');return false;});if($('#remaining li').length)
$('#do-remaining').show();$email.removeAttr('title');$who.delegate('click',{'.invitee':function(e,el){var $li=$(el),input=$('input',$li)[0];input.checked=!input.checked;$li[input.checked?'addClass':'removeClass']('selected');},'label[for*=select-all]':function(e,el){$(el).parent().next().find('li').addClass('selected').find('input').attr('checked',true);},'[href*="remaining"]':function(e,el){$(el.hash).toggle();$(el).hide();return false;}});function addEmails(emails){if(typeof emails=='string')
emails=$.trim(emails).split(',').map(function(email){return $.trim(email);}).filter(function(email){return email.indexOf('@')>0;});if(!emails.length)
return;var ul=$('#selected-email-invitees')[0]||$('<ul id="selected-email-invitees"></ul>').appendTo('#external')[0],cache=$.data(ul,'cache')||$.data(ul,'cache',{});for(var i=0,email;email=emails[i];i++){if(!(email in cache)){cache[email]=true;id=email.replace('@','');$(ul).append(['<li class="invitee selected">','<input id="'+id+'" class="checkbox" type="checkbox" name="'+$email.attr('name')+'" value="'+email+'" checked="checked" />',email,'</li>'].join(' '));this.value='';}}}
var emailData=$.map($('#email-invitees li'),function(item){return $(item).text();});$email.autocomplete(emailData,$('#email-invitees').empty()).bind('selection',function(e,item,output){addEmails.call(this,$(item).text());}).bind('enter',function(e){addEmails.call(this,this.value);return false;});$('#who input.checkbox').fixVal();}})();(function(){$('#comments').delegate('submit',{'.manage':function(e){if($.plazes.confirm(e.target)){var $form=$(e.target);$form.ajaxSubmit({success:function(){var $li=$form.parents('li:first').fadeOut(500,function(){$li.remove();});},error:function(){}});}
return false;}});var check;function enable(){if(this.value&&this.value!=this.defaultValue){$button.removeClass('disabled');clearInterval(check);}
else
$button.addClass('disabled');}
function disable(){if(!this.value||this.value==this.defaultValue)
$button.addClass('disabled');clearInterval(check);}
var $form=$('#add-comment');var $button=$('button',$form).addClass('disabled');var $textarea=$('textarea',$form).each(function(){this.value=this.defaultValue=this.title;$(this).removeAttr('title').searchField().bind('keyup',enable).bind('blur',disable).bind('focus',function(){var that=this;check=setInterval(function(){enable.apply(that);},100);});});$form.ajaxForm({beforeSubmit:function(){var submit=!!$button.not('.disabled').length;if(submit)
$button.wait();return submit;},success:function(html){$($('#comments ol')[0]||$('<ol></ol>').insertAfter('#comments div.caption')[0]).append(html).find('li:last').css('display','none').animate({opacity:'show',height:'show'},300);},complete:function(){$textarea.val($textarea.attr('defaultValue'));$button.wait(true);}});$('#comments div.caption a').bind('click',function(){$textarea.focus();return false;});})();
