/*
========================================
===> [ Copyright © Swarm Media Ltd ] <==
========> All rights reserved. <========
========================================
#::- This code was written by Swarm -::#
========================================
====> RETAIN THIS COPYRIGHT NOTICE! <===
=======> http://www.swarmuk.com <=======
========================================
*/

$(document).ready(function(){

	var isHovered = false;
	var curBanner = 0;
	var totalBanners = $(".banner_viewer .images li").size();
	
	/* Function to move to a banner. If no index specified, move to next banner. */
	function move_to_banner(banner_index) {
		
		if (banner_index == null) {
			
			if (curBanner < totalBanners - 1) {
				nextBanner = curBanner + 1;
			} else {
				nextBanner = 0;	
			}
			
		} else {
			nextBanner = banner_index;	
		}
		
		if (curBanner != nextBanner) {
			/* Fade out previous banner. */
			$('.banner_viewer .images li:eq(' + (curBanner) + ')').fadeOut();
			$('.banner_viewer .images li:eq(' + (nextBanner) + ')').fadeIn();
			$('.banner_viewer .selectors li:eq(' + (curBanner) + ')').removeClass('current');
			$('.banner_viewer .selectors li:eq(' + (nextBanner) + ')').addClass('current');
			curBanner = nextBanner;
		}
	}
	
	/* Intermittently move to next banner if not hovered. */
	function banner_rotation() {
		
		if (isHovered == false) {
			move_to_banner(null);	
		}
		
		window.setTimeout(banner_rotation, 5000);
	}
	
	/* On rolling over a selector. */
	$('.banner_viewer .selectors li').mouseover(function() {
		banner_index = $(this).index();
		move_to_banner(banner_index);
	});

	/* On rolling out a selector. */
	$('.banner_viewer .selectors li').mouseout(function() {
												
	});

	/* On rolling over any part of the banner viewer. */
	$('.banner_viewer').mouseover(function() {
		isHovered = true;								
	});

	/* On rolling out of the banner viewer. */
	$('.banner_viewer').mouseout(function() {
		isHovered = false;										
	});

	/* Initially hide all banners after the first, and activate first selector button. */
	$(".banner_viewer .images li:gt(0)").hide();
	$('.banner_viewer .selectors li:eq(' + (curBanner) + ')').addClass('current');

	/* Start rotation. */
	window.setTimeout(banner_rotation, 5000);
});
