/**
 * @author	Satana Charuwichitratana (Mick)
 * @date	23-07-2010
 * @description	SwapBanner class is used to create a sequence of images and their destination links when user clicks on it
 */

SwapBannerManager = {};
SwapBannerManager.arrSwapBanners = new Array;
SwapBannerManager.add = function (swap_banner) {
	this.arrSwapBanners[swap_banner.id] = swap_banner;
}
SwapBannerManager.startSwap = function (id, interval) {
	setTimeout('SwapBannerManager.arrSwapBanners["' + id + '"].swap(); SwapBannerManager.startSwap("' + id + '", ' + interval + ');', interval); 
};

SwapBanner = function (cfg) {
	this.id = 'sb_' + cfg.id;
	this.imagePaths = new Array;
	this.linkTo = new Array;
	this.interval = 5 * 1000; // 5 sec
	this.currentIndex = 0;
	this.image = new Image;
	this.image.id = cfg.id;

	document.getElementById(cfg.renderTo).appendChild(this.image);

	this.push = function(image_path, link_to) {
		this.imagePaths.push(image_path);
		this.linkTo.push(link_to);
		this.currentIndex = this.imagePaths.length - 1;

		this.image.src = image_path;
		eval("this.image.setAttribute('onclick', function() { document.location = '" + link_to + "'; } );");
	}
	this.startSwap = function() {
		SwapBannerManager.startSwap(this.id, this.interval);
	}
	this.swap = function() {
		this.currentIndex++;
		if (this.currentIndex >= this.imagePaths.length) {
			this.currentIndex = 0;
		}
		this.image.src = this.imagePaths[this.currentIndex];
		eval("this.image.setAttribute('onclick', function() { document.location = '" + this.linkTo[this.currentIndex] + "'; } );");
	}

	SwapBannerManager.add(this);
};