
// 'Superfish' - Drop down menus

$(document).ready(function(){
	$("#globalNav").superfish({
		delay : 50,
		animation : {opacity:"show", height:"show"},
		speed : "slow"
	});
});


// Local Nav style workaround
$(document).ready(function(){
	$("#localNav > li:first-child").addClass("ln-first");
	$("#localNav > li:nth-child(2)").addClass("ln-second");
});


// Allens Snapshot Ticker
$(document).ready( function(){ 
	$('#snapshot div').innerfade({ 
		timeout: 4000, 
		speed: 'slow'
	}); 
});



// Footer Navigation Image Rollovers
// Unfortunately Javascript needs to be used for the image rollovers.

// Adds '-over' to the end of the filename of any .gif in #legalsNav.
// eg. nav.gif becomes nav-over.gif
$(document).ready( function(){ 
	$("#legalsNav a img").hover(
		function(){
			var x = $(this).attr("src");
			x = x.substring(0, x.indexOf('.gif'));
			x = x + "-over.gif";
			$(this).attr({
				src: x
			});
		},
		function(){
			var x = $(this).attr("src");
			x = x.substring(0, x.indexOf('-over.gif'));
			x = x + ".gif";
			$(this).attr({
				src: x
			});		
		}
	);
});



// Tabbed navigation
$(document).ready( function() {
	//$("#pubNav > ul").tabs();
});

$(document).ready( function() {
	var hashed = document.location.hash;
	if (hashed != "") {
		$("#pubNav > ul").tabs("select", hashed);
	}
});

// Add 'first' and 'last' classes to first and last items in tabbed nav.
// This removes the dividers and spacing from the first and last items.
$(document).ready( function() {
	$("#pubNav div ul li:first-child").addClass("first");
	$("#pubNav div ul li:last-child").addClass("last");
});



// 3 Column List
function threeCols(src, type) {
	var origList = src;

	var leftList = document.createElement(type);
	var centerList = document.createElement(type);
	var rightList = document.createElement(type);
	var container = document.createElement('div');

	var items = origList.getElementsByTagName('LI');

	var itemsLength = items.length/3;

	for (i = 0; i < itemsLength; i++) {
		leftList.appendChild(items[0]);
	}

	itemsLength = items.length/2;

	for (i = 0; i < itemsLength; i++) {
		centerList.appendChild(items[0]);
	}

	itemsLength = items.length;

	for (i = 0; i < itemsLength; i++) {
		rightList.appendChild(items[0]);
	}

	container.appendChild(leftList);
	container.appendChild(centerList);
	container.appendChild(rightList);

	leftList.setAttribute('class', 'left column');
	centerList.setAttribute('class', 'center column');
	rightList.setAttribute('class', 'right column');
	container.setAttribute('class','threecol');
	if (document.all) {
		leftList.setAttribute('className', 'left column');
		centerList.setAttribute('className', 'center column');
		rightList.setAttribute('className', 'right column');
		container.setAttribute('className','threecol column');
	}
	if (type == 'ol') {
		centerList.setAttribute('start', leftlist.getElementsByTagName('LI').length + 1 );
		rightList.setAttribute('start', centerList.getElementsByTagName('LI').length + 1 );
	}
	origList.parentNode.replaceChild(container, origList);
}

function allThreeCols (whichclass, type) {
	var uls = document.getElementsByTagName(type);
	for (var i=0; i< uls.length; i++) {
		if (uls[i].getAttribute('class') == whichclass || uls[i].getAttribute('className') == whichclass) {
			threeCols(uls[i], type.toLowerCase());
		}
	}
}

// Two Column List
function twoCols(src, type) {
	var origList = src;

	if (origList.getElementsByTagName('LI').length != 1)
	{

		var leftList = document.createElement(type);
		var rightList = document.createElement(type);
		var container = document.createElement('div');

		var items = origList.getElementsByTagName('LI');

		var itemsLength = items.length/2;
		for (i = 0; i < itemsLength; i++) {
			leftList.appendChild(items[0]);
		}

		itemsLength = items.length;

		for (i = 0; i < itemsLength; i++) {
			rightList.appendChild(items[0]);
		}
		container.appendChild(leftList);
		container.appendChild(rightList);

		leftList.setAttribute('class', 'left column');
		rightList.setAttribute('class', 'right column');
		container.setAttribute('class','twocol');
		if (document.all) {
			leftList.setAttribute('className', 'left column');
			rightList.setAttribute('className', 'right column');
			container.setAttribute('class','twocol');
		}
		if (type == 'ol') {
			rightList.setAttribute('start', leftList.getElementsByTagName('LI').length + 1 );
		}
		origList.parentNode.replaceChild(container, origList);

	}
}

function allTwoCols (whichclass, type) {
	var uls = document.getElementsByTagName(type);
	for (var i=0; i< uls.length; i++) {
		if (uls[i].getAttribute('class') == whichclass || uls[i].getAttribute('className') == whichclass) {
			twoCols(uls[i], type.toLowerCase());
		}
	}
}




// sIFR - Flash text replacement
if(typeof sIFR == "function"){
	sIFR.replaceElement(named({
		sSelector:"#pageInfo h2",
		sFlashSrc:"/includes/tradegothic.swf", 
		sColor:"#252525", 
		sBgColor:"#FFFFFF",
		sWmode: "opaque",
		sCase: "lower"
	}));
	
	sIFR.replaceElement(named({
		sSelector:".officeBlock .time p",
		sFlashSrc:"/includes/tradegothic.swf", 
		sColor:"#252525", 
		sBgColor:"#FFFFFF",
		sWmode: "opaque"
	}));
};

// Offices Map
$(document).ready(function(){
	var myFlashMovie = document.getElementById("worldMap");

	$(".officeList a").hover(
	function(){
		var office = $(this).children().html();
		myFlashMovie.changeOffice(office);
	},
	function(){
		myFlashMovie.changeOffice('Default');
	});
});


// Related Profiles Paging
$(document).ready(function(){

	$(".relatedProfiles ul:first-child").addClass("visible");
	$(".btn.nextprofile a").attr("href","javascript:void(0);")
		//.find("span").html("Next 5 Profiles");

	$(".btn.nextprofile a").click(function(){

		if( $(".relatedProfiles div ul:last-child").hasClass("visible") ) {
			// If its the last list.
			$(".relatedProfiles div ul:first-child").css({ left: "720px" }).animate({ left: "20px" }, 1200).addClass("visible");
			$(".relatedProfiles div ul:last-child").removeClass("visible");

		}
		else {
			// If its not the last list.
			$(".relatedProfiles ul.visible").next().css({ left: "720px" }).animate({ left: "20px" }, 1200).addClass("visible").prev().removeClass("visible");
		}
	});
});


// Related Publications expanding. Modified from jTruncate
(function($){
	$.fn.jTruncate = function(options) {
		var defaults = {
			length: 200,
			minTrail: 20,
			moreText: "more",
			lessText: "less",
			ellipsisText: "...",
			moreAni: "",
			lessAni: ""
		};
		var options = $.extend(defaults, options);
		return this.each(function() {
			obj = $(this);
			var body = obj.html();
			if(body.length > options.length + options.minTrail) {
				var splitLocation = body.indexOf(' ', options.length);
				if(splitLocation != -1) {
					// truncate tip
					var splitLocation = body.indexOf(' ', options.length);
					var str1 = body.substring(0, splitLocation);
					var str2 = body.substring(splitLocation, body.length - 1);
					obj.html(str1 + '<span class="truncate_ellipsis">' + options.ellipsisText + 
						'</span>' + '<span class="truncate_more">' + str2 + '</span>');
					obj.find('.truncate_more').css("display", "none");
					// set onclick event for more/less link
					var moreLink = $('.truncate_more_link', obj);
					var moreContent = $('.truncate_more', obj);
					var ellipsis = $('.truncate_ellipsis', obj);
					$(this).hover(
						function() {
							moreContent.slideDown(options.moreAni);
							ellipsis.css("display", "none");
						},
						function() {
							moreContent.slideUp(options.lessAni);	
							ellipsis.css("display", "inline");
						}
					);
				}
			} // end if
		});
	};
})(jQuery);

$(document).ready(function() {  
	$('.news div.desc').jTruncate();  
});  


// Add areas of interest button
$(document).ready(function() {  
	$(".panel-areasofinterest").css("display", "none");
	$(".btn .addareasofinterest").click(function() {
		$(".panel-areasofinterest").slideDown(); 
	});
});  


// Table class automation
$(document).ready(function() {

	// Finds the first row of a 'standard' table and applies a class to it.
	$("table.standard tr:first-child").addClass("first-row");

	// Finds the first cell of a 'standard' table row and applies a class to it.
	$("table.standard tr td:first-child").addClass("first-col");
	
	// Finds the last cell of a 'standard' table header row and applies a class to it.
	$("table.standard tr th:last-child").css("border-right", "none");

});


// Contact button - Shorter heading issue.
$(document).ready(function(){
	var firstobject = $("#content :first-child");
	if(firstobject.hasClass("btn")) { firstobject.next("h3").addClass("longheading"); }
});

// Photo Dynamic Width
$(document).ready(function(){
	$("div.img.photo").each(function(){
		$(this).css("width", $(this).find("img").attr("width"));
	});
});


// Art Collection Thumbnails
$(document).ready(function(){
	$(".artThumbs a").click(function(){
		
		// Get href from thumbnail link
		var bigphoto = $(this).attr("href");
		// Put it into the big photo's img tag
		$("p.bigPhoto img").attr("src", bigphoto);

		// Get description from hidden span
		var photodesc = $(this).find("span").html();
		// Put it in the description span of the big image
		$("p.bigPhoto span.caption").html(photodesc);

		// Prevent from changing page
		return false;
	});
});