(function() {
    YAHOO.util.Event.onContentReady("cmReleases", function() {
        var cmTv = new YAHOO.widget.TabView("ConservativeMovement");

        // Insert left/right tab decorations
        var tabCount = cmTv.get("tabs").length;
        var i;
        for (i = 0; i < tabCount; i++) {
            var el = cmTv.getTab(i).get('labelEl').parentNode;
            el.innerHTML += '<div class="leftImg"></div><div class="rightImg"/></div>';
        }
        YAHOO.util.Dom.addClass("ConservativeMovement", "ready");
        var select = YAHOO.util.Selector.query("select", "cmLatest", true);

        YAHOO.util.Event.addListener(select, "change", function(e) {
            var id = select.options[select.selectedIndex].value;
            if (id !== '') {
                window.location.href = '/issues/issue.aspx?id=' + id;
            }
        });

        // Register the mouseup event to replace the href with our tracking href at the last moment
        YAHOO.util.Event.on("ConservativeMovement", "mouseup", function(e) {
            var target = YAHOO.util.Event.getTarget(e);

            // Check to see if this is a node we care about
            if (target.nodeName == "A" && target.attributes.partner) {
                var activeTabIndex = cmTv.get('activeIndex');
                var activeTabName = cmTv.getTab(activeTabIndex).get('label');
                var sourceId = 1;
                //This value was hardcode into the administrative backend
                var linkType = 1;

                switch (activeTabName) {
                    case 'Latest':
            	        linkType=1;
                        sourceId = 14;
                        break;
                    case 'Releases':
            	        linkType=4;
                        sourceId = 13;
                        break;
                    case 'Action':
            	        linkType=3;
                        sourceId = 12;
                        break;
                    case 'Blog':
            	        linkType=2;
                        sourceId = 11;
                        break;
                    default:
            	        linkType=1;
                        sourceId = 1;
                }

                // Only follow click event for non-right mouse button clicks
                var rightclick;
                if (!e) {
                    e = window.event;
                }
                if (e.which) {
                    rightclick = (e.which == 3);
                } else if (e.button) {
                    rightclick = (e.button == 2);
                }
                if (rightclick) { return; }

                if (target.attributes.hash) {
                    target.href = "/partner.aspx?lt="+ linkType +"&s=" + sourceId + "&h=" + target.attributes.hash.nodeValue + "&p=" + target.attributes.partner.nodeValue + "&t=" + activeTabName;
                } else {
                    target.href = "/partner.aspx?lt="+ linkType +"&s=" + sourceId + "&u=" + target.attributes.partner.nodeValue + "&t=" + activeTabName;
                }
                return false;
            }
            YAHOO.util.Event.stopEvent(e);
        });
    });
})();
