function Banner(args) 
{
	this.instanceId = Banner.instances.length;
	Banner.instances.push(this);
	
	document.getElementById('slides').innerHTML = '<div id="' + args.divId + '"></div>';
	this.hostId = args.divId;
	this.host =  jQuery('#' + args.divId);
	this.host.width(args.width).height(args.height);
	this.width = args.width;
	this.height = args.height;
	this.slides = args.slides;
	this.nextDelay = args.nextDelay;
	this.fadeDelay = args.fadeDelay;
	
	this.loaded = [];
	for (var i = 0, max = this.slides.length; i < max; i++) {
		this.loaded[i] = false;
                onclickImg = '';
                useMap = '';
                mapHtml = '';
                if(this.slides[i].link!=""){
                    onclickImg = 'onclick="window.location=\''+this.slides[i].link+'\'"';
                }else{
                    nameMap = 'useMap'+(i);
                    useMap = 'usemap="#'+nameMap+'"';
                    mapHtml = '<map name="'+nameMap+'">';
                    mapHtml += this.slides[i].map;
                    mapHtml += '</map>';
                }
                content = '<a id="' + this.hostId + i + '"><img '+onclickImg+' src="' + this.slides[i].image + '" onload="Banner.instances[' + this.instanceId + '].onLoad(' + i + ')" '+useMap+'>'+mapHtml+'</a>';
		var slide = jQuery(content).css({'width': 0, 'height': args.height, 'display': 'none'});
		this.host.append(slide);
	}

	this.next();
}

// used for soft references to a banner instance
Banner.instances = [];

Banner.prototype = {
	instanceId: null,
	width: null,
	height: null,
	nextDelay: null,
	fadeDelay: null,
	slides: null,
	hostId: null,
	host: null,
	current: 0,
	zIndex: 0,
	pending: false, // image is pending but not loaded yet
	loaded: null, // array of: int (slide index) => bool (loaded or not)
	onLoad: function (id)
	{
		this.loaded[id] = true;
		if (this.pending && id == this.current) {
			this.pending = false;
			this.next();
		}
	},
	next: function ()
	{
		if (this.loaded[this.current]) {
			var slide = jQuery('#' + this.hostId + this.current);
			// TRICKY: animating irrelevant margin-left as a stub for modified width anim.
			// (see step() handler)
			slide
				.css({'width': 0, 'margin-left': 0, 'display': 'none', 'zIndex': this.zIndex++})
				.css({'display': 'block'})
				.animate({'margin-left': this.width}, {
					'duration': this.fadeDelay,
					'callback': function (current) {
						slide.css('margin-left', 0);
					}, 
					'step' : function (current) {
						// avoid IE6 jitter by using only even widths (rounding error)
						slide.css('width', 2 * Math.ceil(current / 2));
					}
				});
			this.current = (this.current + 1) % this.slides.length;
			var that = this;
			setTimeout(function () {that.next()}, this.nextDelay);
		} else {
			this.pending = true;
		}
		
	}
};
