$(document).ready(function(){

	/*	Used in the main stylesheet as an override for "js disabled" styling
	---------------------------------------------------------------------------*/
	$('body').addClass('js');
	
	/*	Unfolding menu: used on lo-res screens & small windows
	---------------------------------------------------------------------------*/
	var open_text = "Open ";
	var close_text =  "Close ";
	var mm_toggle = $('<a href="#" class="structural toggle open">Main menu</a>');
	var open_close = $('<span></span>');
	$(open_close).text(open_text);
	$(mm_toggle).prepend($(open_close));
	$('#main-menu').prepend($(mm_toggle));
	$('.toggle').toggle(function() {
		$(this).toggleClass('open', 0).toggleClass('close').next().show();
		$(open_close).text(close_text);
	}, function() {
		$(this).toggleClass('close', 0).toggleClass('open').next().hide();
		$(open_close).text(open_text);
	});
	
	/* Jump link to sub menu
	---------------------------------------------------------------------------*/
	if($('#sub-menu').length) {
		$('#sub-menu').prepend('<h2 class="structural menu-heading">Sub menu</h2>');
		$('#focus-spot').prepend('<a href="#sub-menu" class="structural menu-link">Go to the sub menu</a>');
	}	
	
	
	/*	Make entire list items clickable, and apply styling hooks
	---------------------------------------------------------------------------*/
	$('#applications .column').each(function() {
		if($(this).find('h2 a').text() != '') {
			$(this).children().not('.more-reading').wrapAll('<div class="inner"></div>');
		}
	});
	$('#services .item, #applications .inner').hover(function() {
		$(this).addClass("hover"); //over
		}, function() {
		$(this).removeClass("hover"); //out
	});
	$('#services .item, #applications .column').not('.more-reading').click(function() {
		if($(this).find('h2 a')) {
			var the_url = $(this).find('a:first').attr('href');
			if (the_url != null){
				location.href = the_url;
			}
		}
	});	
	
	/*	Google Analytics: Track downloaded files
	---------------------------------------------------------------------------*/
	var trackDownloads = function(){
		var filetype = new Array(
			".pdf"
		);
		$.each(filetype, function() {
			$("a[href $=" + this + "]").click(function() {
				var path_segments = this.href.split('/');
				var filename = path_segments[path_segments.length - 1];
				//alert(filename);
				pageTracker._trackPageview("/downloads/" + filename);
			});
		});
	}
	trackDownloads();
	

	
	/*	Show the current date
	---------------------------------------------------------------------------*/
	/*
	function getCurrentDate(){
		var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		var now = new Date();
		var day = now.getDate();
		var month = now.getMonth();
		var year = now.getFullYear();
		$('<div id="current-date"></div>').text(day + " " + months[month] + " " + year).appendTo($('#alternative-navigation .inner'));
	}
	getCurrentDate();
	*/
	
	/*	Tabs navigation used on index page
	---------------------------------------------------------------------------*/
	var initializeTabs=function(){
		var tabs="";
		var tabIndex=1;
		var itemsinlist="";
		itemsinlist=$("#highlighted-services li").length;
		$("#highlighted-services li").each(function(){
			var servicesHeadings=$(this).find("h3");
			servicesHeadings.addClass("off-screen")
			var headerText=servicesHeadings.html();
			if(headerText){
				tabs=tabs+"<li><a style=\"z-index:"+itemsinlist+"\"href=\"#service-"+tabIndex+"\">"+headerText+"</a></li>";
				tabIndex++;itemsinlist--;
			}
		});
		if(tabs!==""){
			tabs="<ul id=\"services-index\">"+tabs+"</ul>";
			$("#highlighted-services").prepend(tabs);
			$("#services-index").tabs({cookie:{expires:30}});
		}
	}
	initializeTabs();
	
	
	/* Anti Widows - changes the whitespace between the two last words in the paragraph to an &nspb;
	---------------------------------------------------------------------------*/
      var pText = '';
      $('.column p').each(function() {
        pText  = $(this).html().replace(/ (\S+)$/,'&nbsp;$1');
        $(this).html(pText);
      }); 
      
      



	
	/*	Open links with class "new-window" in a new window
	---------------------------------------------------------------------------*/
	var openNewWindow=function(e){
		var event=(!e)?window.event:e;
		if(event.shiftKey||event.altKey||event.ctrlKey||event.metaKey){
			return true;
		} else {
			var win=window.open(this.getAttribute("href"),"_blank");
			if(win){
				if(win.focus)win.focus();
				return false;
			}
			win=null;
			return true;
		}
	}
	$("a.new-window").live("click",openNewWindow);
	
	/* Init the home page slider
	---------------------------------------------------------------------------*/
	
	$('#main-slider').bxSlider({
		mode: 'fade',                 		// 'horizontal', 'vertical', 'fade'
		infiniteLoop: true,                 // true, false - display first slide after last
		hideControlOnEnd: false,            // true, false - if true, will hide 'next' control on last slide and 'prev' control on first
		controls: false,                     // true, false - previous and next controls
		speed: 1000,                         // integer - in ms, duration of time slide transitions will occupy
		easing: 'swing',                    // used with jquery.easing.1.3.js - see http://gsgd.co.uk/sandbox/jquery/easing/ for available options
		pager: true,                        // true / false - display a pager
		pagerSelector: null,                // jQuery selector - element to contain the pager. ex: '#pager'
		pagerType: 'full',                  // 'full', 'short' - if 'full' pager displays 1,2,3... if 'short' pager displays 1 / 4
		pagerActiveClass: 'pager-active',   // string - classname attached to the active pager link
		captions: false,                    // true, false - display image captions (reads the image 'title' tag)
		captionsSelector: null,             // jQuery selector - element to contain the captions. ex: '#captions'
		auto: true,                        // true, false - make slideshow change automatically
		autoDirection: 'next',              // 'next', 'prev' - direction in which auto show will traverse
		autoControls: false,                // true, false - show 'start' and 'stop' controls for auto show
		autoControlsSelector: null,         // jQuery selector - element to contain the auto controls. ex: '#auto-controls'
		autoStart: true,                    // true, false - if false show will wait for 'start' control to activate
		autoHover: false,                   // true, false - if true show will pause on mouseover
		autoDelay: 0,                       // integer - in ms, the amount of time before starting the auto show
		pause: 3000,                        // integer - in ms, the duration between each slide transition
		wrapperClass: 'bx-slider',         // string - classname attached to the slider wraper
		startingSlide: 0,                   // integer - show will start on specified slide. note: slides are zero based!
		displaySlideQty: 1,                 // integer - number of slides to display at once
		moveSlideQty: 1,                    // integer - number of slides to move at once
		randomStart: false               // true, false - if true show will start on a random slide
	});	
	

	/*	Currently not in use
	---------------------------------------------------------------------------*/
	/*
	// Render Table Of Contents based on second level headings
	var headings = $('#main-copy h2');
	if ($(headings).length > 1) {
		var i = 0;
		var toc = $('<div id="toc"><h3>Table of contents</h3></div>');
		var toc_navigation = $('<ol></ol>');
		$(headings).each(function() {
			var section = "section-" + i;
			toc_navigation.append('<li><a href=#' + section + '>' + $(this).text() + '</a></li>');
			$(this).attr('id', section);
			i++;
		});
		toc.append(toc_navigation).prependTo('#news-spot');
	}
	*/
	
});
