function Lookup() {
  this.onAddToMap = function (my_map, position_control) {

    function update_balloon(){
      if(map.getBalloon()!=undefined){
        map.getBalloon().update()
        map.getBalloon().mapAutoPan()
      }
    }

    $('#lookup').ajaxForm({
      beforeSerialize: function(form, options) { 
        form.children('.empty_input').val('')
        $('#found').fadeOut()
      },
      success: function(data, success, form) { 
        form.children('.empty_input').focusout()
        $('#found_container').html(data)
        $('#close_foundes').css({top:$('nav').offset().top + $('nav').innerHeight() + 10})
        $('#open_foundes').css({top:$('nav').offset().top + $('nav').innerHeight() + 10})
        $('#foundes_count').css({top:$('nav').offset().top + $('nav').innerHeight() + 10})
        $('#nothing_found').css({top:$('nav').offset().top + $('nav').innerHeight() + 10})

        $('#found').css('max-height', ( $(window).height() - 150 ) + 'px')
        $('#found').css({top:$('nav').offset().top + $('nav').innerHeight() + 40})

        update_balloon()

        $('#found').fadeIn()

        $('#close_foundes').click(function(){
          $('#found').fadeOut()
          $('#open_foundes').show()
          $(this).hide()
          return false
        })
        $('#open_foundes').click(function(){
          $('#found').fadeIn()
          update_balloon()
          $('#close_foundes').show()
          $(this).hide()
          return false
        })
      }
    })

    $( window ).bind( 'resize', function(){
        $('#found').css('max-height', ( $(window).height() - 150 ) + 'px')
        $('#found').css({top:$('nav').offset().top + $('nav').innerHeight() + 40})
    } )

    $('.move_to_shape').live('click',function(){
        $.getScript('/poly_shapes/house/' + $(this).attr('rel') + '.js')
        return false
    })

    $('.move_to_shape_and_open_balloon').live('click',function(){

      rel = $(this).attr('rel').split(',')
      shape_id = parseInt(rel[0])
      branch_id = parseInt(rel[1])
      house_id = parseInt(rel[2])


      function select_branch(){
        SelectedObject.filter(function(obj){
          return obj.shape_id == shape_id
        })[0].openBalloon()
        $('.selected').removeClass('selected')
        $('#branch_' + branch_id).addClass('selected')
        $('#branch_' + branch_id).parent().children().hide()
        $('#branch_' + branch_id).show()
        show_all_branches_button = $('#branch_' + branch_id).parent().children('.show_all_branches')
        show_all_branches_button.show()
        show_all_branches_button.click(function(){
          $(this).parent().children().show()
          $(this).parent().children('.show_only_selected').show()
          $(this).hide()
          update_balloon()
          return false
        })
        show_only_selected_button = $('#branch_' + branch_id).parent().children('.show_only_selected')
        show_only_selected_button.hide()
        show_only_selected_button.click(function(){
          $(this).parent().children().hide()
          $(this).parent().children('.selected').show()
          $(this).parent().children('.show_all_branches').show()
          $(this).hide()
          update_balloon()
          return false
        })
      }

      if( SelectedObject.house_id != house_id ){
        $.getScript('/poly_shapes/house/' + house_id + '.js',function(){
          select_branch()
        })
      } else {
        select_branch()
      }


      return false

    })

  }

  this.onRemoveFromMap = function () {
  }
}

