/* Core.js */

var Biography	=	function(node, index){
	
	/* Properties */
	this.node		=	$(node);
	this.index		=	index;
	this.vcard		=	$(".vcard", this.node);
	this.id			=	this.vcard.attr("id");
	
	/* Collect hCard Properties */
	this.givenName	=	$(".given-name", this.node).html();
	this.familyName	=	$(".family-name", this.node).html();
	this.role		=	$(".role", this.node).html();
	this.org		=	$(".org", this.node).html();
	this.tel		=	$(".tel .value", this.node).html();
	this.email		=	$(".email", this.node).html();
	this.qualify	=	$(".qualify", this.node);
	this.expertise	=	$(".expertise", this.node);
	this.bio		=	$(".bio", this.node);


	/* Allow user to click hyperlinks in collapsed vcard */
	var _this		=	this;
	$("a[href]", this.vcard)
	.click(function(e){
		e.stopImmediatePropagation();
		return true;
	})
	.mouseover(function(e){
		var parent	=	$(this).closest("#biobrowser > li");
		if(!parent.hasClass("open")){
			_this.node.removeClass("ready");
		}
	}).mouseout(function(e){
		_this.node.addClass("ready");
	});


	/* Append a "Read More" link */
	this.readMore	=	$('\
					<div class="more-link">\
						<a href="#" title="Read more about this member">More&hellip; <span class="meta-nav">&#9658;</span></a>\
					</div>').appendTo(this.vcard);

	/* onOpen Handler */
	this.vcard.click(function(e){
		BioBrowser.open(_this);
		return false;
	});


	/* Append a "Back to Team" button */
	this.backToTeam	=	$('<a href="#" class="close-link" title="Return to list of team members"><span class="meta-nav">&#9664;</span> Back to Team</a>')
		.appendTo(this.node)
		.click(function(e){
			BioBrowser.close();
			return false;
		});
}



var BioBrowser	=	{
	bios:		new Array(),
	current:	null,
	
	
	/* Opens the designated Biography object */
	open:		function(bio){
		this.current	=	bio;
		this.current.node.addClass("open");
		this.refresh();
	},
	
	/* Closes any opened biographies */
	close:		function(){
		if(this.current)
			this.current.node.removeClass("open");
		this.current	=	null;
		this.refresh();
	},
	
	/* Updates the BioBrowser's display when the active bio's been changed */
	refresh:	function(){
		
		/* Currently viewing a biography */
		if(this.current){
			this.node.removeClass("collapsed");
		}
		
		/* No biography opened */
		else{
			this.node.addClass("collapsed");
			for(var i = 0; i < this.bios.length; i++)
				this.bios[i].node.addClass("ready");
		}
	},
	

	/* Initialiser */
	init:		function(){

		//	Don't even bother with IE6
		if($.browser.msie && Number($.browser.version) < 7){
			$("#sidebar").addClass("noscript");
			return false;
		}
		
		this.node	=	$("#biobrowser");
		this.node.children().each(function(count){
			BioBrowser.bios.push(new Biography(this, count));
		});
		
		/*	Check for a fragment identifier */
		if(window.location.hash != ""){
			var hash	=	(window.location.hash).replace("#", "").toLowerCase();
			for(var i = 0; i < this.bios.length; i++)
				if(this.bios[i].id == hash){
					this.open(this.bios[i]);
					break;
				}
		}
		this.refresh();
		
		/*	Scale the sidebar to the maximum height of its containing element */
		this.sidebar	=	$("#sidebar").height(($("#body").height() + 164));
		
		/*	Fix IE7 */
		if($.browser.msie && Number($.browser.version) < 8)
			this.node.height(this.sidebar.height());
	}
};


/*	Animated motto cloud on the "Our Philosophy" page
-	TODO: Replace scripted tweens with CSS3 Animations for Mobile Safari */
var MottoCloud	=	{

	show:	function(index, speed){
		$(this.words[index]).fadeIn(speed);
	},
	
	hideAll: function(speed){
		this.words.fadeOut(speed);
	},
	
	pulse:	function(){
		this.words.hide();
		setTimeout("MottoCloud.show(0, 800)", 600);
		setTimeout("MottoCloud.show(1, 700)", 2700);
		setTimeout("MottoCloud.show(2, 600)", 4800);
		setTimeout("MottoCloud.hideAll(700)", 7500);
	},
	
	init:	function(){
		this.cloud	=	$("#motto-cloud");
		if(this.cloud.length > 0){
			this.words	=	this.cloud.children(".motto");
			this.pulse();
			setInterval("MottoCloud.pulse()", 8700);
		}
	}
};

jQuery(document).ready(function($){
	$(".noscript").removeClass("noscript");
	BioBrowser.init();
	MottoCloud.init();
	$("a[rel^=external]").attr("target", "_blank");
});
