/*!
 * jQuery-загрузчик блока
 * Позволяет:
 * 1) по нажатию загружать в <target> контент если <target> не видим
 * Опции:
 * - target: id контента для загрузки
 * - action: метод для загрузки
 * - onLoad: действие после обновления (опционально)
 */

jQuery.fn.blockloader = function (options) {
    var target = options.target;
    var action = options.action;

    

//    var text = $(this).text();
//    var inner = $("<span/>")
//                .text(text);

    $(this).wrap("<span></span>");
    var outher = $(this).parent();
   
    var loader = $("<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>")
                .addClass('blockloader-loader')
                .hide();
    $(outher).append(loader);

    if(options.onLoad != null && options.onLoad != "")
        $(this).bind("onLoad",options.onLoad);



    $("#"+target).hide();
    $(this).click(function(){

         var visible =  ($("#"+target+":visible").size() > 0) ? true : false;
         if(visible) {
             $("#"+target).hide(300);
         }
         else {
             var parent = $(this).parent();
             var item = $(this);
             $('.blockloader-loader',parent).fadeIn(300);
             $("#"+target).load_post(action, { },function(){
                  $('.blockloader-loader',parent).fadeOut(300);
                  $(item).trigger("onLoad");
                  $("#"+target).show(300);
             });
         }
    })
}