/*
 * SimpleBanner - Alex Boone - 2010-09-26
 * simple banner rotation script
 *  requires that BANNER_CONFIG has been previously defined
 *  if jQuery and Cycle plugin exist, will use that
 */
var SimpleBanner = function(cfg){
	return {
		refresh_secs : !isNaN(cfg.refresh_secs) ? cfg.refresh_secs : 60,
		banners : cfg.banners || [],
		init : function(){
			if (this.banners && this.banners.length) {
				if (typeof jQuery == 'function' && jQuery.fn && jQuery.fn.cycle) {
					var markup = '<div class="banner" style="height: ' + cfg.height + 'px; width: ' + cfg.width + 'px;">';
					for (var i=0; i<this.banners.length; i++) {
						var b = this.banners[i];
						markup += '<a href="' + b.linkURL + '" target="_blank">'
									+ '<img border="0" alt="' + b.altText + '" src="' + b.imgURL + '" />'
								+ '</a>';
					}
					document.write(markup);
					jQuery('.banner').cycle({ 
						fx: 'fade',
						timeout:  cfg.refresh_secs * 1000 
					});
				} else {
					document.write('<a id="simple-banner" target="_blank"><img border="0"/></a>');

					var a = document.getElementById('simple-banner');
					this.setupBanner(this.randomBanner(), a);

					if (this.banners.length > 1 && this.refresh_secs){
						var that = this;
						window.setInterval(function(){
							that.setupBanner(that.randomBanner(), a);
						}, this.refresh_secs * 1000);
					}
				}
			}
		},
		randomBanner : function(){
			var i = Math.floor(Math.random()*this.banners.length);
			return this.banners[i];
		},
		setupBanner : function(banner, a){
			a.setAttribute('href', banner.linkURL);
			var img = a.getElementsByTagName('img')[0];
			img.setAttribute('src', banner.imgURL);
			img.setAttribute('alt', banner.altText);
		}
	}
};

if (BANNER_CONFIG) {
	var banner = new SimpleBanner(BANNER_CONFIG);
	banner.init();
}

