var url_county;
var fix = false;

var counties = [
  {'top' : '66',  'left' : '173', 'name' : 'Budapest'},
  {'top' : '55',  'left' : '55',  'name' : 'Győr-Moson-Sopron megye'},
  {'top' : '95',  'left' : '28',  'name' : 'Vas megye'},
  {'top' : '142', 'left' : '33',  'name' : 'Zala megye'},
  {'top' : '55',  'left' : '120', 'name' : 'Komárom-Esztergom megye'},
  {'top' : '105', 'left' : '78',  'name' : 'Veszprém megye'},
  {'top' : '172', 'left' : '76',  'name' : 'Somogy megye'},
  {'top' : '100', 'left' : '133', 'name' : 'Fejér megye'},
  {'top' : '158', 'left' : '132', 'name' : 'Tolna megye'},
  {'top' : '200', 'left' : '115', 'name' : 'Baranya megye'},
  {'top' : '81',  'left' : '187', 'name' : 'Pest megye'},
  {'top' : '160', 'left' : '187', 'name' : 'Bács-Kiskun megye'},
  {'top' : '21',  'left' : '195', 'name' : 'Nógrád megye'},
  {'top' : '33',  'left' : '235', 'name' : 'Heves megye'},
  {'top' : '95',  'left' : '250', 'name' : 'Jász-Nagykun-Szolnok megye'},
  {'top' : '163', 'left' : '243', 'name' : 'Csongrád megye'},
  {'top' : '0',   'left' : '273', 'name' : 'Borsod-Abaúj-Zemplén megye'},
  {'top' : '57',  'left' : '310', 'name' : 'Hajdú-Bihar megye'},
  {'top' : '130', 'left' : '290', 'name' : 'Békés megye'},
  {'top' : '14',  'left' : '347', 'name' : 'Szabolcs-Szatmár-Bereg megye'}
]

var agent = {
  areaId: null,
  setSelectCountyId: function(id) {
    $('#searchadvisor_noflash select option:selected').attr('selected', false);
    $('#searchadvisor_noflash .jqTransformSelectWrapper li a.selected').removeClass('selected');
    var index = 0;
    $('#searchadvisor_noflash select option').each(function() {
      index += 1;
      if ($(this).val() == id) {
        $(this).attr('selected', 'selected');
        $('.jqTransformSelectWrapper span').html($(this).html());
        $($('#searchadvisor_noflash .jqTransformSelectWrapper li a')[index-1]).addClass('selected');
      }
    });
  },
  chooseSelectCountyId: function() {
    var selectedId = $('#searchadvisor_noflash select option:selected').val();
    agent.setSelectCountyId(selectedId);
    if (selectedId == '') {
      location.href = url_county;
    }
    else {
      location.href = url_county + '?area=' + selectedId;
    }
  },
  sendCountyId: function(id) {
    agent.areaId = id;
    agent.updateAjax(url_county + '?area=' + id);
  },
  updateAjax: function(url) {
    $.ajax({
        dataType: 'json',
        url:      url,
        context:  document.body,
        cache:    false,
        success:  agent.update
    })
  },
  update: function(data) {
    if (data.type == 1) {
      $('#agents_wrap').html(data.template);
      agent.setSelectCountyId(agent.areaId);
    }
    else if (data.type == 0) {
      
    }
  },
  mapMain: function(){
    $('<div class="tooltip"><span><span>&nbsp;</span></span></div>').prependTo('#map #mask');
    $('<div class="tooltip_fix invisible"><span><span>&nbsp;</span></span></div>').prependTo('#map #mask');
    $('#mask .tooltip>span').css({opacity: 0});
    var timer;
    $('#map')
      .mousemove(function(event){
        $('#map .tooltip')
          .stop(true)
          .animate({
            left: event.pageX - parseInt(($(document).width()-960)/2) - 290,
            top: event.pageY - 285
          });
        if (!fix){
          $('#map .tooltip_fix')
            .stop(true)
            .animate({
              left: event.pageX - parseInt(($(document).width()-960)/2) - 290,
              top: event.pageY - 285
            });
          }
      });
    $('#map #mask area')
      .mouseover(function(event){
        window.clearTimeout(timer);
        $('#map #mask')
          .removeClass()
          .addClass($(event.target).attr('id'));
        $('#map .tooltip>span>span').text($(event.target).attr('alt'));
        if (!fix){
          $('#map .tooltip_fix>span>span').text($(event.target).attr('alt'));
        }
        $('#map .tooltip>span').stop(true).animate({opacity: 1});
      })
      .mouseleave(function(event){
        timer = window.setTimeout(function(){
          $('#map #mask').removeClass();
          $('#map .tooltip>span').stop(true).animate({opacity: 0});
        }, 10);
      })
      .click(function(event){
        if(fix){
          $('#map .tooltip_fix').css('left', event.pageX - parseInt(($(document).width()-960)/2) - 290).css('top', event.pageY - 285);
          $('#map .tooltip_fix>span>span').text($(event.target).attr('alt'));
          $('#map .tooltip_fix').stop(true);
        }
        else{
          $('#map').mousemove(function(event){
            $('#map .tooltip_fix').stop(true);
            $('#map .tooltip_fix').removeClass('invisible');
          });
        }
        $('#storage').removeClass();
        $('#storage').addClass($(event.target).attr('id'));

        agent.sendCountyId($(event.target).attr('id').replace('county_', ''));
        fix = true;
      });
  },
  mapMini: function(){
    var timer;
    $('#map_mini')
      .mouseleave(function(event){
      });
    $('#map_mini #mask area')
      .mouseover(function(event){
        window.clearTimeout(timer);
        $('#map_mini #mask')
          .removeClass()
          .addClass($(event.target).attr('id'));
        $('#county_name').text($(event.target).attr('alt'));
      })
      .mouseleave(function(event){
        $('#county_name').text('');
        timer = window.setTimeout(function(){
          $('#map_mini #mask').removeClass();
        }, 100);
      })
      .click(function(event){
        location.href = url_county + '?area=' + $(event.target).attr('id').replace('county_', '');
      });
  }
}


$(document).ready(function(){
  if ($('#map_main').length) agent.mapMain();
  if ($('#map_mini').length) agent.mapMini();
  if ($('#county_area').length){
    var temp_county_id = $('#county_area option:selected').val();
    if (temp_county_id >= 0){
      var temp_county = counties[temp_county_id];

      if (temp_county){
        $('.tooltip_fix')
          .removeClass('invisible')
          .css('top',temp_county.top + 'px')
          .css('left',temp_county.left + 'px');
        $('.tooltip_fix>span>span').text(temp_county.name);
        $('#storage').addClass('county_' + temp_county_id);
        fix = true;
      }
    }
  }
});
