/**
 * Handles conflicts with prototype. Use $j for jquery namespacing until prototype is no more.
 */
var $j = jQuery.noConflict();
/**
 * New onload collection for global js
 */
$j(document).ready( function(){
    updateCopyrightLine();
}
);
/**
 * Cross browser method for updating copyright line with correct year in .html files
 */
function updateCopyrightLine() {
	var pathName = String(document.location.pathname);
	if (pathName.indexOf(".html") > 0) {
    	var todaysDate = new Date();
		var todaysYear = todaysDate.getFullYear();
		var copyrightLine = "&copy; " + todaysYear + " Man Investments | A member of the Man Group";
		$j("#copyright").html(copyrightLine);
	}
}
/**
* Creates a clone of the thead of a table into a span above the table. so it appears while scrolling.
**/
(function($){
 $.fn.scrollableTable = function(options) {
	var defaults = {};
	options = $.extend(defaults, options);
	return this.each(function( scrollingTablesId ) {
		var obj = $(this);
		var table = $("div > table", obj);
		var span = $("span:first", obj);
		span.css("position", "absolute");
		span.css("z-index", "100");
		span.html('<table></table>');
		var newTable = $("table", span);
		var colGroups = table.children("colgroup");
		colGroups.appendTo(newTable);
		newTable.width(table.width());
		newTable.attr("id", "scrollingHeader" + (scrollingTablesId + 1));
		newTable.attr("class", table.attr("class"));
		newTable.attr("border", table.attr("border"));
		newTable.attr("cellpadding", table.attr("cellpadding"));
		newTable.attr("cellspacing", table.attr("cellspacing"));
		var tableHeader = table.children("thead");
		tableHeader.clone().appendTo(newTable);
		var headings = $("thead > tr.header > th", newTable);
		$.each(headings, function( i ) {
			var str = "tr.header > th:eq(" + i + ")";
			var w = $(str, tableHeader).width();
			$(this).width(w);
		});				
		tablesInited = true;
    });
 };
})(jQuery);
/* set table banding */
(function($){
    $.fn.formatTable = function( options ){
        var defaults = {
			cssClass: "alternate",
			altRowHighlight: false
		};
        options = $.extend(defaults, options);
        return this.each(function(){
            //If there is a thead then need to change the order of the banding 
            if ($("thead > tr", this).length > 0) {
                var rows = $("tbody > tr", this);
                addClassesToRows(rows, 0);
            } else {
                var rows = $("tr", this);
                addClassesToRows(rows, 1);
            }
            //Pass in mod of 0 to add classes to even rows or 1 for odd rows 
            function addClassesToRows( rows, mod ) {
                $.each(rows, function(iter){
                    if(options.altRowHighlight) { //if doing alternate banding add 1 to acheive this
                        iter = iter + 1;
                    }
                    if (iter % 2 == mod) {
                        $(this).addClass(options.cssClass);
                    } else {
                        $(this).removeClass(options.cssClass);
                    }
                });
            }
        });
    };
})(jQuery);
/* end set table banding */
/* display overlay */
(function($){
    $.displayOverlay = function( options ) {
        var overlayUrl = "";
        var defaults = {
            type:     "inline",
            inlineId: "overlay-contents",
            url:      "",
            caption:  "",
            modal:    false,
            height:   600,
            width:    600,
            callback: null
        };
        options = $.extend(defaults, options);
        var type = options.type.toLowerCase();
        switch(type) {
            case "inline":
               overlayUrl = "#TB_inline?inlineId=" + options.inlineId + "&";
               break;
            case "iframe": //iframe and ajax have same beginning 
               overlayUrl = options.url + "?TB_iframe=true&";
               break;
            case "ajax":
               overlayUrl = options.url + "?";
               break;
           default:
               return;
        }
        overlayUrl += "height=" + options.height + "&width=" + options.width;
        if(options.modal === true) {
            overlayUrl += "&modal=true";
        }
        tb_show(options.caption, overlayUrl, null, options.callback);
        return overlayUrl; //return the url so it can be tested
    }
})(jQuery);
/* remove overlay */
/* Function for removing the overlay to allow for swapping of overlay libraries */ 
(function($){
    $.removeOverlay = function(){
        tb_remove();        
		$j("#leftNav ul ul, p, #searchForm").redraw(); //redraw certain elements which IE messes up when overlay is hidden
    }
})(jQuery);
/* end overlay */

/**
 * Force a redraw to fix browser redraw issues
 */
(function($){
$.fn.redraw = function(){
	var element = $(this);
	var n = document.createTextNode(' ');
	element.append(n);
	window.setTimeout(function(){n.parentNode.removeChild(n)}, 0);
	return element;
}
})(jQuery);
