/***********************************************
* Jquery link types script
***********************************************/


$(document).ready(function(){

	$("a[href$=pdf]").addClass("pdf").attr({ target: "_blank" });
		
	$("a[href$=doc]").attr({ target: "_blank" });
		
    $("a[href^='http']").attr('target','_blank');

});


/***********************************************
* Toggle Layer script
***********************************************/


function toggleLayer(whichLayer)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

/***********************************************
* Email a friend script
***********************************************/

function EmailLink(){
window.location = "mailto:"+"?subject=hypertension Sydney 2012: I thought this link might interest you." + "&body= "+document.title+"  "+window.location;
}



/***********************************************
* speakers page script
***********************************************/

(function($) {
	/**
	 * Converts a table into a dl list, along with optional functionality
	 *
	 * @option int toggleDurationMs specify the hide/show animate time (0 for no animation)
	 * @option bool|array|string closeByDefault indicate which nodes should be closed by default, accepts 
	 * @option string closeLabel
	 * @option int labelColumn the column of the table to use as the label
	 * @option string bodyTemplate stores the html template used as the body
	 */
	$.fn.listConverter = function(options)
	{
		options = options || {};
		var o = $.extend({
			toggleDurationMs: 0,
			closeByDefault: false,
			closeLabel: 'close',
			openLabel: 'open',
			labelColumn: 0,
			bodyColumn: 1,
			bodyTemplate: false,
			labelTemplate: false
		}, options);
		var $this = this;
		
		// iterate through each object and 
		return $this.each(function() {
			var id = 'list-converter-'+Math.round(Math.random()*100000);
			
			var html = generateHtmlForList($(this), id, o);
			
			$this.replaceWith(html);

			var $id = $('#'+id);
			
			$('a.list-close-button', $id).click(function(e) {
				e.preventDefault();
				
				$(this).toggleClass('data-open').toggleClass('data-close');
				$(this).parent().next('dd').toggle(o.toggleDurationMs);
				$(this).text($(this).hasClass('data-open') ? o.closeLabel : o.openLabel);
				return false;
			});
			if (o.closeByDefault === true) {
				$('dd', $id).hide();
				$('.list-close-button', $id).text(o.openLabel).removeClass('data-open').addClass('data-close');
			} else if (typeof o.closeByDefault === 'object') {
				$('dd', $id).filter(arrayFilter).hide();
				$('.list-close-button', $id).filter(arrayFilter).text(o.openLabel).removeClass('data-open').addClass('data-close');
			} else if (typeof o.closeByDefault === 'string') {
				$('dd', $id).filter(o.closeByDefault).hide();
				$('.list-close-button', $id).filter(o.closeByDefault).text(o.openLabel).removeClass('data-open').addClass('data-close');
			}
		});
	}
	
	/**
	 *
	 */
	function arrayFilter(i)
	{
		return o.closeByDefault.indexOf(i) > -1;
	}
	
	/**
	 * Generate a data list for the provided table
	 *
	 * @param jQuery $table table selector
	 * @param string id id attribute for list 
	 * @return string
	 */
	function generateHtmlForList($table, id, o)
	{
		var html = '<dl id="'+id+'" class="'+$table.attr('class')+'">';
		var body;
		var label;
		var regex = /\$(\d+)/;
		$('tr', $table).each(function() {
			if(o.bodyTemplate === false) {
				body = $('td:eq('+o.bodyColumn+')',this).html()
			} else {
				body = o.bodyTemplate;
				var matches;
				while(matches = regex.exec(body)) {
					body = body.replace(matches[0], $('td:eq('+matches[1]+')',this).html());
				}
				
			}
			if(o.labelTemplate === false) {
				label = $('td:eq('+o.labelColumn+')',this).html();
			} else {
				label = o.labelTemplate;
				var matches;
				while(matches = regex.exec(label)) {
					label = label.replace(matches[0], $('td:eq('+matches[1]+')',this).html());
				}
			}
			html += '<dt>'+label+'<br>'+'<a class="list-close-button data-open" href="">'+o.closeLabel+'</a></dt><dd>'+body+'</dd>';
		});
		html += '</dl>';
		return html;
	}

})(jQuery);


$(function() {
	$('table.speakers-1').listConverter({
		toggleDurationMs: 300,
		openLabel: 'Biography',
		closeLabel: 'Biography',
		labelTemplate: '$0$1',
		bodyColumn: 2,
		closeByDefault: true
	});
	$('table.speakers-2').listConverter({
		toggleDurationMs: 300,
		openLabel: 'Biography',
		closeLabel: 'Biography',
		labelTemplate: '$0$1',
		bodyColumn: 2,
		closeByDefault: true
	});
	$('table.speakers-3').listConverter({
		toggleDurationMs: 300,
		openLabel: 'Biography',
		closeLabel: 'Biography',
		labelTemplate: '$0$1',
		bodyColumn: 2,
		closeByDefault: true
	});
	$('table.speakers-4').listConverter({
		toggleDurationMs: 300,
		openLabel: 'Biography',
		closeLabel: 'Biography',
		labelTemplate: '$0$1',
		bodyColumn: 2,
		closeByDefault: true
	});
	$('table.speakers-5').listConverter({
		toggleDurationMs: 300,
		openLabel: 'Biography',
		closeLabel: 'Biography',
		labelTemplate: '$0$1',
		bodyColumn: 2,
		closeByDefault: true
	});
	$('table.speakers-6').listConverter({
		toggleDurationMs: 300,
		openLabel: 'Biography',
		closeLabel: 'Biography',
		labelTemplate: '$0$1',
		bodyColumn: 2,
		closeByDefault: true
	});
});
