﻿/***********************************************************
*  J3Button Version:1.0.0  2008.07.18
*  Author: Jinxin
* cnseven@live.com
*
***********************************************************/

function J3ButtonClass()
{
    jQuery.fn.active = function() {
        if (jQuery(this).hasClass("button")) {
            if (arguments.length == 0) {
                return jQuery(this).hasClass("disabled") ? false : true;
            }
            else {
                if (arguments[0] == true) {
                    jQuery(this).removeClass("disabled");

                    jQuery(this).find("td.left").css("backgroundImage", jQuery(this).data("leftBg"));
                    jQuery(this).find("td.right").css("backgroundImage", jQuery(this).data("rightBg"));
                    jQuery(this).find("td.ButtonContent").css("backgroundImage", jQuery(this).data("centerBg"));
                }
                else {
                    jQuery(this).addClass("disabled");

                    jQuery(this).find("td.left").css("backgroundImage", jQuery(this).data("leftBgDisabled"));
                    jQuery(this).find("td.right").css("backgroundImage", jQuery(this).data("rightBgDisabled"));
                    jQuery(this).find("td.ButtonContent").css("backgroundImage", jQuery(this).data("centerBgDisabled"));
                }

                return jQuery(this);
            }
        }
    }

    this.bind = function(szFilter) {

        $(szFilter + " :button.button,:submit.button").each(function(index) {
            var ButtonType = $(this).attr("type");
            var evt_Click = $(this).get(0).onclick;
            var style = $(this).attr("style");
            var id = $(this).attr("id");
            var name = $(this).attr("name");

            $(this).replaceWith("<table type='button' class='" + $(this).attr("class") + "' border='0' cellpadding='0' cellspacing='0'><tbody><tr><td class='left'>&nbsp;</td><td class='ButtonContent'>" + $(this).attr("value") + "</td><td class='right'>&nbsp;</td></tr></tbody></table>");
            var currButton = $("table[type=button]");

            if (typeof (id) != "undefined") {
                currButton.attr("id", id);
            }
            if (typeof (name) != "undefined") {
                currButton.attr("name", name);
            }
            if (typeof (style) != "undefined") {
                currButton.attr("style", style);
            }
            if (typeof (evt_Click) == "function") {
                currButton.bind("click", function() {
                    // can add some other effect here beofre click
                    if (!currButton.hasClass("disabled")) {
                        evt_Click();
                    }
                }
		    );
            }
            if (ButtonType.toLowerCase() == "submit") {
                currButton.bind("click", function() {
                    __doPostBack(currButton.attr("name"), "");
                });
            }
            currButton.removeAttr("type");

            /*
            currButton.mouseover(function() {
            if (!currButton.hasClass("disabled")) {
            $(this).addClass("over");
            }
            }).mouseout(function() {
            if (!currButton.hasClass("disabled")) {
            $(this).removeClass("over");
            }
            });
            */

            if (currButton.hasClass("disabled")) {
                currButton.data("leftBg", currButton.find("td.left").css("backgroundImage"));
                currButton.data("rightBg", currButton.find("td.right").css("backgroundImage"))
                currButton.data("centerBg", currButton.find("td.ButtonContent").css("backgroundImage"));

                currButton.data("leftBgDisabled", currButton.find("td.left").css("backgroundImage").replace("\.", "_gray."));
                currButton.data("rightBgDisabled", currButton.find("td.right").css("backgroundImage").replace("\.", "_gray."))
                currButton.data("centerBgDisabled", currButton.find("td.ButtonContent").css("backgroundImage").replace("\.", "_gray."));

                currButton.active(false);
            }
            else {
                currButton.data("leftBg", currButton.find("td.left").css("backgroundImage"));
                currButton.data("rightBg", currButton.find("td.right").css("backgroundImage"))
                currButton.data("centerBg", currButton.find("td.ButtonContent").css("backgroundImage"));

                currButton.data("leftBgDisabled", currButton.find("td.left").css("backgroundImage").replace("\.", "_gray."));
                currButton.data("rightBgDisabled", currButton.find("td.right").css("backgroundImage").replace("\.", "_gray."))
                currButton.data("centerBgDisabled", currButton.find("td.ButtonContent").css("backgroundImage").replace("\.", "_gray."));

                currButton.active(true);
            }

            currButton.css("visibility", "visible");



        });

    }
}
J3Button = new J3ButtonClass();

jQuery(function($){
	J3Button.bind("");
});

