//
// metro
//

var metros = new Array();

function metro_update_array(e){
  metros  = e;
  var metros_string = "";
  for(var i = 0; i < metros.length; i++){
    metros_string += metros[i] + ',';
  }
  $('#metro_select_num').html(metros.length);
  if ($('#id_metros').length){
    $('#id_metros').val(metros_string);
  } else {
    $('<input>').attr({
      type: 'hidden',
      id: 'id_metros',
      name: 'metros',
      value: metros_string
    }).appendTo('form');
  }
}

function metro_get_array(e){
  return metros;
}

$(document).ready(function(){
  if ($("#search_form_id").length && $("select#city_name").length){
    var city = $("select#city_name").val();
    display_metro(city);
  }
});

$(function(){

  $('#metro_confirm').click(function(){
    for(var i = 0; i < metros.length; i++){
      alert(metros[i]);
    }
    return false; 
  });

  $('#metro_select').click(function(){
    var city_id = $('#city_name').val().split('_');
    city_id = city_id[2];

    if (city_id != '2992300' && city_id != '101'){
      alert("message to replace: 'у данного города нет метро' ");
      return false;
    }

    var url = "metro_select.php" + "?city_id=" + city_id;
    var width  = 830;
    var height = 700;
    var left   = (screen.width  - width)/2;
    var top    = (screen.height - height)/2;
    var params = 'width='+width+', height='+height;
    params += ', top='+top+', left='+left;
    params += ', directories=no';
    params += ', location=no';
    params += ', menubar=no';
    params += ', resizable=no';
    params += ', scrollbars=yes';
    params += ', status=no';
    params += ', toolbar=no';
    newwin = window.open(url, 'metro', params);
      //if (window.focus) {newwin.focus()}
    return false; 
  });

});

var g_PopupIFrame;

function display_metro(city){
  metros = new Array();
  var city_obj_arr = city.split('_');
  var city_id = city_obj_arr[city_obj_arr.length-1];
  var show_metro = false;
  for (var i=0; i<cities_with_metro.length; i++) {
    if (city_id == cities_with_metro[i]) {
      show_metro = true;
      break;
    }
  }

  if (show_metro){
    if ($("#metro_tr").length){
      $("#testme").html("start");
      $.getJSON("/city_select.php",{form_action: 'METRO_LIST', city_id: city_id, ajax: 'true'}, function(j){
        var options = '';
        for (var i = 0; i < j.length; i++) {
          if (i == 0){
            options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';
          } else {
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
          }
        }
        $("select#metro_name").html(options);
      });
    }
    $("#metro_table").css("display", "block");
    if ($.browser.msie && $.browser.version < 8) {
      $("#metro_tr").css("display", "inline");
    } else {
      $("#metro_tr").css("display", "table-row");
    }
  } else {
    $("#metro_table").css("display", "none");
    $("#metro_tr").css("display", "none");
  }
}

$(function(){

  // выбор страны и подгрузка регионов

  $("select#ctl_country").change(function(){
    $("select#ctl_region").empty();
    $("select#ctl_city").empty();
    $.getJSON("/city_select.php",{form_action: 'REGION_LIST', country_id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        if (i == 0){
          options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';
        } else {
          options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        }
      }
      $("select#ctl_region").html(options);
      $("#change_city_ok").removeAttr("disabled");
    })
  });

  // выбор региона и подгрузка городов

  $("select#ctl_region").change(function(){

    $("select#ctl_city").empty();

    if ( $("select#ctl_region").val() == -1 ){  // ничего не грузить если выбран любой регион
      return;
    }

    $.getJSON("/city_select.php",{form_action: 'CITY_LIST', country_id: $("#ctl_country").val(), region_id: $(this).val(), ajax: 'true'}, function(j){
      var options = '';
      for (var i = 0; i < j.length; i++) {
        if (i == 0){
          options += '<option value="' + j[i].optionValue + '" selected="selected">' + j[i].optionDisplay + '</option>';
        } else {
          options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
        }
      }
      $("select#ctl_city").html(options);
      $("select#ctl_city option:first").selected = true;
    })
  });


  $("select#city_name").change(function(){
    
    var i = $("select#city_name").val();
    if (i == "1_1_1"){

      $("#overlay").show();
      $("#change_city").show();

      var divPopup = document.getElementById("overlay");

      if (navigator.appName == "Microsoft Internet Explorer"){
        divPopup.style.zIndex = divPopup.style.zIndex+1;

        var iFrame = document.createElement("IFRAME");
        iFrame.setAttribute("src", "");

        iFrame.style.position = "absolute";
        iFrame.style.left = divPopup.offsetLeft + 'px';
        iFrame.style.top = divPopup.offsetTop + 'px';
        iFrame.style.width = divPopup.offsetWidth + 'px';
        iFrame.style.height = divPopup.offsetHeight + 'px';
        iFrame.style.opacity = 0;
        iFrame.style.filter = "alpha(opacity='30')";

        document.body.appendChild(iFrame);

        g_PopupIFrame = iFrame;
      }

      var n = $("select#ctl_country option:first").val();

      if (n == -2){
        $.getJSON("/city_select.php",{form_action: 'COUNTRY_LIST', ajax: 'true'}, function(j){
          var options = '';
          for (var i = 0; i < j.length; i++) {
            options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
          }
          $("select#ctl_country").html(options);
          $("select#ctl_country option:first").selected = true;
        })
      }
    } else {
      display_metro(i);
    }

  });

  $("#change_city_ok").click(function () {
    cmd_select_city();
    return false;
  });

  $("select#ctl_city").dblclick(function () {
    cmd_select_city();
    return false;
  });

  function cmd_select_city(){
    var vvalue  = "";
    var vtext   = "";

    //alert($("select#ctl_region").val());

    if ( $("select#ctl_region").val() == -1 || $("select#ctl_region").val() == null){
      vvalue  = $("select#ctl_country").val() + "_0_0";
      vtext   = $("select#ctl_country option:selected").text() + ", Любой регион";
    } else {
      vvalue  = $("select#ctl_country").val() + "_" + $("select#ctl_region").val() + "_" + $("select#ctl_city").val();
      if ($("select#ctl_city").val() == 0 || $("select#ctl_city").val() == null){
        vtext   = $("select#ctl_country option:selected").text() + ", " + $("select#ctl_region option:selected").text();
      } else {
        vtext   = $("select#ctl_country option:selected").text() + ", " + $("select#ctl_city option:selected").text();
      }

    }

    addCity('fSearch', vvalue, vtext);

    $("#change_city").hide();
    $("#overlay").hide();

    if (navigator.appName == "Microsoft Internet Explorer"){
      document.body.removeChild(g_PopupIFrame);
      g_PopupIFrame=null;
    }

  }

  $("#loading").bind("ajaxSend", function(){
    $(this).show();
  }).bind("ajaxComplete", function(){
    $(this).hide();
  });

});


function addCity(vformname,vvalue,vtext){
  if (document.getElementById("city_name")){
    fcity = document.getElementById("city_name");
  }
  if (fcity.length){
    flength=fcity.length;
    fcurrent=flength-1;
  }
  if (fcity[fcurrent]){
    fcity[fcurrent].selected=true;
  }
  if (fcity.options[fcurrent]){
    fcity.options[fcurrent].text=vtext;
    fcity.options[fcurrent].value=vvalue;
    if (fcity.options[fcurrent].label){
      fcity.options[fcurrent].label=vtext;
    }
  }
  var oOption = document.createElement("option");
  oOption.appendChild(document.createTextNode("Выбрать другой город..."));
  oOption.setAttribute("value", "1_1_1");
  fcity.appendChild(oOption);
}

