$.feature('my_games_anyone',function(){comment_textarea_focus(false);$.live("ul.fancy_list a.unexpanded, ul.fancy_list a.expanded",'click',function(){var new_state=!$(this).hasClass("expanded");$(this).toggleClass('expanded',new_state).toggleClass('unexpanded',!new_state);$(this).parent().find("ul").eq(0).toggle(new_state);return false;});$.live('#pod_show_off','click',function(){$('#txt_collection_url').select();$.get('/a/track_event/general/show_off_click');});if(!$.feature_enabled('f_game_toggle')){$.live('div.game_buttons li a','click',function(){return false;});}});function update_list_counts(counts,current_list_context)
{var i;if(counts.filters)
{$.each(counts.filters,function(idx,toggle){var count_els=$(".filter_menu_bar ."+toggle.slug+" .num");count_els.text(parseInt(count_els.text(),10)+toggle.delta);});}
if(counts.list_total_delta){var title=$('.title .num',current_list_context);title.each(function(){var t=$(this);t.text(t.text().replace(/(\d+)/,function(match,num){return(parseInt(num,10)+counts.list_total_delta);}));});var filter_all=$('.filter_menu_bar .all .num');filter_all.text(parseInt(filter_all.text(),10)+counts.list_total_delta);}
if(counts.builtin)
{var lgc=$('#list_games_consoles');for(i=0;i<counts.builtin.length;i++)
{switch(counts.builtin[i][0])
{case"mygames":if(counts.builtin[i][1]===0){lgc.find('li#overview').hide("slow");}else{lgc.find('li#overview').show("slow");}
lgc.find('li.list_my_games span.num').eq(0).text(counts.builtin[i][1]);break;case"wishlist":$("#list_game_lists li.icon_wishlist span.num").text(counts.builtin[i][1]);break;case"rated":$("#list_game_lists li.icon_rated_games span.num").text(counts.builtin[i][1]);break;}}}
if(counts.custom)
{var by_slug={};for(i=0;i<counts.custom.length;i++){by_slug[counts.custom[i][0]]=counts.custom[i][1];}
$("#list_game_lists li.custom_list a.list").each(function(){var updated=by_slug[$(this).attr("rel")];if(updated!==undefined){$(this).closest('.custom_list').find("span.num").text(updated);}});}
if(counts.platform_counts)
{var remove_element=function(){$(this).remove();};var new_platforms=[];for(i=0;i<counts.platform_counts.length;i++)
{var item=counts.platform_counts[i];var platform={id:item[0],game_count:item[1],name:item[2],slug:item[3]};var platform_li=$("#list_games_consoles li.platform-"+platform.id);if(platform_li.length>0)
{var platform_count_span=$("span.num",platform_li);if(platform.game_count>0)
{platform_count_span.text(platform.game_count);}
else
{platform_li.slideUp(500,remove_element);}}
else
{new_platforms.push(platform);}}
var platform_html=function(platform){return"<li class='platform-"+platform.id+"' style='display: none'><span class='inner'><a href='/"+
current_user.username+"/games/"+platform.slug+"'>"+platform.name+"</a><span class='num'>"+
platform.game_count+"</span></span></li>";};if(new_platforms&&new_platforms.length>0){new_platforms.sort(function(a,b){return a.name.localeCompare(b.name);});var platform_ul=$("#list_games_consoles li.list_my_games > ul");var platform_items=$("li",platform_ul);for(i=0;i<platform_items.length&&new_platforms.length>0;i++){var li=platform_items.eq(i);var cursor_name=$('a',li).text();while(new_platforms.length>0&&cursor_name>new_platforms[0].name){var p=new_platforms.shift();li.before(platform_html(p)).parent().find('li.platform-'+p.id).slideDown(500);}}
$.each(new_platforms,function(){platform_ul.append(platform_html(this)).find('li.platform-'+this.id).slideDown(500);});}}}
function textarea_jiggery_pokery()
{$("textarea.comment").each(function(){var old_height=$(this).height();$(this).height(old_height+1);$(this).height(old_height);});}
$.feature('f_edit_game_lightbox',function(){$(".games_list a.edit").live('click',function(){var li=$(this).closest('li.game');var gpid=/\bgameplatform_id_(\d+)\b/.exec(li.attr('class'))[1];window.features.edit_game_lightbox(gpid);return false;});});$.feature('my_games',function(){function reload_gameplatform_li(li){li=$(li).eq(0);var url=li.attr('data-reload-url');$.ajax({url:url,type:'GET',dataType:'JSON',success:function(data,textstatus){li.replaceWith(data);}});}
signals.mygames.edited.connect(function(data){update_list_counts(data.list_counts);var list_item=$('li.gameplatform_id_'+data.gameplatform_id);var ident=list_item.closest('.games_list').attr('data-ident');var keep_in_list=data.lists[ident]||/^rated-/.test(ident);if(keep_in_list){reload_gameplatform_li(list_item);}else{list_item.slideUp(500,function(){$(this).remove();});}});$.live(".games_list input[name=submit]",'click',function(){var li=$(this).parents('li.game');var gpid=/\bgameplatform_id_(\d+)\b/.exec(li.attr('class'))[1];var wrapper=li.find('div.ratings_wrapper');var template_type=/type_(\w+)/.exec(wrapper.attr('class'))[1];var url='/'+current_user.username+'/games/xhr/rate_comment_game/'+gpid;var data={return_template:template_type};var rating=li.find('input[name=rating_gp_'+gpid+']').val();if(rating!==""){data.rating=rating;}
var comment=li.find('textarea').val();if(comment===undefined){comment=li.find('div.comment p').text();}
if(comment!==''){data.comment=comment;}
var submit_errh=error_handler.generic_ajax({context:wrapper.eq(0),margin:"5px 0 0",position:'append'},ajax_error_translator.mygames);pf_ajax({url:url,data:data,error_handler:submit_errh,success:function(data,textStatus){wrapper.html(data.rating_div_html);var count_span=$('#list_game_lists a[rel=rated]').parents('li').eq(0).find('span.num').eq(0);count_span.html(data.num_rated);}});});$("li.custom_list").live("mouseenter",function(){$(this).addClass("custom_list_hover");});$("li.custom_list").live("mouseleave",function(){$(this).removeClass("custom_list_hover");});window.show_delete_lightbox=function(gp_id,list_ident){$.ajax({type:'GET',url:"/a/xhr/remove_gameplatform/"+list_ident+'/'+gp_id,success:function(lb_html){var rg_lightbox=$(lb_html).attr('id','remove_game_lb');$('#remove_game_lb').remove();$(document.body).append(rg_lightbox);rg_lightbox.data('err_handler',error_handler.generic_lightbox(rg_lightbox,ajax_error_translator()));rg_lightbox.showPopup({close_selector:".footer a.cancel",fade:false,verticalOffset:"center"});}});};$.live(".games_list a.remove",'click',function(){var rg_li=$(this).closest('li.game');var m=/gameplatform_id_(\d+)/.exec(rg_li.attr('class'));var gameplatform_id=m[1];var list_ident=rg_li.closest('.games_list').attr('data-ident');window.show_delete_lightbox(gameplatform_id,list_ident);return false;});$.live(".lightbox_remove_game .buttons a.delete, .lightbox_remove_game .buttons a.remove",'click',function(){var rg_lightbox=$(this).closest('.lightbox_remove_game');var rg_gameplatform_id=rg_lightbox.find("input[name=rg_gameplatform_id]").val();var rg_err_handler=rg_lightbox.data('err_handler');rg_err_handler.hide();pf_ajax({data:{},url:$(this).attr('data-url'),error_handler:rg_err_handler,success:function(data){rg_lightbox.closePopup();$(".games_list .gameplatform_id_"+rg_gameplatform_id).slideUp(500,function(){$(this).remove();});update_list_counts(data.counts);}});return false;});var lightbox=$(".lightbox_delete_list").eq(0);var get_dl_err_handler=error_handler.generic_ajax({context:$("#list_game_lists"),position:"before",margin:"0 36px 9px 18px"},ajax_error_translator({DEFAULT:"There was a problem accessing information about the list."}));var dl_err_handler=error_handler.generic_lightbox(lightbox,ajax_error_translator({DEFAULT:'There was a problem deleting the list.'}));$.live("li.custom_list a.remove","click",function(){var list_item={};list_item.link=$(this).parent().find(".list").eq(0);list_item.slug=list_item.link.attr("rel");list_item.name=list_item.link.text();list_item.num_games=0;lightbox.find("#list_item_slug").val(list_item.slug);list_item.num_games_url="/"+current_user.username+"/games/xhr/list/"+list_item.slug+"/num_games";pf_ajax({data:{},url:list_item.num_games_url,error_handler:get_dl_err_handler,success:function(data){list_item.num_games=data.count;list_item.num_games_pluralised=(list_item.num_games!=1);lightbox.find(".item_info p").html("<strong>"+list_item.name+"</strong><br />Contains "+list_item.num_games+" game"+(list_item.num_games_pluralised?"s":""));lightbox.showPopup({close_selector:".footer a.cancel",fade:false,verticalOffset:"center"});}});return false;});$.live(".lightbox_delete_list .buttons a.delete",'click',function(){dl_err_handler.hide();var list_item={};list_item.slug=lightbox.find("#list_item_slug").val();list_item.link=$("#list_game_lists a.list[rel="+list_item.slug+"]");pf_ajax({url:"/"+current_user.username+"/games/xhr/list/"+list_item.slug+"/delete",data:{slug:list_item.slug,user:current_user.username},error_handler:dl_err_handler,success:function(data){lightbox.closePopup();list_item.li=list_item.link;while(!list_item.li.attr("class").match("custom_list")){list_item.li=list_item.li.parent();}
list_item.li.slideUp(500,function(){var visible_list_slug=$('.games_list').attr('data-slug');$(this).remove();if(visible_list_slug==list_item.slug){document.location.href='/'+current_user.username+'/games/';}});}});return false;});var cl_lightbox=$(".lightbox_create_list").eq(0);var cl_err_handler=error_handler.generic_lightbox(cl_lightbox,ajax_error_translator({"value_error":"List names must contain at least one letter or number."}));$("#create_another_list a").click(function(){cl_err_handler.hide(false);cl_lightbox.showPopup({close_selector:".footer a.cancel",fade:false,verticalOffset:"center"});cl_lightbox.find("input.txt").val("").focus();return false;});cl_lightbox.keypress(function(e){if(e.which==13){cl_lightbox.find(".btn_create_list").eq(0).click();}});cl_lightbox.find("a.btn_create_list").eq(0).click(function(){var list_name=cl_lightbox.find("input.txt").eq(0).val();cl_err_handler.hide();if(list_name==="")
{cl_err_handler.show("Please enter a name for your list.");}
else
{pf_ajax({url:"/"+current_user.username+"/games/xhr/list/create",data:{name:list_name},error_handler:cl_err_handler,success:function(data){location.href="/"+current_user.username+"/games/list/"+data.slug;}});}
return false;});var el_lightbox=$(".lightbox_edit_list").eq(0);var el_err_handler=error_handler.generic_lightbox(el_lightbox,ajax_error_translator({"value_error":"List names must contain at least one letter or number."}));$.live("li.custom_list a.edit","click",function(){var edit_list_textbox=el_lightbox.find("input.txt");var edit_list_link=$(this).parent().find(".list");var edit_list_slug=edit_list_link.attr("rel");var edit_list_name=edit_list_link.text();el_lightbox.find("#edit_list_slug").val(edit_list_slug);el_err_handler.hide(false);el_lightbox.showPopup({close_selector:".footer a.cancel",fade:false,verticalOffset:"center"});edit_list_textbox.val(edit_list_name).focus();return false;});$.live(".lightbox_edit_list input.txt",'keypress',function(e){if(e.which==13){el_lightbox.find(".buttons a.save").click();}});$.live(".lightbox_edit_list .buttons a.save",'click',function(){var edit_list_textbox=el_lightbox.find("input.txt");var edit_list_slug=el_lightbox.find("#edit_list_slug").val();var edit_list_link=$("#list_game_lists a.list[rel="+edit_list_slug+"]");el_err_handler.hide();if(edit_list_textbox.val()==="")
{el_err_handler.show("Please enter a name for the list.");}
else
{pf_ajax({url:"/"+current_user.username+"/games/xhr/list/"+edit_list_slug+"/rename",data:{name:edit_list_textbox.val()},error_handler:el_err_handler,success:function(data){el_lightbox.closePopup();edit_list_link.text(edit_list_textbox.val());if($("input[name=custom_list_slug]").length>0)
{var custom_list_slug=$("input[name=custom_list_slug]").eq(0).val();if(custom_list_slug==edit_list_slug)
{var custom_list_title=$("#pod_currently_playing .title h3");var custom_list_amount=custom_list_title.find("span").text();custom_list_title.html(edit_list_textbox.val()+" <span class='num'>"+custom_list_amount+"</span>");}}}});}
return false;});$.live(".games_list li.game","mouseenter",function(){$(this).find(".edit_remove").show();});$.live(".games_list li.game","mouseleave",function(){$(this).find(".edit_remove").hide();});$.live(".games_list .friends_bar a","click",function(){var content_type=$(this).hasClass("also_have")?"also_have":"also_rated";var friends_bar=$(this).parent().parent();var potential_friends_content=friends_bar.parent().parent().find('div.friends_content');if((potential_friends_content.hasClass("friends_also_have")&&content_type=="also_have")||(potential_friends_content.hasClass("friends_also_rated")&&content_type=="also_rated"))
{potential_friends_content.remove();return false;}
var li=$(this).parents('li.game');var game_id=/\bgame_id_(\d+)\b/.exec(li.attr('class'))[1];$.ajax({url:"/"+current_user.username+"/games/xhr/friends/"+content_type+"/"+game_id,data:{},type:'get',dataType:'json',success:function(data){friends_bar.parent().parent().find(".friends_content").remove();friends_bar.parent().parent().append(data.content);textarea_jiggery_pokery();},error:function(xhr,textStatus,errorThrown){}});return false;});$.live('.games_list a.next',"click",function(){var link=this;var page=$(this).attr('id').split('-')[1];var game_id=$(this).get_parent_object_id('game_id_');var content_type=$(this).hasClass("also_have")?"also_have":"also_rated";$.ajax({url:"/"+current_user.username+"/games/xhr/friends/"+content_type+"/"+game_id,data:{'page':page},type:'get',dataType:'json',success:function(data){$(link).parent().after(data.content).remove();textarea_jiggery_pokery();}});return false;});var console_pod=$("#pod_what_consoles");if(console_pod.length){var err_opts={context:console_pod.find(".title .title_inner"),margin:"18px 0 0"};var err_trans_add=ajax_error_translator({'redundant':'You have already added this console.','GENERIC':"There was a problem adding this console to the list."});var console_add_err_handler=error_handler.generic_ajax(err_opts,err_trans_add);var selected_platform=$("#ddl_platform");var repopulate_platforms=function(select_data){selected_platform.empty(select_data);for(var i=0;i<select_data.length;i++)
{var row=select_data[i];if(!row)
{selected_platform.append("<option disabled='disabled'>----------</option>");}
else
{selected_platform.append("<option value='"+row[1]+"'>"+row[0]+"</option>");}}};var change_num_consoles=function(amount)
{var num_consoles=parseInt($("span.num_consoles").eq(0).text(),10);$("span.num_consoles").each(function(){$(this).text(num_consoles+amount);});};$("#pod_what_consoles .add_platform .btn_add").click(ajax_action("/"+current_user.username+"/games/xhr/list/platforms/add",{data:function(){return"slug="+selected_platform.val();},error_handler:console_add_err_handler,confirm:function(){return(selected_platform.val()!==""&&selected_platform.val()!="disabled");},success:function(data,textStatus){change_num_consoles(1);$(".consoles_list ul.consoles").append(data.console_html_li);repopulate_platforms(data.new_console_select);}}));var err_trans_rm=ajax_error_translator({'GENERIC':"There was a problem removing this console from the list."});var console_rm_err_handler=error_handler.generic_ajax(err_opts,err_trans_rm);$.live(".consoles_list .consoles a.remove","click",ajax_action("/"+current_user.username+"/games/xhr/list/platforms/remove",{data:function(el){return"slug="+$(el).attr("rel");},error_handler:console_rm_err_handler,confirm:null,success:function(data,textStatus,el){var console_li=$(el).parent();console_li.fadeOut(500,function(){$(el).remove();});repopulate_platforms(data.new_console_select);change_num_consoles(-1);}}));}
$.live(".rating_comment_wrapper .not_rated a","click",function(){var gameplatform_id=$(this).get_parent_object_id('gameplatform_id_');$(this).parent().attr({className:"add_rating_comment just_add_rating"}).text("Add a rating:").after('<div class="game_list_add_rating"><span class="rating_stars rating-5-stars" name="rating_gp_'+gameplatform_id+'"><span></span></span><input type="hidden" value="" name="rating_gp_'+gameplatform_id+'"><input height="21" width="53" type="image" alt="Submit" src="http://b.pfcdn.net/static/images/buttons/submit.png?b3a6f1" name="submit" style="margin-left: 15px"></div>');return false;});$.live(".comment .add_comment a","click",function(){$(this).parent().after('<div class="comment_textarea_wrapper"><p>Add a rating and a comment to this game:</p><textarea rows="5" cols="20"></textarea></div><input class="rating_comment_submit" height="21" width="53" type="image" alt="Submit" src="http://b.pfcdn.net/static/images/buttons/submit.png?b3a6f1" name="submit">').remove();return false;});var action=window.location.hash.substring(1);if(action==='create_new_list'){$("#create_another_list a").click();}});