
$(document).ready(function() {
	if($.browser.msie) $("#body-wrapper").addClass("ie");
	if($.browser.msie && $.browser.version=="6.0") $("#body-wrapper").addClass("ie6");
    $("#page-wrapper table tr:nth-child(odd)").addClass("table-alt-row");
    $("ul.content-list > li:nth-child(odd)").addClass("list-alt-row");
    $("#content-wrapper a:has(img)").removeClass("externallink");
    $("a[href$=pdf], a[href*='oid=']").addClass("pdf");
	
	$("a[href$=pdf], a[href*='oid=']")
		.addClass("externallink")
        .attr({
            target: "_blank"
        });
	$("a[href^='http'], a[href*='target=_blank']")
		.addClass("externallink")
        .attr({
            target: "_blank"
        });	
    $("#content-wrapper a[target='_blank' or target='_new']")
        .addClass("externallink")
        .attr({
            target: "_blank"
        });
	$("#country-selector a")
		.removeClass("externallink")
        .attr({
            target: "_parent"
        });	
    $("a.nopdfbg").removeClass("pdf").removeClass("externallink");
    $("a[href$=zip]").addClass("zip");
    $("a[href$=xls]").addClass("xls");
    $("td p:first-child").addClass("first-para");
    $("#breadcrumb ul li:last-child").addClass("last");
    $("ul.inline li:first-child").addClass("first");
    $("ul.inline li:last-child").addClass("last");
	$(".subnav").expandNavigation();
    $("#content-header:not(:has(*))").remove();
    $("#content-primary > *:first-child").css("margin-top", "0px");

	if($(".quicklinks .pdf")){
		$(".quicklinks li:has(.pdf)").css("background","url('/css/images/pdf.gif') no-repeat scroll right 5px transparent");
	}

	if($(".quicklinks .xls")){
		$(".quicklinks li:has(.xls)").css("background","url('/css/images/excel-icon.gif') no-repeat scroll right 5px transparent");
	}

	$(".fadehover img.top").hover(
        function() {
            $(this).animate({"opacity": "0"}, "slow");
        },
        function() {
            $(this).animate({"opacity": "1"}, "slow");
        }
    );
    $("a[href*='rel=lightbox'], a.lightbox[href*='.swf'], a.lightbox[href*='.html']").each(function(index) {
        $(this).showLightbox({resize: true});
    });
    $("a.lightbox").each(function(index) {
        $(this).showLightbox();
    });
	$(".nav li.selected > a").css("font-weight", "bold"); // Fix for IE6 left navigation so selected item bolds
	$("a.pdf:has(img)").removeClass("pdf");
	$("#compliance-popup").showDisclaimer(); // Do this last so nothing is waiting while the pop-up is being show.
    $(".strip-bar").each(function(index) {
        //$(this).showAlert();
    });
	$("iframe").load(function() {
		scroll(0,0);
	});
	$(".video-overlay").overlay("video-overlay");
	$(".audio-overlay").overlay("audio-overlay");
});

jQuery.fn.showAlert = function() {
    if ($("#content-wrapper").hasClass("invisible") == false) { // Only show if page doesn't show compliance warning
        var cookieName = "schroders.alert." + $(this).attr("id");
        if ($.cookie(cookieName) != "1") {
            $(this).delay(1800).slideDown(600);
            $(this).click(function() { // Hide alert and set cookie when user clicks on background (including image
                $(this).slideUp(600);
                $.cookie(cookieName, "1", {
                    expires: 365,
                    path: "/"+siteId
                });
            });
        }
    }
}

// showDisclaimer function
// params
// show - whether to show the disclaimer. Passing false will not show the disclaimer but will make sure
//        the content becomes visible (similar to the cookie being set).
jQuery.fn.showDisclaimer = function() {
	$("#content-wrapper").removeClass("invisible");
	if (this.length) {
        if ($.getParameter("nd")) {
            $("#content-wrapper").removeClass("invisible");
            return;
        }
        if ($.cookie("schroders.compliance") != "1") {
            var dialogButtons = {};
            var confirmButton = $("#complianceConfirmButtonText").text();
            var cancelButton = $("#complianceCancelButtonText").text();
            var expiryDays = $("#complianceExpiryDays").text();
            dialogButtons[confirmButton] = function() {
                $.cookie("schroders.compliance", "1", {
                    expires: parseInt(expiryDays),
                    path: "/"+siteId
                });
                $("#content-wrapper").removeClass("invisible");
                $(this).dialog('close');
            };
            dialogButtons[cancelButton] = function() {
				location.pathname = "/";
            };
            this.dialog({
                height:480,
                width:760,
                bgiframe: true,
                resizable: false,
                draggable: false,
                modal: true,
                closeOnEscape:false,
                overlay: {
                    backgroundColor: '#000',
                    opacity: 0.6
                },
                buttons: dialogButtons,
				open: function(event, ui) { $(".ui-dialog-titlebar-close").hide(); }
            });
			$(".ui-dialog-buttonpane button:first-child").addClass("button-highlight");
			$(".ui-dialog-buttonpane button:last-child").addClass("button-lowlight");
        } else {
            $("#content-wrapper").removeClass("invisible");
        }
    } else {
        $("#content-wrapper").removeClass("invisible");
    }
};

// Sorts out the sub-navigation treeview to expand section with selected item in.
jQuery.fn.expandNavigation = function() {
    this.treeview({
        collapsed: true,
        //unique : true,
        animated: "fast"
    });
    var selectedItem = this.find("li.selected");
	$(selectedItem).expandNode();
	if ($.getParameter("ex")) {
		selectedItem.parents("ul").children().expandNode();
	}
}

jQuery.fn.expandNode = function() {
	this.parents("li").andSelf().children("div").swapClass("expandable-hitarea", "collapsable-hitarea");
    this.parents("li").andSelf().swapClass("expandable", "collapsable");
    this.parents("ul").css("display", "block");
	this.children("ul").css("display", "block");
}

jQuery.extend ({
     getParameter: function(name, qs) {
         var queryString = !qs ? window.location.href : qs;
         name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
         var regexS = "[\\?&]"+name+"=([^&#]*)";
         var regex = new RegExp(regexS);
         var results = regex.exec(queryString);
         if (results == null)
             return "";
         else
             return results[1];
     }
});

// Shows a lightbox. Takes width and height from the destination URL and uses them if there. Otherwise uses defaults
jQuery.fn.showLightbox = function(resize) {
    if (resize) {
        var href = $(this).attr("href");
        var w = $.getParameter("w", href);
        var h = $.getParameter("h", href);
		var t = $.getParameter("title", href).replace(/%20/g, " ");
        $(this).colorbox({title: t,innerWidth:(w!="" ? w : "640")+"px", innerHeight:(h!="" ? h : "400")+"px", iframe:true, opacity:0.80, scrolling: false});
    } else {
        $(this).colorbox({title: t,iframe:true, opacity:0.80, scrolling: false});
    }
}

jQuery.fn.chartable = function(options) {
	var table = $(this);
	var defaults = {
		legend: 'right', 
		enableTooltip: true, 
		width: 480, 
		height: 240, 
		is3D: true,
		title: table.find("caption").text(),
		colors:['#003772','#ff9900','#114883','#ee8800','#224993','#dd7700','#335aa4','#cc6600']
	};
	var opts = $.extend(defaults, options);
	google.load("visualization", "1", {packages:["piechart", "linechart", "table"]});
	google.setOnLoadCallback(function() {
		var data = new google.visualization.DataTable();
		data.addColumn('string', table.find("thead tr th:nth-child(1)").text());
		for (i=2; i<table.find("thead tr th").size()+1; i++) {
			data.addColumn('number', table.find("thead tr th:nth-child(" + i + ")").text());
		}
		data.addRows(parseInt(table.find("tbody tr").size()));
		var i = 0;
		table.find("tbody tr").each(function() {
			data.setValue(i,   0, $(this).find("td:nth-child(1)").text());
			for (j=2; j<table.find("thead tr th").size()+1; j++) {
				data.setValue(i, j-1, parseInt($(this).find("td:nth-child(" + j +")").text()));			
			}		
			i++;
		});
		if (table.hasClass("pie")) {
			var chart = new google.visualization.PieChart(document.getElementById(table.attr("id")));
			chart.draw(data, opts);
		} else if (table.hasClass("line")) {
			var chart = new google.visualization.LineChart(document.getElementById(table.attr("id")));
			chart.draw(data, opts);
		} else if (table.hasClass("table")) {
			var newTable = new google.visualization.Table(document.getElementById(table.attr("id")));
			newTable.draw(data, {showRowNumber: false});
		}
	});
	table.show();
}

jQuery.fn.log = function (msg) {
	if (window.console) window.console.log("%s: %o", msg, this);
	return this;
};

jQuery.fn.embedMedia = function(mediaUrl, splashImageUrl, config) {
	$f("player", {src: "/css/images/swf/flowplayer-3.2.6.swf", wmode: 'transparent'}, {
		playlist: [
			//{url: splashImageUrl},
			{
				url: mediaUrl,
				autoPlay: false,
				autoBuffering: true,
				onStart: function(clip) {
					gaPageTracker._trackEvent(siteId, "media-play", clip.url);
				},
				onPause: function(clip) {
					gaPageTracker._trackEvent(siteId, "media-pause", clip.url, parseInt(this.getTime()));
				},
				onStop: function(clip) {
					gaPageTracker._trackEvent(siteId, "media-stop", clip.url, parseInt(this.getTime()));
				},
				onFinish: function(clip) {
					gaPageTracker._trackEvent(siteId, "media-finish", clip.url);
				}
			}
		],
		plugins: {
			controls: {
				stop: true
			}
		}
	}).ipad();
};

$(document).ready(function() {
	$(".embedded-media").each(function() {
		$(this).embedMedia2($(this).attr("href"),$(this).attr("src"), "");
	});
});
jQuery.fn.embedMedia2 = function(mediaUrl, splashImageUrl, config) {
	$f($(this).attr("id"), {src: "/css/images/swf/flowplayer-3.2.6.swf", wmode: 'transparent'}, {
		playlist: [
			{url: splashImageUrl},
			{
				url: mediaUrl,
				autoPlay: false,
				autoBuffering: true
			}
		],
		plugins: {
			controls: {
				stop: true
			}
		}
	}).ipad();
};

jQuery.fn.embedMedia3 = function(mediaUrl, splashImageUrl, config) {
	$f($(this).attr("id"), {src: "/css/images/swf/flowplayer-3.2.6.swf", wmode: 'transparent'}, {
		playlist: [
			//{url: splashImageUrl},
			{
				url: mediaUrl,
				autoPlay: false,
				autoBuffering: true
			}
		],
		plugins: {
			controls: {
				stop: true
			}
		}
	}).ipad();
};

jQuery.fn.overlay = function(className) {
	//$(this).after("<div class='" + className + "'>");
};

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.toEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
		$(this).children().css({'height': currentTallest}); 
	});
	return this;
};
