//  @import com.hedgehoglab.application.view.BaseView;


function NewTaskView(scope){
    BaseView.call(this);
    var _this=this;

    var _users_model_ready = false;
    var _task_type_model_ready = false;
    var _contacts_model_ready = false;
    var _users_model;
    var _task_type_model;
    var _contacts_model;
    var _obj=null;
    var _visible=false;

    scope.users_model.subscribe_to('model_changed',function(e){
    	_users_model_ready = true;
    	_users_model = e;
    	_this.render_view(_obj);
    });

    scope.task_type_model.subscribe_to('model_changed',function(e){
    	_task_type_model_ready = true;
    	_task_type_model = e;
    	_this.render_view(_obj);
    });

    scope.contacts_model.subscribe_to('model_changed',function(e){
    	_contacts_model_ready = true;
    	_contacts_model = e;
    	_this.render_view(_obj);
    });

//    _this.show=function(){
//       $(scope.element).show();
//    };

    _this.hide=function(){
        $(scope.element).hide();
    };
    
    _this.render_view=function(obj,callback){
        var everything_ready = _users_model_ready  && _task_type_model_ready && _contacts_model_ready;
    	if(everything_ready && obj!=null) {
            _obj=obj.task;
            _this.render_template_with_view('tasks/new-task-view',{
                task:_obj,
                current_user:obj.current_user,
                contacts:_contacts_model,
                users:_users_model,
                task_types:_task_type_model
            },{},function(rendered_template){
                $(scope.element).html(rendered_template);
                $(scope.element).hide().removeClass('hide')

                $('.date-pick',scope.element).datepicker({clickInput:true,constrainInput:true,dateFormat:'D M dd yy'})


                //  Set field defaults!
                _obj.assignedTo=$("select[name='assignedTo']").val();
                _obj.contact=$("select[name='contact']").val();
                _obj.type=$("select[name='type']").val();
                obj.dueOn=Date.today().toString('D M dd yy');
                $("input[name='dueOn']").val(obj.dueOn);
                
                //  Hook onto title field
                $("input[name='title']").bind('change',function(e){_obj.title = $(this).val();});

                //  Hook onto due on field
                $("input[name='dueOn']").bind('change',function(e){_obj.dueOn = $(this).val();});

                //  Hook onto contact field
                $("select[name='contact']").bind('change',function(e){_obj.contact = $(this).val();});

                //  Hook onto addignedTo field
                $("select[name='assignedTo']").bind('change',function(e){_obj.assignedTo = $(this).val();});

                //  Hook onto type field
                $("select[name='type']").bind('change',function(e){_obj.type = $(this).val();});

                //  Hook onto save button
                $('.edit-task-save',scope.element).bind('click',function(e){
                    _this.raise_event('on_task_save',{
                        task:_obj,
                        event:{
                            hide:function(e){scope.element.slideUp(0);_visible=false;}
                        }
                    });

                });

                //  Hook onto save button
                $('.edit-task-cancel',scope.element).bind('click',function(e){
                    _this.raise_event('on_task_cancel',{
                        task:_obj,
                        event:{
                            hide:function(e){scope.element.slideUp(0);_visible=false;}
                        }
                    });

                });

                $(scope.element).removeClass('ajaxLoading');
                $('.ajaxLoading',scope.element).removeClass('ajaxLoading');
                if(callback) callback({
                    show:function(e){scope.element.slideDown(0);_visible=true;}
                });
            });

    	};
    };
};