  autoline = {
    process_ads: [],
    all_ads: [],
    translate: {},
    notepadCodesStr: '',
    pricemonCodesStr: '',
    imagesUrl: 'http://images.autoline.com.ua/',
    user: { code: 0, login: ''},
    initPage: {
      all: function() {
        autoline.initPage.adsList();
      },
      addFuncs: {
        Checkbox: function(ad_code){
          return chbox = $('<input type="checkbox" name="adverts_list[' + ad_code + ']" value="' + ad_code + '" class="ad-checkbox" id="ad-checkbox-' + ad_code + '" title="' + autoline.dict.add_to_notepad + '">').click(function(){
            $('.ad-' + ad_code).toggleClass('ad-selected');
          });
        }
      },
      groupFuncs: {
        Notepad: function(){
          return $('<a href="/my/notepad/" class="ad-notepad" alt="'  + autoline.dict.add_to_notepad + '" title="'  + autoline.dict.add_to_notepad + '"><img src="http://images.autoline.com.ua/design/user/notepad-yellow.png"> ' + autoline.dict.add_to_notepad + '</a>')
            .click(function(){
              if (autoline.user.code > 0) {
                $.each($('.ad-checkbox'), function(){
                  if ($(this).attr('checked') == true) {
                    addToNotepad($(this).val());
                  }
                });

                $('#ad-select-all').removeAttr('checked');
                $('#notepad-added-done').remove();
                $(this).after(
                  "<div id='notepad-added-done' style='font-size: 11px; float: left; display: none; font-weight: bold; margin: 4px 0px 0px 7px;'>" + autoline.dict.done + "</div>"
                );
                $('#notepad-added-done').fadeIn( 500, function(){
                  setTimeout(function() { $('#notepad-added-done').fadeOut(); }, 1000);
                });

                return false;
              } else {
                document.location = "/my/notepad/?add&" + $('.ad-checkbox:checked').serialize() + "&back=" + document.location;
                return false;
              }
            });
        }
      },
      adsList: function() {
        var ads_number = $('.ad').length;
        var i;
        $('.ad').each(
          function (ad_index, ad) {
            ad = $(ad);
            var det_link = ad.find('.ad-title-link').attr('href');
            var links = $(ad.find('td.ad-links'));
            var ad_code = links.attr('id');

              links.show();

              for (i in autoline.initPage.addFuncs) {
                if (i != 'Checkbox' || ads_number > 1) {
                  if (autoline.user.code > 0 || i == 'Checkbox') {
                    if (i != 'Notes') {
                      links.append(autoline.initPage.addFuncs[i](ad_code, det_link));
                    } else {
                      ad.before(autoline.initPage.addFuncs[i](ad_code, det_link));
                    }
                  }
                }
              }

              if (ad.next().find('.ad-detail').length > 0) {
                det_link = ad.prev().find('.ad-title-link').attr('href');
                ad.next().find('.ad-detail').append('<a href="' + det_link + '" target="_blank" class="blue u">' + autoline.dict.details + '</a>  <span class="blue">&raquo;</span>');
              }

          }
        );

        $('.ad-detail.ad-admin-inactive a').removeAttr('target');

        var ws = $('#with-selected');
        if (ws.length > 0) {

            if (ads_number > 1) {
              ws.append($('<img src="http://images.autoline.com.ua/short_view_icons/arr_up' + (autoline.lang == 'ar' ? '_ar' : '') + '.png" class="up_arrow">'));
              for (i in autoline.initPage.groupFuncs) {
                ws.append(autoline.initPage.groupFuncs[i]());
              }
              ws.find('a').wrap('<div />');
            }
        }

        $.each ($('.ad-checkbox'), function(){
          autoline.all_ads.push($(this).val());
        })

      }
    }
  }

  // ------------------------------------------------------------------------------------------------------------------------------

  // utils

  autoline.eventStopper = function(event) {
    event.stopPropagation();
  }

  // ------------------------------------------------------------------------------------------------------------------------------

  autoline.performatives = {};
  autoline.performatives.open = function() {
    window.open(
      '/performatives.php?ref=' + escape(document.URL),
      '_blank',
      'center:yes, resizable=no, menubar=no, toolbar=no, status=no, scrollbars=no, width=550, height=670, left=50, top=40'
    );
    return false;
  };

  // ------------------------------------------------------------------------------------------------------------------------------

  autoline.gmap_loader = function(gmap_loader_callback) {

    var document_write = document.write;
    document.write = function(str){ $('body').append(str); };


    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = 'http://maps.google.com/maps/api/js?sensor=false';
    var timer = setInterval(function() {
      if (window.google && window.google.maps && window.google.maps.LatLng!=undefined) {
        clearInterval(timer);
        document.write = document_write;
        gmap_loader_callback();
      }
    }, 10);
    $('head').get(0).appendChild(script);

  };

  // ------------------------------------------------------------------------------------------------------------------------------

  function autolineLinkGo(link, url) {
    $(link).attr("href", url);
    return true;
  }

  // ------------------------------------------------------------------------------------------------------------------------------

  function open_email_form(email_id, sellinfo_code) {
    window.open('/email_form.php?email_id='+email_id+'&sellinfo_code='+sellinfo_code ,'email_form_' + email_id, 'center:yes,resizable=no, menubar=no, toolbar=no, status=no, scrollbars=no,width=490, height=370');
  }

  // ------------------------------------------------------------------------------------------------------------------------------

  function open_image_window(url) {
    return window.open(url, '', 'width=640 height=480 top=100 left=100');
  }

  function toggle_block(block_id, icon_id) {
    if ($('#' + block_id).css('display')=='none') {
      $('#' + block_id).show();
      $('#' + icon_id).attr('src', 'http://images.autoline.com.ua/images/minus.gif');
      $.cookie(block_id + '_hidden', '0', { path: '/', expires: 30 });
    } else {
      $('#' + block_id).hide();
      $('#' + icon_id).attr('src', 'http://images.autoline.com.ua/images/plus.gif');
      $.cookie(block_id + '_hidden', '1', { path: '/', expires: 30 });
    }
  }


  // ------------------------------------------------------------------------------------------------------------------------------

  // ajax add to notepad (user in modules/adverts/list_common.php)
  function addToNotepad(advert_code) {
    if ($('.ad-' + advert_code).length == 0) return false;
    var key = $.inArray(advert_code, autoline.process_ads);
    if (key > -1) {
      autoline.process_ads.splice(key, 1);
    }
    $.get(
      "/my/notepad/?add=" + advert_code + '&ajax',
      function(result) {
        if (result != 'error') {
//          if ($('#' + advert_code + ' .ad-notepad').length > 0) {
//            var notepad = $('<a href="/my/notepad/" class="ad-notepad-remove" alt="'  + autoline.dict.remove_from_notepad + '" title="'  + autoline.dict.remove_from_notepad + '"></a>').css('opacity', 0.5)
//                .click(function(){
//                  if (autoline.user.code > 0) {
//                    removeFromNotepad(advert_code);
//                    return false;
//               }
//             });
//            $('#' + advert_code + ' .ad-notepad').after(notepad);
//            $('#' + advert_code + ' .ad-notepad').remove();
//          }
          $('.user-notepad-num').html(result);
          //showMessage(autoline.dict.notepad_added + ' ' + result, advert_code);
        }
        $('#ad-checkbox-' + advert_code).click();
      }
    );
  }

  function removeFromNotepad(advert_code) {
    if ($('#' + advert_code + ' .ad-notepad-remove').length == 0) return false;
    $.get(
      "/my/notepad/?d=" + advert_code + '&ajax',
      function(result) {
        if (result!='error') {
          var notepad = $('<a href="/my/notepad/" class="ad-notepad" alt="'  + autoline.dict.add_to_notepad + '" title="'  + autoline.dict.add_to_notepad + '"></a>').css('opacity', 0.5)
            .click(function(){
              if (autoline.user.code > 0) {
                addToNotepad(advert_code);
                return false;
              }
            });
          $('#' + advert_code + ' .ad-notepad-remove').after(notepad);
          $('#' + advert_code + ' .ad-notepad-remove').remove();
          $('.user-notepad-num').html(result);
          //showMessage(autoline.dict.removed, advert_code);
        }
      }
    );
    $('#ad-checkbox-' + advert_code).click();
  }

  function addToPriceMon(advert_code) {
    if ($('#' + advert_code + ' a.ad-watch-price').length == 0) return false;
    $.get(
      "/my/price_mon/?add=" + advert_code + '&ajax',
      function(result) {
        if (result!='error') {
        price = $('<a href="/my/" class="ad-watch-price-remove" alt="' + autoline.dict.remove_from_pricemon + '" title="' + autoline.dict.remove_from_pricemon + '"></a>').css('opacity', 0.5)
          .click(function(){
            if (autoline.user.code > 0) {
              removeFromPriceMon(advert_code);
              return false;
            }
          });
          $('#' + advert_code + ' .ad-watch-price').after(price);
          $('#' + advert_code + ' a.ad-watch-price').remove();
          //showMessage(autoline.dict.added_to_pricemon, advert_code);
        }
      }
    );
    $('#ad-checkbox-' + advert_code).click();
  }


  function removeFromPriceMon(advert_code) {
    $('#ad-checkbox-' + advert_code).click();
    $.get(
      "/my/price_mon/?delete=" + advert_code + '&ajax',
      function(result) {
        if (result!='error') {
          $('.ad-' + advert_code).next().remove();
          $('.ad-' + advert_code).remove();
          if ($('.ad-checkbox').length == 0) {
            document.location.reload();
          }
        }
      }
    );
  }

  // ------------------------------------------------------------------------------------------------------------------------------
  function showMessage(text, advert_code) {
//    var info = $('<div class="info"></div>').append(text);
//    $('#' + advert_code).append(info);
//    info.show();
//    setTimeout(function(){ info.animate({ opacity: 0 }, 500, function(){ $(this).remove() })}, 1000);
  }

  // ------------------------------------------------------------------------------------------------------------------------------

  function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if ((charCode < 48 || charCode > 57) &&
        charCode!=46 &&
        charCode!=35 &&
        charCode!=36 &&
        charCode!=37 &&
        charCode!=39) {
      return false;
    }
    return true;
  }

  // ------------------------------------------------------------------------------------------------------------------------------

  function validateEmail(str) {
    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
      return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
      return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
      return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false;
    }
    if (str.indexOf(dot,(lat+2))==-1){
      return false;
    }
    if (str.indexOf(" ")!=-1){
      return false;
    }
    return true;
  }

  // ------------------------------------------------------------------------------------------------------------------------------

  function ad_print() {
    if (null === w_print) return false;

    if ($.browser.mozilla) {
      w_print.print();
      return true;
    }

    if (w_print.document.readyState == 'complete') {
      w_print.print();
    } else {
      setTimeout('ad_print()', 100);
    }
  }

  function adsSelectAll() {
      var checked = $('#ad-select-all').attr('checked');
      $('.ad-checkbox').attr('checked', checked);
      if (checked) {
        autoline.process_ads = autoline.all_ads;
      } else {
        autoline.process_ads = [];
      }
      if (checked) {
        $('.ad').addClass('ad-selected');
        $('.ad-notes-tr').addClass('ad-selected');
        $('.ad-select-all').addClass('ad-selected');
      } else {
        $('.ad').removeClass('ad-selected');
        $('.ad-notes-tr').removeClass('ad-selected');
        $('.ad-select-all').removeClass('ad-selected');
      }
//      $('.ad-checkbox:checked').click();
//      $.each($('.ad-checkbox'), function(){
//        if (checked != $(this).attr('checked')) {
//          $(this).click();
//        }
//      })

  }

  $(document).ready(function(){

    $('#ad-select-all').click(adsSelectAll);

    $('.ad').click(function(e){
    	if ($(e.target).attr('tagName') != 'INPUT' && $(e.target).attr('tagName') != 'A' && $(e.target).attr('tagName') != 'IMG') {
        var code = $(this).attr('className').match(/ad-\d+/);
        code = String(code);
    		$('#ad-checkbox-' + code.substr(3)).click();
    	}
    });

    $('.ad-checkbox').live('click', function(){
      if ($(this).attr('checked')) {
        autoline.process_ads.push($(this).val());
      } else {
        var key = $.inArray($(this).val(), autoline.process_ads);
        if (key > -1) {
          autoline.process_ads.splice(key, 1);
        }
      }
    });

    $('.pages a').click(function(){
      if (autoline.process_ads.length > 0) {
        if (confirm(autoline.dict.add_to_notepad_selected + '?')) {
          if (autoline.user.code > 0) {
            $.each($('.ad-checkbox'), function(){
              if ($(this).attr('checked') == true) {
                addToNotepad($(this).val());
              }
            });
          } else {
            document.location = "/my/notepad/?add&" + $('.ad-checkbox:checked').serialize() + "&back=" + $(this).attr('href');
            return false;
          }
        }
      }
    });
    $('a.blue-button').wrapInner($('<span>').addClass('blue-button-left-side').append($('<span>').addClass('blue-button-right-side').append($('<span>').addClass('blue-button-center'))));

  });

  // ------------------------------------------------------------------------------------------------------------------------------
  // ------------------------------------------------------------------------------------------------------------------------------


  autoline.bookmarkServices = {};
  autoline.bookmarkServices.getTitle = function() {
    return document.title;
  };
  autoline.bookmarkServices.getDescription = function() {
    if (document.selection) {
      if (document.selection.type!='None') {
        return document.selection.createRange().text;
      }
    } else {
      if (document.getSelection) {
        if (document.getSelection().length>0) {
          return document.getSelection();
        }
      }
    }
    return $('meta[name=description]').attr('content');
  };
  autoline.bookmarkServices.init = function() {
    $('.bs-bobrdobr').click(function(){
      window.open('http://bobrdobr.ru/add.html?url=' + encodeURIComponent(location.href) + '&amp;title=' + encodeURIComponent(document.title) + '&amp;desc=' + encodeURIComponent(autoline.bookmarkServices.getDescription()), 'bobrdobr','toolbar=no,width=1000,height=600');
    });
    $('.bs-delicious').click(function(){
      window.open('http://delicious.com/save?v=5&amp;noui&amp;jump=close&amp;url='+encodeURIComponent(location.href)+'&amp;title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550');
    });
    $('.bs-digg').click(function(){
      ID=document.getSelection();if(!ID){location.href='http://digg.com/submit?phase=2&url='+location.href+'&title='+encodeURIComponent(document.title)}if(ID){z=open().document;z.location.href='http://digg.com/submit?phase=2&url='+location.href+'&title='+encodeURIComponent(document.title)+'&bodytext='+encodeURIComponent(document.getSelection())}
    });
    $('.bs-google').click(function(){
      var a=window,b=document,c=encodeURIComponent,d=a.open("http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk="+c(b.location)+"&title="+c(b.title)+"&annotation="+c(autoline.bookmarkServices.getDescription()),"bkmk_popup","left="+((a.screenX||a.screenLeft)+10)+",top="+((a.screenY||a.screenTop)+10)+",height=420px,width=550px,resizable=1,alwaysRaised=1");a.setTimeout(function(){d.focus()},300);
    });
    $('.bs-yandex').click(function(){
      /* location.href = 'http://zakladki.yandex.ru/newlink.xml?url='+encodeURIComponent(location.href)+'&name='+encodeURIComponent(document.title)+'&descr='+encodeURIComponent(autoline.bookmarkServices.getDescription()); */
      var a=window,b=document,c=encodeURIComponent,d=a.open('http://zakladki.yandex.ru/newlink.xml?url='+encodeURIComponent(location.href)+'&name='+encodeURIComponent(document.title)+'&descr='+encodeURIComponent(autoline.bookmarkServices.getDescription()),"yandex_bm_popup","left="+((a.screenX||a.screenLeft)+10)+",top="+((a.screenY||a.screenTop)+10)+",height=420px,width=550px,resizable=1,alwaysRaised=1");a.setTimeout(function(){d.focus()},300);
    });
    $('.bs-memori').click(function(){
      var d=document; var txt = encodeURIComponent(autoline.bookmarkServices.getDescription());
      var winp = window.open('http://memori.ru/link/?sm=1&book=1&u_data[url]='+encodeURIComponent(d.location.href)+'&u_data[name]='+encodeURIComponent(d.title)+'&u_data[descr]='+txt+'','winp','width=670, height=600,resizable=yes,status=no,toolbar=no,menubar=no,scrollbars=yes, left=75,top=20, dependent=1');
      winp.focus();
    });
    $('.bs-livejournal').click(function(){
      var u='http://www.livejournal.com/',w=window.open('','','toolbar=0,resizable=1,scrollbars=1,status=1,width=800,height=500');if(window.LJ_bookmarklet){return LJ_bookmarklet(w,u)};var e=document.createElement('script');e.setAttribute('type','text/javascript');e.onload=function(){LJ_bookmarklet(w,u)};e.setAttribute('src',u+'js/bookmarklet.js');document.getElementsByTagName('head').item(0).appendChild(e);
    });
    $('.bs-facebook').click(function(){
      var d=document,f='http://www.facebook.com/share',l=d.location,e=encodeURIComponent,p='.php?src=bm&v=4&i=1275289879&u='+e(l.href)+'&t='+e(d.title);1;try{if (!/^(.*\.)?facebook\.[^.]*$/.test(l.host))throw(0);share_internal_bookmarklet(p)}catch(z) {a=function() {if (!window.open(f+'r'+p,'sharer','toolbar=0,status=0,resizable=1,width=626,height=436'))l.href=f+p};if (/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}}void(0);
    });
    $('.bs-myspace').click(function(){
      window.open('http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(document.location.toString()),'ptm','height=450,width=440').focus();
    });
    $('.bs-yahoo').click(function(){
      var d=document,f='http://bookmarks.yahoo.com/toolbar/savebm?',l=d.location,e=encodeURIComponent,p='u='+e(l.href)+'&t='+e(d.title)+'&opener=bm&ei=UTF-8';1;a=function(){if(!window.open(f+p,'popup','toolbar=0,status=0,resizable=1,scrollbars=1,width=450,height=480'))l.href=f+p};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else{a()}
    });
    $('.bs-misterwong').click(function(){
      //location.href='http://www.mister-wong.ru/index.php?action=addurl&bm_url='+encodeURIComponent(location.href)+'&bm_description='+encodeURIComponent(document.title);
      var a=window,b=document,c=encodeURIComponent,d=a.open('http://www.mister-wong.ru/index.php?action=addurl&bm_url='+encodeURIComponent(location.href)+'&bm_description='+encodeURIComponent(document.title),"mister_wong_bm_popup","left="+((a.screenX||a.screenLeft)+10)+",top="+((a.screenY||a.screenTop)+10)+",height=550px,width=800px,resizable=1,alwaysRaised=1");a.setTimeout(function(){d.focus()},300);
    });
    $('.bs-twitter').click(function(){
      window.open("http://twitthis.com/twit?url="+encodeURIComponent(location.href)+"&title="+((document.title) ? encodeURIComponent(document.title.replace(/^\s*|\s*$/g,'')) : ""), "TwitThisPop", "width=780, height=600, location, status, scrollbars, resizable");
    });
    $('.bs-iua').click(function(){
      var d=document; var w = window.open('http://links.i.ua/mark/?p=1&url='+encodeURIComponent(d.location.href)+'&ename='+encodeURIComponent(d.title),'Test','status=no,menubar=no,scrollbars=yes,toolbar=no,resizable=yes,width=730,height=465'); w.focus();
    });
    $('.bs-ukrnet').click(function(){
      d=document;ukrnet_bmlet={id:new Date().valueOf()};s=d.createElement('script');s.src='http://zakladki.ukr.net/bookmarks/js/addlink.js?bmlet=1&'+ukrnet_bmlet.id;s.id='ukrnet_links_script';d.body.appendChild(s);
    });
    $('.bs-stumbleupon').click(function(){
      document.location.href = 'http://www.stumbleupon.com/submit?url='+encodeURIComponent(document.location.href)+'&title='+document.title.replace(/%20/g,'+');
    });
    $('.bs-tochkanet').click(function(){
      document.location.href = 'http://links.tochka.net/index.php?page=diggAdd&link=' + encodeURIComponent(document.location.href);
    });
    $('.bs-links-about').click(function(){
      location.href='/social-bookmarks.php';
    });
  }