/*FULLSCREEN CAROUSEL*/
function carousel_initCallback(carousel)
{
	// Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });

	//bind external nav controls
	$('.carousel-next').bind('click', function() {
		carousel.next();
		return false;
	});
 
	$('.carousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});
}
function carousel_itemVisibleInCallback(carousel, activeLi, index, state)
{
	var textli = $(activeLi).attr("rel");
	$(textli).delay(250).fadeIn(500);
}

function carousel_itemVisibleOutCallback(carousel, activeLi, index, state)
{
	var textli = $(activeLi).attr("rel");
	$(textli).fadeOut(150);
}


/*FANCYBOX TITLE FORMAT*/
function fancyboxFormatTitle(title, currentArray, currentIndex, currentOpts) {
    return '<span id="fancybox-title-over"><a href="javascript:;" onClick="$.fancybox.prev()" class="left fancybox-nav-prev">&larr;</a><a href="javascript:;" onClick="$.fancybox.next()" class="left fancybox-nav-next">&rarr;</a><a class="fancybox-close right" href="javascript:;" onclick="$.fancybox.close();">x</a>' + (title && title.length ? title : '' ) + ' Image ' + (currentIndex + 1) + ' of ' + currentArray.length + '</span>';
}






$(function(){
	/*menu hover with js, mainly for IE6*/
	$('ul#main-menu li.campaigns-drop-down').live('mouseenter', function(){
		$(this).addClass('hovered');
	}).live('mouseleave', function(){
		$(this).removeClass('hovered');
	});


	/*box slider*/
	$('.box.has-slide').live('mouseenter', function(){
		//slideup
		$('.box-slide', this).animate({top: 0}, {duration: 200, queue: false});
	}).live('mouseleave', function(){
		//slide down
		$('.box-slide', this).animate({top: '200px'}, {duration: 200, queue: false});
	});

	/*main menu border fix*/
	$('ul#main-menu li.campaigns-drop-down').live('mouseenter', function(){
		$('ul#main-menu li.home-link').css({'border-right':'none'});
	}).live('mouseleave', function(){
		$('ul#main-menu li.home-link').css({'border-right':'2px solid #ddd'});
	});

	/*accordion*/
	activeItem = $("#accordion li.active");
	$("#accordion li").live('click', function(){
		$(activeItem).animate({width: "74px"}, {duration:300, queue:false});
		$("#accordion li").removeClass('active');
		$(this).animate({width: "659px"}, {duration:300, queue:false}).addClass('active');
		$('.caption', this).animate({bottom: 0}, {duration: 200, queue: false});
		activeItem = this;
	});
	/*accordion captions slider*/
	$('#accordion li.active.has-caption').live('mouseenter', function(){
		//slideup
		$('.caption', this).animate({bottom: 0}, {duration: 200, queue: false});
	}).live('mouseleave', function(){
		//slide down
		$('.caption', this).animate({bottom: '-40px'}, {duration: 200, queue: false});
	});

	/*scroller*/
	//if there is more than one li do the carousel
	if ( $('.carousel').children().size() > 1 ) {
		//could do with some easing
		$('.carousel').jcarousel({
			scroll: 1,
			auto: 4,
			animation: 1000,
			wrap: 'both',
			itemFallbackDimension: 720,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: carousel_initCallback,
			itemVisibleInCallback: {onBeforeAnimation: carousel_itemVisibleInCallback},
			itemVisibleOutCallback: {onBeforeAnimation: carousel_itemVisibleOutCallback}
		});
	}
	//if only 1 or no child li's, remove nav
	if( $('.carousel').children().size() <= 1 ){
		$('.carousel-controls').remove();
	}


	/*
	 * fancybox gallery
	 */
	$(".gallery-trigger").click(function(e) {
		e.preventDefault();

		//build array of images
		var galleryImagesArray = [];
		$("#gallery-images a").each(function() {
			galleryImagesArray.push(
				{
					'href'	: $(this).attr('href'),
					'title'	: '<b>'+$(this).attr('title')+'</b> // '
				}
			);
		});

		//launch fancybox with the array
		$.fancybox(galleryImagesArray, {
			'padding'			: 0,
			'type'              : 'image',
			'showCloseButton'	: false,
			'showNavArrows'		: false,
			'titlePosition' 	: 'over',
	 		'titleFormat'		: fancyboxFormatTitle
		});
	});

	/*
	* Field Clearing
	*/
	$('.input-clear').live('focus', function () {
		if (this.value == this.defaultValue) {
			this.value = '';
		}
	}).live('blur', function () {
		if (this.value === '') {
			this.value = this.defaultValue;
		}
	});
});
