// remap jQuery to $
(function($){

    // ####################################
    // If JS present remove class
    // ####################################
	$('body').removeClass('no-js');
	
	
	
	
	// ####################################
	// Set width of nav links
	// ####################################
	$(document).ready(function() {
		$('.dropdown li.sub_link').each(function(){
			$(this).width($(this).width())
		});
	});
	
	
	
	// ####################################
	// Blank inputs on focus
	// ####################################
	
	var formText = ''
	
	$('input.text ').focus(function() {
		
		// get default value
		formText = this.defaultValue;
		
		//if current value is default value, blank it
		if($(this).val() == formText) {
			$(this).val('');
		}
	});
	
	$('input.text ').blur(function() {
		
		//if the value is blank or is the default value, restore default value or set to completed
		if($(this).val() == '' || $(this).val() == formText) {
			$(this).val(formText);
			$(this).removeClass('complete')
		} else {
			$(this).addClass('complete')
		}
	});
	
	
	
	
	
	// ####################################
	// Homepage slider
	// ####################################
	
	$("#myController").jFlow({
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlider", // must be id, use # sign
		slides: "#home-box-slider",  // the div where all your sliding divs are nested in
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		width: "940px",  // this is the width for the content-slider
		height: "414px",  // this is the height for the content-slider
		duration: 800,  // time in miliseconds to transition one slide
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext", // must be class, use . sign
		auto: true	
	});
	
	
	
	
	// ####################################
	// Press Centre - show more/less
	// ####################################
	
	$('.press-section').each(function() {
		
		$(this).find('.extra').hide();	
		
		$(this).find('.link a').click(function(event) {
			event.preventDefault();
			
			$(this).parents('.press-section').find('.extra').slideToggle();
			$(this).parents('.link').toggleClass('less');
			
			if($(this).text() == 'Show all') {
				$(this).text('Show less');
			} else {
				$(this).text('Show all');
			}
			
		});
		
	});
	
	
	
	// ####################################
	// Resource Centre - show more/less
	// ####################################
	
	$('.resource-section').each(function() {
		
		$(this).find('.extra-container').hide();	
		
		$(this).find('h3').click(function(event) {
			event.preventDefault();
			
				$('.resource-section').find('.extra-container').slideUp();
				$('.resource-section').removeClass('open');
			
			if($(this).parents('.resource-section').hasClass('clicked')) {
				$(this).parents('.resource-section').removeClass('clicked');
				return false;
			} else {
				$('.resource-section').removeClass('clicked');
			}
			
			$(this).parents('.resource-section').find('.extra-container').slideToggle();
			$(this).parents('.resource-section').toggleClass('open');
			$(this).parents('.resource-section').addClass('clicked');
					
		});
		
	});
	
	
	
	// ####################################
	// Glossary - Show/Hide
	// ####################################
	
	$glossaryID = $('.glossary-nav a:first').attr('href');
	$('.glossary-section').hide();
	$($glossaryID).show();
	$('a[href='+$glossaryID+']').parents('li').addClass('current');
	
	setGlossary($glossaryID);
	
	$('.glossary-nav a').click(function(event){
		event.preventDefault();
		
		$($glossaryID).hide();
		$('a[href='+$glossaryID+']').parents('li').removeClass('current');
		
		$glossaryID = $(this).attr('href');
		$($glossaryID).show();
		$('a[href='+$glossaryID+']').parents('li').addClass('current');
		
		setGlossary($glossaryID);
		
	});
	
	
	
	// ####################################
	// Glossary - Align heights / remove last borders
	// ####################################
	
	function setGlossary(id) {
		
		selector = id + ' .glossary-term';
		
		$(selector).each(function() {
		
			if($(this).hasClass('last')) {
				if($(this).height() < $glossaryHeight) {
					$(this).height($glossaryHeight)
				} else {
					$(this).prev().height($(this).height())
				}
			} else {
				$glossaryHeight = $(this).height();
			}
			
		});
		
		selector = selector + ':last'
		
		$(selector).addClass('noborder');
		
		if($(selector).hasClass('last')) {
			$(selector).prev().addClass('noborder');
		}
	}
	
	
	
	
	
	// ####################################
	// In Page Glossary - Add tooltips
	// ####################################
	
	$('span[title]').qtip({
		content: {
			prerender: true,
			text: false // Use each elements title attribute
		},
		position: {
			corner: {
				target: 'bottomleft'
			},
			adjust: {
				x: 0,
				y: 0
			}
		},
		style: {
			width: 300,
			padding: 5,
			background: '#ffffff',
			color: 'black',
			textAlign: 'left',
			border: {
				width: 3,
				radius: 3,
				color: '#cccccc'
			}
		},
		show: {
			when: 'click',
			delay: 0
		},
		hide: {
			when: 'mouseout',
			fixed: true
		},
		api: {
			onRender:function() {
			
				$('.qtip').each(function() {
					$thestring = $(this).text();
					$theword = $thestring.substr(0,$thestring.indexOf(':'));
					//alert($theword)
					$thedefinition = $thestring.substr($thestring.indexOf(':')+1,$thestring.length);
					//alert($thedefinition)
					$(this).find('.qtip-content').html('<b>'+$theword+'</b>:'+$thedefinition)
				});
				
			}
		}
	});
	
})(this.jQuery);
