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

//	@import com.hedgehoglab.application.helper.BaseHelper;
//  @import com.hedgehoglab.solomon.model.ContactModel;

function ContactsListView(scope){
    BaseView.call(this);
    var _this=this;
    var _helpers=new _this.generate_helpers(this,[BaseHelper]);
	var all_checked=false;
	var _all_checked_clicked=false;
	var _checkboxes=null;
   	var _firsttime=true;

    function update(state){
		if(_all_checked_clicked!=true){
		    var _indices=$(".working-pane .scroll-pane dd input[checked='true']",scope.element);
			var _sle=[];
			$.each(_indices,function(i,n){
				_sle.push($(n).attr('rel'));
			});
		    
			//	THIS OBJECT SHOULD NOT! BE UPDATING THE MODEL!    
			//scope.selected_contacts_model.model(_sle);
			//	SOMETHING IS GOING ON HERE!!!!
		    _this.raise_event('on_selected_contacts_changed',{model:_sle});
		}
    };

	$('a#select-all-contacts-action',scope.element).bind('click',function(){
		_all_checked_clicked=true;
		// setTimeout(function(e){
			// _checkboxes.attr('checked',true);
			for(i=0;i<_checkboxes.length;i++){
				$(_checkboxes[i]).attr('checked',true);
			}
			// $('.working-pane .scroll-pane dd input',scope.element).trigger('click',[]);
			_all_checked_clicked=false;
			update();
		// },1);
        return false;
    });

    $('a#select-no-contacts-action',scope.element).bind('click',function(){
        // _checkboxes.attr('checked',false);
		for(i=0;i<_checkboxes.length;i++){
			$(_checkboxes[i]).removeAttr('checked');
		}
        update();
        return false;
    });

    scope.state_machine_model.subscribe_to('searching_for_contacts',function(e){
        $('.working-pane .scroll-pane',scope.element).html("");
        $(".working-pane .scroll-pane",scope.element).addClass('loading-pane');
    });

   

    scope.contacts_model.subscribe_to('model_removed',function(event){
        event.model=scope.contacts_model.model(event.model);
    });

    //  Subscribe to the contacts model changing to update the list
    scope.contacts_model.subscribe_to('model_changed',function(event){
		// alert("Model Changed")
		//	Models need to be sorted
		//alert("SORT MODEL")
        _list=new LinkedList();
        if(event.model==null){

            _this.render_template_with_view('contacts/NoContactsListView',{},_helpers,function(rendered_template){
                $('.working-pane .scroll-pane',scope.element).html(rendered_template);
                $(".working-pane .scroll-pane",scope.element).removeClass('loading-pane');
            });
        }else{
				var _selected={};
				var _id_selected=[];
				
				//	Loop through and grab the ID's of the models
				$.each(scope.selected_contacts_model.model(),function(i,n){
					_id_selected[i]=event.model.contact[n].id;
				});
				
				var __model=event.model.contact.sort(function(a,b){
					var name_a=$.trim(String(a.lastName+" "+a.firstName).toLowerCase());
					var name_b=$.trim(String(b.lastName+" "+b.firstName).toLowerCase());
					
					return (name_a>name_b)?1:-1;
				});
				
				//	Get the indexes of the new order!
				var _new_indexes={};
				$.each(__model,function(i,n){
					_new_indexes[n.id]=i;
				});
				
				//	Now work out the new index 
				$.each(scope.selected_contacts_model.model(),function(i,n){
					var id=_id_selected[i];
					
					_selected[_new_indexes[id]]=true;
				});
				
				var _model={model:{contact:__model},selected:_selected};

                _this.render_template_with_view('contacts/ContactsListView',_model,_helpers,function(rendered_template){
                    $('.working-pane .scroll-pane',scope.element).html(rendered_template).show();
                    

					_checkboxes=$('.working-pane .scroll-pane dd input',scope.element);
					
					           

					$('.working-pane .scroll-pane dd',scope.element).hover(function(e){
						$(this).addClass('hover');						
					},function(e){
						$(this).removeClass('hover');						
					});
					
                    //   bind events when contacts in the contacts list are clicked
                    $('.working-pane .scroll-pane dd a',scope.element).bind('click',function(){

                        $('dd input',scope.element).removeAttr('checked');
						$('dd ',scope.element).removeClass('checked');
						$(this).parent().addClass('checked');
						$('input',$(this).parent()).attr('checked',true);
						update();
                        // $('input',$(this).parent()).trigger('click',[]);
                        return false;
                    });

                    //   bind events when contacts in the contacts list are checked
                    _checkboxes.bind('click',function(e){
						$(this).parent().addClass('checked');
						if(_all_checked_clicked==false) update();
					});
                    
                    // if(event.model && event.model.contact){
                    //                         if(event.model.contact.length==1){
                    //                             _checkboxes.trigger('click',{});
                    //                         }
                    //                     }
					
                });
			
			$(".working-pane .scroll-pane",scope.element).removeClass('loading-pane');
			


			
			if(_firsttime){
				_this.raise_event('first_time_loaded',{
					update:function(id){
						setTimeout(function(e){
							$(".working-pane .scroll-pane dd a[href='#"+id+"']",scope.element).trigger('click',[]);			
						},1);
					}
				});
			}
			_firsttime=false;
        };
    });

	this.set_selected=function(id){
		// alert(id);
		// var a=$("a[href='#"+id+"']",scope.element);

		// setTimeout(function(e){
		

	};
};


