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

//  @import com.hedgehoglab.solomon.helper.ContactViewHelper;
//  @import com.hedgehoglab.solomon.helper.TagHelper;
    
function ContactVEView(scope){
    BaseView.call(this,scope);
    var _this=this;

     var _helpers=new _this.generate_helpers(this,[TagHelper]);
     
     var model = null;

    this.setElement=function(element){
        scope.element=element;
    };

    this.render = function(datamodel,mode){
	
      if (mode==null||mode!="edit") mode = "view";
      model = datamodel;
      model=$.extend(model,scope.general_types_model.get_types());
      var t=scope.element;
      var flag=false;
      var s=null;

      //TODO check this stays here.
      _this.render_template_with_view('contacts/ContactCard',model,_helpers,function(f){
        $(scope.element).html(f);
        $('.add-tags',t).hide().removeClass('hide');
          
        $('a#link-contact-edit', scope.element).bind('click', function(e){
          _this.raise_event('on_edit_contact_clicked',{});
          return false;
        });
        
        $('a#link-contact-delete', scope.element).bind('click', function(e){

          _this.raise_event('on_delete_contact_clicked',{});
          return false;
        });

		
		$('a#link-contact-export',t).bind('click',function(e){
			if($(this).attr('href')!="#"){
				var url=$(this).attr('href').split("?");
				$.download(url[0],url[1],'post');
			}
			return false;
		})

        function re_render_tags(ee){
            _this.render_template_with_view('static_tags2',{tags:ee.tag},_helpers,function(f){
              $('.static-tags',t).html(f);
          });

          //  ren-render static tags
          _this.render_template_with_view('deletable_tags2',{tags:ee.tag},_helpers,function(f){
              $('.deletable-tags',t).html($(f).html());
          });

          bind_tag_actions();
        };

        function apply_tag_be(){
          $('.deletable-tags a',t).bind('click',function(eee){
              var index=$(this).attr('rel');

              _this.raise_event("on_remove_tag",{contact:model.contact,index:index,render:function(ee){

                  re_render_tags(ee);
              }});
              return false;
          });
        };

        bind_tag_actions();


        function bind_tag_actions(){
            $('.add-tags',t).removeClass('hide');

            $('.open-tag-edit',t).bind('click',function(e){
              $('#the_tags',t).hide();
              $('.add-tags',t).show();
              $("input[name='new_tag']",t).focus();
            });

            $('.close-tag-action',t).bind('click',function(e){
              $('#the_tags',t).show();
              $('.add-tags',t).hide();
            });

            s=function (){
              $('.open-tag-edit',t).bind('click',function(e){
                  $('#the_tags',t).hide();
                  $('.add-tags',t).show();
              });


            };

            //  Add autocomplete to tag field
            function load_data(options,callback){
                _this.raise_event('on_tag_term_changed',{
                    term:options.q,
                    update_tags:function(e){
                        callback(e);
                    }
                });
            };

            $("input[name='new_tag']",t).focus();
            
            $("input[name='new_tag']",scope.element).autocomplete(load_data,{
                cacheLength:1
            });


            $('.add-atag-action',t).bind('click',function(e){
              if(flag){
                  var tag=$("input[name='new_tag']").val();
                  _this.raise_event("on_add_tag",{contact:model.contact,tag:tag,render:function(ee){
                      re_render_tags(ee);
                      $("input[name='new_tag']",t).val("")
                      $("input[name='new_tag']",t).trigger('keyup');
                      $("input[name='new_tag']",t).focus();
                      flag=false;
                  }});
              }
                return false;
            });


            $("input[name='new_tag']",t).bind('keyup',function(e){
              //  Check if tag already exists
              if($(this).val()!=""){
                  flag=true;
                  $('.add-atag-action',t).attr('disabled',false);
              }else{
                  flag=false;
                  $('.add-atag-action',t).attr('disabled',true);
              }

              if (e.keyCode==13) {
                $('.add-atag-action',t).trigger('click');
                //$(this).focus();
              }

              if (e.keyCode==27) {
                $('.close-tag-action',t).trigger('click');
                //$(this).focus();
              }
            });

            $("input[name='new_tag']",t).trigger('keyup');

            apply_tag_be();

           };
      });
      
    };
    
   
};