﻿//Author: A.Reinermann
//Company: ICATT interactive media
//Date: 2008/09/26

var rotators = {};
				
function getRotator(rotatorId) {
	var rot = rotators["rotator" + rotatorId];
	if ("object" != typeof rot) {
		rotators["rotator" + rotatorId] = new rotator(rotatorId);
		rot = rotators["rotator" + rotatorId];
	}
	return rot;
}

function rotator(rotatorId) {
	/*----------------
		Fields
	----------------*/
	//configure the below variables to change the style of the scroller
	this.scrollerwidth='250px';
	this.scrollerheight='202px';
	this.scrollerbgcolor='#fff';
	
	this.mainDivClass='mainDiv';
	this.clipDivClass='clipDiv';
	this.firstDivClass='firstDiv';
	this.secondDivClass='secondDiv';
	this.titleLinkClass='titleLink';
	this.descriptionDivClass='descriptionDiv';
	this.readmoreLinkClass='readmoreLink';
	
	this.scrollerbackground=''; // no background = ''; background image = 'path/backgroundimage.extension'
	
	this.scrollInterval=5 // in seconds
	this.scrollStepSize=5 // in pixels

	//!!!! DO NOT EDIT ANYTHING BELOW !!!!
	this.scrollDuration=50 // in miliseconds
	this.ie=document.all
	this.dom=document.getElementById
	this.rotatorId = rotatorId;

	//Div settings
	this.mainDivId = 'rotator_' + this.rotatorId;
	this.containerDivId = this.mainDivId + '_containerdiv'
	this.firstItemDivId = this.mainDivId + '_firstdiv';
	this.secondItemDivId = this.mainDivId + '_seconddiv';
	this.firstItemDiv;
	this.secondItemDiv;
	this.topDiv;
	this.bottomDiv;
	
	this.messages=new Array();
	this.currentMessageIndex=0;
	
	//Return the message from the messages collection to which the message pointer currently is pointing
	this.currentMessage = function() {
		if (this.currentMessageIndex < this.messages.length) {
			return this.messages[this.currentMessageIndex];
		}else{
			return null;
		}
	};
	
	//Move the current message pointer to the next message
	this.moveToNextMessage = function () {
		if (this.currentMessageIndex + 1 < this.messages.length) {
			this.currentMessageIndex = this.currentMessageIndex + 1;
		}else{
			this.currentMessageIndex = 0;
		}
	};


	/*----------------
		Methods
	----------------*/
	
	//Add a message to the rotator
	this.addMessage = function addMessage(title, description, url){
		var msg = new message(title, description, url);
			msg.titleLinkClass = this.titleLinkClass;
			msg.descriptionDivClass = this.descriptionDivClass;
			msg.readmoreLinkClass = this.readmoreLinkClass;
		this.messages[this.messages.length] = msg;
	};
	
	//Write the rotator HTML
	this.write = function write(){
		if (this.ie||this.dom){
			document.writeln('<div id="' + this.mainDivId + '" class="' + this.mainDivClass + '" style="position:relative;width:'+this.scrollerwidth+';height:'+this.scrollerheight+';overflow:hidden;background-color:'+this.scrollerbgcolor+' ;background-image:url('+this.scrollerbackground+')">')
			document.writeln('<div id="' + this.containerDivId + '" class="' + this.clipDivClass + '" style="position:absolute;width:'+this.scrollerwidth+';height:'+this.scrollerheight+';clip:rect(0 '+this.scrollerwidth+' '+this.scrollerheight+' 0);left:0px;top:0px">')
			document.writeln('<div id="' + this.firstItemDivId + '" class="' + this.firstDivClass + '" style="position:absolute;width:'+this.scrollerwidth+';left:0px;top:1px;">')
			document.write(this.currentMessage().toHtml())
			document.writeln('</div>')
			document.writeln('<div id="' + this.secondItemDivId + '" class="' + this.secondDivClass + '" style="position:absolute;width:'+this.scrollerwidth+';left:0px;top:0px;visibility:hidden">')
			document.write(this.messages[dyndetermine=(1==this.messages.length)? 0 : 1].toHtml())
			document.writeln('</div>')
			document.writeln('</div>')
			document.writeln('</div>')
		}
	};
	
	//Swap the 2 message divs
	this.switchDivs = function switchDivs() {
		var oldTopDiv;
		oldTopDiv = this.topDiv;
		this.topDiv = this.bottomDiv;
		this.bottomDiv = oldTopDiv;
	};
	
	//Scroll the next message into view
	this.scrollMessages = function scrollMessages(){
		var topDivTop = parseInt(this.topDiv.style.top);
		var bottomDivTop = parseInt(this.bottomDiv.style.top);
		
		if (bottomDivTop > 0 && bottomDivTop <= this.scrollStepSize) {
			//Set as top
			this.bottomDiv.style.top = "0px";
			this.switchDivs();

			setTimeout("getRotator(" + this.rotatorId + ").displayNextMessage()",this.scrollInterval * 1000);
			return
		}else{
			if (bottomDivTop >= this.bottomDiv.offsetHeight*-1) {
				this.bottomDiv.style.top = bottomDivTop - this.scrollStepSize + "px";
				this.topDiv.style.top = topDivTop - this.scrollStepSize + "px";
				setTimeout("getRotator(" + this.rotatorId + ").scrollMessages()",this.scrollDuration);
			}
		}
	};
	
	//Display the next message in the rotator
	this.displayNextMessage = function displayNextMessage(){
		this.moveToNextMessage();
		this.bottomDiv.innerHTML=this.currentMessage().toHtml();
		this.bottomDiv.style.top=this.scrollerheight;
		this.bottomDiv.style.visibility='visible';
		this.scrollMessages();						
	};
	
	//Display the first message in the rotator
	this.displayFirstMessage = function displayFirstMessage() {
		//Make sure the next message will start to appear after messageRotating interval
		setTimeout("getRotator(" + this.rotatorId + ").displayNextMessage()",this.scrollInterval * 1000);
	};
	
	//Fire up the rotating
	this.startRotate = function startRotate(){
		if (this.ie||this.dom){
			this.firstItemDiv = document.getElementById(this.firstItemDivId);
			this.secondItemDiv = document.getElementById(this.secondItemDivId);
			this.topDiv = this.firstItemDiv;
			this.bottomDiv = this.secondItemDiv;
			this.displayFirstMessage();
		}
	};
	
	//Make sure the rotating starts when the page is loaded
	this.registerOnLoad = function registerOnLoad(){
		var rotator = this;
		var loadEvents = window.onload;
		
		if ('function' != typeof loadEvents){
			window.onload = rotator.startRotate();
		} else {
			window.onload = function() {
				loadEvents();
				rotator.startRotate();
			}
		}
	};
	
	/*----------------
		Objects
	----------------*/
	function message(title, description, url){
		this.titleLinkClass;
		this.descriptionDivClass;
		this.readmoreLinkClass;
		this.title = title;
		this.description = description;
		this.url = url;
		this.toHtml = function toHtml(){
			var href='';
			var readMore='';
			
				if (''!=this.url){
					href=' href="' + this.url + '" ';
					readMore = '<a class=" ' + this.readmoreLinkClass + '" ' + href + '>lees verder </a>'
				};
			return '<a class=" ' + this.titleLinkClass + '" ' + href + '><h1>' + this.title + '</h1></a><div class="' + this.descriptionDivClass + '">' + this.description + readMore + '</div>'
		};
	};
}
