hs.graphicsDir = '/usr/system/plugins/highslide-full/graphics/';
hs.showCredits = false;
var lang="ru";
var state=false;
var countries=new Array();
var urls=new Array();
var hid=true;
var sat=false;

$(document).ready(function() {

   $(".jmenu").find("a:eq(0)").each(function () {
      var e=$(this).parent().find("ul");
      if (e.length) $(this).click(function () {
         return false;
      });
   });

   $(this).find("span.s_h").show();
   $("#show_all").click(function () {
      hid=!hid;
      $(this).find("span").hide();
      if (hid) {
         $(this).find("span.s_h").show();
         $("#roaming_list").hide();
      }
      else {
         $(this).find("span.s_s").show();
         $("#roaming_list").show();
      }
      return false;
   });

   if ($("#sel_c").length) {
      $("#roaming_list td a.c").each(function () {
         countries[countries.length]=$(this).html();
         urls[$(this).html()]=$(this).attr('href');
      });
      $("#sel_c").autocomplete(countries, {
          delay:1,
          minChars:1,
          autoFill:true,
          matchSubset:1
      }).result(function (event, item) {
            $("#sel_c").attr('disabled', 'disabled');
            $("#sel_c").parent().find("img").css({
               'visibility': 'visible'
            });
            //alert(item);
            window.location.href=urls[item];
      });
   }
   
   if ($("#sel_s").length) {
      $("#sel_s").autocomplete("/ajax/?lang="+lang+"&sub=get_streets&parent="+$("#sel_s_id").val(), {
          delay:1,
          minChars:1,
          autoFill:false,
          matchSubset:0,
          parse: function parse(data) {
            var parsed = [];
            var rows = data.split("\n");
            for (var i=0; i < rows.length; i++) {
               var row = $.trim(rows[i]);
               if (row) {
                  row = row.split("|");
                  parsed[parsed.length] = {
                     data: row,
                     value: row[0],
                     result: row[0]
                  };
               }
            }
            return parsed;
         }
      });
   }
   
   if (!sat) {
      $.ajax({
         url: "/ajax/?lang="+lang+"&sub=get_countries",
         type: "GET",
         data: "",
         dataType: "json",
         success : function (v) {
            var e;
            var i;
            e=$('.tr').find("select");
            e.children().remove().end()/*.append('<option>'+v.choose+'</option>')*/;
            v.countries=v.countries.split("|");
            for (i=0; i<v.countries.length; i++) {
               e.append('<option>'+v.countries[i]+'</option>');
            }
            e.change(function() {
               //alert("/ajax/?sub=get_prices&country="+encodeURIComponent($(this).val())+"&lang="+lang);
               $.ajax({
                  url: "/ajax/?sub=get_prices&country="+encodeURIComponent($(this).val())+"&lang="+lang,
                  type: "GET",
                  data: "",
                  dataType: "json",
                  success : function (v) {
                     //alert(v);
                     $("#p1").html("$"+v.incoming);
                     $("#p2").html("$"+v.outgoing);
                     $(".tr").find("span").show();
                  }
               });
            });
            e.trigger('change');
         }
      });
   }

   if ($(".faq_entry").length) {
      $(".faq_entry").find("a:first").click(function () {
         var e;
         e=$(this).parent().find("div");
         if (e.html()) {
            e.html('');
            return false;
         }
         e.html('<img src="/usr/system/loading.gif" /> '+( lang=='ru' ? 'Загрузка' : lang=='ua' ? 'Завантаження' : 'Loading' )+'...');
         $.ajax({
            url: "/ajax?sub=get_answer&lang="+lang+"&id="+$(this).attr('id').replace('q', ''),
            type: "GET",
            data: "",
            dataType: 'json',
            success : function (v) {
               e.html(v.answer);
            }
         });
         return false;
      });
      var q;
      q=window.location.href.replace(/[^\#]+\#/, '');
      if (q) {
         $("#"+q).click();
      }
   }

   if ($("#slideshow").length && !$("#slideshow").hasClass('sortable')) {
      $("#slideshow").cycle({ 
          fx:     'fade', 
          speed:   3500, 
          timeout: 3500, 
          next:   '#slideshow',  
          pager:  '#navig',
          pause:  true,
          pauseOnPagerHover: true
      });
      $(".control_play").find("a").click(function () {
         $('#slideshow').cycle(state ? 'resume' : 'pause');
         $(".control_play").find("a").css({
            'background-position': '0px '+(!state ? '-19' : '0')+'px'
         });
         state=!state;
         return false;
      });
   }
   
});

$(document).ready(function () {
   var no_hash_change = 0;
   $(".tree li").each(function () {
      var li = $(this);
      var sub = li.children(".contents");
      li.children(".subitem_title").children("a").click(function () {
         if (sub.is(":visible"))
         {
            //sub.slideUp(500);
            sub.hide();
         }
         else
         {
            //sub.slideDown(500);
            sub.show();
            if (!no_hash_change)
               window.location.hash = $(this).attr("rel");
         }
         return false;
      });
   });
   if (window.location.hash)
   {
      var h = window.location.hash;
      h = h.replace('#', '');
      var the_link = $(".tree a[rel="+h+"]");
      no_hash_change = 1;
      while ( the_link.length )
      {
         the_link.trigger("click");
         the_link = the_link.parent().parent().parent().parent().parent().children(".subitem_title").children("a");
      }
      no_hash_change = 0;
   }
});

// contacts

$(document).ready(function () {
   $("a.highslide").click(function() {
      return hs.expand(this);
   });
   $("input[type=button]").each(function () {
      if ($(this).attr('href')) {
        $(this).click(function () {
           window.location.href=$(this).attr('href');
        });
      }
   });
   if ($.validationEngine) {
      $(".valForm").validationEngine({
         ajaxSubmit: true,
         ajaxSubmitFile: $(".valForm").attr('action')
      });
   }
});

