/* Treeview */
/*
 *     Easy TreeView - jQuery plugin
 *    written by SCINER <sciner@yandex.ru>
 *
 *    Copyright (c) 2011 SCINER
 * 
 *    Built for jQuery library
 *    http://jquery.com
 *
 *    Markup example for $("#treeview").treeView(options);
 *    
 *    // default options
 *    var options = {
 *        click:                 '',
 *        allow_collapse_expand: true,
 *        collapse_expand_animate: true
 *    }; 
 * 
 *    <ul id="treeview">
 *        <li id="id1"><em>Item-1</em>
 *           <!-- sub list -->
 *           <ul>
 *             <li id="id2"><em>Item-A</em></li>
 *             <li id="id3"><em>Item-B</em></li>
 *           </ul>
 *        </li>
 *    </ul>
 *
 */
(function($) {
    $.fn.treeView = function(options){

        // default configuration properties
        var defaults = {
            click:                 '',
            allow_collapse_expand: true,
            collapse_expand_animate: true,
            icon_folder_class:     'tree-icon-folder',
            icon_file_class:       'tree-icon-file'
        }; 

        var options = $.extend(defaults, options);  

        this.each(function() {
            var obj = $(this);
            obj.addClass('ui-nice-treeview');
            $('li em', obj).click(function() {
                var id = $(this).parent().attr('id');
                if(options.allow_collapse_expand)
                {
                    // скрытие/раскрытие
                    if(options.collapse_expand_animate)
                    {
                        $(this).parent().children('ul:first').slideToggle('fast');
                    }
                    else
                    {
                        $(this).parent().children('ul:first').toggle();
                    }
                }
                options.click(id);
            });
        });
      
    };

})(jQuery);


(function($) {
    $.fn.skinRadio = function(options){

        // default configuration properties
        var defaults = {
            checked:        '',
            icon_normal:    'skin-radio',
            icon_checked:   'skin-radio-checked'
        }; 

        var options = $.extend(defaults, options);  

        this.each(function() {
            var obj = $(this);
            /*span = document.createElement('span');
            span.className = options.icon_normal;
            obj.insertBefore(span);
            */
        });
      
    };

})(jQuery);

$(function() {
    $('body').prepend('<div id="shadow" style="display:none;"></div>');
    setButtonHandlers();
    // $('.skin-radio-list input[type=radio]').skinRadio();
});

function setButtonHandlers()
{
    /**
    * Нажатие кнопки на панели инструментов
    */
    $('.button,.button-simple').click(function() {
        var dis = $(this).attr('disabled');
        if(dis != 'disabled')
        {
            // id - операция
            var id = $(this).attr('id');
            func = $('#form-'+id).data('before_show');
            if(func != null){func(this);}
            try
            {
                show_form('form-'+id);
                return false;
            }
            catch(e)
            {
                alert('Ошибка: ' + e)
            };
        }
        return false;
    });
}

/**
* Центрование открытых/видимых форм
*/
$(window).resize(function(){
    center_forms();
});

function show_wait()
{
    if($('.popup-form:visible').size() < 1)
    {
    }
}

/**
* Открытие формы
*/
function show_form(id, can_close)
{
    if($('#'+id+' .popup-form-toolbox').size() < 1)
    {
        var title = $('#'+id).attr('title');
        if(title==undefined){title='';}
        $('#'+id).removeAttr('title');
        var buttons = '';
        if(can_close == null || can_close === true)
        {
            buttons += '<a href="#" style="font-size: 24px; text-decoration: none" onclick="hide_form();return false;">&times;</a>';
        }
        $('#'+id).prepend('<table width="100%" class="popup-form-toolbox"><tr><td align="left"><h2>'+title+'</h2></td><td align="right">'+buttons+'</td></tr></table>');
    }
    var w = $('#'+id).outerWidth();
    var h = $('#'+id).outerHeight();
    var x = $(window).width() / 2 - w/2;
    var y = $(window).height() / 2 - h/2;
    $('.popup-form').hide();
    $('#shadow').show();
    $('#'+id).addClass('popup-form').data('shadow', $('#shadow')).css({position: 'fixed', left: x, top: y}).show();
    $('#'+id+' input:visible:first').focus();
    return false;
}

function center_forms()
{
   $('.popup-form:visible').css({left: function(){
       return $(window).width() * 0.5 - $(this).outerWidth() * 0.5;
   }, top: function(){
       return $(window).height() * 0.5 - $(this).outerHeight() * 0.5;
   }});
}

/**
* Закрытие всех открытых форм
*/
function hide_form(id) {
    $('.popup-form').hide();
    $('#shadow').hide();
    return false;
}

