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


function EditContactDialogView(scope){
    BaseView.call(this);
    var _this=this;
    var _callback=null;
    var _field=null;
    var _model=null;
    var _element=null;
    var _view=null;

    //  Override tag_box methods
   

    /**
     * Show the model
     * @param model
     */
    this.show=function(model){
        _view=$.extend(model,scope.general_types_model.get_types());
        _this.render_template_with_view('contacts/new_contact',model,{
            render_partial:function(name,model){
                var tmp="";
                //_mod=$.extend(model,_model);
                _this.render_template_with_view(name,model,_view,function(f){
                    tmp=f;
                });
                return tmp;
            }
        },
            function(rendered_template){
                var t=$(rendered_template)
                //_element=t;
                model.element.html(t);
                show_template(t,model);
            }
        );

    };


    function encapsulate_field(options){
        options.bind_events(options);

        //  Generic remove for every field
        $('a',options.field).bind('click',function(e){
            options.on_remove_clicked(options);
        });
    };

    function build_field_bindings(options){
        var i=0;
        while(options.iterator.hasNext()){
            encapsulate_field($.extend({
                field:options.fields[i]
                ,object:options.iterator.next()
            },options.handlers));

            i++;
        };

        options.add_action.bind('click',function(ee){
            _this.raise_event(options.controller_add_message,{
                show:function(model){
                    _this.render_template_with_view(options.field_template,model.value(),_view,function(f){
                        var f=$(f).hide();options.field_container.append(f);f.fadeIn();
                        encapsulate_field($.extend({
                            field:f
                            ,object:model
                        },options.handlers));
                    });
                }
            });
            return false;
        });
    };


    function show_template(e,model){
        //  Item hover
        $('dd ',e).bind('mouseover',function(){
            $(this).addClass('hover');
        });

        $('dd ',e).bind('mouseout',function(){
            $(this).removeClass('hover');
        });

        $("input[name='firstName']",e).bind('keyup',function(f){
            model.contact.firstName=$(this).val();
        });

        $("input[name='lastName']",e).bind('keyup',function(f){
            model.contact.lastName=$(this).val();
        });

        /**
         * Bind email address fields
         */
        build_field_bindings({
            iterator:model.email_addresses.iterator()

            ,add_action:$('#add_email_address',e)
            ,fields:$('#email_addresses .email_field',e)
            ,field_template:"email_field"
            ,field_container:$('#email_addresses',e)

            ,controller_add_message:'on_add_email_field_clicked'

            ,handlers:{
                bind_events:function(options){
                    $("input[name='email']",options.field).bind('keyup',function(eee){
                        options.object.value().email = $(this).val();
                    });

                    $("select[name='type']",options.field).bind('change',function(eee){
                        options.object.value().type = scope.general_types_model.get_types().emailTypes.emailType[$(this).val()];
                    });
                }
                ,on_remove_clicked:function(options){
                    _this.raise_event('on_remove_email_clicked',{email:options.object,remove:function(e){
                        $(options.field).fadeOut(250,function(){$(this).remove();});
                    }});
                }
            }
        });

        /**
         * Bind phone number fields
         */
        build_field_bindings({
            iterator:model.telephones.iterator()

            ,add_action:$('#add_telephone_number',e)
            ,fields:$('#telephone_numbers .phone_field',e)
            ,field_template:"telephone_field"
            ,field_container:$('#telephone_numbers',e)

            ,controller_add_message:'on_add_phone_field_clicked'

            ,handlers:{
                bind_events:function(options){
                    $("input[name='number']",options.field).bind('keyup',function(eee){
                        options.object.value().number = $(this).val();
                    });

                    $("select[name='type']",options.field).bind('change',function(eee){
                        options.object.value().type = scope.general_types_model.get_types().telephoneTypes.telephoneType[$(this).val()];
                    });
                }
                ,on_remove_clicked:function(options){
                    _this.raise_event('on_remove_phone_clicked',{phone:options.object,remove:function(e){
                        $(options.field).fadeOut(250,function(){$(this).remove();});
                    }});
                }
            }
        });

        /**
         * Bind address fields
         */
        build_field_bindings({
            iterator:model.addresses.iterator()

            ,add_action:$('#add_address',e)
            ,fields:$('#addresses .address-field',e)
            ,field_template:"address_field"
            ,field_container:$('#addresses',e)

            ,controller_add_message:'on_add_address_field_clicked'

            ,handlers:{
                bind_events:function(options){
                    $("input[name='street']",options.field).bind('keyup',function(eee){
                        options.object.value().street = $(this).val();
                    });
                    $("input[name='city']",options.field).bind('keyup',function(eee){
                        options.object.value().city = $(this).val();
                    });
                    $("input[name='state']",options.field).bind('keyup',function(eee){
                        options.object.value().state = $(this).val();
                    });
                    $("input[name='zip']",options.field).bind('keyup',function(eee){
                        options.object.value().zip = $(this).val();
                    });
                    $("input[name='country']",options.field).bind('keyup',function(eee){
                        options.object.value().country = $(this).val();
                    });

                    $("select[name='type']",options.field).bind('change',function(eee){
                        options.object.value().type = scope.general_types_model.get_types().addressTypes.addressType[$(this).val()];
                    });

                }
                ,on_remove_clicked:function(options){
                    _this.raise_event('on_remove_address_clicked',{address:options.object,remove:function(e){
                        $(options.field).fadeOut(250,function(){$(this).remove();});
                    }});
                }
            }
        });


        /**
         * Bind website fields
         */
        build_field_bindings({
            iterator:model.websites.iterator()

            ,add_action:$('#add_website',e)
            ,fields:$('#websites .website_field',e)
            ,field_template:"website_field"
            ,field_container:$('#websites',e)

            ,controller_add_message:'on_add_website_field_clicked'

            ,handlers:{
                bind_events:function(options){
                    $("input[name='url']",options.field).bind('keyup',function(eee){
                        options.object.value().url = $(this).val();
                    });

                    $("select[name='type']",options.field).bind('change',function(eee){
                        options.object.value().type = scope.general_types_model.get_types().websiteTypes.websiteType[$(this).val()];
                    });
                }
                ,on_remove_clicked:function(options){
                    _this.raise_event('on_remove_website_clicked',{website:options.object,remove:function(e){
                        $(options.field).fadeOut(250,function(){$(this).remove();});
                    }});
                }
            }
        });
    };
};