/*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,window:true*/
/**********************************************************************
 Universal Sharing
**********************************************************************/
(function () {
 
 var Event = YAHOO.util.Event,
 Dom = YAHOO.util.Dom;
 
 YAHOO.EU.UniversalSharing = (function () {
 var aAnchors, oActive;
 function toggle(o) {
 if (Dom.hasClass(o.parentNode, 'active')) {
 Dom.removeClass(o.parentNode, 'active');
 oActive = null;
 }
 else {
 Dom.addClass(o.parentNode, 'active');
 oActive = o;
 }
 }
 
 function reset(o) {
 if (oActive && oActive !== o) {
 toggle(oActive);
 }
 }
 
 function hide(o) {
 Dom.removeClass(o.parentNode, 'active'); 
 oActive = o;
 }
 
 function show(o) {
 Dom.addClass(o.parentNode, 'active');
 oActive = o;
 }
 function init(o) {
 Event.on(o, "click", function (e) {
 Event.stopEvent(e);
 reset(this);
 toggle(this);
 }); 
 Event.addFocusListener(
 o.parentNode.getElementsByTagName('ul')[0],
 function (e) {
 show(this);
 }, null, o); 
 Event.addBlurListener(
 o.parentNode.getElementsByTagName('ul')[0],
 function (e) {
 hide(this);
 }, null, o); 
 }
 
 return function (a) {
 aAnchors = a;
 
 for (var i = 0; i < a.length; i = i + 1) {
 init(Dom.get(a[i]));
 }
 
 Event.on(document.body, "click", reset);
 }; 
 }());
 Event.onDOMReady(function enhanceUniversalSharingToolbar() {
 var fixHover = function (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(el, 'mouseover', function addHoverClass(evt) {
 Dom.addClass(this, 'hover');
 });
 Event.on(el, 'mouseout', function removeHoverClass(evt) {
 Dom.removeClass(this, 'hover');
 }); 
 } 
 
 };
 
 if (!document.getElementById('ynw-share')) {
 return;
 }
 
 
 Dom.addClass(document.getElementById("ynw-universal-sharing")
 .parentNode, "js");
 
 
 Dom.batch('ynw-share',
 function replaceShareLabelWithShareButton(shareLabelEl) {
 var button = document.createElement('button'),
 i,
 node,
 span = document.createElement('span');
 // dropdownLinks =
 // shareLabelEl.parentNode.getElementsByTagName('a'); 
 
 /* 1. Delete superfluous punctuation. */ 
 Dom.getElementsByClassName('punctuation', 'span', shareLabelEl,
 function deletePunctuationEl(punctuationEl) {
 Event.purgeElement(punctuationEl);
 punctuationEl.parentNode.removeChild(punctuationEl);
 });
 
 /* 2. Create a button using the contents of the label: */
 
 /* IE6 errors if you set "button.type" */ 
 button.setAttribute('type', 'button'); 
 
 for (i = 0; (node = shareLabelEl.childNodes[i]); i = i + 1) {
 span.appendChild(node.cloneNode(true));
 }
 
 button.id = shareLabelEl.id;
 button.className = shareLabelEl.className; 
 
 button.appendChild(span); 
 fixHover(button);
 
 /* 3. Swap in the button for the existing label: */
 shareLabelEl.parentNode.replaceChild(button, shareLabelEl);
 Event.purgeElement(shareLabelEl);
 
 YAHOO.EU.UniversalSharing(['ynw-share']); 
 });
 if ('undefined' !== window.print &&
 'undefined' !== YAHOO.EU.UniversalSharingPrintString) {
 
 Dom.batch('ynw-universal-sharing',
 function appendPrintButton(universalSharingListEl) { 
 var li = document.createElement('li'),
 button = document.createElement('button'),
 span = document.createElement('span');
 /* IE6 errors if you set "button.type" */ 
 button.setAttribute('type', 'button');
 button.className = 'print';
 span.appendChild(document.createTextNode(
 YAHOO.EU.UniversalSharingPrintString));
 button.appendChild(span);
 Event.on(button, 'click', function printPage() {
 window.print();
 });
 fixHover(button);
 li.appendChild(button);
 universalSharingListEl.appendChild(li);
 });
 
 }
 
 });
 
}()); 
