// JavaScript Document


		var ua = $.browser;

/* Adapt CSS to screen width
-------------------------------------------------------------------------------------------*/
	var ADAPT_CONFIG;
	if ( ua.msie && parseInt( ua.version, 10 ) < 9 ) {
			// Non-standards browser
			ADAPT_CONFIG = {
				path: '/css/',
				dynamic: false,
				range: [
					'0px 		to	1300px 		= 1000.css'
				]
			};			
			
		} else {
			// Standards browser
			ADAPT_CONFIG = {
				path: '/css/',
				// Where is your CSS?
			
				dynamic: true,
				// false = Run when page first loads | true = Change on window resize
			
				range: [
					'0px 		to	1000px 		= 0760.css',
					'1000px to 	1300px 		= 1000.css',
					'1300px           		= 1300.css'
				]
				// First range entry is the minimum.
				// Last range entry is the maximum.
				// Separate ranges by "to" keyword.
			};			
		};
	

/* Stuff to run after the page loads
-------------------------------------------------------------------------------------------*/
	$(document).ready(function() {
														 
		// JS-only content
		$(".jsOnly").show();


		// Fade class
		$(".fade10").delay(10000).hide("slow");


		// Set up "More..." objects (for fixed pages)
		//-------------------------------------------------------------------------------------
		$(".more").hide();
		$(".showMore").show();

		$(".showMore").click(
			function() {
				var id = $(this).attr("id");
				$("#" + id ).hide();
				$("#" + id + "-show .showLess").show();
				$("#" + id + "-show").show("fast");
			}
		);								 

		$(".showLess").click(
			function() {
				var id = $(this).parent().attr("id");
				var m = id.replace("-show", "");
				$("#" + id ).hide("fast");
				$("#" + m).show();
			}
		);


		// Set up "More..." objects (for dynamic tables)
		//-------------------------------------------------------------------------------------
		$("tr.moreRow").hide();
		$(".showLink").show();
		
		$(".showLink").click(
			function() {
				var grp = $(this).closest("tr").attr("class");
				grp = grp.replace( "hover ", "" );
				$("." + grp).show("fast");
				$("." + grp + " .showLink").hide();
				$("." + grp + " .hideLink").show();
			}
		);								 

		$(".hideLink").click(
			function() {
				var grp = $(this).closest("tr").attr("class");
				grp = grp.replace( "hover ", "" );
				$("." + grp + " ~ .moreRow").hide("fast");
				$("." + grp + " .showLink").show();
				$("." + grp + " .hideLink").hide();
			}
		);


		// Set up "More..." objects (within dynamic text block)
		// adapted from http://viralpatel.net
		//-------------------------------------------------------------------------------------
		var showChar = 150;
		var el = "&hellip;";
		var moreTxt = "more";
		var lessTxt = "less";

		$('.trunc8').each(function() {
			var content = $(this).html();
			 if ( content.length > showChar ) {
				var c = content.substr( 0, showChar );
				var h = content.substr( showChar-1, content.length - showChar );
				var html = c + '<span class="moreellipses">' + el + '&nbsp;</span>';
				html = html + '<span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;';
				html = html + '<a href="" class="morelink">' + moreTxt + '</a></span>';
				$(this).html(html);
			}
    });

    $(".morelink").click(function() {
			if($(this).hasClass("less")) {
				$(this).removeClass("less");
				$(this).html(moreTxt);
			} else {
				$(this).addClass("less");
				$(this).html(lessTxt);
			}
			$(this).parent().prev().toggle();
			$(this).prev().toggle();
			return false;
    });


		// Set up modal popup objects - adapted from www.queness.com
		//-------------------------------------------------------------------------------------
		$(".modalContent").hide();

		/* Fixed text version */
		if ( ua.msie && parseInt( ua.version, 10 ) == 7 ) {
			// IE7 can't handle this modal code so give up on it for this browser
			$(".modalLinkFixed").hide();
		};

		$(".modalLinkFixed").click(
			function() {
				// Measure document & window
				var docW = $(document).width();
				var docH = $(document).height();
				var winW = $(window).width();
				var winH = $(window).height();
				var posY = $(window).scrollTop();

				// Size mask (#mask is within rel-positioned div so extend to top left of page)
				var maskPos = $("#boxes").offset(); 
				var maskT = maskPos.top; 
				var maskL = maskPos.left; 
				$('#mask').css({
					"top" 		: maskT * -1 + "px", 
					"left" 		: maskL * -1 + "px", 
					'width' 	: ( docW + maskL ) + "px",
					'height'  : ( docH + maskT ) + "px"
				});

				// Size & position popup
				var diaW = $("#dialog").width();
				var diaH = $("#dialog").height();
				$("#dialog").css( 'left', ( winW - diaW ) / 2 + "px" );
				if ( diaH < winH ) {
					/* Dialog fits window */
					$("#dialog").css( 'top',  ( winH - diaH ) / 2 + posY - maskT + "px" );
				} else {
					$("#dialog").css( { "height" : winH - 200 + "px", "top" : 100 + posY - maskT + "px" } );
				};

				// Display
				$('#mask').fadeTo("slow",0.8); 
				$("#dialog").fadeIn(2000); 
			}
		);								 
		
		/* Table version */
		$(".modalLinkTD").click(
			function() {
				//Assign the content to the popup
				var row = $(this).closest("tr").attr("id");
				var txt = $( "#" + row + " .modalContent" ).html();
				$("#dialog div").html( txt );

				// Measure document & window
        var docW = $(document).width();
				var docH = $(document).height();
        var winW = $(window).width();
				var winH = $(window).height();
				var posY = $(window).scrollTop();
				
				// Size mask
				var maskPos = $("#boxes").offset(); 
				var maskT = maskPos.top; 
				var maskL = maskPos.left; 
        $('#mask').css({
					"top" 		: maskT * -1 + "px", 
					"left" 		: maskL * -1 + "px", 
					'width' 	: ( docW + maskL ) + "px",
					'height'  : ( docH + maskT ) + "px"
				});

				// Size & position popup
				var diaW = $("#dialog").width();
				var diaH = $("#dialog").height();
        $("#dialog").css( 'left', ( winW - diaW ) / 2 + "px" );
				if ( diaH < winH ) {
					/* Dialog fits window */
					$("#dialog").css( 'top',  ( winH - diaH ) / 2 + posY - maskT + "px" );
				} else {
					$("#dialog").css( { "height" : winH - 200 + "px", "top" : 100 + posY - maskT + "px" } );
				};

				// Display
        $('#mask').fadeTo("slow",0.8); 
        $("#dialog").fadeIn(2000); 

			}
		);								 
		
    //if close button or mask is clicked
    $('#dialog .close, #mask').click(function () {
        $('#mask, #dialog').hide();
    });    


		// Elasticate designated textareas
		//-------------------------------------------------------------------------------------
		$('textarea.elastic').elastic();


		// Set up user fields for blog comment
		//-------------------------------------------------------------------------------------
		$(".deferShow").one( "click", 
			function(event) {
				$(".deferred").show("slow");
//				$(this).attr( "disabled", "enabled" );
			}
		);


		// Attach scroll events to quick links
		//-------------------------------------------------------------------------------------
		$('#jumpLinks a').click(
			function() {
				var t = $(this).attr('id');
				if ( typeof(t) !== 'undefined' ) {
					t = t.replace( "go-", "" );
					$.scrollTo( '#' + t, 2000, {offset: {top: -10, left: 0} } );
				}
			}
		);


		// Activate loading image
		//-------------------------------------------------------------------------------------
		$(".spinner a").click(
			function() {
				$(".loadImg").hide();
				$(this).next("img").show();
			}
		);
		

		// Hide backstage noscript message (IE8 incorrectly shows the box (but not the text))
		//-------------------------------------------------------------------------------------
		$("noscript").hide();


		//-------------------------------------------------------------------------------------
		// Standard slickforms stuff
		//-------------------------------------------------------------------------------------
		// Attach maxlength to textarea (not required for HTML5 browsers)
		//-------------------------------------------------------------------------------------
		$("textarea").each( function() { 
			var m = $(this).attr("maxlength");
			if ( m > 0 ) {
				var c = $(this).val().length;
				$(this).next("br").after("<p class='cue'><span>" + c + "</span> used of " + m + " characters allowed</p>");
				$(this).keyup( function() { 
					var l = $(this).val().length;
					$(this).nextAll().find("span").html(l);
					if ( l > m ) {
						$(this).nextAll().find("span").addClass("bold red");
					} else {
						$(this).nextAll().find("span").removeClass("bold red");
					}
				});
			};
		});
		
		// Reveal save prompt
		//-------------------------------------------------------------------------------------
		$("form").change( function() { $(".savePrompt").show(); });

	});


/* Functions
-------------------------------------------------------------------------------------------*/
	function fHideDeferred() {
		$(".deferred").hide(); 
	};


	$(function() {
		$('input').filter('.datepicker').datepicker({
			dateFormat: 'dd/mm/yy',
			firstDay: 1,
			changeMonth: true,
			changeYear: true,
			yearRange: '-1:+1',
			inline: true,
			showAnim: 'fadeIn'
		});
	});


