var Carousel = function() {

	var carousel; // Carousel element;
	var carousel_wrap; // Element that contains the changing background
	var step = 220; // Carousel element width in pixels
	var speed = 6; // Animation in seconds

	var children = []; // Elements within carousel

	var active = ''; // Currently active element, number reference to children
	var _transition = false; // Cycle through carousel items
	var running = false; // Is the carousel currently running
	var _interval_id = 0; // Used to kill current interval

	var fetch = function()
	{
		carousel = jQuery("#brand-carousel");
		carousel_wrap = jQuery("#brand-carousel-wrap");
		if (!carousel.length) return false;

		settings(carousel_wrap);

		var counter = 1;
		var t = [];
		jQuery("li", carousel).each(function(){
			children[counter] = this;
			this.carouselItem = counter;
			counter++;

			t[counter] = img = new Image(561,440);
			t[counter].src = jQuery(this).attr("rel");
		});
		active = 0;

		jQuery("a", carousel).hover(
			function() {
				stop();

				current = this.parentNode;
				jQuery(".active", carousel).removeClass("active");
				jQuery(current).addClass("active");
				active = this.parentNode.carouselItem;
				addCurrentStyling();
			},
			function() {
				start();
			}
		);

		if (_transition) {
			transition_help();
		}
		start();
	};

	var settings = function()
	{
		var settings = carousel_wrap.attr("class");
		var patt = new RegExp(/carousel\[([^>]+)\]/);
		var values = patt.exec(settings)[1].split(";");

		for (i = 0, el = values.length; i < el; i++) {
			value = values[i].split(":");
			switch(value[0])
			{
				case 'transition':
					_transition = value[1] == 'false' ? false : true;
				break;
			}
		}
	};

	var start = function()
	{
		if (_transition) {
			if (active > 0) {
				jQuery("a", children[active]).removeClass('active');
			}
			_interval_id = setInterval(animate, speed * 1000);
			running = true;
		}
	};

	var stop = function()
	{
		clearInterval(_interval_id);
		running = false;
	};

	var animate = function()
	{
		if (_transition) {
			transition_help();
		}

		if (active > 0) {
			child = jQuery(children[active]);
			jQuery(children[active]).removeClass('active');
		}
		active++;
		active = active == children.length ? 1 : active;

		addCurrentStyling();
		if (_transition) {
			jQuery("#carousel-transition").fadeOut("slow");
		}
	};

	var transition_help = function()
	{
		var wrap = jQuery('#brand-carousel-wrap');
		transition_block = jQuery("#carousel-transition");
		if (transition_block.length) {
			transition_block.css({'display': 'block'});
		}
		else {
			wrap.append(
				'<div id="carousel-transition"></div>'
			);
		}

		var bg = active > 0
			? 'url(' + jQuery(children[active]).attr('rel') + ')'
			: wrap.css("background-image")
		;
		transition_block.css({
			'background-image': bg
		});
	};

	var addCurrentStyling = function()
	{
		jQuery(children[active]).addClass('active');
		carousel_wrap.css({
			'background-image': 'url(' + jQuery(children[active]).attr('rel')  +')'
		});
	};

	return {
		init : function() {
			fetch();
		}
	}
}();

jQuery(document).ready(function() {
	// delay by 8 seconds
	setTimeout('Carousel.init()', 8000);
});