//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,
    $inspect_lock:false,
		
		inspect_allow_open:function(){
			return this.$inspect_lock==false;
		},
		lock_inspector:function(ms){
			if(ms == null) ms = 250;
			this.$inspect_lock = true;
			setTimeout(function(){
				this.$inspect_lock = false;
			}.bind(this), ms);
		},
		
		//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);
       stx = $F(o).strip();
			
			if(this.search_text == stx){
				return;
			}else{
				this.search_text = stx;
			}
			
			
       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};
				
				//add in optional criteria
				if($(this.name+'_list_show_deleted') != null){
					conditions['show_deleted'] = $(this.name+'_list_show_deleted').checked;
				}
				if($(this.name+'_list_search') != null){
					conditions['search'] = $F(this.name+'_list_search');
					if(conditions['search']=='') conditions['search'] = null;
				}
				
				//pass along extra data if needed, and override any default items (like the two added above)
				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){
            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(){
			if($('poster_'+this.name) != null){
				Notice.flash_error($('poster_'+this.name).select('.poster_content')[0]);
			}
		},
		
		
		
		warn:function(msg, tgt){
			Warn(msg);
			if(tgt==undefined || tgt==null){
				tgt = this.$name;
			}
			Poster.flash_error(tgt);
		},
		
		
		
		
		//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, target_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){
				Page.stop_spinning(target_area);
				if(r.ok){
					$(target_area).update(r.html);
					callback.apply(this, [true, r, this._after_inspect]);
				}else{
					$(target_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');
		},
		set_poster_icon:function(icon){
			var pci = $('poster_'+this.name).select('.poster_chrome_icon')[0].src = icon;
		},
		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.
		
		
		
		//start_add_new_args
		start_add:function(){
			Poster.show('new_'+this.name);
		},
		cancel_add:function(){
			Poster.hide('new_'+this.name);
		},
		load_add_new:function(callback, target_area){
			var args = {};
			if(this.start_add_new_args != null) args = this.start_add_new_args();
			
			Request(this.controller_path+'/inspect_add_new', args, function(r){
				Page.stop_spinning(target_area);
				if(r.ok){
					$(target_area).update(r.html);
					callback.apply(this, [true, r, this._after_add_new]);
				}else{
					$(target_area).update(r.msg);
					callback.apply(this, [false, r, this._after_add_new]);
				}
			}.bind(this));
		},
		_after_add_new:function(r, complete_ui_callback){
			if(this.after_add_new != null) this.after_add_new(r);
			complete_ui_callback();
		},
		after_save_new:function(){
			
		},
		before_save_new:function(){
			return true;
		},
		
		extra_save_new_args:function(){return null;},
		get_save_new_args:function(){
			var iargs = {};

			$w(this.save_new_args_string).each(function(t){
				try{
					iargs[t.toString()] = $F('new_'+this.name+'_'+t);
				}catch(ex){
					alert('Could not find ' + 'new_'+this.name+'_'+t);
				}
			}.bind(this));
			
			var extras = this.extra_save_new_args();
			
			if(extras!=null){
				Object.keys(extras).each(function(k){
					iargs[k] = extras[k];
				});
			}

			return iargs;
		},
		save_new:function(){
			if(this.before_save_new()===false){
				return;
			}
			var new_args = this.get_save_new_args();
			Request(this.controller_path+'/save_new', new_args, function(r){
				if(r.ok){
					Poster.hide('new_'+this.name);
					this.after_save_new();
					this.refresh_list();
				}else{
					//flash red
					Notice.flash_error($('poster_new_'+this.name).select('.poster_content')[0]);
				}
			}.bind(this));
		},
		
		
		
		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){
				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, target_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){
				Page.stop_spinning(target_area);
				if(r.ok){
					$(target_area).update(r.html);
					callback.apply(this, [true, r, this._after_start_new]);
				}else{
					$(target_area).update(r.msg);
					callback.apply(this, [false, r, this._after_start_new]);
				}
			}.bind(this));
		}
		
    
});


































































