jQuery.noConflict();
jQuery(document).ready(function() {
    var ur = window.location.href;
    var loc = ur.split('/')[3];
    var loc_prefix ='';
    if (loc == 'ru' || loc == 'de' || loc == 'fr') loc_prefix = loc+'/';
    else loc_prefix = '';

  id = jQuery("#country_id").attr('value');
  if ( id != 0 ) {
    jQuery.get("/"+loc_prefix+"utils/registration-cities-options", {
          type: "get",
          id: id
          },
          AfterData);
  }
                        jQuery('div.optionsDivInvisible > span').mousedown(function(){ // когда список стран без прокрутки
                                        jQuery("#cities").empty();
					//jQuery("#cities").css("display","block");
                                        jQuery("#cities").html('<span class="" name="0">загрузка..</span>');

                                        id = jQuery("#country_id").attr('value');

                                        //alert(id);

                                        if ( id != 0 ) {
                                                jQuery.get("/"+loc_prefix+"utils/registration-cities-options", {
                                                type: "get",
                                                        id: id
                                                },
                                                AfterData);
                                        } else {
    					        jQuery("#cities").html('<select name="city_id"><option value="0">--Выберите город--</option></select>');
                                                //jQuery("#cities").empty();
                                        }
                                });
                        jQuery('div#country_id_fake > span').mousedown(function(){ // когда список стран с прокруткой
                                        var selectedId = jQuery(this).attr('name');
                                        jQuery('#country_id').attr('value',selectedId);
                                        jQuery("#cities").empty();
                                        jQuery("#cities").html('<span class="" name="0">загрузка..</span>');
                                        id = selectedId; //jQuery("#country_id").attr('value');
                                        if ( id != 0 ) {
                                                jQuery.get("/"+loc_prefix+"utils/registration-cities-options", {
                                                type: "get",
                                                        id: id
                                                },
                                                AfterData);
                                        } else {
    					        jQuery("#cities").html('<select name="city_id"><option value="0">--Выберите город--</option></select>');
                                        }
                                });

                                jQuery('#vcountry_id').focus(function(){
                                   if (jQuery('#country_id').val() == '0')
                                   jQuery(this).attr('value','');
                                });
                                jQuery('#vcountry_id').blur(function(){
                                   var txt = jQuery('#country_id_fake span[name=0]').text();
                                   if (jQuery('#country_id').val() == '0')
                                   jQuery(this).attr('value',txt);
                                });
                                jQuery('#vcity_id').focus(function(){
                                   if (jQuery('#city_id').val() == '0')
                                   jQuery(this).attr('value','');
                                });
                                jQuery('#vcity_id').blur(function(){
                                   var txt = jQuery('#city_id_fake span[name=0]').text();
                                   if (jQuery('#city_id').val() == '0')
                                   jQuery(this).attr('value',txt);
                                });












                        });
                        /* Вывод списка городов по выбранной стране */
                        function AfterData(data){
                          if ( data == 1 ) {
                              alert("Ошибка передачи данных");
                          } else {

                             jQuery("#cities").empty();
                             jQuery("#cities").append(data);
                          }
                                        myChangeSelects();
                        }

                        /* Вывод списка городов по выбранной стране */
                        function AfterData2(data){
                          if ( data == 1 ) {
                              alert("Ошибка передачи данных");
                          } else {
                             var name = jQuery("#city_id").attr("value"); 
                             var text = jQuery("#vcity_id").attr("value"); 
                             jQuery("#cities").empty();
                             jQuery("#cities").append(data);
                             jQuery("#city_id").attr("value",name); //  (value)   input
                             jQuery("#vcity_id").attr("value",text); //  (value)   input  

                          }
                                        myChangeSelects();
                        }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//jQuery("select#city_id").each(
function myChangeSelects()
{
    var ur = window.location.href;
    var loc = ur.split('/')[3];
    var loc_prefix ='';
    if (loc == 'ru' || loc == 'de' || loc == 'fr') loc_prefix = loc+'/';
    else loc_prefix = '';

var num = 1;                                                // num -  select
var selectOrCombobox = 8;       // - option.   select option -  ,  select ,  - combobox ( )
var kolOptions=jQuery("select#city_id").children().length; //  option   select

var className=jQuery("select#city_id").attr("class");//      ,    .
var selName=jQuery("select#city_id").attr("name");           //
//var selID=this.id;                     // id
var selID='city_id';

//combobox
if(kolOptions>selectOrCombobox)
{

        jQuery("select#city_id").css("display","none"); //  select

        //   select
        //    z-index  ,     ,
        //     id,    num
        jQuery("select#city_id").before("<div class='selectArea "+className+"' style='z-index:"+(100-num)+"'>"+
                                                        "<div class='left'></div>"+ //   select
                                                        "<div class='center_a'></div>"+ //   ( )  select
                                                        "<div class='optionsDivInvisible' id='optInvis_"+num+"'>"+ //   option
                                                                "<div class='scrollbar-container' id='scroll_container_"+num+"'>"+ //
                                                                        "<div class='scrollbar-up'></div>"+ //
                                                                        "<div class='scrollbar-down' id='scrollbar-down'></div>"+ //
                                                                        "<div class='scrollbar-track' id='scrollbar-track'><b class='scrollbar-handle' id='scrollbar-handle'></b></div>"+ //
                                                                        "<div class='container2' id='container'>"+ //    option
                                                                                "<div class='scroller-1' id='scroller_"+num+"'>"+
                                                                                "<div class='scroller-container' id='"+selID+"_fake'></div>"+
                                                                                "</div>"+
                                                                        "</div>"+
                                                                "</div>"+
                                                        "</div>"+
                                                        "<input id='v" + selID + "' type='text' name='v" + selName + "' />" +
                                                        "<input type='hidden' class='tmp' />"+
                                                "</div>");

jQuery("select#city_id option").each(function(){
       jQuery("#scroller_"+num+" > div").append("<span name='"+jQuery(this).val()+"'>"+jQuery(this).text()+"</span>");
});

//

jQuery(".tmp").val(jQuery("select#city_id").children("option[@selected]").val());
jQuery("#v"+selID).val(jQuery("select#city_id").children("option[@selected]").text());

//    option
jQuery("#sel"+num).css("display","none").css("visibility","visible");

//
jQuery("select#city_id").remove();
jQuery(".tmp").attr("id", selID);
jQuery(".tmp").attr("name", selName);
jQuery("#"+selID).attr("class", "");



//   option   select
  var id_1='scroll_container_'+num;
  var id_2='scroller_'+num;
  scroller = new jsScroller(document.getElementById(id_2), 0, 143);
  scrollbar = new jsScrollbar(document.getElementById(id_1), scroller, false, false);

        //    option
        jQuery("#optInvis_"+num).css("display","none").css("visibility","visible");
}

//  ,
else
{
//alert(kolOptions);
//
jQuery("select#city_id").before("<div class='selectArea "+className+"' id='c' style='z-index:"+(100-num)+"'>"+
                                                "<div class='left'></div>"+
                                                "<div class='center_a'></div>"+
                                                "<div class='optionsDivInvisible' id=sel"+num+"></div>"+
                                                "<input id='v" + selID + "' type='text' name='v" + selName + "' readonly='readonly'/>" +
                                                "<input type='hidden' class='tmp' />"+
                                        "</div>");

//  <option>
// name='"+selName+"' id='"+selID+"'

jQuery("select#city_id option").each(function(){
       jQuery("#sel"+num).append("<span name='"+jQuery(this).val()+"'>"+jQuery(this).text()+"</span>");
});
//
jQuery(".tmp").val(jQuery("select#city_id").children("option[@selected]").val());
jQuery("#v"+selID).val(jQuery("select#city_id").children("option[@selected]").text());

//    option
jQuery("#sel"+num).css("display","none").css("visibility","visible");

//
jQuery("select#city_id").remove();
jQuery(".tmp").attr("id", selID);
jQuery(".tmp").attr("name", selName);
jQuery("#"+selID).attr("class", "");
}

//  /   option    select
jQuery('span#cities div.center_a').mousedown(
function()
{
        if(jQuery("#sel"+num).css("display")=="none") 
              jQuery("#sel"+num).slideDown(200); //   ,
        else jQuery("#sel"+num).slideUp(200); //

        if(jQuery("#optInvis_"+num).css("display")=="none") 
              jQuery("#optInvis_"+num).slideDown(200); //   ,
        else jQuery("#optInvis_"+num).slideUp(200); //

        //  select  ,
    var inp=jQuery("#"+selID); //this.parentNode.getElementsByTagName('input').item(0);
        if(jQuery(inp).attr("readonly")!="readonly")
                {
                jQuery(inp).focus(); jQuery(inp).select(); //    ,
                //  scroll
                var id_1=this.parentNode.getElementsByTagName('div').item(3).id;
                var id_2=this.parentNode.getElementsByTagName('div').item(8).id;
                scroller  = new jsScroller(document.getElementById(id_2), 0, 143);
                scrollbar = new jsScrollbar(document.getElementById(id_1), scroller, false, false);
                }
});

//    option    input  selectArea ( combobox)
jQuery('span#cities div.selectArea > input').mousedown(
function()
{
  if(jQuery("#sel"+num).css("display")=="none")
      jQuery("#sel"+num).slideDown(200);

  if(jQuery("#optInvis_"+num).css("display")=="none")
      jQuery("#optInvis_"+num).slideDown(200);
});

//   option ( )
jQuery('div.optionsDivInvisible > span').mousedown( // когда без прокрутки
function()
{
        var text=jQuery(this).text(); //  option
        jQuery("#"+selID).attr("value",jQuery(this).attr("name")); //  (value)   input
        jQuery("#v"+selID).attr("value",text); //  (value)   input
        jQuery("#sel"+num).css("display","none"); //  option'
});
jQuery('div#city_id_fake span').mousedown( // когда с прокруткой
function()
{
        var text=jQuery(this).text(); //  option
        jQuery("#"+selID).attr("value",jQuery(this).attr("name")); //  (value)   input
        jQuery("#v"+selID).attr("value",text); //  (value)   input
        jQuery("#optInvis_"+num).css("display","none"); //  option'
});

//
jQuery('div.optionsDivInvisible > span').mouseover(
function()
{
  this.className="over";
});

jQuery('div.optionsDivInvisible > span').mouseout(
function()
{
  this.className="";
});

//    option
sel_span_click();

//  hover  option
sel_span_hover();

//   option      select
jQuery('div.selectArea').mouseout(
function(e)
{
//alert('div.selectArea mouseout');
var x = 0, y = 0; //
    if (!e) e = window.event;
    if (e.pageX || e.pageY){
        x = e.pageX;
        y = e.pageY;
    } else if (e.clientX || e.clientY){
        x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
        y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
    }
  var obj = this;
  var posx=findPosX(obj);
  var posy=findPosY(obj);
  var length_obj = obj.offsetWidth-2;
  var left=this.getElementsByTagName('div')[0].offsetWidth;
  var h = this.getElementsByTagName('div')[1].offsetHeight;
  var minus;
  if(jQuery.browser.msie) {minus=0;min2=1;left-=1;}
  else {minus=2;min2=1;left-=3;}
  var h1 = this.getElementsByTagName('div')[2].offsetHeight-minus;
  //     ,  option
  if((x<=posx+left || x>posx+length_obj+min2 || y<posy+2 || y>posy+h+h1) && jQuery(this).children('div.optionsDivInvisible').css("display")=="block")
  {
  jQuery(this).children('div.optionsDivInvisible').slideUp(200);
  }
});

//  option
jQuery('div.selectArea > input').keyup(
function()
{
//alert('div.selectArea keyup');
var id_1=this.parentNode.getElementsByTagName('div').item(3).id;
//id blocka scroll
var id_2=this.parentNode.getElementsByTagName('div').item(8).id;
//cicl proverka na sootvetstvie vvedenim simvolam

if(id_2 == 'scroller_1') // для городов
{
  var col=jQuery("#"+id_2+" > div > span").length; //  - option
  var span=jQuery("#"+id_2+" > div > span"); //   (option)
  var val_input=jQuery(this).val().toUpperCase(); // ,
  id = jQuery("#country_id").attr('value');
  if ( id != 0 ) 
  {
     jQuery.get("/"+loc_prefix+"utils/registration-cities-options-str", {
                  type: "get",
                  id: id,
                  str: val_input
               },
               AfterDataStr);
 } 
}
else if(id_2 == 'scroller_0') // для стран
{
  var col=jQuery("#"+id_2+" > div > span").length; //  - option
  var span=jQuery("#"+id_2+" > div > span"); //   (option)
  var val_input=jQuery(this).val().toUpperCase(); // ,
    for(i=0;i<col;i++)
    {
      var val_list=span.eq(i).text().toUpperCase();
  
      var pos=val_list.indexOf(val_input);
          if(pos!=0) span.eq(i).css("display","none"); //
      else span.eq(i).css("display","block");
    }
    //
    scroller  = new jsScroller(document.getElementById(id_2), 0, 143);
    scrollbar = new jsScrollbar(document.getElementById(id_1), scroller, false, false);
}

});

/* Вывод списка городов по выбранной стране и первым введенным символам */
                        function AfterDataStr(data){                      
                          if ( data == 1 ) {
                              alert("Ошибка передачи данных");
                          } else {

                             jQuery("#cities").find('.scroller-1').remove();
                             jQuery("#cities").find('.container2').append(data);
                             jQuery("#cities").find('.optionsDivInvisible').css('display','block');
                          }
                         myCitySelects();
                        }


function findPosY(obj) {
  var posTop = 0;
  while (obj.offsetParent) {posTop += obj.offsetTop; obj = obj.offsetParent;}
  return posTop;
}
function findPosX(obj) {
  var posLeft = 0;
  while (obj.offsetParent) {posLeft += obj.offsetLeft; obj = obj.offsetParent;}
  return posLeft;
}

                                jQuery('#vcity_id').focus(function(){
                                   if (jQuery('#city_id').val() == '0')
                                   jQuery(this).attr('value','');
                                });
                                jQuery('#vcity_id').blur(function(){
                                   var txt = jQuery('#city_id_fake span[name=0]').text();
                                   if (jQuery('#city_id').val() == '0')
                                   jQuery(this).attr('value',txt);
                                });
};





function myCitySelects()
{
    var ur = window.location.href;
    var loc = ur.split('/')[3];
    var loc_prefix ='';
    if (loc == 'ru' || loc == 'de' || loc == 'fr') loc_prefix = loc+'/';
    else loc_prefix = '';

selID = 'city_id';
num = '1';
jQuery('div#city_id_fake span').mousedown( // когда с прокруткой
function()
{
        var text=jQuery(this).text(); //  option
        var name = jQuery(this).attr("name");
        jQuery("#"+selID).attr("value",name); //  (value)   input
        jQuery("#v"+selID).attr("value",text); //  (value)   input
        jQuery("#optInvis_"+num).css("display","none"); //  option'

  id = jQuery("#country_id").attr('value');
  if ( id != 0 )
  {
     jQuery.get("/"+loc_prefix+"utils/registration-cities-options", {
                  type: "get",
                  id: id,
		  current: name
               },
               AfterData2);
 }
});

//
jQuery('div#city_id_fake > span').mouseover(
function()
{
  this.className="over";
});

jQuery('div#city_id_fake > span').mouseout(
function()
{
  this.className="";
});

}

