$(document).ready(function() {

    Buttons.init();

});

Buttons = {

    init: function() {

        $("a.button, button, input[type=button], input[type=submit]").each( function() {; 

            if( !$(this).data('button') ) {

                // Den '<span class="icon"> nur dazu, wenn class icons vorhanden!
                // sonst darst. fehler in IW und Webkit
                var spantext ='<span class="text">';
                if ($(this).hasClass('icons')) {
                  spantext= spantext + '<span class="icon"></span>';
                }
 
               // Links
                if(this.tagName.toUpperCase()=="A") {
                    
                    var text = $(this)
                        .addClass("Button")
                        .addClass("icons")
                        .html();

                  if ($(this).find('span').length == 0) {
                    $(this).empty().append(spantext + text + "</span>").bind("mousedown", function(){
                      $(this).addClass('buttonActive');
                    }).bind("mouseup", function(){
                      $(this).removeClass('buttonActive');
                    });
                  }
                }

                else if(this.tagName.toUpperCase()=="BUTTON") {

                    var original = this;

                    var classes = $(this).attr('class');
                    var style = $(this).attr('style');

                    var text = $(this)
                        .css({position: "absolute", top: "-1000px"})
                        .html();

                    $("<a href='' class='Button "+classes+"' style='"+style+"'>" + spantext + text+"</span></a>")
                        .bind("click", function() {
                            original.click();
                            return false;
                        })
                        .bind("mousedown", function() {$(this).addClass('buttonActive');})
                        .bind("mouseup", function() {$(this).removeClass('buttonActive');})
                        .insertAfter(original);

                }

                else if(this.tagName.toUpperCase()=="INPUT") {

                    var original = this;
                    var text = this.value;

                    var classes = $(this).attr('class');
                    var style = $(this).attr('style');

                    if(this.type=="submit") {
                        text = "<b>"+text+"</b>";
                    }

                    $(this).css({position: "absolute", top: "-1000px"});



                    $("<a href='' class='Button "+classes+"' style='"+style+"'>" + spantext +text+"</span></a>")
                        .bind("click", function() {
                            original.click();
                            return false;
                        })
                        .bind("mousedown", function() {$(this).addClass('buttonActive');})
                        .bind("mouseup", function() {$(this).removeClass('buttonActive');})
                        .insertAfter(original);

                }

                $(this).data("button", true);

            }
        });

    }

}
