var iswinxs = function(){
}

iswinxs.showlatestblog = function(){
	iswinxs.hidelatestblog();
	// this = a link
	var alias = this.id.substring( 13 );

	var span = document.getElementById( 'latest_blog_' + alias );

	if ( span ){

		var div = document.createElement( 'div' );
		div.id = 'LatestBlog';
		var paragraphe = document.createElement( 'p' );
		paragraphe.innerHTML = span.innerHTML;
		div.appendChild( paragraphe );
		var footer = document.createElement( 'div' );
		div.appendChild( footer );

		$('#profiles').parent().append( div );

		var linkPosition = $(this).offset();
    var profilesPosition = $('#profiles').parent().offset();

		var windowSize = {
			width: 239,
			height: $(div).height()
		}

		$(div).css({
			left: linkPosition.left - profilesPosition.left + 50,
			top: linkPosition.top - profilesPosition.top + 50
		});

	}
}

iswinxs.hidelatestblog = function(){
	$('#LatestBlog').remove();
}

function Carrousel( handlersObj ){
	this.ContainerObj = handlersObj[ 'container' ];
	this.ListObj = handlersObj[ 'list' ];
	this.Btn_LeftObj = handlersObj[ 'btn_left' ];
	this.Btn_RightObj = handlersObj[ 'btn_right' ];

	this.Items = [];

	this.VisibleItems = [];
	this.ContinuesVisible = null;

	this.List_Height = null;
	this.List_Width = null;
	this.Init();

	this.Jobs = [];
	this.AddJobInterval = null;

	this.SmallestHeight = null;
}

Carrousel.prototype.Init = function(){
	var self = this;

	this.ReInit();

	/* Get the children */
	this.SmallestHeight = this.List_Height;

	var total_height = 0;
	$(this.ListObj).children('li').each( function( index ){
		var obj = {
      Element: this,
      Width: $(this).width(),
			Height: $(this).find('img').height()
    };

		self.SmallestHeight = Math.min( self.SmallestHeight, obj.Height );

		$(this).css( {
			position: 'absolute',
			top: Math.floor( self.List_Height / 2 ),
			left: Math.floor( self.List_Width / 2 ),
			display: 'none'
		} );

		self.Items.push( obj );
	} );

	$(this.Btn_LeftObj).mouseover(function(){self.MouseOverLeft();}).mouseout(function(){self.MoveOut();}).click(function(){self.ClickLeft();return false;});
	$(this.Btn_RightObj).mouseover(function(){self.MouseOverRight();}).mouseout(function(){self.MoveOut();}).click(function(){self.ClickRight();return false;});

	this.ReInit();

  this.DivideVisibleItems( 500 );

}

Carrousel.prototype.DivideVisibleItems = function( durence ){
	var availableHeight = this.List_Height;
	var length = this.VisibleItems.length;
	var first_index = this.VisibleItems[ 0 ];
	var last_index = this.VisibleItems[ length - 1 ];

	availableHeight -= this.Items[ last_index ].Height;
	$(this.Items[ last_index ].Element).animate( { top: availableHeight + 'px', left: this.GetRandomLeft( last_index ) }, durence );
	availableHeight -= this.Items[ first_index ].Height;
  $(this.Items[ first_index ].Element).animate( { top: '0px', left: this.GetRandomLeft( first_index ) }, durence );
	
	for( i = 1; i <= length - 2; i ++ ){
		var index = this.VisibleItems[ i ];
		availableHeight -= this.Items[ index ].Height;
	}

	var avg_offset = Math.floor( availableHeight / length - 2 );

	var top = this.Items[ first_index ].Height + avg_offset;
	for( i = 1; i <= length - 2; i ++ ){
		var index = this.VisibleItems[ i ];
		var animateTo = {top: top + 'px', left: this.GetRandomLeft( index )};
		$(this.Items[ index ].Element).animate( animateTo, durence );
		top += this.Items[ index ].Height + avg_offset;
	}

}

Carrousel.prototype.ClickLeft = function(){
	this.AddJob( {direction: -1, durence: 500} );
}

Carrousel.prototype.ClickRight = function(){
	this.AddJob( {direction: 1, durence: 500} );
}

Carrousel.prototype.MouseOverLeft = function(){
	var self = this;
	
	if ( this.Jobs.length == 0 ){
		self.AddJob( {direction: -1, durence: 1500} );
	}

  this.AddJobInterval = setInterval( function(){
		if ( self.Jobs.length == 0 ){
	    self.AddJob( {direction: -1, durence: 1500} );
		}
  }, 1500 );
}

Carrousel.prototype.MouseOverRight = function(){
	var self = this;
	if ( this.Jobs.length == 0 ){
		self.AddJob( {direction: 1, durence: 1500} );
	}
	this.AddJobInterval = setInterval( function(){ 
		if ( self.Jobs.length == 0 ){
			self.AddJob( {direction: 1, durence: 1500} ); 
		}
	}, 1500 );
}

Carrousel.prototype.MoveOut = function(){
	clearInterval( this.AddJobInterval );
}

Carrousel.prototype.AddJob = function( Job ){
	if ( this.Jobs.length > 0 ){
		if ( this.Jobs[ 0 ].direction != Job.direction ){
			for( var i = this.Jobs.length - 1; i > 1; i -- ){
				this.Jobs.pop();
			}
		}
	}
	this.Jobs.push( Job );

	if ( this.Jobs.length == 1 ){
		this.HandleJob( );
	}
}

Carrousel.prototype.HandleJob = function( ){
	if ( this.Jobs.length == 0 ){
		return false;
	}
	var Job = this.Jobs[ 0 ];
	var self = this;

	if ( Job.direction > 0 ){
		var index_to_hide = this.VisibleItems.pop();
		var next_visible = this.VisibleItems[ 0 ] == 0 ? this.Items.length - 1 : this.VisibleItems[ 0 ] - 1;
		this.VisibleItems.unshift( next_visible );
		var next_visible_top = - 1 * this.Items[next_visible].Height;
		var index_to_hide_top = this.List_Height;
	} else {
		var index_to_hide = this.VisibleItems.shift();
    var next_visible = this.VisibleItems[ this.VisibleItems.length - 1 ] == this.Items.length - 1 ? 0 : this.VisibleItems[ this.VisibleItems.length - 1 ] + 1;
    this.VisibleItems.push( next_visible );
    var next_visible_top = this.List_Height;
		var index_to_hide_top = - 1 * this.Items[next_visible].Height;
	}

	$(this.Items[next_visible].Element).css( {
		display: 'block',
		top: next_visible_top + 'px',
		left: this.GetRandomLeft( next_visible )
	} );

	$(this.Items[index_to_hide].Element).animate( { top: index_to_hide_top + 'px' }, Job.durence, function(){
		var is_visible = false;
		for( var i = 0; i < self.VisibleItems.length; i ++ ){
			if ( self.VisibleItems[ i] == index_to_hide ){
				is_visible = true;
			}
		}
		if ( is_visible == false ){
			$(self.Items[ index_to_hide ].Element).hide();
		}
 } );

	this.DivideVisibleItems( Job.durence );

	var self = this;
	setTimeout( 
		function(){ 
			self.Jobs.shift();
			self.HandleJob() 
		}, 
		Job.durence 
	);
}

Carrousel.prototype.GetRandomLeft = function( index ){
	var max_offset = this.List_Width - this.Items[ index].Width;
	return Math.floor( Math.random() * max_offset );
}

Carrousel.prototype.ReInit = function(){
	var height = $('.PageContent').parent().height();
	$( this.ContainerObj ).parent().height( height + 'px' );

	var button_heights = 0;
	$( this.ContainerObj ).find('a').each(function(){
//		button_heights += $(this).height();
		button_heights = 200;
	});

	$( this.ContainerObj ).height( ( height - button_heights ) + 'px' );

	$( this.ListObj ).height( ( $( this.ContainerObj ).height() - $(this.Btn_RightObj).height() ) + 'px' );

	this.List_Height = $(this.ListObj).height();
	this.List_Width = $(this.ListObj).width();

	if ( this.Items.length > 0 ){
		var length = this.Items.length;
		while ( this.Items.length * this.SmallestHeight < this.List_Height * 1.5 ){
			for( var i=0; i < length && this.Items.length * this.SmallestHeight < this.List_Height * 1.5; i ++ ){
				var clone = document.createElement( 'li' );
				clone.style.position = 'absolute';
				clone.style.left = this.List_Width / 2 + 'px';
				clone.style.top = ( this.Items[ i ].Height * - 2 ) + 'px';
				clone.innerHTML = this.Items[ i ].Element.innerHTML;
				this.Items[ i ].Element.parentNode.appendChild( clone );
				this.Items.push( {
					Element: clone,
					Width: this.Items[ i ].Width,
					Height: this.Items[ i ].Height
				} );
			}
		}

	  this.ContinuesVisible = Math.ceil( this.List_Height / this.SmallestHeight ) + 1;

	  for( var i =0; i < Math.min( this.ContinuesVisible, this.Items.length ); i ++ ){
  	  $(this.Items[ i ].Element).css( { display: 'block' } );
	 	  this.VisibleItems.push( i );
	  }

	}
}

Carrousel.Init = function( handlersObj ){
	for( key in handlersObj ){
		handlersObj[ key ] = document.getElementById( handlersObj[ key ] );
		if ( handlersObj[ key ] == null ){
			return false;
		}
	}
	return new Carrousel( handlersObj );
}

function blog_reaction_open( div_id, button ){
	$(button).hide();
	$('#' + div_id).show( 1000 );
	return false;
}

function iswinxs_toggle_reactions( button ){
	if ( $(button).parent().parent().find('.Reactions').is(":visible") ){
		$(button).text( 'Toon reacties' );
	} else {
		$(button).text( 'Verberg reacties' );
	}
	$(button).parent().parent().find('.Reactions').toggle( 1000 );
	return false;
}

