/**
 * Biblioteka służy do wyświetlania banerów.
 * Funkcje:
 * - losowe wyświeltanie banerów w postaci obrazków,
 * - deklarowanie dla każdego obrazka: nazwy pliku podstawowego
 *   i w wersji hover, wymiarów, tekstu "alt", adresu url,
 *   maksymalnej ilości wyświetleń
 * - blokowanie banera po przekroczeniu maksymalnej liczby odsłon
 * @version $Id: MafiBanners.js,v 1.1 2005/02/23 11:53:32 jarek Exp $
 * @author Jarek Wasilewski <jaroslaw.wasilewski@biatel.pl>
 */

function Banner(ban_name, img_src, ban_width, ban_height, ban_alt, ban_url) {
	this.Name = ban_name;
	this.Src = img_src;
	this.Alt = ban_alt;
	this.Width = ban_width;
	this.Height = ban_height;
	this.Url = ban_url;
	this._counter = -1; // wskazuje na brak ograniczenia
	this.CssClass = '';

	// preload image
	this.Image = new Image();
	this.Image.src = this.Src;
	this.Image.width = this.Width;
	this.Image.height = this.Height;
	this.Image.name = this.Name;
	this.Image.alt = this.Alt;

	// metody
	this.DecreaseCounter = BannerDecreaseShowCounter;
	this.Show = BannerShowBanner;
	this.Active = BannerActive;
	this.SetCounter = BannerSetCounter;
}

function BannerSetCounter (counter) {
	if (counter < -1)
		counter = -1;
	this._counter = counter;
}

function BannerDecreaseShowCounter () {
	if (this._counter > 0) this._counter--;
}

function BannerActive () {
	return (this._counter > 0 || this._counter == -1);
}

function BannerShowBanner() {
	if (this.Counter > -1) {
		// pokaż baner
		document.write('<div><a href="'+this.Url+'" title="'+this.Alt+'" class="'+this.CssClass+'">');
		document.write('<img src="'+this.Src+'" alt="'+this.Alt+'" id="'+this.Id+'"></a></div>');
	}
}

function SetBannerCssClass (class_name) {
	this.CssClass = class_name;
}

/**
 * Klasa wyświetlania sekwencji banerów
 * @param string id nazwa obiektu; MUSI być identyczna z nazwą zmiennej przechowującej obiekt
 * @param int id czas odświeżania baneru w sekundach
 */
function BannerRotator (id, interval) {
	this.Id = id;// id linku banera
	this._banners = Array();
	this.Interval = interval;
	this._timerId = null;
	this._works = false;
	this._currentBaner = null;
	this._prefix = 'mbr';
	this._synchronized = Array();

	// methods
	this.Init = BRInit;
	this.AddBanner = BRAddBanner;
	this.Change = BRChangeBanner;
	this.Synchronize = BRSynchronize;
	this.Show = BRShow;
	this.Stop = BRStop;
	this._checkSynchronization = _brCheckSynchronization;
}

function BRAddBanner () {
	if (arguments.length > 0) {
		for(var i = 0; i < arguments.length; i++) {
			bannerObj = arguments[i];
			if (bannerObj != undefined && typeof (bannerObj) == 'object')
				this._banners[this._banners.length] = bannerObj;
		}
	}
}

// TODO: Obsługa sytuacji, w której zostaje tylko jeden aktywny baner
function BRChangeBanner () {
	var i = 0;
	
	// losuje indeks nowego banera do pokazania
	
	do {
		var new_index = Math.floor(this._banners.length * Math.random());
		i++;
		
		// alert('BRChangeBanner ' + new_index);
	}
	
	while (i < 1000 && this._checkSynchronization(new_index));
	this._currentBaner = this._banners[new_index];
	linkIE = document.getElementById(this._prefix+'_'+this.Id);
	if (linkIE) {
		linkIE.href = this._currentBaner.Url;
		linkIE.title = this._currentBaner.Alt;
		
		if (document.images[this._prefix+'_img_'+this.Id]) {
			document.images[this._prefix+'_img_'+this.Id].src = this._currentBaner.Src;
			
			document.images[this._prefix+'_img_'+this.Id].alt = this._currentBaner.Alt;
			if (this._currentBaner.Width) {
				document.images[this._prefix+'_img_'+this.Id].width = this._currentBaner.Width;
			}
			if (this._currentBaner.Height) {
				document.images[this._prefix+'_img_'+this.Id].height = this._currentBaner.Height;
			}
		}
		this._currentBaner.DecreaseCounter();
	}
}

function _brCheckSynchronization (banner_index) {
	if (this._banners[banner_index] == this._currentBaner || !this._banners[banner_index].Active()) {
		return true;
	}
	// sprawdzenie synchronizacji z innymi banerami
	else {
		for (sbName in this._synchronized) {
			if (this._synchronized[sbName]._currentBaner && this._synchronized[sbName]._currentBaner.Name == this._banners[banner_index].Name) {
				return true;
			}
		}
	}
	return false;
}

function BRInit () {
	if (this._banners.length > 1) {
		this.Change();
		if (arguments.length > 0 && arguments[0] > 0) {
			setTimeout(this.Id + ".Init("+(this.Interval + arguments[0])*1000+")", (this.Interval + arguments[0])*1000);
		}
		else {
			this._timerId = setInterval(this.Id + ".Change()", this.Interval*1000);
		}	
		this._works = true;
	}
	else {
		this._currentBaner = this._banners[0];
	}
}

function BRStop () {
	if (this._works)
		clearInterval(this._timerId);
	this._works = false;
}

function BRShow() {
   // pokaż baner
   document.write('<div class="brBanner"><a href="'+this._currentBaner.Url+'" title="'+this._currentBaner.Alt+'" class="'+this._currentBaner.CssClass+'" id="'+this._prefix+'_'+this.Id+'">');
   document.write('<img src="'+this._currentBaner.Src+'" alt="'+this._currentBaner.Alt+'" id="'+this._prefix+'_img_'+this.Id+'"></a></div>');
}

function BRSynchronize () {
	if (arguments.length > 0) {
		for(var i = 0; i < arguments.length; i++) {
			brObj = arguments[i];
			if (!this._synchronized[brObj.Id]) {
				this._synchronized[brObj.Id] = brObj;
			}
			if (!brObj._synchronized[this.Id])
				brObj.Synchronize(this);
		}
	}
}
