/**
 *
 *	sgm.js
 *	- Methods for www.sgm.ac.uk
 ---------------------------------------------------------------------------------------*/

// Toggle module inner content

function toggleModules(){
	var mod_content = 	$('.module .module-inner p, .module-s .module-inner p').not('.module.full-width p');
	$(mod_content).hide().each(function(){
		var this_mod_content = this;
		$(this_mod_content).parents('.module, .module-s').mouseenter(function(){
			$(this_mod_content).slideDown(250, function(){
			})
		}).mouseleave(function(){
			$(this_mod_content).slideUp(250, function(){
			})
		})
	})
}
$(document).ready(function(){
	
	$('#top-panel-content').addClass('hidden').hide();
	toggleTopPanel();	//	Top panel controls
	
	
	imageCaptions();	//	Captions on images
	$.localScroll();	// Scrolling for glossary
	
	toggleModules();
	
	$('a.enlarge').nyroModal({bgColor: '#ffffff'});	// Modal overlays
	$('ul.page-links').not('.quiz ul.page-links').centreNav();	// centre pagination on quiz
	
	if($("#order-form").length){
		validateForms();
	}
	
	/* Equal heights for elements */
	
	$('.links .section ul').each(function(){
		var cols = $(this).find('li').not('.links .section ul li li');
		if(cols.length > 0){
			equalHeight(cols);
		}
	});
	

	
	/* The carousels */
	
	// General carousels with hover captions
	/*$("div.carousel-auto ul")
	.not("div.carousel-auto.full-caption ul").captionedSlideShow({
		speed: 500, 
		timeout:5000,
		buffer: 50
	});*/

	/**
	 * Carousels with static captions ("Carbon-cycle" etc)
	 * - Separate call for each one to account for different speeds and timeouts
	**/ 
	
	// Carbon cycle
	$("div.carousel-auto.full-caption.carbon-cycle ul")
	.not('div.carousel-auto.full-caption ul ul').captionedSlideShow({
		speed: 700, 
		timeout:11000,
		buffer: 50,
		height: '560px',
		fullCaption: true
	});	
	
	// Nitrogen cycle
	$("div.carousel-auto.full-caption.nitrogen-cycle  ul")
	.not('div.carousel-auto.full-caption ul ul').captionedSlideShow({
		speed: 700, 
		timeout:15000,
		buffer: 50,
		height: '560px',
		fullCaption: true
	});	
	
	// Food chain
	$("div.carousel-auto.full-caption.food-chain  ul")
	.not('div.carousel-auto.full-caption ul ul').captionedSlideShow({
		speed: 700, 
		timeout:13000,
		buffer: 50,
		height: '560px',
		fullCaption: true
	});
	
	// Binary fission
	$("div.carousel-auto.full-caption.binary-fission  ul")
	.not('div.carousel-auto.full-caption ul ul').captionedSlideShow({
		speed: 700, 
		timeout:13000,
		buffer: 50,
		height: '560px',
		fullCaption: true
	});			
	
	/*	Togglers	*/
	
	$('ol.tips').not('ol.tips.facts').toggleTips({
		speed: 200,
        startText: 'Show answer',
		closeText: 'Hide answer'
	});
	$('ol.tips').css({ // have to add custom numbering to accordion items due to IE7 bugs
		'list-style': 'none'
	}).find('li').each(function(i){
			$(this).find('.tip-inner').prepend('<span class="number">'+(i+1)+'</span>');
	});

	/*$('.section').not('.credits .section').each(function(){
		equalHeight($(this).find('li').not('ul.link-list li'));
	});*/
	
});


$(window).bind("load", function() { 
	buildCarousel();
});


//Full Caption Sliding (Hidden to Visible)

function imageCaptions(){
	var c_images = $('.captioned-img-wrapper');
	c_images.each(function(){

		var inner_html = $(this).html();
		inner_html = '<div class="corners">'+inner_html+'<div class="corners-outer">';
		var width = $(this).find('img').width();
		var height = $(this).find('img').height();
		
		var caption_height = $(this).find('.boxcaption').innerHeight();
		var anim_height = height-caption_height-10;
		//console.log($(this).find('.boxcaption').innerHeight());
		
		$(this).find('img')
		.css({
			'position': 'absolute'
		})
		$(this)
		.css({
			'width': width+'px',
			'height': height+'px',
			'overflow': 'hidden'
		})
		
		$(this)
		.html(inner_html)
		.find('div.corners')
		.css({
			'width': width+'px',
			'height': height+'px',
			'position': 'absolute'
		})
		
		$(this)
		.find('div.corners-outer')
		.css({
			'height': height+'px',
			'position': 'absolute'
		})		
		$(this).find(".boxcaption ")
		.css({
			'top': height-40+'px',
			'height': '200px',
			'position': 'absolute',
			'width': '100%'
		});
		//console.log($(this).find(".boxcaption ").innerHeight());
		$(this).hover(function(){
			$(".cover", this).stop().animate({
					top: anim_height+'px'},{
						queue:false,
						duration:500
						});
		}, function() {
			$(".cover", this).stop().animate({
				top: height-40+'px'
			},{queue:false,duration:500});
		});
	
	})
}

/*	Form validation	*/

function validateForms(){
	$.metadata.setType("attr", "validate");

	$('#order-form" input:hidden').remove();

	$("#order-form").validate({
		errorElement: "div",
		errorPlacement: function(error, element) {
			if(element.attr('type') == 'checkbox' && !element.hasClass('checkbox-ignore')){
				element = element.parent().parent();
				error.addClass('long');
			}
			error.hide().insertBefore( element).fadeIn(500);
		},
		rules: {
			'data[Order][forename]': "required",
			'data[Order][surname]': "required",
			'data[Order][school]': "required",
			'data[Order][resources][]': "required",
			'data[Order][read_data_protection]': "required",
			//'data[Order][share_data]': "required",
			'data[Order][department]': "required",
			'data[Order][position]': "required",
			'data[Order][email]': {
				required: true,
				email: true
			}
		},
		messages: {
			'data[Order][forename]': "Please enter your first name",
			'data[Order][surname]': "Please enter your surname",
			'data[Order][email]': "Please enter a valid email address",
			'data[Order][resources][]' : "Please choose at least one resource for your order",
			'data[Order][read_data_protection]' : "Please indicate that you have read and understand how your details will be used",
			//'data[Order][share_data]': "Please indicate as to whether or not you are happy for us to pass on your details",
			'data[Order][school]' : "Please tell us the name of your school",
			'data[Order][department]' : "Please tell us the department you work in",
			'data[Order][position]' : "Please tell us your position at the school"
		}
	});	
}

