//init cufon font replacement
Cufon.replace('h2')('h3')('h4', {hover: true});

$(document).ready(function() {
									
	//add loading div for twitter updates
	$('#latest_tweet').html('<div class="loading">&nbsp;</div>');
	
	//remove hover class from main navigation to allow progressive enhancement
	$('#nav').removeClass('hover');
	$('#nav a').hover(function() {
		$(this).stop().animate({backgroundPosition: '(0 -75px)'}, 300);				 
	}, function() {
		if (!$(this).parent().hasClass('current_page_item') && !$(this).parent().hasClass('current_page_parent')) {
			$(this).stop().animate({backgroundPosition:'(0 0)'}, 300);
		}
	});
	
	//apply an index to all thumbnails so we can scroll
	$('#portfoliothumbs li a').each(function(idx) {
   	$(this).data('index', (++idx));
	});
	
	//jCarousel callback function
	function mycarousel_initCallback(carousel) {
		
		//next scroll button
		$('#mycarousel-next').bind('click', function() {
			carousel.next();
			return false;
    	});
		
		//previous scroll button
		$('#mycarousel-prev').bind('click', function() {
			carousel.prev();
			return false;
		});
		
		//slideshow trigger function
		$('#portfolioimage').bind('image-loaded', function() {
			var idx =  $('#portfoliothumbs .activethumb').data('index');
			curidx = $.jcarousel.intval(idx);
			if ((curidx == carousel.last) || (curidx == 1)) {
				carousel.scroll(curidx);
			}
			return false;
		});
		
		//next trigger function
		$('#portfolioimage').bind('image-loaded-up', function() {
			var idx =  $('#portfoliothumbs .activethumb').data('index');
			curidx = $.jcarousel.intval(idx);
			if (curidx == carousel.last) {
				carousel.next();
			}
			return false;
		});
		
		//previous trigger function
		$('#portfolioimage').bind('image-loaded-down', function() {
			var idx =  $('#portfoliothumbs .activethumb').data('index');
			curidx = $.jcarousel.intval(idx);
			if (curidx == carousel.first) {
				carousel.prev();
			}
			return false;
		});
		
	};
	
	//thumbnail carousel for portfolio pages
	if(jQuery().jcarousel) {
		
		$('#portfoliothumbs').jcarousel({
			  scroll: 1,
			  initCallback: mycarousel_initCallback,
			  buttonNextHTML: null,
			  buttonPrevHTML: null
		 });
	
	}

	if(jQuery().innerfade) {
		
		//home page image slider
		$('#imageslider').innerfade({
			speed: 2000,
			timeout: 5000,
			type: 'sequence',
			containerheight: '420px'
		});
		
		//about page image slider
		$('#aboutslider').innerfade({
			speed: 2000,
			timeout: 5000,
			type: 'sequence',
			containerheight: '413px'
		});
		
		//prices page image slider
		$('#pricesslider').innerfade({
			speed: 2000,
			timeout: 5000,
			type: 'sequence',
			containerheight: '344px'
		});
		
		//portfolio page image slider
		$('#portfolioslider').innerfade({
			speed: 2000,
			timeout: 5000,
			type: 'sequence',
			containerheight: '431px',
			controlimg: '/images/'
			//controlimg: '/ndp/images/'
		});

		//headshot portfolio page image slider
		$('#headshotslider').innerfade({
			speed: 2000,
			timeout: 5000,
			type: 'sequence',
			containerheight: '362px',
			controlimg: '/images/'
			//controlimg: '/ndp/images/'
		});

	}
	
	//Initialise booking calendar
	if(jQuery().fullCalendar) {
		
		$('#bookingcalendar').fullCalendar({
			events: $.fullCalendar.gcalFeed('http://www.google.com/calendar/feeds/oaofkoa8871uiulcug3gkf2fno%40group.calendar.google.com/public/basic'),
			//events: $.fullCalendar.gcalFeed('http://www.google.com/calendar/feeds/9ajctm3psu3iv4kncgdlqcsp40%40group.calendar.google.com/public/basic'),
			header: {left:'prev title next', center:'', right:'today'},
			weekends: false,
			timeFormat: 'h(:mm)tt',
			buttonText: {prev:'<img src="/images/play-prev.png" alt="" />', next:'<img src="/images/play-next.png" alt="" />', today:'Today'},
			//buttonText: {prev:'<img src="/ndp/images/play-prev.png" alt="" />', next:'<img src="/ndp/images/play-next.png" alt="" />', today:'Today'},
			dayClick: function(dayDate) {
				var d = $.fullCalendar.formatDate(dayDate, 'd');
				var m = $.fullCalendar.formatDate(dayDate, 'MMM');
				var y = $.fullCalendar.formatDate(dayDate, 'yyyy');
				var session = $('#frmsession').val();
				$('#frmday').val(d);
				$('#frmmonth').val(m);
				$('#frmyear').val(y);
				$('#fullsession').val(y+'-'+m+'-'+d+'-'+session);
			},
			eventClick: function() {
				return false;
			}
		});
		
		$('.fc-grid tbody td').hover(function() {
			$(this).addClass('hoverbg');
		}, function() {
			$(this).removeClass('hoverbg');
		});
	
	}
	
	//FAQ
	$('.faq > p').hide();  
	$('.faq > h4').click(function() {
		$(this).toggleClass('activefaq');
		$(this).next().slideToggle();
	});
	
	//add show/hide link
	var showText='show all';
	var hideText='hide all';
	$('#faqs').prepend('<p>Please click on the questions to display the answers (<a href="#" class="toggleLink">'+showText+'</a>)</p>');
	$('.toggleLink').click(function() {
		if ($(this).html()==hideText) {
			$('.faq > p').slideUp();
			$('.faq > h4').removeClass('activefaq');
		} else {
			$('.faq > p').slideDown();
			$('.faq > h4').addClass('activefaq');
		}
		$(this).html ($(this).html()==hideText ? showText : hideText);
	});
	
});
