﻿
$.fn.extend({
 zi_button: function (){ /*анимированные кнопки*/
  (this).each(function(){  
    var button= $(this);  var text=button.html();  var cssclass_pref= button.attr('class').split(' ')[0];
	var InFocus=false;
	var InClick=false;
    if(cssclass_pref != "zi_autobutton"){
     button.html(" <table cellspacing='0' cellpadding='0' class='"+cssclass_pref+"_out' ><tbody><tr> <td class='"+cssclass_pref+"_right'> "+ text+ "</td></tr></tbody></table>")
     .addClass("zi_autobutton_ready").removeClass("zi_autobutton")
     .children("table:first")
     .mouseup(function() 	{InClick=false; if(button.hasClass("disable")) return false; $(this).removeClass( cssclass_pref+"_down").addClass(cssclass_pref+ "_over");	})
	 .mousedown(function() 	{InClick=true; 	if(button.hasClass("disable")) return false; $(this).removeClass( cssclass_pref+"_over").addClass(cssclass_pref+ "_down"); })
     .mouseout(function() 	{InFocus=false; if(button.hasClass("disable")||InClick) return false; $(this).switchClass( cssclass_pref+"_over", cssclass_pref+'_out', 50); })
     .mouseover(function() 	{InFocus=true; 	if(button.hasClass("disable")||InClick) return false; $(this).switchClass( cssclass_pref+"_out", cssclass_pref+'_over', 50);})      
	.click(function() 		{ if(button.hasClass("disable")) return false; });
   }  
   });
  }
});

function placezicontrols(){
    $('.zi_autobutton').zi_button();  	
}
$(document).ready(function () {  
	placezicontrols();
});

/*живой поиск*/
jQuery.fn.zi_liveSearch = function (conf) { //conf -доступные настройки плагина
    var config = jQuery.extend({
      //значения по умолчанию
      url: '/search-results.php?q=',  //адрес страницы возвращающей результаты
      id: 'jquery-live-search', //id элемента в который вывводятся результаты           
      typeDelay: 200,
      onCompleteLoad: function () { },
      onStartLoad: function () { },
      onHidePreload: function () { },
      onShowPreload: function () { },
      setParametr: function (name, value) { this[name] = value; },
      ajaxid: 0 // счетчик отправленных запросов
    }, conf);

    var liveSearch = jQuery('#' + config.id);

    return this.each(function () {
      var input = jQuery(this).attr('autocomplete', 'off');
      input
      // On focus, if the live-search is empty, perform an new search
      // If not, just slide it down. Only do this if there's something in the input
            .focus(function () {
              if (this.value !== '') {
                // Perform a new search if there are no search results
                if (liveSearch.html() == '') {
                  this.lastValue = '';
                  input.keyup();
                }
              }
            })
      // Auto update live-search onkeyup
            .keyup(function () {
              // Don't update live-search if it's got the same value as last time
              if (this.value != this.lastValue) {
                var q = this.value;

                // Stop previous ajax-request
                if (this.timer) {
                  clearTimeout(this.timer);
                }

                // Start a new ajax-request in X ms
                this.timer = setTimeout(function () {
                  var currentajaxid = ++(config.ajaxid);
                  config.onStartLoad();
                  config.onShowPreload();
                  $.ajax({
                    url: config.url + q, type: 'GET',
                    success: function (data) {liveSearch.html(data); config.onCompleteLoad(data);},
                    complete: function () { if (currentajaxid == config.ajaxid) config.onHidePreload(); }
                  });
                }, config.typeDelay);
                this.lastValue = this.value;
              }
            });
    });
  };

