// Home page timed ad campaign carousel

CampaignRotator = function() {
	var self = this;
	this.root = document.getElementById("campaigns");
	this.list = document.getElementById("campaigns-index");
	this.campaigns = new Array;
	this.listItems = new Array;
	this.activeCampaign, this.previousCampaign, this.fader, this.timer;
	this.session = new Cookie("campaignPos");

	// index campaigns
	els = getElementsByAttributeValue("class", "campaign", this.root);
	var calamity = -1;
	for(var i = 0; i < els.length; i++) {
		this.campaigns[this.campaigns.length] = new Campaign(els[i],i);
		if (this.campaigns[i].calamity) calamity = i;
		if(els[i].className.match("active")) this.activeCampaign = this.campaigns.length - 1;
		
	}

	// index list
	els = this.list.getElementsByTagName("li");
	for(var i = 0; i < els.length; i++) {
		els[i].campaign = i;
		this.listItems[this.listItems.length] = els[i];
	}

	if(this.campaigns.length == 0 || 
		this.listItems.length == 0) {
		return;
	}

	// click in the campaign bar
	EventListener.addEvent(this.list, "click", function(e) { 
		return self.selectCampaign(e);
	});

	// pause and resume the timer on several elements
	var pausers = [
		this.root,
		document.getElementById("navigation"),
		document.getElementById("form-reisplanner"),
		document.getElementById("form-actuele-vertrektijden")
	]
	var els = document.getElementsByTagName("a");
	for (var i=0; i<els.length; i++) pausers.push(els[i]);

	EventListener.addEvents(pausers, "mouseover", this.pause, this);
	EventListener.addEvents(pausers, "mouseout", this.resume, this);

	// show default campaign
	if (calamity != -1)
		this.activeCampaign = calamity;
	else
		this.activeCampaign = (parseInt(this.session.get(), 10) || 0) % this.campaigns.length;

	this.showCampaign(this.activeCampaign);
	

	if(this.campaigns.length > 1 && this.listItems.length > 1) {
		
		//this.shiftCampaignList();
		this.startTimer(this.activeCampaign);
		
		// preload other campaign backgrounds
		this.cacheCampaigns();
	}	
}

CampaignRotator.prototype.updateSession = function(campaign) {
	this.session.value = (campaign +1) % this.campaigns.length;
	this.session.set();
}

CampaignRotator.prototype.pause = function() {
	if(this.timer && this.timer.isAnimating()) {
		this.timer.pause();
	}
}

CampaignRotator.prototype.resume = function() {
	if(this.timer && !this.timer.isAnimating()){
		this.timer.resume();
	}
}

CampaignRotator.prototype.showCampaign = function(campaign) {
	this.updateSession(campaign);
	this.campaigns[campaign].element.style.backgroundImage = "url(" + this.campaigns[campaign].background + ")";
	addClass(this.campaigns[campaign].element, "active");
	addClass(this.listItems[campaign], "active");
}

CampaignRotator.prototype.initCampaign = function(campaign) {
	// initialize campaign, make it the current one
	this.updateSession(campaign);
	addClass(this.campaigns[this.activeCampaign].element, "fading");
	removeClass(this.listItems[this.activeCampaign], "active");
	removeClass(this.campaigns[this.activeCampaign].element, "active");
	
	this.previousCampaign = this.activeCampaign;
	this.activeCampaign = campaign;
	
	this.fadeCampaign(1);			
	this.showCampaign(campaign);
	//this.shiftCampaignList();
	this.startFader();
}

CampaignRotator.prototype.selectCampaign = function(e) {
	// run after clicked in the list
	target = EventListener.getTarget(e);
	if(target.tagName.toLowerCase() == "a") {	
		if(target.parentNode.campaign == this.activeCampaign) return EventListener.preventDefault(e);
		if(this.fader) {
			this.fader.stop();
			this.fader = null;
		}
		if(this.timer) {
			this.timer.stop();
			this.timer = null;
		}
		this.updateTimer(-400);
		if(this.previousCampaign) removeClass(this.campaigns[this.previousCampaign].element, "fading");
		this.initCampaign(target.parentNode.campaign);
	}
	return EventListener.preventDefault(e);
}

/* fader */

CampaignRotator.prototype.startFader = function() {
	this.fader = new Animator(1, 100, this.method(this.fadeCampaign), this.method(this.endFader));
	this.fader.setDuration(1000);
	this.fader.setType(this.fader.NONE);
	this.fader.start();
}

CampaignRotator.prototype.fadeCampaign = function(x) {
	this.campaigns[this.activeCampaign].element.style.opacity = x / 100;
	this.campaigns[this.activeCampaign].element.style.filter = "alpha(opacity=" + x + ")";
}

CampaignRotator.prototype.endFader = function() {
	removeClass(this.campaigns[this.previousCampaign].element, "fading");
	this.startTimer(this.activeCampaign);
}

/* timer */

CampaignRotator.prototype.startTimer = function(campaign) {
	// set timer
	if(this.timer) this.timer.stop();
	if ((this.listItems.length > 1) && (this.campaigns[campaign].duration > 0)) {
		var width = this.listItems[campaign].offsetWidth;
		this.timer = new Animator(-400, width - 400, this.method(this.updateTimer), this.method(this.endTimer));
		this.timer.setDuration(this.campaigns[campaign].duration);
		this.timer.setRate(1000);
		this.timer.setType(this.timer.NONE);
		this.anch = this.listItems[this.activeCampaign].getElementsByTagName("a")[0];
		this.timer.start();
	}
}

CampaignRotator.prototype.updateTimer = function(x) {
	this.anch.style.backgroundPosition = x + "px 100%";
}

CampaignRotator.prototype.endTimer = function() {
	this.timer = null;
	this.updateTimer(-400);
	var c = this.activeCampaign + 1;
	if(c > this.campaigns.length - 1) c = 0;
	this.initCampaign(c);
}

/* preloader */

CampaignRotator.prototype.cacheCampaigns = function() {
	var cache = new Array;
	for(var i = 0; i < this.campaigns.length; i++) {
		cache[i] = new Image();	
		cache[i].src = this.campaigns[i].background;
	}
}

Campaign = function(obj, num) {
	this.element = obj;
	this.number = num;
	this.background = obj.getAttribute("rs:background");
	this.backgroundColor = obj.getAttribute("rs:backgroundcolor");
	this.duration = obj.getAttribute("rs:duration") * 1000;
	this.calamity = obj.getAttribute("rs:calamity") ? obj.getAttribute("rs:calamity"): false;
	this.element.style.backgroundColor = this.backgroundColor;
}

EventListener.addEvent(window, "load", function() {
	window.homeCampaigns = new CampaignRotator();
} );
