// JavaScript used on the Resource Center.

// Setup namespaces
if (!CC) {
    var CC = {};
}
if (!CC.RC) {
    CC.RC = {};
}

// Displays the close winodw link if the window was opened as a popup
function toggleCloseWindowLink() {
    return window.name && window.name != '' ? document.getElementById("close-window-link").style.display = '' : null;
}

// handles skinning the product poll for the Resource Center Community homepage.
var PollController = function() {

    var _pollDiv;

    return {
        getEl: function() {
            if (!_pollDiv) {
                _pollDiv = function() {
                    var divs = document.getElementsByTagName('div');
                    for (var i = 0, len = divs.length; i < len; i++) {
                        if (divs[i].className == 'cc-poll-container') {
                            return divs[i];
                        }
                    }
                }();
            }

            return _pollDiv;
        },

        reskinPoll: function() {

            var el = this.getEl();

            if (el.firstChild.nodeName == 'TABLE') {
                return this.reskinResults();
            }

            var o = el.parentNode;
            while (o && o.nodeName != 'TABLE') {
                o = o.parentNode;
            }

            // rm wrapper tbl
            o.parentNode.appendChild(el.parentNode.removeChild(el));
            o.parentNode.removeChild(o);

            el.firstChild.firstChild.className = 'question';
            el.lastChild.className = 'footer';

            var f = function() {
                el.style.visibility = 'hidden';
                setTimeout(function(){
                    PollController.reskinNewResults();
                }, 100);
            }

            var btn = el.firstChild.lastChild.firstChild;
            btn.className = 'btn';
            (!btn.addEventListener ? btn.attachEvent('onclick', f) : btn.addEventListener('click', f, false));
        },

        reskinResults: function() {
            var o = this.getEl().firstChild;
            o.width = 180;

            while (o && o.nodeName != 'TD') {
                o = o.firstChild;
            }
            o = o.firstChild;

            o.firstChild.firstChild.className = 'question';
            o.parentNode.lastChild.className = 'note';
            o.lastChild.className = 'footer';
        },

        reskinNewResults: function() {
            var el = this.getEl().firstChild.firstChild;
            while (el.className != 'question') {
                el.className = 'question';
            }
            this.getEl().style.visibility = 'visible';
        }
    };
}();