Container = function( el ){
  var
    me = {
      el:el,
      win: $( window )
    }
  return me
}

Panel = function( el, params ){
  var me = Container( el ),
    //dom elements
    general_block = el.children( '.general_block' ),
    block = general_block.children( '.block' ),
    block = general_block.children( '.block' ),
    block_head = block.children( '.block_head' ),
    from = params.from,
    to = params.to,
    head = el.children( '.head' ),
    show_hide_panel = head.children( '.show_hide_panel' ),
    show_hide_block = block_head.children( '.show_hide_block' ),
    controls = block.children( '.controls' ),
    max_height = null,
    //--

    bottom = me.win.height() - from.offset().top,
    calc_max_heigth = function(){
      max_height = me.win.height() - ( to.offset().top + to.innerHeight() + bottom + head.innerHeight() ) 
      general_block.css( { 'max-height': max_height } )
    }

    //GLOBAL
    me.show_block = function( c ){
      var em = general_block.children( '.block.' + c )
        em.removeClass('hidden')
        em.show()
    }
    me.hide_block = function( c ){
      var em = general_block.children( '.block.' + c )
        em.addClass('hidden')
        em.hide()
    }
    me.head_by_class = function( c ){
      return general_block.children( '.block.' + c ).children( '.block_head' )
    }
    me.checkbox_by_block_class = function( c ){
      return me.head_by_class( c ).children( '.name' ).children( '.tile_layer_visible_checkbox' )
    }
    me.is_all_block_hidden = function(){
      return block.filter( '.hidden' ).length == block.length
    }
    me.init = function(){
      calc_max_heigth()
      el.css( { visibility: 'visible', display: 'none' } )
      el.fadeIn(3000)
    }

    me.general_block = function(){ return general_block }
    me.block_head = function(){ return block_head }
    me.controls = function(){ return controls }
    me.from = function(){ return from }
    me.bottom = function(){ return bottom }
    //--

    //bindings


  show_hide_panel.toggle(function()
    {
      var img = $( this ).children()
      img.attr( 'src', '/images/maximize_gray.png')
      general_block.hide()
    },
    function()
    {
      var img = $( this ).children()
      img.attr( 'src', '/images/minimize_gray.png')
      general_block.show()
    }
  )

  show_hide_block.toggle(
      function()
      {
        var el = $( this ).parent().next( '.controls' ),
          img = $( this ).children()
        img.attr( 'src', '/images/maximize_gray.png')
        el.hide()
      },
      function()
      {
      var el = $( this ).parent().next( '.controls' ),
          img = $( this ).children()
        img.attr( 'src', '/images/minimize_gray.png')
        el.show()
      }
    )

  me.win.bind( 'resize', calc_max_heigth )
  //--

  //onload
  //--
  return me
}

ControllPanel = function( el, params ){
  var me = Panel( el, params ),

    //dom elements
    visible_checkbox = me.block_head().children( '.name' ).children( '.tile_layer_visible_checkbox' ),
    slider = me.controls().children( '.tile_layer_opacity_slider' ),
    move_to_project_center = me.controls().children( '.move_to_project_center' )
    //--

  //GLOBAL
  me.check_visible = function( c ){ 
    me.checkbox_by_block_class( c ).attr( 'checked', true ) }

  me.uncheck_visible = function(c ){ 
    me.checkbox_by_block_class( c ).attr( 'checked', false ) }
  //--

  //bindings
  slider.slider({
    value: 1,
    orientation: "horizontal",
    animate: true,
    min: 0.2,
    max: 1,
    step: 0.2,
    change: function(event, ui) {
        var em = $(this)
        if( navigator.appName == 'Microsoft Internet Explorer' ){
          $('#YMapsID .' + em.attr('rel') + '_tiles').children().children().css('filter', 'alpha(opacity=' + ui.value * 100 + ')')
        }else{
          $('#YMapsID .' + em.attr('rel') + '_tiles').css(
            {
              '-moz-opacity': ui.value,
              '-webkit-opacity': ui.value,
              opacity: ui.value
            }
          )
        }
      }
    })

    move_to_project_center.click( function(){
        var em = $( this ),
        c=em.attr( 'rel' ).split( ',' )
        map.panTo(new YMaps.Point(parseInt( c[ 0 ] ),parseInt( c[ 1 ] )));
        return false
    } )

    visible_checkbox.change( function(){
      var em = $( this )
      if(em.attr('checked')){
        map.addLayer(em.attr('rel') + '_tiles')
      }else{
        map.removeLayer(em.attr('rel') + '_tiles')
      }
    } )
  //--

  //onload
  var left = me.from().offset().left
  el.css( { left: left, bottom: me.bottom() } )
  me.check_visible( '.basic' )
  me.init()
  //--

  return me
}

SignPanel = function( el, params ){
  var me = Panel( el, params )
    //dom elements
    var empty_sign_panel = me.general_block().children( '.empty_sign_panel' ),
    show_tiles_side = empty_sign_panel.children( 'a' ),
    //--
    show_block = me.show_block,
    hide_block = me.hide_block
  //GLOBAL
  me.show_block = function( c ){
    show_block( c )
    if(!me.is_all_block_hidden())empty_sign_panel.hide()
  }
  me.hide_block = function( c ){
    hide_block( c )
    if(me.is_all_block_hidden())empty_sign_panel.show()
  }
  me.nav_to_tiles_side_bind = function( func ){
    show_tiles_side.click( func )
  }
  //
  //bind
  //--
  //onload
  var right = me.win.width() - ( me.from().offset().left + me.from().innerWidth() )
  el.css( { right: right, bottom: me.bottom() } )
  if(!me.is_all_block_hidden())empty_sign_panel.hide()
  me.init()
  //--
  return me
}

SearchResultPanel = function( el, params ){
  var me = Panel( el, params ),
  //onload
  left = me.to().offset().left,
  top = me.to().offset().top + me.to().innerHeight()
  el.css( { left: left, top: me.bottom() } )
  //--
  return me
}

Side = function( el, controll_panel, sign_panel ){

  var me = Container( el ),

    //dom elements
    limiter = el.children( '.limiter' ),
    moving_part = limiter.children(),
    hide_side = el.children( '.hide_side' ),
    move_left_control = el.children( '.move_left' ),
    move_right_control = el.children( '.move_right' ),
    first_el = moving_part.children().first(),
    last_el = moving_part.children().last(),
    checkbox = moving_part.children( '.collection' ).children('.layer_visible_checkbox'),
    selected_checkbox = moving_part.children( '.collection' ).children('.layer_visible_checkbox:checked'),
    //--

    ax = 0,
    sx = 0,
    adragable = false,
    full_width = 0,
    space = 0,
    min_offset_left = 0,
    max_offset_left = 0,
    dx = 0,
    right_on = false,
    left_on = false,
    step = 0,
    ie = 'Microsoft Internet Explorer',
    op = 'Opera',
    op = 'Safary',
    unsupport_browser = [ op, ie ],
    
  //functions
  calc = function(){
    dx = limiter.offset().left
    full_width = last_el.offset().left
      - first_el.offset().left
      + first_el.innerWidth()
    space = me.win.width()/3
    min_offset_left = -( full_width - 2*space ) - dx
    max_offset_left = space - dx
  },
  moving = function(){
    if( right_on || left_on  ){

      if( max_offset_left - min_offset_left < 0 ) return

      var
        left = parseInt( moving_part.css( 'left' ).split( 'px' )[ 0 ] ),
        max_speed = 6,
        ineria = 100,
        speed = max_speed/ineria*step

        if( isNaN( left ) ) left = 0
        if( step < ineria ) step ++
        if( right_on )
        {
          left += speed
        }else{
          left -= speed
        }


        if( left > min_offset_left && left < max_offset_left ){
          moving_part.css( {'left': left} )
          move_left_control.addClass( 'active' )
          move_right_control.addClass( 'active' )
          setTimeout(function(){moving(moving_part)},10)
        }

        if( left < min_offset_left ){
          moving_part.css( {'left': min_offset_left} )
          move_left_control.removeClass( 'active' )
        }

        if( left > max_offset_left ){
          moving_part.css( {'left': max_offset_left} )
          move_right_control.removeClass( 'active' )
        }
      }
    }
  //--

  //GLOBAL
  me.show = function(){
    el.show()
  if( $.inArray( navigator.appName, unsupport_browser ) == -1  ){
      calc()
    }
    me.is_showed = true

    if( max_offset_left - min_offset_left < 0 ) {
        move_left_control.removeClass( 'active' )
        move_right_control.removeClass( 'active' )
    }
  }
  me.hide = function(){
    el.hide()
    me.is_showed = false
  }
  me.nav_to_side_bind = function( func ){
    hide_side.click( func )
  }
  me.is_showed = false
  me.checkbox = function(){ return checkbox }
  me.selected_checkbox = function(){ return selected_checkbox }
  me.sign_panel = function(){ return sign_panel }
  //---

  //bindings
  checkbox.change( function()
      {
        var em = $( this )
        if( em.attr('checked') ){
          controll_panel.show_block( em.attr( 'rel' ) )
          controll_panel.check_visible( em.attr( 'rel' ) )
          sign_panel.show_block( em.attr( 'rel' ) )
        } else {
          controll_panel.hide_block( em.attr( 'rel' ) )
          controll_panel.uncheck_visible( em.attr( 'rel' ) )
          sign_panel.hide_block( em.attr( 'rel' ) )
        }
      }
  )
  if( $.inArray( navigator.appName, unsupport_browser ) == -1  ){
    el.bind('mousedown', function(){ return false } )
    limiter.bind('mousedown', function(e){

        if( max_offset_left - min_offset_left < 0 ) return

        adragable = true
        sx = e.clientX
        ax = parseInt( moving_part.css( 'left' ).split( 'px' )[ 0 ] )
        return false
    })


    limiter.bind('mousemove', function(e){
        if( adragable ){
          var cx = e.clientX,
            left = ax-sx+cx

          if( left > min_offset_left && left < max_offset_left ){
            moving_part.css( {'left': left} )
            move_left_control.addClass( 'active' )
            move_right_control.addClass( 'active' )
          }

          if( left < min_offset_left ){
            moving_part.css( {'left': min_offset_left} )
            move_left_control.removeClass( 'active' )
          }

          if( left > max_offset_left ){
            moving_part.css( {'left': max_offset_left} )
            move_right_control.removeClass( 'active' )
          }

        }

        return false
    })

    me.win.bind('mouseup', function(){
        adragable = false
        return false
    })

    move_right_control.bind( 'mouseover',
        function(){
          if( !right_on ){
            right_on = true
            step = 0
            moving()
          }
        }
    )

    move_right_control.bind( 'mouseout',
        function(){
          right_on = false
        }
    )

    move_left_control.bind( 'mouseover',
        function(){
          if( !left_on ){
            left_on = true
            step = 0
            moving()
          }
        }
    )

    move_left_control.bind( 'mouseout',
        function(){
          left_on = false
        }
    )

    me.win.bind( 'resize', calc )
  }


  //--
  //onload


  selected_checkbox.each( function(){
      var em = $( this )
      controll_panel.show_block( em.attr( 'rel' ) )
      controll_panel.check_visible( em.attr( 'rel' ) )
      sign_panel.show_block( em.attr( 'rel' ) )
  } )
  //--
  return me
}

 TorgSide = function( el, controll_panel, sign_panel ){
  var me = Side( el, controll_panel, sign_panel )
    //dom elements
    //--
    //bindings
    me.checkbox().change(function(){
      var em = $( this )
      if(em.attr('checked')){
        map.addLayer(em.attr('rel'))
        map.addLayer(em.attr('rel') + '_tiles')
      }else{
        map.removeLayer(em.attr('rel'))
        map.removeLayer(em.attr('rel') + '_tiles')
      }
    })
    //--
    //onload
    me.selected_checkbox().each( function(){
      var em = $( this )
      map.addLayer(em.attr('rel'))
      map.addLayer(em.attr('rel') + '_tiles')
    } )
    //--
  return me
}


MediaSide = function( el, controll_panel, sign_panel ){
  var me = Side( el, controll_panel, sign_panel )
    //dom elements
    //--
    //bindings
    me.checkbox().change(function(){
      var em = $( this )
      if(em.attr('checked')){
        map.addLayer(em.attr('rel'))
        map.addLayer(em.attr('rel') + '_tiles')
      }else{
        map.removeLayer(em.attr('rel'))
        map.removeLayer(em.attr('rel') + '_tiles')
      }
    })
    //--
    //onload
    me.selected_checkbox().each( function(){
      var em = $( this )
      map.addLayer(em.attr('rel'))
      map.addLayer(em.attr('rel') + '_tiles')
    } )
    //--
  return me
}



TilesSide = function( el, controll_panel, sign_panel ){
  var me = Side( el, controll_panel, sign_panel )
    //dom elements
    //--
    //bindings
    me.checkbox().change(function(){
      var em = $( this )
      if(em.attr('checked')){
        map.addLayer(em.attr('rel') + '_tiles')
      }else{
        map.removeLayer(em.attr('rel') + '_tiles')
      }
    })
    //--
    //onload
    me.selected_checkbox().each( function(){ 
      var em = $( this )
      map.addLayer(em.attr('rel') + '_tiles')
    } )
    //--
  return me
}

CompaniesSide = function( el, controll_panel, sign_panel ){
  var me = Side( el, controll_panel, sign_panel )
    //dom elements
    //--
    //bindings
    me.checkbox().change(function(){
      var em = $( this )
      if(em.attr('checked')){
        map.addLayer(em.attr('rel'))
        map.addLayer(em.attr('rel') + '_tiles')
      }else{
        map.removeLayer(em.attr('rel'))
        map.removeLayer(em.attr('rel') + '_tiles')
      }
    })
    //--
    //onload
    me.selected_checkbox().each( function(){ 
      var em = $( this )
      map.addLayer(em.attr('rel'))
      map.addLayer(em.attr('rel') + '_tiles')
    } )
    //--
  return me
}

Navigation = function( el, companies_side, tiles_side, torg_side, media_side ){
  var
    me = Container( el ),

    //dom elements
    form = el.children( '#lookup' ),
    show_hide_companies_side = form.children( '.show_hide_companies_side' ),
    show_hide_tiles_side = form.children( '.show_hide_tiles_side' ),
    show_hide_torg_side = el.children( '.show_hide_torg_side' ),
    show_hide_media_side = el.children( '.show_hide_media_side' ),
    search_input = form.children( '#lookup_query' ),
    //--

    //functions
    hide_all = function(){ 
      show_hide_companies_side.removeClass('blue_on')
      show_hide_tiles_side.removeClass('green_on')
      show_hide_torg_side.removeClass('green_on')
      show_hide_media_side.removeClass('green_on')
      companies_side.hide()
      torg_side.hide()
      media_side.hide()
      tiles_side.hide()
      return false
    },
    show_side = function(em, en, on_class){
      if(!em.is_showed){
        hide_all()
          en.addClass(on_class)
          em.show()
      }else{
        hide_all()
      }
      return false
    }
    //--

    //bindings
    show_hide_tiles_side.click(function(){
        return show_side(tiles_side, $( this ), 'green_on')
    })

    show_hide_torg_side.click(function(){
        return show_side(torg_side, $( this ), 'green_on')
    })

    show_hide_media_side.click(function(){
        return show_side(media_side, $( this ), 'green_on')
    })

    show_hide_companies_side.click(function(){
        return show_side(companies_side, $( this ), 'blue_on')
    })

    search_input.focus(function(){
        if($(this).val() == 'Улица Дом Предприятие'){
          $(this).val('')
          $(this).removeClass('empty_input')
        }
    })

    search_input.focusout(function(){
        if($(this).val() == ''){
          $(this).val('Улица Дом Предприятие')
          $(this).addClass('empty_input')
        }
    })

    tiles_side.sign_panel().nav_to_tiles_side_bind( function(){
        hide_all()
        show_hide_tiles_side.addClass('green_on')
        tiles_side.show()
        return false
    } )

    tiles_side.nav_to_side_bind( hide_all )
    companies_side.nav_to_side_bind( hide_all )
    torg_side.nav_to_side_bind( hide_all )
    media_side.nav_to_side_bind( hide_all )
    //--

    //onload
    if( search_input.val() == ''){
          search_input.val('Улица Дом Предприятие')
          search_input.addClass('empty_input')
    }

    if( search_input.val() != 'Улица Дом Предприятие'){
          search_input.removeClass('empty_input')
    }
    //--

}

