$(document).ready(function() {
    setupNewWindowLinks();
    EIP.UTIL.removePaddedBlock();
    EIP.UTIL.labelApplyOver();
});

//SETTING GLOBAL NAMESPACE FOR EIP
var EIP = EIP || {};

//UTILITIES NAMESPACE FOR GENERAL TOOLS USED THROUGHOUT EIP
EIP.UTIL = {

    removePaddedBlock : function () {
	    var podcastWidget = $('.podcast_widget');
    	if (podcastWidget.length) {
    		podcastWidget.removeClass('padded-block');
    	}
    },
    labelApplyOver: function() {
        $('label.apply-over').labelOver("over");
    }
    /*
    setupNewWindowLinks : function () {
	var domain = document.domain;
	// Complex selector but really just says get all links in footer or content which contain http and aren't in the EIP domain
	$("#footer a[href*=http]:not([href*=" + domain + "]),#content a[class!='mark'][href*=http]:not([href*=" + domain + "]):not(ul.qualitystrategyLogo a):not(ul.searchProviders a):not(a.externalImage)")
	.append('<span class="newWindow" title="Opens in a new window">&nbsp;</span>')
        .attr("target", "_blank");
	$('.externalImage').attr("target", "_blank");
    },
    

    */

};

//This function need to be called by widgetlibrary ajax function. t must have the same name.
function setupNewWindowLinks() {
    var domain = document.domain;
	// Complex selector but really just says get all links in footer or content which contain http and aren't in the EIP domain
    appendNewWindowSpan("#footer a[href*=http]:not([href*=" + domain + "]),#content a[class!='mark'][href*=http]:not([href*=" + domain + "]):not(ul.qualitystrategyLogo a):not(ul.searchProviders a):not(a.externalImage)");
	//.append('<span class="newWindow" title="Opens in a new window">&nbsp;</span>')
      //  .attr("target", "_blank");
	$('.externalImage').attr("target", "_blank");
}

function appendNewWindowSpan(selector) {
    $(selector).append('<span class="newWindow" title="Opens in a new window">&nbsp;</span>')
        .attr("target", "_blank");
}

//Global Tab functionality - using fading
function tabFader(tabTrigger, tabSetions) {
	tabSetions.hide().filter(':first').show();
	tabTrigger.filter(':first').parent().addClass('active');
	
	tabTrigger.click(function()
	{
		var them = $(this.hash);		
		
		tabTrigger.parent().removeClass('active');
		$(this).parent().addClass('active');
		
		tabSetions.filter(':visible').fadeOut("slow", function() {
			them.fadeIn("slow");
		});
		
		return false
	});
}




//Global Tab functionality - using show/hide
function tabShowHide(tabTrigger, tabSetions) {
	tabSetions.hide().filter(':first').show();
	tabTrigger.filter(':first').parent().addClass('active');
	
	tabTrigger.click(function()
	{
		var them = $(this.hash);
		
		tabTrigger.parent().removeClass('active');
		$(this).parent().addClass('active');
		
		tabSetions.hide();
		them.show();
		
		return false
	});
}




//Global Tab functionality - using sliding
function tabSlider(tabTrigger, tabSetions) {
	tabSetions.hide().filter(':first').show();
	tabTrigger.filter(':first').parent().addClass('active');
	
	tabTrigger.click(function()
	{
		var them = $(this.hash);
		
		tabTrigger.parent().removeClass('active');
		$(this).parent().addClass('active');
		
		tabSetions.filter(':visible').slideUp("slow", function() {
			them.slideDown("slow");
		});
		
		return false
	});
}

//----- Recent Activity Widget Functionality -----//
jQuery.hookUpRecentActivityWidget = function(maxItems) {
    $(document).ready(function() {
        var recentActivityWidget = $('div.RecentActivity div.boxContent div.boxInner');

        if ($('.ActivityList').size() > 1) {
            $('.ActivityList:first').remove();
        }

        var html = '<ul class="ActivityMenu fc">' +
                    '<li class="first selected"><a href="#AllList">All</a></li>';

        recentActivityWidget.find('div.ActivityList h3').each(function() {
            var list = $(this).next('ul');
            html += '<li><a href="#' + list.attr('class') + '">' + $(this).text() + '</a></li>';
        });

        html += '</ul>';

        recentActivityWidget.find('ul, h3').css('display', 'none');

        recentActivityWidget.prepend(html);

        recentActivityWidget.find('ul.ActivityMenu li a').click(function() {
            $(this).parents('ul.ActivityMenu').find('li').removeClass('selected');
            $(this).parents('li').addClass('selected');

            recentActivityWidget.find('ul:not(.ActivityMenu), h3').css('display', 'none');

            var indexOfHash = $(this).attr('href').indexOf('#');
            var className = $(this).attr('href').substring(indexOfHash + 1);
            recentActivityWidget.find('ul.' + className).css('display', 'block');

            return false;
        });

        var allItemClasses = new Array();

        recentActivityWidget.find('.ActivityList > ul > li').each(function() {
            allItemClasses.push($(this).attr('class').replace(' alt', ''));
        });

        allItemClasses.sort();
        allItemClasses.reverse();

        var allItemsHtml = '<ul class="AllList">';

        var itemLength = allItemClasses.length < maxItems ? allItemClasses.length : maxItems;

        var altRow = false;
        for (var i = 0; i < itemLength; i++) {
            var element = recentActivityWidget.find('.ActivityList ul li.' + allItemClasses[i]);
            allItemsHtml += '<li' + (altRow ? ' class="alt"' : '') + '>' + element.html() + '</li>';

            altRow = !altRow;
        }

        allItemsHtml += '</ul>';

        recentActivityWidget.find('.ActivityList').append(allItemsHtml);

    });
};

