$(document).ready(function(){

//By Default put the Menu in collapsed state

	$('.children').parent().children('ul').slideUp('fast');
	$('#tabs').tabs();

	$('.nav a').click(function() {
		var url=$(this).attr('href');
		$load(url);
      		$('.nav li ul').hide();
      		scrollTo(0,0);
		return false;
	});	

// FAQ

	$('.answer').hide();
	$('#faq1 h2').toggle(
		function() {
			$('.answer').hide();
			$(this).next('.answer').fadeIn();
			$(this).addClass('close');
		},
		function() {
			$(this).next('.answer').fadeOut();
			$(this).removeClass('close');
		});
	$('#faq2 h2').toggle(
		function() {
			$('.answer').hide();
			$(this).next('.answer').fadeIn();
			$(this).addClass('close');
		},
		function() {
			$(this).next('.answer').fadeOut();
			$(this).removeClass('close');
		});
	$('#faq3 h2').toggle(
		function() {
			$('.answer').hide();
			$(this).next('.answer').fadeIn();
			$(this).addClass('close');
		},
		function() {
			$(this).next('.answer').fadeOut();
			$(this).removeClass('close');
		});

//Build accordion where present

	$('#accordion').accordion({
		header: 'h2',
		selectedClass:'current'
	});

//Build gallery photo cycle where present

	$('#gallery').cycle({
		fx: 'scrollLeft',
		timeout: 7000,
		speed: 1000,
		delay: -1000,
		pager: '#pager',
		next: '#next',
		prev: '#prev'
	});

	$('#playControl').toggle(
			function() {
				$('#gallery').cycle('pause');
				$(this).text('Play');
			},
			function() {
				$('#gallery').cycle('resume');
				$(this).text('Pause');
			});

//Click puts the selected HTML page up

// set current wrapper height from initial load and set the new wrapper height
	minWrapper = $('#wrapper').height();

// set detail min height as sidemenu + banner 
	minDetail = $('#sideMenu').height() + $('#banner').height();

// set heading as title picture + menu div + footer
	heading = $('#logo').height() + $('#menu').height() + $('#footer').height();

// make sure the detail is as big as the left side divs to ensure page layout is maintained
	if ($('#detail').height() < minDetail) {
//		alert('set3');
		$('#detail').height(minDetail)
	}

	currDetail = $('#detail').height() + 70;

// set the filler for div selector allowing for expanding FAQ
	faqFiller=0;
	$('.faq h2').each(function(){
		faqFiller = 150;
	})

// Set heading above tab selector to adjust the height of the div if required
	tabHeader = 200;

	$('.divSelector').each(function() {
		thisDiv = $(this).height() + tabHeader + faqFiller;
		if (currDetail < thisDiv) {
//			alert('set2');
			currDetail = thisDiv;
		}
	
	})

// set heading above selection then test the detail of the current selection
	selectHeader = 50;

	$('#selectedData').each(function() {
		thisDiv = $(this).height() + selectHeader;
		if (currDetail < thisDiv) {
//			alert('set1');
			currDetail = thisDiv;
		}

	})

// set wrapper to the heading and new detail heights
	$('#wrapper').height(heading + currDetail);

})