/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


(function($) {
    // 插件的定义
    $.fn.listable = function() {
        $.fn.listable.remove = function(cfm, deleturl,returnurl) {
            if (confirm(cfm)) {
                $.getJSON(deleturl,
                    function(data){
                         if (data.message)
                        {
                            alert(data.message);
                        }
                        if (data.error == 0)
                        {
                            window.location.href=returnurl;
                        }
                
                    //alert("Data Loaded: " + data);
                    });
            }           
        };

        $.fn.listable.selectAll = function(obj, chk)
        {
            if (chk == null)
            {
                chk = 'checkboxes';
            }

            var elems = obj.form.getElementsByTagName("INPUT");

            for (var i=0; i < elems.length; i++)
            {
                if (elems[i].name == chk || elems[i].name == chk + "[]")
                {
                    elems[i].checked = obj.checked;
                }
            }
        };

        /**
     * 切换状态
     */
        $.fn.listable.toggle = function(toggleurl,obj, acttype, id)
        {
            var val = (obj.src.match(/yes.gif/i)) ? 0 : 1;

            $.getJSON(toggleurl,{
                acttype: acttype,
                val: val,
                id: id
            },
            function(data){
                if (data.message)
                {
                    alert(data.message);
                }

                if (data.error == 0)
                {
                    obj.src = (data.content > 0) ? '/images/yes.gif' : '/images/no.gif';
                }
                   
            });        
        };     
    }
       
})(jQuery);     


