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

function TagFilterContactsViewHelper(scope){
    BaseView.call(this,scope);

    this.get_tag_groups=function(tags){
        if(tags!=null){
            if(!$.isArray(tags)){
                tags=[tags];
            }
        }

        var tag_groups=[];
        try{
        var sorted_tags=tags.sort();
        var _char="-";
        var _current_group=null;

        $.each(sorted_tags,function(i,n){
            var _char2=String(n.label).substring(0,1).toUpperCase();
            if(_char!=_char2){
                _char=_char2;
                _current_group={title:_char,tags:[]};
                tag_groups.push(_current_group);
            }
            _current_group.tags.push(n);
            
        });
        }catch(e){
          //  console.warn(e);
        };

        return tag_groups;
    };
};

function TagFilterContactsView(scope){
    BaseView.call(this,scope);
    var _this=this;
    var _view={};
    var _visible=false;
    var _model=null;

    TagFilterContactsViewHelper.call(_view);
    $("#filter_by_tags", scope.element).bind('click',function(e){
        if(!_visible){
            _visible=true;
            $(this).blur();
            _this.raise_event('on_tag_filter_clicked',{});
        }
        else{
            _visible=false;
            setTimeout(function(e){
                $('.floating-tag-container').fadeOut(150);
            },1);

        }
        return false;
    });


    /**
     * Enables filter by tags link
     */
    this.enable=function(){
        $("#filter_by_tags", scope.element).show();
    };

    /**
     * Disables the filter by tags link if there are no tags
     */
    this.disable=function(){
        $("#filter_by_tags", scope.element).hide();
        _visible=false;
            setTimeout(function(e){
                $('.floating-tag-container').fadeOut(150);
            },1);
    };

    this.update=function(model){
        if(model){
        _model=model;
        this.render_template_with_view('tags/tag-table',model,_view,function(ef){
              $('.tag-table',scope.element).html(ef);

              //    Apply behaviour to all links
              $('.tag-table a',scope.element).bind('click',function(e){
                  var h=$(this).attr('href').replace("#","");
                  var ids=String(h).split(":");
                  var gid=parseInt(ids[0]);
                  var tid=parseInt(ids[1]);

                  var tg=_view.get_tag_groups(_model.tags.tag);
                  
                  $(this).toggleClass('checked');
                  $(this).blur();
                  setTimeout(function(){
                      _this.raise_event('on_tag_clicked',tg[gid].tags[tid]);
                  },10);

                  return false;
              });

              //  Close the panel
              $(".tag-table #filter_by_tags_done", scope.element).bind('click', function(e){
                    _visible=false;
                    setTimeout(function(e){
                        $('.floating-tag-container').fadeOut(150);
                    },1);
              });
          });
      };


    };

    this.show=function(){
        setTimeout(function(e){
            $('.floating-tag-container').fadeIn(150);//({"height":"26em"});
        },1)
    };

     this.hide=function(){
        setTimeout(function(e){
            $('.floating-tag-container').fadeOut(150);//({"height":"26em"});
        },1)
    };

    _this.enable();
};
