/**
 * Purpose of this sroller is to scroll horizontal trough photos
 *
 *	@param ID the ID from the element that needs to be scrolled in
 *	@param TotaleBreedte is the total width of the ID
 *	@param ElementBreedte is the width of a single element inside ID
 *	@param HoeveelElementen How many showable elements
 *
 */
function ReferentieScroller( ID, TotaleBreedte, ElementBreedte, HoeveelElementenZichtbaar, StartPosition, LeftButtonID, RightButtonID ) {
	/** Setting up the scroller */
	this.Setup( ID, TotaleBreedte, ElementBreedte, HoeveelElementenZichtbaar, StartPosition, LeftButtonID, RightButtonID );
}

/**
 * Constructing the scroller
 * @param ID the ID from the element that needs to be scrolled in
 * @param TotaleBreedte is the total width of the ID
 * @param ElementBreedte is the width of a single element inside ID
 */
ReferentieScroller.prototype.Setup = function( ID, TotaleBreedte, ElementBreedte, HoeveelElementenZichtbaar, StartPosition, LeftButtonID, RightButtonID ) {
	/**
	 * The ID of the element that needs to scroll.
	 */
	this.ElementID = ID;

	/**
	 * Total width of the element
	 */
	this.TotaleBreedte = TotaleBreedte;
	/**
	 * Width of one element inside the ID
	 */
	this.ElementBreedte = ElementBreedte;
	/**
	 * To check if everything is ok
	 */
	this.Ok = false;
	/**
	 * All the childs inside the ID
	 */
	this.Childs = new Array();

	this.StartPosition = StartPosition;
	
	/**
	 * The counter of current element
	 */
	this.Counter = 0 + this.StartPosition;
	
	this.HoeveelElementenZichtbaar = HoeveelElementenZichtbaar;
	
	/**
	 * The ID's for the Left and Right buttons
	 */
	this.LeftButtonID = LeftButtonID;
	this.RightButtonID = RightButtonID;
	
	/**
	 * Getting childs that are inside ElementID and aligning them after (if OK = true)
	 */
	this.getChilds();
	
	this.setupNavigationButtons();	
	
	this.alignChilds();

}

/**
 * Aligning all childs
 */
ReferentieScroller.prototype.alignChilds = function() {
	if (this.Ok) {

		var Start = this.StartPosition;
		var Width = this.ElementBreedte;
		/* Initializing the counter */
		var Teller = parseInt( ( Start * Width ) * -1 );

		for ( var i = 0; i < this.Childs.length; i++) {
			/* Getting one single child */
			var Child = this.Childs[i];
			/* Setting the childs on the right position */
			Child.style.left = Teller + 'px';
			/* Add the width of a single element to the total width */
			Teller += this.ElementBreedte;
		}
	}
}

/**
 * Funtion to get all the Childs
 */
ReferentieScroller.prototype.getChilds = function() {
	/**
	 * If the ID excists 
	 */
	if (this.ElementID) {

		/* Getting the element */
		Current = document.getElementById(this.ElementID);
		if ( Current) {
			Current.style.width = this.TotaleBreedte + 'px';

			/* Getting all chulds inside the Element of ID */
	
			for ( var i = 0; i < Current.childNodes.length; i++) {
				var Child = Current.childNodes[i];
				if (Child.tagName == "LI") {
					this.Childs.push(Child);
				}
			}
		}

		/* If there are childs, then it is workable, and we set the Ok to true */
		if (this.Childs.length) {
			this.Ok = true;
		}
	}
}

/**
 * This function will move all the childs to right
 */
ReferentieScroller.prototype.moveRight = function() {
	
	var Left = 0;
	var Child = 0;
	var NewLeft = 0;

	if (this.Ok) {

		/* Calculating the current position of the first element */
		Left = parseInt(this.Childs[0].style.left.split("px").join(""));

		/* To check if you can go left */
		if ( ( this.Counter < ( ( this.Childs.length - 1 ) - this.HoeveelElementenZichtbaar ) ) && ( ( Left % this.ElementBreedte ) == 0 ) ) {
			
			this.setYearsInButtons( 'More' );
			
			for ( var i = 0; i < this.Childs.length; i++) {
				/* Initializing the current Child */
				Child = this.Childs[i];
				/* Left = Current left */
				Left = parseInt( Child.style.left.split("px").join("") );
				/* Calculating the new left position */
				NewLeft = Left - this.ElementBreedte + 'px';

				/* Setting default easing */
				jQuery.easing.def = "easeInQuad";

				/* Animate the child to the left */
				$(Child).animate( {
					left: NewLeft
				}, {
					queue: false,
					duration: 900
				});
			}
			this.Counter++;
			this.setRightButtonInActive();
		}
	}
}

/**
 * This function will move all the childs to left
 */
ReferentieScroller.prototype.moveLeft = function() {
	
	var Left = 0;
	var Child = 0;
	var NewLeft = 0;

	if (this.Ok) {

		/* Calculating the current position of the first element */
		Left = parseInt(this.Childs[0].style.left.split("px").join(""));

		/* To check if you can go left */
		if( ( this.Counter > 0 ) && ( ( Left % this.ElementBreedte ) == 0 ) ) {
			
			this.setYearsInButtons( 'Less' );
			
			for ( var i = 0; i < this.Childs.length; i++) {
				/* Initializing the current Child */
				Child = this.Childs[i];
				/* Left = Current position of the Child */
				Left = parseInt(Child.style.left.split("px").join(""));
				/* Calculating the new left position */
				NewLeft = Left + this.ElementBreedte + 'px';

				/* Setting default easing */
				jQuery.easing.def = "easeInQuad";

				/* Animate the child to the left */
				$(Child).animate( {
					left :NewLeft
				}, {
					queue :false,
					duration :900
				});
			}
			this.Counter--;
			this.setLeftButtonInActive();
		}
	}	
}

ReferentieScroller.prototype.setupNavigationButtons = function() {	
	var RightButton = document.getElementById( this.RightButtonID );
	var LeftButton = document.getElementById( this.LeftButtonID );
	
	if ( LeftButton && RightButton ) {
		if ( ( this.Counter > 0 ) && ( this.StartPosition > 0 ) ) {
			LeftButton.className = "Actief"
		} else {
			LeftButton.className = "Inactief"
		}
		
		if ( ( this.StartPosition + 1 ) == ( this.Childs.length - 1 ) ) {
			RightButton.className = "Inactief"
		} else {
			RightButton.className = "Actief" //deze 
		}
		
		this.setYearsInButtons();
	}
}

ReferentieScroller.prototype.setYearsInButtons = function( Aanduiding ) {
	var VolgendeWeek = document.getElementById( 'VolgendeWeek' );
	var VorigeWeek = document.getElementById( 'VorigeWeek' );
	
	switch( Aanduiding ) {
		case 'Less' :
			CurrentWeek--;
		break;
		
		case 'More' :
			CurrentWeek++;
		break;
		
		default :
			//Standaard
		break;
	}
	
	if( VorigeWeek && VolgendeWeek ) {
		VorigeWeek.innerHTML = CurrentWeek - 2;
		VolgendeWeek.innerHTML = CurrentWeek + 1;
	}
}

ReferentieScroller.prototype.setLeftButtonInActive = function() {	
	var RightButton = document.getElementById( this.RightButtonID );
	var LeftButton = document.getElementById( this.LeftButtonID );
	RightButton.className = "Actief";
	
	if ( this.Counter == 0 ) {
		LeftButton.className = "Inactief";
	} else {
		LeftButton.className = "Actief"
	}
}

ReferentieScroller.prototype.setRightButtonInActive = function() {
	var LeftButton = document.getElementById( this.LeftButtonID );
	var RightButton = document.getElementById( this.RightButtonID );
	
	LeftButton.className = "Actief";
	
	if ( this.Counter == ( ( this.Childs.length - 1 ) - this.HoeveelElementenZichtbaar ) ) {
		RightButton.className = "Inactief";
	} else {
		RightButton.className = "Actief";
	}
}