//
// banks.js
// Copyright (c) Connect Group Ltd. All rights reserved.
//
// $Id: banks.js 112 2010-04-30 13:55:05Z paul $
//

//
// Video Configuration
//

var keys = {
	'connectdigitalllp.co.uk': '2605cdbc720a85cf2d3',
	'connectdigitalllp.co.uk:8090': 'c50e3372c74a08f1dc7',
	'bankssbeer.co.uk': '6869f769cdc03abc5df',
	'banksbeer.co.uk': 'e44e6b0050a40bfed22',
	'bankssbeer.com': 'c853136ee9d6b5d2287'
};

//
// Background Configuration
//

var background = {
	fadeLength: 3000,
	imageChangeInterval: 5000,
	images: [ 'bg-home-master-image-mild.jpg', 'bg-home-master-image-bitter.jpg' ],
	lastImageIndex: 0,
	nextBackgroundElement: $('<div id="next-background">&nbsp;</div>'),
	nextBackgroundSelector: 'body.home div#outer-wrapper div#next-background',
	wrapperSelector: 'body.home div#outer-wrapper'
};

$(document).ready(function() {
	$.localScroll(800, { easing:'elasout' });
	$('a[rel="external"]').click(function(e) { e.preventDefault(); window.open(this.href); return false; });
	$('a[rel="video"]').each(function() { playVideo($(this), false) });
	$('div.drinking-age form input#verify').hover(function() { $(this).attr('src', $(this).attr('src').replace('off', 'on')); }, function() { $(this).attr('src', $(this).attr('src').replace('on', 'off')); });

	var carousel = $('ul#video-carousel');
	if (carousel.length) {
		$('ul#video-carousel').jcarousel({
			wrap: 'circular',
			scroll: 1,
			itemFirstInCallback: { onAfterAnimation: onItemFirstIn },
			itemVisibleInCallback: { onBeforeAnimation: onItemVisibleIn },
			itemVisibleOutCallback: { onAfterAnimation: onItemVisibleOut }
		});
	}

	var wrapper = $(background.wrapperSelector);
	if (wrapper.length == 1) {
		var nextBackground = background.nextBackgroundElement;

		nextBackground.width(wrapper.width());
		nextBackground.height(wrapper.height());
		setBackgroundImage(nextBackground, background.images[0]);

		wrapper.prepend(nextBackground);
		setBackgroundImage(wrapper, background.images[1]);

		setTimeout(setNextBackgroundImage, background.imageChangeInterval);
	}
});

$(window).resize(function() {
	var wrapper = $(background.wrapperSelector);
	if (wrapper.length == 1) {
		var nextBackground = $(background.nextBackgroundSelector);
		nextBackground.width(wrapper.width());
		nextBackground.height(wrapper.height());
	}
});

//
// Carousel Support Functions
//

//
// getCarouselItemHtml(item)
// Gets the HTML for an list item in the carousel for the specified video.
// PARAMS
//  item: A video configuration instance.
//
function getCarouselItemHtml(item) {
	return '<img src="' + item.thumbnail + '" height="148" width="195" alt="' + item.title + '"/>';
}
//
// getCarouselVideoItemHtml(item)
// Gets the HTML for an item in the carousel that will display the specified video.
// PARAMS
//  item: A video configuration instance.
//
function getCarouselVideoItemHtml(item) {
	return '<strong>' + item.title + '</strong><a href="' + item.flv + '" style="background-image:url(\'' + item.thumbnail + '\');"><img src="' + item.screencap + '" height="352" width="464" alt="' + item.title + '"/></a>';//
}
//
// onItemFirstIn(carousel, item, i, state)
// Handles the "First Item In" event on the carousel.
// Sets up the active video element.
// PARAMS
//  carousel: The carousel.
//  item: The HTML object of the first item in the carousel viewport.
//  i: The identifier for the first item in the carousel viewport.
//  state: The carousel state.
//
function onItemFirstIn(carousel, item, i, state) {
	$(item).removeClass('active').html(getCarouselItemHtml(videos[carousel.index(i, videos.length)-1])).children('img').click(function(e) { e.preventDefault(); carousel.scroll(i-1); return false; });
	$(item).next('li').addClass('active').html(getCarouselVideoItemHtml(videos[carousel.index(i+1, videos.length)-1])).children('a').click(function(e) { e.preventDefault(); playVideo($(this), true); return false;});
	$(item).next('li').next('li').removeClass('active').html(getCarouselItemHtml(videos[carousel.index(i+2, videos.length)-1])).children('img').click(function(e) { e.preventDefault(); carousel.scroll(i+1); return false; });;
}
//
// onItemVisibleIn(carousel, item, i, state, evt)
// Handles the "Item Visible In" event on the carousel.
// PARAMS
//  carousel: The carousel.
//  item: The HTML object of the first item in the carousel viewport.
//  i: The identifier for the first item in the carousel viewport.
//  state: The carousel state.
//
function onItemVisibleIn(carousel, item, i, state, evt) {
	var idx = carousel.index(i, videos.length);
	carousel.add(i, getCarouselItemHtml(videos[idx - 1]));
}
//
// onItemVisibleOut(carousel, item, i, state, evt)
// Handles the "Item Visible Out" event on the carousel.
// PARAMS
//  carousel: The carousel.
//  item: The HTML object of the first item in the carousel viewport.
//  i: The identifier for the first item in the carousel viewport.
//  state: The carousel state.
//
function onItemVisibleOut(carousel, item, i, state, evt) {
	carousel.remove(i);
}
//
// playVideo(link)
// Initializes and launches Flowplayer and plays the video specified by the link.
// PARAMS
//  link: A link to an FLV.
//  autoplay: Specifies whether the video should begin playing immediately.
//
function playVideo(link, autoplay) {
	$(link).flowplayer('images/flowplayer/flowplayer.swf', {
		key: keys[location.host.substring(location.host.indexOf('.')+1)] || '',
		clip: {
			url: $(link).attr('href'),
			autoPlay: autoplay,
			autoBuffering: true
		}
	});
	$(link).flowplayer(0).load();
}

//
// Background Support Functions
//

//
// setNextBackgroundImage()
// Sets the next background image.
//
function setNextBackgroundImage() {
	var nextBackground = $(background.nextBackgroundSelector);

	nextBackground.fadeTo(background.fadeLength, 0, function() {
		var imageIndex = (background.lastImageIndex == 1) ? 0 : 1;
		setBackgroundImage(nextBackground, background.images[imageIndex]);

		nextBackground.fadeTo(0, 1, function() {
			setBackgroundImage($(background.wrapperSelector), background.images[background.lastImageIndex]);
			background.lastImageIndex = imageIndex;

			setTimeout(setNextBackgroundImage, background.imageChangeInterval)
		});
	});
}
//
// setBackgroundImage(element, image)
// Sets the background image of the specified element to the specified image.
// PARAMS
// element: An element.
// image: The image to be used as the background image.
//
function setBackgroundImage(element, image) {
	element.css('background-image', 'url(\'images/' + image + '\')');
}

