$(function(){$.manager_shared=function(pagePluralName,controller,preview,options){var permission=false;options.published=="NO";extArray=controller.split("/");if(pagePluralName=="Users"){extName="user";var addUrl="settings";}else{extName=extArray[0];var addUrl="add";}
permission=hasPermission(extName);if(permission){var defaults={user_id:$.session.user.id,shared:"YES",paginate:{perPage:10,overlap:1,selector:"#nav-updater"}}
var opts=$.extend(defaults,options);childController=$.controller.array(controller,opts);$.retrieveFilter=function(){if($("#filter-input").val()!=""){childController.conditions.filter=$("#filter-input").val();opts.filter=$("#filter-input").val();childController.retrieve();}else{delete opts.filter;delete childController.conditions.filter;childController.retrieve();}}
var timer;$("#filter-input").keyup(function(e){clearTimeout(timer);timer=setTimeout("$.retrieveFilter()",700);});$("#content-replace h1:first").prepend("<div>Sharing with Others</div>");$("a.show-entries").live("click",function(){if($(this).attr("id")=="shared_with_others"){delete childController.conditions["user_id!"];childController.conditions.user_id=$.session.user.id;$("#content-replace h1:first div").text("Sharing with Others");}else{delete childController.conditions.user_id;childController.conditions["user_id!"]=$.session.user.id;$("#content-replace h1:first div").text("Shared by Others");}
childController.conditions.offset=0;childController._page=1;$("#filter-input").val("");delete opts.filter;delete childController.conditions.filter;$("div.categories-link").removeClass("active");$(this).parent("div.categories-link").addClass("active");childController.retrieve();return false;});$(".ti_next").addClass("disabled").click(function(){return false;});$(".ti_prev").addClass("disabled").click(function(){return false;});$(".ti_item_start").text("0");$(".ti_item_end").text("0");$(childController).observe("contents",function(){$("#content-replace h1 span").text(childController.count+" "+pagePluralName);});var errStr="";$("div.manager-item div h2").format(function(elem,data){$(elem).text("#"+data.id);});$("div.manager-item-image img").format(function(elem,data){if((data.cover_pic!=null&&data.cover_pic!="NULL")||(data.thumbnail&&data.thumbnail!="")){if(data.cover_pic){var theImage="http://demo.firerift.com/upload/media/frt_"+data.cover_pic;}else if(data.thumbnail){var theImage=data.thumbnail;}
$(elem).hide().load(function(){$(this).imgCenter();}).attr("src",theImage);}else{$(elem).parent().hide();}});var arrayId='';$("div.manager-item div h1 a").format(function(elem,data){$(elem).attr("href","http://demo.firerift.com/app/"+extName+"/edit?id="+data.id);var text=data.title;if(text){if(text.length>24){text=text.slice(0,24)+"...";}}
$(elem).text(text);});var aTag=$(".manager-item-opts a").clone();$(".manager-item-opts").format(function(elem,data){var fn=$.formatDate("M j, Y | g:i A","created_at",true);fn(elem,data);$(elem).append($(aTag).clone());$(elem).children(".lb-preview").attr("href","http://demo.firerift.com/app/core/shared/preview"+preview+"?id="+data.id);$(elem).children(".lb-preview").fancybox({'zoomSpeedIn':300,'zoomSpeedOut':300,'frameWidth':700,'frameHeight':400,'overlayShow':true});if(childController.conditions.user_id==undefined){$(elem).children(".sharing").hide();$(elem).children(".delete_item").hide();}
$(elem).children(".sharing").attr("title","Stop Sharing "+data.title);$(elem).children(".sharing").click(function(event){$(elem).parent().parent().fadeOut(1000);$.ajax({url:"http://demo.firerift.com/api/"+controller+"?id="+data.id+"&shared=NO",type:"PUT",success:function(){childController.retrieve();},error:function(){errorAlert();}});return false;});var title;(pagePluralName=="Users")?title=data.user[0].name:title=data.title;$(elem).children(".delete_item").attr("title","Delete "+title);$(elem).children(".delete_item").click(function(event){$("div#dropdown-holder").html($("#delete_confirmation").html());$("div#dropdown-holder .btn-delete").click(function(event){childController.destroy(data.id);$("div#dropdown-holder").hide("slide",{direction:"up"},500);return false;});$("div#dropdown-holder").show("slide",{direction:"up"},500);return false;});});$(".cat_sort").template(childController,{success:function(){if($("#filter-input").val()!=""){$("div.handle").hide();}else{$("div.handle").show();}
$(".cat_sort").show();}});}}});
