﻿/*
* JSUtility - 通用JS脚本集（基于jQuery）
*  
* TableSetter:表格
* 
* Copyright (c) 2008 Wang Yiyong
*
* @version 0.1
* @param config {Object} 参数
* @return {Object} jQuery对象
*/

jQuery.fn.TableSetter = function(config) {
    config = jQuery.extend({
        id: null,    //ID
        name: null,    //Name
        className: 'jsu_tableSetter', //样式
        data: null,
        innerHtml: ""

    }, config || {});
    return this.each(function() {

        var me = $(this);

        //Table head
        SetHead = function() {

            $.each(eval("$('#" + config.id + " th')"), function(i, o) { $(o).mouseover(function() { $(this).addClass('over'); }).mouseout(function() { $(this).removeClass('over'); }); });
        }

        //Table Body
        startTab = function(o) {

            loadDate();
            setStyle();
            setEvent();
        }

        //生成表格
        loadDate = function() {
            if (config.innerHtml == 'undefined' || config.innerHtml == null) return;
            $('#' + config.id + ' tbody').empty().append(config.innerHtml);

        };

        setStyle = function() {
            me.addClass("tableSetter");
            SetHead();
            $('#' + config.id + ' tbody tr:even').addClass('odd');
            $('#' + config.id + ' tbody tr').hover(function() { $(this).addClass('highlight'); }, function() { $(this).removeClass('highlight'); });
        };

        setEvent = function() {
            $('#' + config.id + ' tbody tr').click(function() {

                $('#' + config.id + ' tbody tr').removeClass('selected');

                $(this).toggleClass('selected');
            });
        };


        startTab();
    });

}

