var check=0;
var active;
var _aId;
var _bId;

function BannerControl(id, delay, fadeTimeout) {
	this.bannerElement = $("#"+id);
	this.delay = delay;
	this.fadeTimeout = fadeTimeout;
	this.timerId;
	this.immediatelyId;
	this.slideIndex=0;
	this.slides=new Array();
	_aId = "#"+id+"_a img";
	this._a = $(_aId);
	_bId = "#"+id+"_b img";
	this._b = $(_bId);	
	this._init();
}

BannerControl.prototype = {
	_init: function() {
		var view = this;
		this.bannerElement.mouseenter(function() {
	   		view.stop();
		}).mouseleave(function() {
			view.start(true);
		});
	},
	setSlides: function (slides) {
		this.slides = slides;
	},
	start: function (immediately) {
		var view = this;
		if (immediately) {
			this.immediatelyId = setTimeout( function() {
				view._nextSlide(view);
			},500);
		}
		this.timerId = setInterval( function() {
				view._nextSlide(view);
			}, this.delay );		
	},
	stop: function() {
		clearInterval(this.timerId);
		clearTimeout(this.immediatelyId);				
	},
	_nextSlide: function(view) {
			if (view.slideIndex >= view.slides.length) {
				view.slideIndex = 0;
			}
			var src = view.slides[view.slideIndex];
			swapBanner(src, view.fadeTimeout, view._a, view._b);
			view.slideIndex++;			
			active=null;
		}
}

function LinkControl(id, img, fadeTimeout, bigBanner) {
	this.id = id;
	this.linkElement = $("#"+id)
	this.img = img;
	this.fadeTimeout = fadeTimeout;
	this.bigBanner = bigBanner;
	this._a = $(_aId);
	this._b = $(_bId);	
	this._init();
}

LinkControl.prototype = {
	_init: function() {
		var view = this;
		$(this.linkElement).mouseenter(function() {
			view.bigBanner.stop();
			if (active != view.id) {
				view._a.stop(true, true);
				view._b.stop(true, true);
				swapBanner(view.img, view.fadeTimeout,view._a,view._b);
				active = view.id;
			}
		}).mouseleave(function() {
			view.bigBanner.start(true);
		});
	}
}
	
function swapBanner(src, speed, a, b) {	
	if (check == 1) {
		a.unbind("load");
		a.attr('src', "");
		a.attr('src', src);
		a.bind("load", function () {
			a.fadeIn(speed);
			b.fadeOut(speed); 
		});		
		check = 0;
	} else {
		b.unbind("load");
		b.attr('src', "");
		b.attr('src', src);
		b.bind("load", function () {
			b.fadeIn(speed);
			a.fadeOut(speed); 
		});		
		check = 1;
	}
}

