
//version of controller for poster-enabled stuff (old one will be deleted eventually)
var PosterController = Class.create({
    name:'',
    controller_path:'',
    order_by:'id asc',
    search_text:'',
    $id:-1,
    search_timer:null,
    
		//for pagnation--the server ignores this for non-pagnated lists
		limit:50,
		offset:0,
		
		show_page:function(pg){
			this.offset = pg*this.limit;
			this.refresh_list();
		},
		
		
    initialize:function(name, controller_path){
        this.name = name;
        this.controller_path = controller_path;
    },
    
		//so we can clear the pagnation
		toggle_show_deleted:function(){
			//clear the pagnation on changing the criteria
			this.offset = 0;
			this.refresh_list();
		},
    //before_search:function(){return true;}
    //after_search:function(){}
    search_keyup:function(o){
			
       if(this.search_timer != null) clearTimeout(this.search_timer);
       o=$(o);
       this.search_text = $F(o).strip();
       if(this.before_search != null){
           if(!this.before_search()) return;
       }
			
			//clear the pagnation on searching
			this.offset = 0;
			
       this.search_timer = setTimeout(function(){
           this.refresh_list(function(){
               if(this.after_search != null){this.after_search();}
           }.bind(this));
       }.bind(this), 250);
    },
		
		search_blur:function(o){
			if($F(o)==''){
				$('recording_list_home_search').value = this.default_search_box_text;
			}
			
			if($F(o)==this.default_search_box_text){
				$('recording_list_home_search').addClassName('dim_search');
			}else{
				$('recording_list_home_search').removeClassName('dim_search');
			}
		},
		search_focus:function(o){
			if($F(o)==this.default_search_box_text){
				$('recording_list_home_search').value = '';
			}
			$('recording_list_home_search').removeClassName('dim_search');
		},
    
    //before_refresh_list:function(){return true;}
    //after_refresh_list:function(){}
    refresh_list:function(callback){
        //if the child object has a before_refresh_list, and it returns false, we do not refresh the list
        if(this != null && this.before_refresh_list != null){
            if(!this.before_refresh_list()) return;
        }
				
				//conditions for the server
				var conditions = {order:this.order_by, search:this.search_text, limit:this.limit, offset:this.offset};
				
				//pass along extra data if needed
				var extra_data = {};
				if(this._refresh_list_extra_data != null){
					extra_data = $H(this._refresh_list_extra_data());
					extra_data.each(function(pair){
						conditions[pair.key] = pair.value;
					});
				}
				
				Page.start_spinning(this.name + '_list');
				
        //actually refresh the list
        Request(this.controller_path + '/list', conditions, function(r){
            Notice.respond(r);
            Page.stop_spinning(this.name + '_list');

            $(this.name + '_list').update(r.html);
						
            if(this.after_refresh_list != null) this.after_refresh_list(r);//post list load actions
            if(callback != null) callback();//do whatever callback needed after. be sure it uses
                                            //self.foo, not this.foo, as this has broken binding.
        }.bind(this));
    },
    
    //before_sort:function(){return true;}
    //after_sort:function(){}
    sort:function(by){
        //add asc/desc checks
        if(this.order_by.gsub(' asc', '').gsub(' desc', '') == by){
					if(this.order_by.indexOf(' asc') > 0){
						this.order_by = by + ' desc';
					}else{
						this.order_by = by + ' asc';
					}
				}else{
					this.order_by = by + ' asc';
				}
        
        //if the child object has a before_refresh_list, and it returns false, we do not refresh the list
        if(this.before_sort != null){
            if(!this.before_sort()) return;
        }
        
        this.refresh_list(function(){
            if(self.after_sort != null){self.after_sort();}
        });
    },
		
		
		
		
		
		flash_inspector_ok:function(){
			Notice.flash_ok($('poster_'+this.name).select('.poster_content')[0]);
		},
		flash_inspector_error:function(){
			Notice.flash_error($('poster_'+this.name).select('.poster_content')[0]);
		},
		
		
		
		
		
		
		//start new
		//new one for new sheets
		//handles a click on a row
		inspect:function(o, id){
			//some callers require the ability to not inspect on certain types of clicks--handle this here.
			if(this.inspect_allow_open != null){
				var res = this.inspect_allow_open();
				if(!res) return;
			}
			this.$id = id;
			Poster.show.apply(this, [this.name]);
		},
		//not async safe in that the complete_ui_callback is called after the custom after_inspect returns.
		//clean up after inspect or do other actions
		//calls the optional after_inspect
		//must call complete_ui_callback at the end to
		//size the poster and show the buttons
		_after_inspect:function(inspect_r, complete_ui_callback){
			if(this.after_inspect != null) this.after_inspect(inspect_r);
			complete_ui_callback();
		},
		//loads the relevant html into the poster
		load_inspect:function(callback, the_content_area){
			
			var inspect_args = {};
			if(this.extra_inspect_args != null){
				inspect_args = this.extra_inspect_args();
			}

			inspect_args['id'] = this.$id;

			Request(this.controller_path+'/inspect', inspect_args, function(r){
				Notice.respond(r);
				Page.stop_spinning(the_content_area);
				if(r.ok){
					$(the_content_area).update(r.html);
					callback.apply(this, [true, r, this._after_inspect]);
				}else{
					$(the_content_area).update(r.msg);
					callback.apply(this, [false, r, this._after_inspect]);
				}
			}.bind(this));
		},
		//wrapper for mark_poster_deleted and unmark_poster_deleted
		mark_poster_status:function(deleted){
			if(deleted){
				this.mark_poster_deleted();
			}else{
				this.unmark_poster_deleted();
			}
		},
		mark_poster_deleted:function(){
			$(this.name+'_delete_button').hide();
			$(this.name+'_restore_button').show();
			$('poster_'+this.name).select('.poster_content')[0].addClassName('red_poster_content');
			var pci = $('poster_'+this.name).select('.poster_chrome_icon')[0];
			var src = pci.src;
			pci.src = src.replace('_deleted.png', '.png').replace('.png', '_deleted.png');
		},
		unmark_poster_deleted:function(){
			$(this.name+'_delete_button').show();
			$(this.name+'_restore_button').hide();
			$('poster_'+this.name).select('.poster_content')[0].removeClassName('red_poster_content');
			var pci = $('poster_'+this.name).select('.poster_chrome_icon')[0];
			var src = pci.src;
			pci.src = src.replace('_deleted.png', '.png');
		},
		cancel_inspect:function(){
			this.close_inspect();
		},
		close_inspect:function(){
			this.close_inspect_poster();
			this.$id = -1;
			if(this.after_close_inspect != null) this.after_close_inspect();
		},
		close_inspect_poster:function(){
			Poster.hide(this.name);
		},
		//get_editing_args:function(){} -- returns all values the server needs from the page. selected id is auto-added here
		//in the base class.
		
		
		
		
		
		
		save_changes:function(){
			var edit_args = this.get_editing_args();
			edit_args['id'] = this.$id;
			Request(this.controller_path+'/save_changes', edit_args, function(r){
				Notice.respond(r);
				if(r.ok){
					if(this.after_save != null){
						this.after_save(r);
					}else{
						this.close_inspect();
						this.refresh_list();
					}
				}else{
					//flash red
					this.flash_inspector_error();
				}
			}.bind(this));
		},
		start_delete:function(){
			
		},
    
		
		_after_start_new:function(inspect_r, complete_ui_callback){
			if(this.after_start_new != null) this.after_start_new(inspect_r);
			complete_ui_callback();
		},
		load_start_new:function(callback, the_content_area){
			
			var inspect_args = {};
			if(this.load_start_new_args != null){
				inspect_args = this.load_start_new_args();
			}
			Request(this.controller_path+'/new', inspect_args, function(r){
				Notice.respond(r);
				Page.stop_spinning(the_content_area);
				if(r.ok){
					$(the_content_area).update(r.html);
					callback.apply(this, [true, r, this._after_start_new]);
				}else{
					$(the_content_area).update(r.msg);
					callback.apply(this, [false, r, this._after_start_new]);
				}
			}.bind(this));
		}
		
    
});


































































