$(function()
 {
  function wordend(cnt)
   {
    cnt = cnt%10;
    if(cnt == 0 ) return 'городов';
    if(cnt <=1 ) return 'город';
    if(cnt > 1 && cnt <= 4 ) return 'города';
    if(cnt > 4 && cnt <= 9 ) return 'городов';
   }
  /*---------------------------------------------------------------------------------------*/
  $.fn.extend({
    SearchTown: function(str)
     {
      return this.each(function()
            {
             var thist = $(this).text().toLowerCase();
             if( thist.indexOf(str)<0 )
                {
                 $(this).hide();
                }
             else
                {
                 $(this).show();
                }
            });
     }
  });
  /*---------------------------------------------------------------------------------------*/
  var search_town_text = $('#search_town_text');
  /*---------------------------------------------------------------------------------------*/
  function st_change()
    {
     var str =  $.trim(  search_town_text.val().toLowerCase() );

     $(".townlink").SearchTown( str );

     var towncount = $('.townlink:visible').size();
     var title1 = 'Найдено ' + towncount + ' ' + wordend(towncount) + ' похожих на '+str;
     search_town_text.attr('title',title1);

     if(0==towncount)
       $('div#towncount').html( 'Города похожего на <b>'+str+'</b> не найдено' ).show();
     else
       $('div#towncount').hide();
    }

  search_town_text.bind('change', st_change).bind('keydown', st_change).bind('keyup', st_change).bind('keypress', st_change).change();
  /*---------------------------------------------------------------------------------------*/
   search_town_text.focus( function(){
     $(this).addClass('search_town_textsel');
    }).blur( function(){
     $(this).removeClass('search_town_textsel');
    });
  /*---------------------------------------------------------------------------------------*/
 });

  /*=======================================================================================*/
   function showimgbox(targInd,url,imgArr){
      var imgdiv;
      if(0==$('#showimgbox').size())
        {
         imgdiv = $(document.createElement('div'))
                  .attr('id','showimgbox')
                  .append('<div class="closeimg" id="closeimg">Закрыть</div>')
                  .append('<div id="pvimgbox"></div>')
                  .append('<div class="bigimgbox"><img src="" id="bigimg" class="closeimg" border="0"><span style="display:none"><br>ссылка на это фото: <input id="fotolink" type="text" value=""></span></div>')
                  .appendTo('body');
         var reg = /((?:[-.\w]+\/)+)([-\w]+)(\.[\w]+)/i;
         var arr = reg.exec(url);
         imgdiv.get(0).baseurl = '/'+arr[1] + arr[2].replace(/(\w+[-_])\d+/i, "$1");
         imgdiv.get(0).fexturl = arr[3];
         reg = /^(http:\/\/[-.\w]+)\/.*/i;
         arr = reg.exec(window.location.href);
         imgdiv.get(0).baseurl = arr[1] + imgdiv.get(0).baseurl;

         imgdiv.find('.closeimg').bind('click',function(){imgdiv.slideUp();});
         imgdiv.find('#pvimgbox').bind('click',function(e){
                 var the = $(e.target);
                 if(the.is('.prvimg') && !the.is('.prvimgsel'))
                   {
                    $(this).find('img').removeClass('prvimgsel');
                    the.addClass('prvimgsel');
                    imgdiv.find('span').hide();
                    imgdiv.find('#bigimg').hide().attr('src', imgdiv.get(0).baseurl +'b_'+ the.attr('curimg') + imgdiv.get(0).fexturl ).load( function() { $(this).fadeIn('slow'); imgdiv.find('span').show(); })
                    imgdiv.find('#fotolink').val(imgdiv.get(0).baseurl +'b_'+ the.attr('curimg') + imgdiv.get(0).fexturl);
                   }
                 return false;
                });
         imgdiv.find('#fotolink').bind('focus',function(){$(this).select();});
        }
      else
        {
         imgdiv = $('#showimgbox');
         imgdiv.show().find('#pvimgbox').html('');
        }

      var pvimgboxText = '';
      $.each( imgArr, function(i, n){
         var icl = 'prvimg';
         if(targInd==i) icl = 'prvimg prvimgsel';
         pvimgboxText += '<img src="'+imgdiv.get(0).baseurl + n + imgdiv.get(0).fexturl+'" curimg="'+n+'" class="'+icl+'">';
       });
      imgdiv.find('#pvimgbox').html(pvimgboxText);
      imgdiv.find('#bigimg').hide().attr('src', imgdiv.get(0).baseurl +'b_'+ imgArr[targInd] + imgdiv.get(0).fexturl ).load( function() { $(this).fadeIn('slow'); imgdiv.find('span').show();});
      imgdiv.find('#fotolink').val(imgdiv.get(0).baseurl +'b_'+ imgArr[targInd] + imgdiv.get(0).fexturl);
      imgdiv.get(0).scrollTop = 0;
    }