/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: true, 
 onevar: true, plusplus: true, regexp: true, undef: true, white: true, 
 indent: 4 */ 
/*global YAHOO:true, document:true */ 
/**
 * Fix Buzz Up! button hover effects for all Buzz Up! forms on the page.
 */
(function () { 
 var Dom = YAHOO.util.Dom,
 Event = YAHOO.util.Event;
 
 Event.onDOMReady(function fixBuzzUpForms() { 
 
 Dom.getElementsByClassName('buzz-up', 'form',
 document.body, function fixBuzzUpForm(el) {
 Dom.getElementsBy(function isSubmit(el) {
 return (el.type.toLowerCase() === 'submit');
 }, 'input', el, function fixSubmit(el) {
 
 /**
 * Opera does not support "text-decoration: underline"
 * on "input type='submit'" (bug DSK-260057). Test case:
 *
 * http://public.yahoo.com/benh/tests/submit-input-underline-test.html
 * 
 * But it does support rendering underline on "button"
 * elements. We want to use "input type='submit'" for
 * C-grade browsers (which don't all implement "button"),
 * but, as C-grade browser should not be served JS, we can
 * replace "input type='submit'" with "button" when
 * JS is available.
 *
 * Known bug: Underline does not render (on hover) in
 * Opera 9.6 when CSS is on but JS is off.
 */
 
 var button = document.createElement('button'),
 span;
 
 /**
 * IE6, IE7, and IE8 error with "button.type", so use
 * "button.setAttribute" instead.
 */
 button.setAttribute('type', 'submit'); 
 
 /**
 * IE6 and IE7 fail to set "type" to match the "type"
 * attribute. But they do set type if you set it with
 * "innerHTML". So bug detect, then feature detect before
 * applyng fix.
 */
 if (button.getAttribute('type').toLowerCase() !== button.type.toLowerCase()) {
 span = document.createElement('span');
 
 if ('undefined' !== typeof(span.innerHTML)) {
 span.innerHTML = '<button type="submit"></button>';
 button = span.removeChild(span.firstChild);
 }
 else {
 /**
 * Browser can't set "type" and doesn't understand
 * unstandardized "innerHTML", so give up.
 */
 return;
 }
 
 } 
 
 /**
 * Ticket 2952959: Add Buzz button tooltip.
 */
 if (el.title) {
 button.title = el.title;
 }
 
 button.appendChild(document.createTextNode(el.value));
 
 el.parentNode.replaceChild(button, el);
 
 /**
 * Clear any listeners attached to the replaced element
 * by other scripts:
 */
 Event.purgeElement(el);
 
 /**
 * Of the A-grade browsers, only IE6 does not support
 * ":hover" on "button". Hence the ugly browser sniffing.
 * 
 * @todo Could we bug detect this? I tried detecting it by 
 * introspecting rules in a test stylesheet, but it seems
 * "button:hover" is recognized as a selector by IE6 if 
 * added with "addRule".
 */
 if (YAHOO.env.ua.ie > 0 && YAHOO.env.ua.ie < 7) { 
 Event.on(button, 'mouseover', function addHoverClass(evt) {
 Dom.addClass(this, 'hover');
 });
 Event.on(button, 'mouseout', function removeHoverClass(evt) {
 Dom.removeClass(this, 'hover');
 }); 
 
 }
 
 });
 
 });
 
 });
 
}());