$(function(){	
	//#open external links in new window
	$("a[href^='http']").not("[href*='firestation.ie']").attr('target', '_blank');
	
	//#correct borders on subnavs
	$('#sidebar li li:last-child').css('border-bottom','0');
	
	//#text resizing
	//#detect current size preference if any
	applyStyle();
	//#show resize widget
	$('#resize').show();
	//#change text size when widget is used
	$('#resize a').click(function(event){
		event.preventDefault();
		setStyle($(this).attr('id'));
	});
	
	//#masterclasses - if empty, show explanation
	if(window.location.pathname.match(/masterclass/) && !$('#text div').length)
	{
		$('#text').html('<p>There are currently no Masterclasses scheduled for this period.</p>');	
	}

});

//#text resizer supporting functions
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

//#adjust style based on choice and save preference in cookie
function setStyle(action)
{
	//#action will be either increase or reduce
	//#detect current body class and then adjust one size up or down
	var style = '';
	switch(action)
	{
		case 'increase':
			if($('body').hasClass('size-large'))
			{
				style = 'size-largest';
			}
			else if($('body').hasClass('size-normal'))
			{
				style = 'size-large';
			}
			break;
		case 'reduce':
			//#todo - similar to increase
			if($('body').hasClass('size-largest'))
			{
				style = 'size-large';
			}
			else if($('body').hasClass('size-large'))
			{
				style = 'size-normal';
			}
			break;
	}	
	//#if size change needed, change body style and set cookie
	if(style != '')
	{
		$('body').removeClass('size-normal size-large size-largest').addClass(style);
		createCookie('style',style);
	}
}

//#detect style preference cookie and apply it
function applyStyle() {
  var style = readCookie('style');
  if(style && !$('body').hasClass(style))
  {
		$('body').addClass(style);	
  }
}
