var banners = {
	count: 0,
	duration: 500,
	rotate: function(bannerId) {
		var data = $('#' + bannerId).data('options');
		var next = data.current;
		next++; if (next > data.count) next = 1;
		$('#' + bannerId + ' ul li:nth-child(' + data.current + ')').css('z-index', 0).show();
		if($('#' + bannerId + ' ul li:nth-child(' + next + ')').find('object').size() > 0)
			$('#' + bannerId + ' ul li:nth-child(' + next + ')').flash(function(){this.GotoFrame(0);this.Play();});
		$('#' + bannerId + ' ul li:nth-child(' + next + ')')
			.css('z-index', 1)
			.fadeIn(this.duration);
		$('#' + bannerId + ' ul li:nth-child(' + data.current + ')').delay(this.duration).hide();
		$('#' + bannerId).data('options').current = next;
		this._setTimer(bannerId);
	},
	start: function(bannerId) {
		this._setTimer(bannerId);
	},
	pause: function(bannerId) {
		window.clearTimeout($('#' + bannerId).data('options').timer);
		$('#' + bannerId).data('options').timer = null;
	},
	next: function(bannerId) {
	if ($('#' + bannerId).data('options').timer)
			this.pause(bannerId);
	this.rotate(bannerId);
	},
	_setTimer: function(bannerId) {
		if ($('#' + bannerId).data('options').timer)
			this.pause(bannerId);
		$('#' + bannerId).data('options').timer = window.setTimeout('banners.rotate(\'' + bannerId + '\')', $('#' + bannerId).data('options').interval);
	}
};
$(function() {
	function objFromRel(selector) {
		var obj = {},
			el = $(selector),
			str = el.attr('rel');
		el.removeAttr('rel');
		var lines = str.split(';');
		for (line in lines) {
			var v = lines[line].split(':');
			obj[v[0]] = v[1];
		};
		return obj;
	};
	$('div.banner').each(function() {
		var data = {
			current: 1,
			timer: null,
			interval: 3000,
			count: 0
		};
		var width = $(this).width(),
			height = $(this).height(),
			bannerId = 'banner' + banners.count++;
		$(this).attr('id', bannerId);
		$.extend(true, data, objFromRel($(this)));
		$(this).find('a').each(function() {
			data.count++;
			var ext = $(this).text().match(/([^\/\\]+)\.(\w+)$/)[2],
				parent = $(this).parent();
			if (ext == 'swf') {
				var src = $(this).text(),
					href = $(this).attr('href'),
					parent = $(this).parent('li');
				var options = {
					swf: src,
					expressInstaller: '/imp/swf/expressinstall.swf',
					width: width,
					height: height,
					hasVersion: 8,
					params: {
						bgcolor: '#f5f5f5',
						quality: 'high',
						wmode: 'opaque',
						allowScriptAccess: 'sameDomain'
					},
					flashvars: {
						ad_url: href,
						banner_id: bannerId
					}
				};
				$(parent).html('').flash(options);
			} else {
				$(this).html('<img src="' + $(this).text() + '" />');
			};
			if(data.count > 1) parent.hide();
		});
		if (data.count > 1) {
			$(this)
				.mouseenter(function() {
					banners.pause($(this).attr('id'));
				})
				.mouseleave(function() {
					banners.start($(this).attr('id'))
				});

			$(this).data('options', data);
			banners.start(bannerId);
		};
	});
});

