/**
 * The snaapi js library.
 * 
 * Copyright 2009 snaapi.
 * Created by Jeff Verkoeyen.
 * jverkoey@gmail.com
 * twitter.com/featherless
 */
Snap.Database=function(){if(Snap.Database.singleton){return Snap.Database.singleton;}
this._database={filters:{},all:[]};this._id_to_name={};this._id_to_type={};this._hierarchy_cache=null;this._function_cache={};this._callbacks={receive_categories:{callbacks:[],called:false},receive_function:{callbacks:[],called:false},receive_social:{callbacks:[],called:false},receive_hier:{callbacks:[],called:false},receive_hierarchy:{callbacks:[],called:false},navigate_immediately:{callbacks:[],called:false}};Snap.Database.singleton=this;};Snap.Database.singleton=null;Snap.Database.prototype={register_callbacks:function(callbacks){for(var key in callbacks){if(this._callbacks[key].called){callbacks[key]();}
this._callbacks[key].callbacks.push(callbacks[key]);}},load_functions:function(category,data){for(var i=0;i<data.length;++i){data[i].l=data[i].n.toLowerCase();data[i].e={type:this._id_to_name[category],category:category,hierarchy:data[i].h,function_id:data[i].i,name:data[i].n,lower_name:data[i].l,matches:null};}
this._database.all[category]=data;},load_categories:function(categories){this._database.filters=categories;for(var i=0;i<categories.length;++i){var data=categories[i].d;for(var i2=0;i2<data.length;++i2){var item=data[i2];this._id_to_name[item.i]=item.n;this._id_to_type[item.i]=categories[i].t;}}
this._notify_callbacks('receive_categories');},load:function(){$.ajax({type:'GET',url:'/js/static/hier.js?rev='+Revisions.static_hier_build,dataType:'json',success:this._receive_hier.bind(this),failure:this._fail_to_receive_hier.bind(this)});for(var key in Revisions.static_fun_build){var revision=Revisions.static_fun_build[key];$.ajax({type:'GET',url:'/js/static/fun/'+key+'.js?rev='+revision,dataType:'script'});}},get_filters:function(){return this._database.filters;},search:function(query,active_filters){var results=[];var hash_results={};if(query[0]=='#'){var query=$.trim(query.substr(1));var words=query.split(' ');var i=0;while(i<words.length){if(words[i]==''){words.splice(i,1);}else{words[i]=words[i].toLowerCase();++i;}}
if(this._database.filters.length>0){var filters=this._database.filters;for(var i=0;i<filters.length;++i){var filter=filters[i];for(var i2=0;i2<filter.d.length;++i2){var unique_id='filter'+i+'-'+i2;var any_succeed=false;var any_fail=false;for(var i3=0;i3<words.length||words.length==0;++i3){var offset;var word;if(words.length==0){offset=0;word='';}else{word=words[i3];offset=filter.d[i2].n.toLowerCase().indexOf(word);}
if(offset>=0){if(hash_results[unique_id]==undefined){hash_results[unique_id]={type:filter.t,filter_id:filter.d[i2].i,name:filter.d[i2].n,lower_name:filter.d[i2].n.toLowerCase(),matches:[]};}
hash_results[unique_id].matches.push(word);any_succeed=true;}else{any_fail=true;}
if(any_fail&&any_succeed){delete hash_results[unique_id];break;}
if(words.length==0){break;}}
if(any_succeed&&!any_fail){results.push(unique_id);}}}}else{return-1;}}else if(this._database.all.length>0){var words=query.split(' ');var i=0;while(i<words.length){if(words[i]==''){words.splice(i,1);}else{words[i]=words[i].toLowerCase();++i;}}
var any_filters=false;for(var key in active_filters){any_filters=true;break;}
var all=this._database.all;for(var category in all){if(any_filters&&undefined==active_filters[category]){continue;}
var list=all[category];for(var i=0;i<list.length;++i){var unique_id='function'+category+'-'+i;var any_succeed=false;var any_fail=false;for(var i2=0;i2<words.length;++i2){var offset=list[i].l.indexOf(words[i2]);if(offset>=0){if(hash_results[unique_id]==undefined){hash_results[unique_id]=list[i].e;hash_results[unique_id].matches=[];}
hash_results[unique_id].matches.push(words[i2]);any_succeed=true;}else{any_fail=true;}
if(any_fail&&any_succeed){delete hash_results[unique_id];break;}}
if(any_succeed&&!any_fail){results.push(unique_id);}}}}
if(results.length<3000){for(var i=0;i<results.length;++i){var entry=hash_results[results[i]];var joined_areas=new Array(entry.name.length);for(var i2=0;i2<entry.name.length;++i2){joined_areas[i2]=0;}
for(var i2=0;i2<entry.matches.length;++i2){var word=entry.matches[i2];var offsets=entry.lower_name.gindexOf(word);for(var i3=0;i3<offsets.length;++i3){var start=offsets[i3];joined_areas[start]++;var end=start+word.length;if(end<joined_areas.length){joined_areas[end]--;}}}
entry.score=0;var on=0;var starts_with=joined_areas[0]>0;for(var i2=0;i2<joined_areas.length;++i2){on+=joined_areas[i2];if(on>0){entry.score++;if(starts_with){entry.score++;}}else{starts_with=false;}}
entry.score/=entry.name.length;}}else{for(var i=0;i<results.length;++i){var entry=hash_results[results[i]];entry.score=0;for(var i2=0;i2<entry.matches.length;++i2){var word=entry.matches[i2];if(entry.lower_name.indexOf(word)==0){entry.score++;}}
entry.score/=entry.name.length;}}
function by(left,right){var left_entry=hash_results[left];var right_entry=hash_results[right];var result=(right_entry.score-left_entry.score)||(left_entry.lower_name<right_entry.lower_name?-1:(left_entry.lower_name>right_entry.lower_name?1:0));return result;}
results=results.sort(by);var list;if(results.length>0){list=[];for(var i=0;i<results.length;++i){list.push(hash_results[results[i]]);}}else{list=null;}
return list;},id_to_name:function(id){return this._id_to_name[id];},id_to_type:function(id){return this._id_to_type[id];},hierarchies_loaded:function(){return this._hierarchy_cache!=null;},get_hierarchies:function(){return this._hierarchy_cache;},get_hierarchy:function(category,hierarchy){return this._hierarchy_cache[category][hierarchy];},is_function_cached:function(category,id){var result=undefined!=this._function_cache[category]&&undefined!=this._function_cache[category][id];return result;},get_function:function(category,id){return this._function_cache[category][id];},request_function:function(category,id,name,type,hierarchy,silent){if(undefined==this._function_cache[category]){this._function_cache[category]={};}
this._function_cache[category][id]={name:name,type:type,category:category,hierarchy:hierarchy,id:id,loading:true,loading_social:true};$.ajax({type:'GET',url:'/function',dataType:'json',data:{category:category,id:id,silent:silent},success:this._receive_function.bind(this),failure:this._fail_to_receive_function.bind(this)});$.ajax({type:'GET',url:'/function/social',dataType:'json',data:{category:category,id:id},success:this._receive_social.bind(this),failure:this._fail_to_receive_social.bind(this)});},_notify_callbacks:function(name){var args=[];for(var i=1;i<arguments.length;++i){args.push(arguments[i]);}
for(var i=0;i<this._callbacks[name].callbacks.length;++i){this._callbacks[name].callbacks[i].apply(null,args);}
this._callbacks[name].called=true;},ensure_hierarchy_loaded:function(category,child){if(this.hierarchies_loaded()){var hierarchy=this._hierarchy_cache[category][child];var to_request=[];if(undefined==hierarchy.source_url){to_request.push(child);}
if(hierarchy.ancestors){for(var i=0;i<hierarchy.ancestors.length;++i){if(undefined==this._hierarchy_cache[category][hierarchy.ancestors[i]].source_url){to_request.push(hierarchy.ancestors[i]);}}}
if(to_request.length){$.ajax({type:'GET',url:'/hierarchy/info',dataType:'json',data:{c:category,h:to_request.join(',')},success:this._receive_hierarchy.bind(this),failure:this._fail_to_receive_hierarchy.bind(this)});}}},_receive_hierarchy:function(result,textStatus){if(result.s){for(var category in result.i){var hierarchies=result.i[category];for(var hierarchy in hierarchies){this._hierarchy_cache[category][hierarchy].source_url=hierarchies[hierarchy].source_url;}}
this._notify_callbacks('receive_hierarchy');}},_fail_to_receive_hierarchy:function(result,textStatus){},_receive_hier:function(result,textStatus){this._hierarchy_cache={};for(var category in result){if(undefined==this._hierarchy_cache[category]){this._hierarchy_cache[category]={};}
function process_children(hierarchy){for(var i=0;i<hierarchy.length;++i){var item=hierarchy[i];if(undefined==this._hierarchy_cache[category][item.d.i]){this._hierarchy_cache[category][item.d.i]={};}
this._hierarchy_cache[category][item.d.i].name=item.d.n;this._hierarchy_cache[category][item.d.i].ancestors=item.d.h;this._hierarchy_cache[category][item.d.i].fun_count=item.d.c;if(undefined!=item.c){process_children.bind(this)(item.c);}}}
process_children.bind(this)(result[category]);}
this._notify_callbacks('receive_hier');},_fail_to_receive_hier:function(result,textStatus){},_receive_social:function(result,textStatus){if(result.succeeded){var category=result.category;var id=result.id;var function_info=this._function_cache[category][id];function_info.social=result.data;function_info.loading_social=false;this._notify_callbacks('receive_social',result.category,result.id);}},_fail_to_receive_social:function(result,textStatus){},_receive_function:function(result,textStatus){if(result.succeeded){var category=result.category;var id=result.id;var function_info=this._function_cache[category][id];for(var key in result.data){function_info[key]=result.data[key];}
function_info.loading=false;if(function_info.data){switch(function_info.category){case 30:case 9:case 28:case 26:function_info.data=function_info.data.replace(/<\/s>/g,'</span>');function_info.data=function_info.data.replace(/<st>/g,'<span class="type">');function_info.data=function_info.data.replace(/<si>/g,'<span class="initializer">');function_info.data=function_info.data.replace(/<sm>/g,'<span class="methodname">');function_info.data=function_info.data.replace(/<smp>/g,'<span class="methodparam">');function_info.data=function_info.data.replace(/<sp>/g,'<span class="methodarg">');break;case 29:case 34:function_info.data=window["eval"]("("+function_info.data+")");break;case 25:function_info.data=window["eval"]("("+function_info.data+")");if(!function_info.data.d){function_info.data.d='Not defined';}
if(!function_info.data.i){function_info.data.i='No';}
break;}}
if(function_info.navigate_immediately&&result.data.url){delete function_info.navigate_immediately;this._notify_callbacks('navigate_immediately',result.data.url);}
this._notify_callbacks('receive_function',result.category,result.id);}else{this._notify_callbacks('receive_function',result.category,result.id);}},_fail_to_receive_function:function(result,textStatus){},receive_vote_update:function(result,textStatus){if(result.succeeded){if(result.updated){$.ajax({type:'GET',url:'/function/social',dataType:'json',data:{category:result.category,id:result.id},success:this._receive_social.bind(this),failure:this._fail_to_receive_social.bind(this)});}}},fail_to_receive_vote_update:function(result,textStatus){}};Snap.EmailCheck=function(input,info){this._input=input;this._info=info;this._last=null;this._timeout=null;this._s={DEFAULT:'in case of forgetfulness',INVALID:'invalid email',VALID_EMAIL:'looks good'};this.init();};Snap.EmailCheck.prototype={init:function(){var obj=this;this._input.keyup(function(){obj._checkInput.bind(obj)();});if(this._input.val()!=''){this._checkInput({now:true});show(this._info.parent());}else{this._info.text(this._s.DEFAULT);}},_checkInput:function(options){var email=$(this._input).val();if(this._last==email){return;}
this._last=email;if(this._timeout){clearTimeout(this._timeout);this._timeout=null;}
if(email==''){this._info.text(this._s.DEFAULT).parent().animate({backgroundColor:Snap.ColourPalette.White},500);}else{if(options&&options.now){this.checkValidity();}else{this._timeout=setTimeout(this.checkValidity.bind(this),500);}}},checkValidity:function(){var email=this._input.val();if(email.length==0){return;}
var error=null;if(email.length<5){error=this._s.INVALID;}else if(!(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(email))){error=this._s.INVALID;}
if(error){this._info.text(error).parent().animate({backgroundColor:Snap.ColourPalette.Error},500);}else{this._info.text(this._s.VALID_EMAIL).parent().animate({backgroundColor:Snap.ColourPalette.Success},500);}}};Snap.GhostInput=function(id,suggestionText){var actions={focus:function(){if($(this).val()==suggestionText){$(this).val('');}
$(this).removeClass('inactive');},blur:function(){if($(this).val()==''||$(this).val()==suggestionText){$(this).val(suggestionText).addClass('inactive');}},init:function(){if($(this).val()==''||$(this).val()==suggestionText){$(this).val(suggestionText).addClass('inactive');}}};$(id).focus(actions.focus).blur(actions.blur).each(actions.init);}
Snap.OpenIdLogin=function(login){var openid_services={google:{url:'http://www.google.com/accounts/o8/id'},myopenid:{url:'http://username.myopenid.com/'},flickr:{url:'http://flickr.com/'},yahoo:{url:'http://me.yahoo.com'},aol:{url:'http://openid.aol.com/username'},blogger:{url:'http://username.blogspot.com/'},livejournal:{url:'http://username.livejournal.com/'},verisign:{url:'http://username.pip.verisignlabs.com/'},myvidoop:{url:'http://username.myvidoop.com/'},claimid:{url:'http://claimid.com/username'},technorati:{url:'http://technorati.com/people/technorati/username'},vox:{url:'http://username.vox.com/'}};var activeService;$(login+' .form form').submit(function(){return $(login+' .loginform .text').val()!='';});$(login+' .loginform .text').focus(function(){$(login+' .services .list').show();});$(login+' .loginform .button').focus(function(){$(login+' .services .list').hide();});$(login+' .userloginform .button').focus(function(){$(login+' .services .list').hide();});$(login+' .userloginform .text').focus(function(){$(login+' .services .list').hide();});$(login+' .services img:first').click(function(){$(login+' .services .list').toggle();}).hover(function(){$(login+' .services .list').show();});$(login+' .services .list .service').each(function(index){var service=openid_services[$(this).text().toLowerCase().replace('!','')];if(service===undefined){return;}
$(this).click(function(){if(service.url.indexOf('username')>=0){$(login+' .services .list .bottombar .servicename').text($(this).text());var input=$(login+' .services .list .bottombar .text')
input.val('');$(login+' .services .list .bottombar').slideDown('fast',function(){input.focus();});}else{$(login+' .services .list .bottombar').slideUp('fast');$(login+' .loginform .text').focus();}
$(login+' .services .list .service').each(function(){$(this).removeClass('selected');});$(this).addClass('selected');$(login+' .loginform .text').val(service.url);activeService=service;});$(login+' .services .list .bottombar .text').keyup(function(){var newUrl=$(this).val()!=''?activeService.url.replace('username',$(this).val()):activeService.url;$(login+' .loginform .text').val(newUrl);});});}
Snap.PasswordCheck=function(input,info,opt_usernamecheck){this._input=input;this._info=info;this._username=opt_usernamecheck;this._last=null;this._timeout=null;this._s={MIN_LENGTH:'5 or more characters',VALID_PASS:'looks good',PASS_SAME:'username = password'};this.init();};Snap.PasswordCheck.prototype={init:function(){var obj=this;this._input.keyup(function(){obj._checkInput.bind(obj)();});if(this._input.val()!=''){this._checkInput({now:true});show(this._info.parent());}else{this._info.text(this._s.MIN_LENGTH);}},_checkInput:function(options){var pass=$(this._input).val();if(this._last==pass){return;}
this._last=pass;if(this._timeout){clearTimeout(this._timeout);this._timeout=null;}
if(pass==''){this._info.text(this._s.MIN_LENGTH).parent().attr('style','display:block').animate({backgroundColor:Snap.ColourPalette.White},500);}else{if(options&&options.now){this.checkValidity();}else{this._timeout=setTimeout(this.checkValidity.bind(this),500);}}},checkValidity:function(){var pass=this._input.val();var error=null;if(pass.length<5){error=this._s.MIN_LENGTH;}else if(this._username&&this._username.isValid()&&this._username.getName()==pass){error=this._s.PASS_SAME;}
if(error){this._info.text(error).parent().animate({backgroundColor:Snap.ColourPalette.Error},500);}else{this._info.text(this._s.VALID_PASS).parent().animate({backgroundColor:Snap.ColourPalette.Success},500);}}};Snap.UsernameCheck=function(input,info){this._input=input;this._info=info;this._last=null;this._timeout=null;this._isvalid=false;this._s={DEFAULT:'pick your nickname',UNIQUE_NICKNAME:'nice, it\'s unique',NICKNAME_DUPE:'that name\'s taken',CHECKING:'Checking availability...'};this.init();};Snap.UsernameCheck.prototype={isValid:function(){return this._isvalid;},getName:function(){return this._input.val();},init:function(){var obj=this;this._input.keyup(function(){obj._checkInput.bind(obj)();});this._info.text(this._s.DEFAULT);if(this._input.val()!=''){this._checkInput({now:true});show(this._info.parent());}},_checkInput:function(options){var name=$.trim($(this._input).val());if(this._last==name){return;}
this._last=name;if(this._timeout){clearTimeout(this._timeout);this._timeout=null;}
if(name==''){this._info.text(this._s.DEFAULT).parent().css({backgroundColor:Snap.ColourPalette.White});this._isvalid=false;}else{if(options&&options.now){this.checkAvailability();}else{this._timeout=setTimeout(this.checkAvailability.bind(this),500);}}},checkAvailability:function(){var obj=this;var delayText=function(){this._info.text(this._s.CHECKING);}
var timeout=setTimeout(delayText.bind(this),300);var success=function(result,textStatus){clearTimeout(timeout);if(result.nickname==this.nickname){if(!result.exists){obj._info.text(obj._s.UNIQUE_NICKNAME).parent().animate({backgroundColor:Snap.ColourPalette.Success},500);obj._isvalid=true;}else{obj._info.text(obj._s.NICKNAME_DUPE).parent().animate({backgroundColor:Snap.ColourPalette.Error},500);obj._isvalid=false;}}}
var nickname=this._input.val();$.ajax({type:'GET',url:'/user/exists',dataType:'json',nickname:nickname,data:{nickname:nickname},success:success});}};Snap.FilterBar=function(elementIDs){this._elementIDs=elementIDs;this._elementIDs.list=this._elementIDs.filters+' .list';this._elementIDs.list_button=this._elementIDs.filters+' .list-button';this._elementIDs.active=this._elementIDs.filters+' .active';this._elements={};for(var key in this._elementIDs){if(key!='canvas'){this._elements[key]=$(this._elementIDs[key]);}else{this._elements[key]=$('#'+this._elementIDs[key]);}}
this._packages=[{name:'Web (jQuery)',filters:[31,25,32,30]},{name:'Web (mootools)',filters:[31,25,36,30]},{name:'Web (Zend)',filters:[9,26]},{name:'Web (Django)',filters:[33,28]}];this._filter_list_shown=false;this._active_filters={};this._is_category_filtered={};this._elements.list_button.click(this._toggle_filter_list.bind(this));this._callbacks=[];this._db=Snap.Database.singleton;this._db.register_callbacks({receive_categories:this._receive_categories.bind(this)});}
Snap.FilterBar.prototype={toggle:function(type,id){if(this._is_category_filtered[id]){this._remove_filter(type,id);}else{if(undefined==this._active_filters[type]){this._active_filters[type]={};}
this._active_filters[type][id]=this._db.id_to_name(id);}
this.refresh();},clearAll:function(){this._active_filters={};},enable:function(type,id){if(undefined==this._active_filters[type]){this._active_filters[type]={};}
this._active_filters[type][id]=this._db.id_to_name(id);},refresh:function(){this._save_active_filters();this._render_filters();this._notify_callbacks();},is_filtered:function(id){return this._is_category_filtered[id];},any_filtered:function(){for(var key in this._is_category_filtered){return true;}
return false;},register_change_callback:function(fn){this._callbacks.push(fn);},_notify_callbacks:function(){for(var i=0;i<this._callbacks.length;++i){this._callbacks[i]();}},_flatten_filters:function(){var flat_filters=[];for(var filter_type in this._active_filters){var filter=this._active_filters[filter_type];for(var filter_id in filter){flat_filters.push(filter_id);}}
return flat_filters.join(',');},_simplify_filters:function(){this._is_category_filtered={};for(var filter_type in this._active_filters){var filter=this._active_filters[filter_type];for(var filter_id in filter){this._is_category_filtered[filter_id]=true;}}},_save_active_filters:function(){this._simplify_filters();$.cookie('filters',this._flatten_filters());},_receive_categories:function(){if(window.sel){if(undefined==this._active_filters[window.sel.filter_type]){this._active_filters[window.sel.filter_type]={};}
this._active_filters[window.sel.filter_type][window.sel.category]=this._db.id_to_name(window.sel.category);}else if($.cookie('filters')){var filters=$.cookie('filters').split(',');for(var i=0;i<filters.length;++i){var type=this._db.id_to_type(filters[i]);if(undefined==this._active_filters[type]){this._active_filters[type]={};}
this._active_filters[type][filters[i]]=this._db.id_to_name(filters[i]);}}
this._simplify_filters();this._render_filters();},_toggle_filter_list:function(){if(this._filter_list_shown){this._elements.list.slideUp('fast');this._elements.list_button.html('View all currently supported frameworks and languages');}else{this._elements.list.slideDown('fast');this._elements.list_button.html('Hide');}
this._filter_list_shown=!this._filter_list_shown;},_render_filter_list:function(){var html=[];html.push('<div class="header">All frameworks and languages</div><table><tbody><tr>');var filters=this._db.get_filters();for(var i=0;i<filters.length;++i){var filter_type=filters[i].t;html.push('<td class="col',i,'"><div class="cat_header">',filter_type,'</div>');var filter_list=filters[i].d;for(var i2=0;i2<filter_list.length;++i2){var filter_id=filter_list[i2].i;var toggle_class=this._is_category_filtered[filter_id]?'hide':'show';html.push('<div class="filter ',toggle_class,'" title="Click to toggle" id="toggle_',filter_type,'-',filter_id,'">',filter_list[i2].n,'</div>');}
html.push('</td>');}
html.push('</tr></tbody></table>');html.push('<div class="packages"><div class="header">Packages</div>');for(var i=0;i<this._packages.length;++i){var package=this._packages[i];var toggle_class='show';html.push('<div class="filter ',toggle_class,'" title="Click to use this package" id="package_',i,'">',package.name,'</div>');}
html.push('</div><div class="clearfix"></div><div class="all-selected"></div>');this._elements.list.html(html.join(''));var t=this;$(this._elementIDs.list+' .filter').click(function(){var toggle_prefix='toggle_';var package_prefix='package_';if(this.id.indexOf(toggle_prefix)>=0){var filter_type=this.id.substr(toggle_prefix.length,this.id.indexOf('-')-toggle_prefix.length);var filter_id=this.id.substr(this.id.indexOf('-')+1);t.toggle(filter_type,filter_id);}else if(this.id.indexOf(package_prefix)>=0){var package=t._packages[this.id.substr(package_prefix.length)];t.clearAll();for(var i=0;i<package.filters.length;++i){var filter_id=package.filters[i];t.enable(t._db._id_to_type[filter_id],filter_id);}
t.refresh();}});},_render_filters:function(){var html=[];var any_filters=false;var type_set=[];for(var filter_type in this._active_filters){var filter=this._active_filters[filter_type];var this_type=[];this_type.push('<div class="row">');if(any_filters){this_type.push('and');}else{this_type.push('Filtering by');}
this_type.push(' <span class="type">',filter_type.toLowerCase());var filter_set=[];var count=0;for(var filter_id in filter){var item=filter[filter_id];filter_set.push('<span class="filter" id="remove_'+
filter_type+'-'+filter_id+'" title="Click to remove">'+item+'<span class="xme">X</span></span>');any_filters=true;count++;}
if(count>1){this_type.push('s');}
this_type.push(': </span>');if(filter_set.length>2){var last=filter_set.splice(filter_set.length-1);this_type.push(filter_set.join(', '));this_type.push(' and ',last);}else if(filter_set.length==2){this_type.push(filter_set.join(' and '));}else{this_type.push(filter_set[0]);}
this_type.push('</div>');type_set.push(this_type.join(''));}
html.push(type_set.join(''));if(any_filters){this._elements.active.html(html.join(''));var t=this;$(this._elementIDs.active+' .filter').click(function(){var filter_type=this.id.substr('remove_'.length,this.id.indexOf('-')-'remove_'.length);var filter_id=this.id.substr(this.id.indexOf('-')+1);t._remove_filter(filter_type,filter_id);});this._elements.active.show();}else{this._elements.active.hide();}
this._render_filter_list();},_remove_filter:function(filter_type,filter_id){delete this._active_filters[filter_type][filter_id];var any_filters_left=false;for(var key in this._active_filters[filter_type]){any_filters_left=true;break;}
if(!any_filters_left){delete this._active_filters[filter_type];}
this._save_active_filters();this._render_filters();this._notify_callbacks();}}
Snap.TreeView=function(elementIDs,filters,typeahead){this._elementIDs=elementIDs;this._elements={};for(var key in this._elementIDs){if(key!='canvas'){this._elements[key]=$(this._elementIDs[key]);}else{this._elements[key]=$('#'+this._elementIDs[key]);}}
this._filters=filters;this._typeahead=typeahead;this._filters.register_change_callback(this._filters_changed.bind(this));this._tree=[];this._tree_ui=[];this._tree_visibility=[];this._hierarchy_to_node={};this._elements.view.html('<div class="initial_loading">Loading table of contents...</div>');this._function_cache={};this._db=Snap.Database.singleton;this._db.register_callbacks({receive_hier:this._receive_hier.bind(this)});var width=$.cookie('resizer-x-pos');if(width){width=parseInt(width);$('#tree-view').width(width+'px');var sizer_width=$('#tree-view-sizer').width();$('#content-table').css('marginLeft',(width+sizer_width)+'px');$('#external-page').css('marginLeft',(width+sizer_width)+'px');$('#external-page').css('marginRight',-(width+sizer_width)+'px');}
$(window).bind('resize',this._resize_frame.bind(this));this._resize_frame();$(document).bind('mousedown',this._mouseDown.bind(this)).bind('mousemove',this._mouseMove.bind(this)).bind('mouseup',this._mouseUp.bind(this));};Snap.TreeView.prototype={_filters_changed:function(){this._render();},_render:function(){var any_filters=this._filters.any_filtered();for(var i=0;i<this._tree.length;++i){var tree=this._tree[i];if(!any_filters||this._filters.is_filtered(tree.id)){if(!this._tree_visibility[i]){this._tree_visibility[i]=true;this._tree_ui[i].fadeIn('fast');}}else{if(this._tree_visibility[i]){this._tree_visibility[i]=false;this._tree_ui[i].fadeOut('fast');}}}},_resize_frame:function(){var height=($(window).height()-$('#topbar').height())+'px';$('#tree-view').height(height);$('#tree-view-sizer').height(height);},_mouseDown:function(event){if($(event.target).attr('id')==$('#tree-view-sizer').attr('id')){this._mouse_dragging=true;this._mouse_start={x:event.screenX,y:event.screenY};this._mouse_last={x:event.screenX,y:event.screenY};event.stopPropagation();event.preventDefault();}},_mouseMove:function(event){if(this._mouse_dragging){var delta={x:(event.screenX-this._mouse_last.x),y:(event.screenY-this._mouse_last.y)};this._mouse_last={x:event.screenX,y:event.screenY};var new_width=$('#tree-view').width()+delta.x;if(new_width<400&&new_width>100){$('#tree-view').width(($('#tree-view').width()+delta.x)+'px');$('#content-table').css('marginLeft',(parseInt($('#content-table').css('marginLeft'))+delta.x)+'px');$('#external-page').css('marginLeft',(parseInt($('#external-page').css('marginLeft'))+delta.x)+'px');$('#external-page').css('marginRight',(parseInt($('#external-page').css('marginRight'))-delta.x)+'px');}
event.stopPropagation();event.preventDefault();}},_mouseUp:function(event){if(this._mouse_dragging){this._mouse_dragging=false;event.stopPropagation();event.preventDefault();$.cookie('resizer-x-pos',$('#tree-view').width());}},_create_ui:function(){var html=[];html.push('<ul class="top">');for(var i=0;i<this._tree.length;++i){var tree=this._tree[i];this._tree_visibility.push(false);var has_children=tree.children.length>0||tree.fun_count>0;var id='cat_'+tree.id+'-1';html.push('<li id="',id,'" class="root" style="display:none"><div class="node_text root_text');if(has_children){html.push(' has_children');}
html.push('">');if(has_children){html.push('<div class="expander">+</div>');}
html.push('<div class="the_text">',tree.name,'</div></div>');if(has_children){html.push('<ul style="display:none"></ul>');}
html.push('</li>');}
html.push('</ul>');this._elements.view.html(html.join(''));var t=this;$(this._elementIDs.view+' .root').each(function(index){t._tree_ui[index]=$(this);});function handle_expand(){if($(this).html()=='+'){$(this).html('-');}else{$(this).html('+');}
var parent=$(this).parent().parent();var id=parent.attr('id');var ul=parent.children('ul:first');var category=parseInt(id.substr(4,id.indexOf('-')-4));var hierarchy=parseInt(id.substr(id.indexOf('-')+1));if(ul.css('display')!='none'){ul.fadeOut('fast',function(){$(this).css({display:'block',visibility:'hidden'}).slideUp('fast');});}else{if(ul.children('.child').length==0){var node_children=t._hierarchy_to_node[category][hierarchy];var html=[];for(var i=0;i<node_children.length;++i){var has_children=node_children[i].children.length>0||node_children[i].fun_count>0;var id='cat_'+category+'-'+node_children[i].id;html.push('<li id="',id,'" class="child"><div class="node_text');if(has_children){html.push(' has_children');}
html.push('">');if(has_children){html.push('<div class="expander">+</div>');}
html.push('<div class="the_text">',node_children[i].name,'</div></div>');if(has_children){html.push('<ul style="display:none"></ul>');}
html.push('</li>');}
ul.html(html.join(''));ul.children('.child').children('.has_children').children('.expander').click(handle_expand);}
if(!t._is_loaded(category,hierarchy)&&!t._is_loading(category,hierarchy)&&t._any_to_load(category,hierarchy)){ul.append('<div class="loading">Grabbing the function list...</div>');t._request(category,hierarchy,ul);}
ul.css({visibility:'hidden'}).slideDown('fast',function(){$(this).css({display:'none',visibility:'visible'}).fadeIn('fast');});}}
$(this._elementIDs.view+' .has_children .expander').click(handle_expand);},_request:function(category,id,ul){this._function_cache[category][id].ul=ul;this._function_cache[category][id].loading=true;this._function_cache[category][id].request=$.ajax({type:'GET',url:'/hierarchy/list',dataType:'json',data:{category:category,id:id},success:this._receive_list.bind(this),failure:this._fail_to_receive_list.bind(this)});},_receive_list:function(result,textStatus){if(result.s){var category=result.c;var hierarchy=result.h;var list=result.l;var cache=this._function_cache[category][hierarchy];cache.loading=false;cache.loaded=true;cache.list=list;var html=[];for(var i=0;i<list.length;++i){var item=list[i];var id='fun_'+category+'-'+item.id;html.push('<li id="',id,'" class="child"><div class="node_text">');html.push('<div class="the_text link_text"><tt>',item.name,'</tt></div></div>');html.push('</li>');}
cache.ul.children('.loading').remove();cache.ul.append(html.join(''));var t=this;cache.ul.children('.child').click(function(){var id=$(this).attr('id');var category=parseInt(id.substr(4,id.indexOf('-')-4));var function_id=parseInt(id.substr(id.indexOf('-')+1));var name=$(this).children('.node_text').children('.the_text').children('tt').html();t._typeahead.force_selection(category,hierarchy,function_id,name);});}},_fail_to_receive_list:function(result,textStatus){},_any_to_load:function(category,id){return this._function_cache[category][id].fun_count>0;},_is_loaded:function(category,id){return this._function_cache[category][id].loaded;},_is_loading:function(category,id){return this._function_cache[category][id].loading;},_receive_hier:function(){var hier=this._db.get_hierarchies();for(var category in hier){this._function_cache[category]={};this._hierarchy_to_node[category]={};var list=hier[category];var tree=[];var map={};this._function_cache[category][1]={fun_count:0};for(var id in list){var node=list[id];this._function_cache[category][id]={fun_count:node.fun_count};var parentnode=null;for(var i=0;i<node.ancestors.length;++i){var iter=node.ancestors[i];if(parentnode){if(undefined==parentnode.map[iter]){parentnode.children.push({map:{},children:[]});parentnode.map[iter]=parentnode.children.length-1;}
parentnode=parentnode.children[parentnode.map[iter]];}else{if(undefined==map[iter]){tree.push({map:{},children:[]});map[iter]=tree.length-1;}
parentnode=tree[map[iter]];}}
var this_node=null;if(!parentnode){if(undefined!=map[id]){tree[map[id]].name=node.name;tree[map[id]].fun_count=node.fun_count;tree[map[id]].id=id;}else{tree.push({name:node.name,id:id,fun_count:node.fun_count,map:{},children:[]});map[id]=tree.length-1;}
this_node=tree[map[id]];}else{if(undefined!=parentnode.map[id]){parentnode.children[parentnode.map[id]].name=node.name;parentnode.children[parentnode.map[id]].fun_count=node.fun_count;parentnode.children[parentnode.map[id]].id=id;}else{parentnode.children.push({name:node.name,id:id,fun_count:node.fun_count,map:{},children:[]});parentnode.map[id]=parentnode.children.length-1;}
this_node=parentnode.children[parentnode.map[id]];}
this._hierarchy_to_node[category][id]=this_node.children;}
this._hierarchy_to_node[category][1]=tree;this._tree.push({id:category,name:this._db.id_to_name(category),children:tree});}
function compare_names(left,right){var result=(left.name.toLowerCase()<right.name.toLowerCase()?-1:(left.name.toLowerCase()==right.name.toLowerCase()?0:1));return result;}
this._tree=this._tree.sort(compare_names);function traverse_tree(node){for(var i=0;i<node.children.length;++i){traverse_tree(node.children[i]);}
node.children=node.children.sort(compare_names);}
for(var i=0;i<this._tree.length;++i){traverse_tree(this._tree[i]);}
this._create_ui();this._render();}};Snap.TypeAhead=function(elementIDs,filterbar){this._elementIDs=elementIDs;this._elements={};for(var key in this._elementIDs){if(key!='canvas'){this._elements[key]=$(this._elementIDs[key]);}else{this._elements[key]=$('#'+this._elementIDs[key]);}}
this._filterbar=filterbar;this._elements.input.keydown(this._handle_key.bind(this)).keyup(this._handle_key.bind(this)).keypress(this._handle_key.bind(this)).focus(this._gain_focus.bind(this)).blur(this._lose_focus.bind(this));this._elements.goback.click(this._hide_iframe.bind(this));this._current_value='';this._list=null;this._selection=-1;this._offset=0;this._list_length=10;this._active_function=null;this._hover_timer=null;this._has_changed_since_selection=null;this._displaying_frame=false;this._frame_url=null;this._db=Snap.Database.singleton;if(window.sel&&window.sel.name){this._elements.input.val(window.sel.name);this._load_function(window.sel);this._display_function(window.sel);this._list=[window.sel];this._selection=0;}
this._db.register_callbacks({receive_categories:this._receive_categories.bind(this),receive_function:this._receive_function.bind(this),receive_social:this._receive_function.bind(this),receive_hier:this._receive_hier.bind(this),receive_hierarchy:this._receive_hierarchy.bind(this),navigate_immediately:this._navigate_immediately.bind(this)});$(window).bind('resize',this._resize_frame.bind(this));};Snap.TypeAhead.prototype={key:{enter:13,escape:27,page_up:33,page_down:34,left:37,up:38,right:39,down:40},clear:function(){this._current_value='';this._elements.input.val('');this._do_search();},force_selection:function(category,hierarchy,id,name){this._list=[{name:name,type:this._db.id_to_name(category),category:category,function_id:id,hierarchy:hierarchy}];this._handle_selection(0);},_handle_key:function(event){var keydown_type='keypress';if($.browser.safari){keydown_type='keydown';}
if(event.type=='keyup'||event.type==keydown_type){var new_val=this._elements.input.val();if(this._current_value!=new_val){if(this._hover_timer){clearTimeout(this._hover_timer);}
this._hover_timer=setTimeout(this._hover.bind(this),1000);this._current_value=new_val;this._elements.dropdown.fadeIn('fast');setTimeout(this._do_search.bind(this),10);this._has_changed_since_selection=true;}}
if(event.type==keydown_type&&this._list){if(event.keyCode==this.key.enter){var do_nothing=false;var selection=this._list[this._selection];if(!this._has_changed_since_selection&&this._active_function.id==selection.function_id){if(this._db.is_function_cached(selection.category,selection.function_id)){var function_info=this._db.get_function(selection.category,selection.function_id);if(function_info.url){this._elements.dropdown.fadeOut('fast');this._show_iframe(function_info.url);}else{function_info.navigate_immediately=true;this._render_function();}
do_nothing=true;}}
if(!do_nothing){if(this._hover_timer){clearTimeout(this._hover_timer);this._hover_timer=null;}
this._handle_selection(this._selection);if(selection.function_id){$.ajax({type:'POST',url:'/function/select',data:{category:selection.category,id:selection.function_id}});}}}else{var old_selection=this._selection;if(event.keyCode==this.key.down){this._selection++;}else if(event.keyCode==this.key.up){this._selection--;}else if(event.keyCode==this.key.page_up){this._selection-=this._list_length;}else if(event.keyCode==this.key.page_down){this._selection+=this._list_length;}
if(this._selection<0){this._selection=this._list.length-1;}else if(this._selection>=this._list.length){this._selection=0;}
if(old_selection!=this._selection){if(this._selection<this._offset){this._offset=this._selection;this._render_selection();}else if(this._selection>=this._offset+this._list_length){this._offset+=this._selection-(this._offset+this._list_length)+1;this._render_selection();}else{this._elements.dropdown.children('.holder').children('.selected').removeClass('selected');this._elements.dropdown.children('.holder').children('.result:eq('+(this._selection-this._offset)+')').addClass('selected');}
if(this._hover_timer){clearTimeout(this._hover_timer);}
this._hover_timer=setTimeout(this._hover.bind(this),500);}}}
if(event.keyCode==this.key.escape&&this._displaying_frame){this._hide_iframe();}
if(event.keyCode==this.key.down||event.keyCode==this.key.up){event.stopPropagation();return false;}
return true;},_resize_frame:function(){if(jQuery.browser.safari&&this._displaying_frame){$('iframe').width(($(window).width()-$('#tree-view').width()-$('#tree-view-sizer').width())+'px');$('iframe').height(($(window).height()-$('#topbar').height())+'px');}},_hover:function(){if(this._list&&this._selection>=0){var selection=this._list[this._selection];if(selection.function_id){this._load_function(this._list[this._selection]);}}},_handle_selection:function(index){this._has_changed_since_selection=false;this._selection=index;var selection=this._list[this._selection];this._current_value=selection.name;this._elements.input.val(selection.name);if(selection.filter_id){this._filterbar.toggle(selection.type,selection.filter_id);this.clear();}else if(selection.function_id){this._elements.dropdown.fadeOut('fast');this._load_function(selection);if(!this._displaying_frame){this._display_function(selection);}else{this._display_function(selection,true);var function_info=this._db.get_function(selection.category,selection.function_id);if(function_info.url){this._show_iframe(function_info.url);}else{this._elements.messages.html('Just a sec, once we\'ve loaded the details we\'ll send you off...').fadeIn('fast');function_info.navigate_immediately=true;}}}else{this._elements.dropdown.fadeOut('fast');}},_load_function:function(selection){if(!this._db.is_function_cached(selection.category,selection.function_id)){this._db.request_function(selection.category,selection.function_id,selection.name,selection.type,selection.hierarchy,false);this._db.ensure_hierarchy_loaded(selection.category,selection.hierarchy);}},_display_function:function(selection,invisible){this._active_function=this._db.get_function(selection.category,selection.function_id);if(!invisible){this._displaying_frame=false;}
this._render_function();},_do_search:function(){var trimmed_value=$.trim(this._current_value);if(trimmed_value==''){this._elements.dropdown.html('<div class="empty"><b>Tip: Type # to filter by specific languages or frameworks.<br/>By default, all languages and frameworks are searched.</b></div>');}else{this._list=this._db.search(trimmed_value,this._filterbar._is_category_filtered);this._selection=this._list?0:-1;this._offset=0;if(this._list==-1){this._elements.dropdown.html('<div class="empty">We\'re still loading the filters, just a sec.</div>');this._list=null;this._selection=-1;}else if(this._list==-2){this._elements.dropdown.html('<div class="empty">Type a language or framework name.</div>');this._list=null;this._selection=-1;}
this._render_selection();}},_render_selection:function(){if(this._list){var html=[];var end=Math.min(this._offset+this._list_length,this._list.length);var is_filter_list=false;for(var i=this._offset;i<end;++i){var entry=this._list[i];var name=entry.name;var regex=[];for(var i2=0;i2<entry.matches.length;++i2){var word=entry.matches[i2];if(word.length>0){regex.push(word);}}
if(regex.length>0){name=name.gsub(new RegExp('('+regex.join('|').replace('(','\\(').replace(')','\\)').replace('*','\\*').replace('$','\\$').replace(':','\\:').replace('.','\\.').replace('+','\\+')+')','i'),function(match){return'<em>'+match[0]+'</em>';});}
html.push('<div class="result');if(i==this._selection){html.push(' selected');}
html.push('">');if(entry.filter_id){is_filter_list=true;if(this._filterbar.is_filtered(entry.filter_id)){html.push('<span class="remove">remove</span> ');}}
html.push(name+' <span class="category">'+entry.type+'</span>');if(undefined!=entry.function_id){var lineage=this._render_lineage(entry.category,entry.hierarchy,false);if(lineage){html.push(' <span class="lineage">'+lineage+'</span>');}}
html.push('</div>');}
html.push('<div class="result_info">');if(this._list.length==1){html.push('The only entry');}else{html.push((this._offset+1),'-',(Math.min(this._list.length,this._offset+this._list_length)),' out of ',this._list.length);}
html.push('</div>');this._elements.dropdown.html('<div class="holder">'+html.join('')+'</div>');var t=this;this._elements.dropdown.children('.holder').children('.result').each(function(index){$(this).click(function(){t._handle_selection.bind(t)(index+t._offset);});});}else{this._elements.dropdown.html('<div class="empty">Bummer, we don\'t have an entry for that.</div>');}},_render_lineage:function(category,hierarchy,with_links){if(this._db.hierarchies_loaded()){var info=this._db.get_hierarchy(category,hierarchy);var lineage=[];var missing_any=false;if(info.ancestors){for(var i2=0;i2<info.ancestors.length;++i2){var ancestor=this._db.get_hierarchy(category,info.ancestors[i2]);var step='';if(with_links&&ancestor.source_url){step+='<a class="external" href="'+ancestor.source_url+'">';}
step+=ancestor.name;if(with_links){step+='</a>';}
lineage.push(step);}}
var step='';if(with_links&&info.source_url){step+='<a class="external" href="'+info.source_url+'">';}
step+=info.name;if(with_links){step+='</a>';}
lineage.push(step);return lineage.join(' &raquo; ');}
return null;},_render_function:function(){if(!this._active_function){return;}
var html=[];html.push('<div class="function"><span class="name">');if(this._active_function.url){html.push('<a class="external" href="',this._active_function.url,'">');}
html.push(this._active_function.name);if(this._active_function.url){html.push('</a>');}
html.push('</span><span class="category">',this._active_function.type,'</span>');var category=this._active_function.category;var hierarchy=this._active_function.hierarchy;var lineage=this._render_lineage(category,hierarchy,true);if(lineage){html.push(' <span class="lineage">',lineage,'</span>');}
html.push('</div>');var permalink='/'+this._active_function.type+'/'+this._active_function.name;html.push('<div class="source"><a class="permalink" href="',permalink,'">snaapi.com',permalink,'</a></div>');if(this._active_function.short_description){html.push('<div class="short-description">',this._active_function.short_description,'</div>');}
var pluralize=function(text,num,plural){return num==1?text:text+plural;}
if(this._active_function.data){var data=this._active_function.data;switch(this._active_function.category){case 30:case 9:case 28:case 26:html.push('<div class="signature">',data,'</div>');break;case 34:html.push('<div class="row"><span class="title">URL:</span><div class="value"><a href="',data.u,'">',data.u,'</a></div></div>');html.push('<div class="row"><span class="title">',pluralize('Format',data.f.length,'s'),':</span><div class="value">',data.f.join(', '),'</div></div>');html.push('<div class="row"><span class="title">',pluralize('Method',data.m.length,'s'),':</span><div class="value">',data.m.join(', '),'</div></div>');html.push('<div class="row"><span class="title">API limit:</span><div class="value">',(data.l||'Not applicable'),'</div></div>');if(data.p){html.push('<div class="row"><span class="title">Parameters:</span>');for(var name in data.p){var param=data.p[name];html.push('<div class="value"><span class="value_name">',name,'</span><span class="description">');if(param.o){html.push('<span class="optional">(Optional)</span>');}
html.push(param.d,'</span></div>');}
html.push('</div>');}
if(data.r){html.push('<div class="row"><span class="title">Returns:</span><div class="value">',data.r,'</div></div>');}
break;case 29:if(data.i&&data.t){var types=['Property','Class Method','Instance Method'];html.push('<div class="type">',types[data.t-1],'</div>');html.push('<div class="signature">',data.i,'</div>');}
break;case 25:html.push('<div class="row"><span class="title">Default value:</span><div class="value">',data.d,'</div></div>');html.push('<div class="row"><span class="title">Expected values:</span>');var values=data.v;for(var i=0;i<values.length;++i){html.push('<div class="value">');if(typeof values[i]=='string'){html.push(values[i]);}else{html.push('<span class="value_name">',values[i].n,'</span><span class="description">',values[i].d,'</span>');}
html.push('</div>');}
html.push('</div>');html.push('<div class="row"><span class="title">Inherited:</span><div class="value">',data.i,'</div></div>');break;}}
if(this._active_function.navigate_immediately){this._elements.messages.html('Just a sec, once we\'ve loaded the details we\'ll send you off...').fadeIn('fast');}else if(this._active_function.loading){html.push('<div class="loading">Loading function details...</div>');}
if(this._active_function.loading_social){html.push('<div class="social">Loading snaapits...</div>');}else if(this._active_function.social.length>0){html.push('<div class="social">');html.push('<div class="header">snaapits');if(!window.user_id){html.push(' - <a href="/login">log in</a> to submit links, comments, and code');}
html.push('</div>');var social=this._active_function.social;for(var i=0;i<social.length;++i){html.push('<div class="row">');html.push('<div class="box"><div class="score">',social[i].score,'</div>');html.push('<div class="ratings"><span class="rater up">+</span><span class="rater down">-</span></div></div>');html.push('<div class="data">');if(social[i].type=='link'){html.push('<div class="link"><a href="',social[i].data,'" class="external">',social[i].data,'</a></div>');}else if(social[i].type=='snippet'){html.push('<div class="snippet"><pre>',social[i].data,'</pre></div>');}
if(social[i].summary){html.push('<div class="summary">',social[i].summary,'</div>');}
if(social[i].user_id){html.push('<div class="user" title="Wow, no usernames? Don\'t worry, they\'re coming!">Submitted by: User #',social[i].user_id,'</div>');}
html.push('</div>');html.push('</div>');}
html.push('</div>');}else if(this._active_function.social.length==0){html.push('<div class="social">');html.push('<div class="header">no snaapits found');if(!window.user_id){html.push(' - <a href="/login">log in</a> to submit links, comments, and code');}
html.push('</div></div>');}
if(undefined!=window.user_id){html.push('<div class="socialness"><div class="methods"><ul>');html.push('<li>Add a link</li>');html.push('<li>Add a snippet</li>');html.push('</ul></div>');html.push('<div class="form" style="display:none"><form method="post" action="/function/addurl"><input type="hidden" name="category" value="',this._active_function.category,'" /><input type="hidden" name="id" value="',this._active_function.id,'" /><div class="row"><label for="url">URL:</label><div class="rightside"><input type="text" class="text" name="url" id="url" size="50" value="" /></div></div><div class="row"><label for="summary">Summary:</label><div class="rightside"><textarea name="summary" id="summary" cols="50" rows="5" ></textarea></div></div><div class="rightside"><input type="submit" class="button" value="add" /></div></form></div>');html.push('<div class="form" style="display:none"><form method="post" action="/function/addsnippet"><input type="hidden" name="category" value="',this._active_function.category,'" /><input type="hidden" name="id" value="',this._active_function.id,'" /><label for="snippet">Snippet:</label><div class="rightside"><textarea class="code" name="snippet" id="snippet" cols="80" rows="10" wrap="off" ></textarea></div><div class="rightside"><input type="submit" class="button" value="add" /></div></form></div>');html.push('</div>');}
if(!this._active_function.loading_social&&undefined==window.user_id){html.push('<div style="text-align:center"><h2>How to use snaapi</h2><object width="380" height="300"><param name="movie" value="http://www.youtube.com/v/A8tm9DkuU2E&hl=en&fs=1&rel=0&hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/A8tm9DkuU2E&hl=en&fs=1&rel=0&hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="380" height="300"></embed></object></div>');}
if(!this._displaying_frame){var t=this;this._elements.whyjoin.fadeOut('fast',function(){t._elements.result.html(html.join('')).fadeIn('fast');});}else{this._elements.result.html(html.join(''));}
new Snap.GhostInput(this._elementIDs.result+' .form:eq(0) .text','Web address');var methods=[function(){$(this._elementIDs.result+' .form:eq(0) .text').focus();}.bind(this),function(){}.bind(this)];var t=this;if(undefined!=window.user_id){$(this._elementIDs.result+' li').each(function(index){$(this).click(function(){$(t._elementIDs.result+' .form:not(:eq('+index+'))').fadeOut('fast',function(){$(t._elementIDs.result+' .form:eq('+index+')').fadeIn('fast');methods[index]();});});});}
if(!this._active_function.loading_social){var fun=this._active_function;var t=this;$(this._elementIDs.result+' .up').each(function(index){$(this).click(function(){if(window.user_id){$.ajax({type:'POST',url:'/function/vote',dataType:'json',data:{category:fun.category,id:fun.id,index:social[index].ix,score:social[index].score,vote:1},success:t._db.receive_vote_update.bind(t._db),failure:t._db.fail_to_receive_vote_update.bind(t._db)});}});});$(this._elementIDs.result+' .down').each(function(index){$(this).click(function(){if(window.user_id){$.ajax({type:'POST',url:'/function/vote',dataType:'json',data:{category:fun.category,id:fun.id,index:social[index].ix,score:social[index].score,vote:-1},success:t._db.receive_vote_update.bind(t._db),failure:t._db.fail_to_receive_vote_update.bind(t._db)});}});});}
var t=this;$(this._elementIDs.result+' a:not(.permalink)').click(function(event){event.preventDefault();t._show_iframe($(this).attr('href'));return false;});},_show_iframe:function(url){if(!this._displaying_frame||this._frame_url!=url){var new_url=this._frame_url!=url;var selection=this._list[this._selection];if(new_url){$.ajax({type:'POST',url:'/function/viewframe',data:{category:selection.category,id:selection.function_id}});}
this._frame_url=url;var speed='fast';if(!this._displaying_frame){this._elements.goback.fadeIn(speed);this._elements.content_table.fadeOut(speed,function(){$('body').css({overflow:'hidden'});$('#footer').hide();this._elements.parent_table.css({position:'absolute'});if(new_url){this._elements.external.html('<div id="eww"><span class="reason">Just a sec, we\'re loading the reference page.<br/>thanks for using sna<span class="snaapi">api</span></span></div><iframe src="'+url+'" width="100%" height="100%" frameborder="0"></iframe>');}
this._elements.external.fadeIn(speed);}.bind(this));}else{var src_iframe=this._elements.external.children('iframe');src_iframe.attr('src',url);}
this._displaying_frame=true;console.log(this._frame_url);setTimeout(this._resize_frame.bind(this),2000);}},_hide_iframe:function(url){if(this._displaying_frame){this._displaying_frame=false;var speed='fast';this._elements.goback.fadeOut(speed);this._elements.external.fadeOut(speed,function(){this._elements.content_table.fadeIn(speed);$('body').css({overflow:'visible'});this._elements.parent_table.css({position:'static'});$('#footer').show();}.bind(this));}},_receive_categories:function(){this._do_search();},_receive_function:function(category,id,succeeded){if(this._active_function&&this._active_function.category==category&&this._active_function.id==id){this._render_function();}},_receive_hier:function(){if(window.sel&&window.sel.hierarchy){this._db.ensure_hierarchy_loaded(window.sel.category,window.sel.hierarchy);}},_navigate_immediately:function(url){this._elements.messages.fadeOut('fast',function(){$(this).html('');});this._show_iframe(url);},_receive_hierarchy:function(){this._render_function();},_gain_focus:function(){this._current_value=this._elements.input.val();this._do_search();this._elements.dropdown.fadeIn('fast');this._elements.input.select();},_lose_focus:function(){if(this._hover_timer){clearTimeout(this._hover_timer);this._hover_timer=null;}
this._elements.dropdown.fadeOut('fast');}};
