/* BOFHnet rjs@biuro.net.pl */

var flashbox = {
	options: {
		interval: 5000,
		speed: 600,
		width: 0,
		height: 0,
		easing: 'easeOutBack',
		returnEasing: 'easeOutSine',
		menuOpacity: 0.9,
		fontSize: 20
	},
	defaultColor: '#333',
	autoPlay: true,
	current: 0,
	timer: null,
	slide: new Array(),
	init: function() {
		$('.flashbox')
			.append('<div class="slider" />')
			.append('<div class="menu" />');
		jQuery.extend(true, this.options, this._getAttrFromTitle('.flashbox'));
		this.options.width = $('.flashbox').width();
		this.options.height = $('.flashbox').height();
		$('.flashbox ul li a').each(function() {
			var el = {
				url: $(this).attr('href'),
				desc: $(this).html()
			};
			jQuery.extend(true, el, flashbox._getAttrFromTitle($(this)));

			$('<a href="' + el.url + '" class="slide">' + el.desc + '</a>')
				.attr('title', el.desc)
				.hover(function() {
					flashbox.over();
				}, function() {
					flashbox.out();
				})
				.css({
					'background-image': 'url(' + el.bg + ')',
					'width': flashbox.options.width,
					'padding-top': flashbox.options.height
				})
				.appendTo('.flashbox .slider');
			$('<a href="' + el.url + '"><span class="desc">' + el.desc + '</span><span class="end"/></a>')
				.attr('title', el.desc)
				.attr('slide', flashbox.slide.length)
				.addClass('tab-' + flashbox.slide.length)
				.css('color', (el.color)?el.color:flashbox.defaultColor)
				.hover(function() {
					flashbox.over();
					flashbox.scroll($(this).attr('slide'));
				}, function() {
					flashbox.out();
				})
				.appendTo('.flashbox .menu')
				.fadeTo(10, parseFloat(flashbox.options.menuOpacity));

			flashbox.slide.push(el);
		});
		$('.flashbox ul').remove();
		this.fit();
		this.scroll('0');
	},
	over: function() {
		this.autoPlay = false;
		this._clearTimer();
	},
	out: function() {
		this.autoPlay = true;
		this._setTimer();
	},
	scroll: function(to) {
		this._clearTimer();
		if (!to) {
			to = this.current;
			to++;
		}
		if (to >= this.slide.length) to = 0;
		this.current = to;
		$('.flashbox .menu a').removeClass('active');
		$('.flashbox .menu a.tab-' + to + '').addClass('active');
		var offset = this.options.width * this.current * -1;
		$('.flashbox .slider').animate({
			'left': offset
		}, {
			queue: false,
			duration: parseInt(this.options.speed),
			easing: (this.current)?this.options.easing:this.options.returnEasing,
			complete: function() {
				flashbox._setTimer();
			}
		});

	},
	fit: function() {
		var widthDiff = widthDiff = this.options.width - $('.flashbox .menu').width();
		while (widthDiff <= 0) {
			flashbox.options.fontSize--;
			$('.flashbox .menu a span.desc').each(function() {
				$(this).css('font-size', flashbox.options.fontSize);
			});
			widthDiff = this.options.width - $('.flashbox .menu').width();
		}
		var expand = parseInt(widthDiff / this.slide.length);
		$('.flashbox .menu a span.desc').each(function() {
			$(this).css('width', $(this).width() + expand);
		});
		widthDiff = this.options.width - $('.flashbox .menu').width();
		$('.flashbox .menu a span.desc:first').each(function() {
			$(this).css('width', $(this).width() + widthDiff);
		});
	},
	_clearTimer: function() {
		window.clearTimeout(this.timer);
		this.timer = null;
	},
	_setTimer: function() {
		if (this.timer)
			this._clearTimer();
		if (this.autoPlay)
			this.timer = window.setTimeout('flashbox.scroll()', this.options.interval);
	},
	_getAttrFromTitle: function(selector) {
		var el = $(selector);
		var str = el.attr('title');
		var obj = {};
		var lines = str.split(';');
		for (line in lines) {
			var v = lines[line].split('=');
			obj[v[0]] = v[1];
		}
		if (obj.title)
			el.attr('title', obj.title);
		else
			el.removeAttr('title');
		return obj;
	}
};

$(function() {
	flashbox.init();
});

