/**
 * 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
 *
 */
function ReferentieScroller( ID, TotaleBreedte, ElementBreedte ){
	/* Setup the scroller */
	this.Setup( ID, TotaleBreedte, ElementBreedte );
}

/**
 * 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 ) {
/**
 * The ID of the element that needs to scroll.
 */
  this.ElementID = ID;
/**
 * The counter of current element
 */
	this.Counter = 0;
/**
 * 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();
/**
 * Getting childs that are inside ElementID and aligning them after (if OK = true)
 */
	this.getChilds();
	this.alignChilds();
}

/**
 * Aligning all childs
 */
ReferentieScroller.prototype.alignChilds = function(){
	if ( this.Ok ) {

		/* Initializing the counter */
		var Teller = 0;

		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 );
		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 == "DIV" ) {
				this.Childs.push( Child );
			}
		}

		/* If there are childs 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 - 3 ) ) && ( ( Left % this.ElementBreedte ) == 0 ) ) {

      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 = "easeInOutCirc";

				/* Animate the child to the left */
				$( Child ).animate({left:NewLeft}, {queue: false, duration: 500});

//Old scroll function        Child.style.left = parseInt( Left ) - this.ElementBreedte + 'px';
      }
      this.Counter++;
    }
	}
}

/**
 * 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 ) ) {
	    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 = "easeInOutCirc"; 

	      /* Animate the child to the left */
				$( Child ).animate({left:NewLeft}, {queue: false, duration: 500});
//Old scroll function       Child.style.left = parseInt( Left ) + this.ElementBreedte + 'px';
      }
	    this.Counter--;
    }
  }
}
