/* 
 * Documentatie:
 * 
 * Cufon:              http://cufon.shoqolate.com/
 * Innerfade:          http://medienfreunde.com/lab/innerfade/
 * jCoverflip:         http://jcoverflip.com/
 *
 */

$(function(){


	// Cufon: font replacement
	Cufon.replace('#left h2, #left h3, #left .projects .name, .rcol h2, #footer h2, .lcol .slider h2, #right h3', { fontFamily: 'Swiss721BlackRounded' });
	Cufon.replace('#header .nav > li > a', { hover: true, fontFamily: 'Swiss721BlackRounded' });
	Cufon.replace('#footer .tags a', { hover: true, fontFamily: 'Swiss721BlackRounded' });
	Cufon.replace('#news .title, #news .date', { fontFamily: 'Calibri' });


	// News slider handler, durations in milliseconds
	var pause = 5000;
	var animationDuration = 200;
	newsHandler(pause, animationDuration);


	// Recent projects slider
	var pause = 3000;
	var animationDuration = 1000;
	var ulwidth = $('.lcol .slider ul').children('li').size() * 95;
	
	if($('.lcol .slider').size() > 0) { setTimeout( function(){ projectNext(pause, animationDuration); }, pause ); }
	$('.lcol .slider').hover(function(){
		$(this).find('ul').toggleClass('hover');
	}).find('ul').css('width', ulwidth + 'px');
	$('.lcol .slider a.forward').click(function(){
		projectNext(pause, animationDuration, true);
	});
	$('.lcol .slider a.back').click(function(){
		projectNext(pause, animationDuration, true, true);
	});


	// Innerfade: Subpage image rotator
	$('.rotator ul').innerfade({
		animationtype: 'fade', // fade or slide
		speed: 500, // animation speed in milliseconds
		timeout: 5000, // time between animations in milliseconds
		type: 'sequence', // sequence, random, or random_start
		containerheight: '212px' // height of the containing element
	});


	// jCoverflip
	$('#right .recent ul').jcoverflip({
          current: 0,
          beforeCss: function( el, container, offset ){
            return [
              $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 210 - 110*offset + 20*offset )/3+70+'px', bottom: '40px' }, { } ),
              $.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset)/3 + 'px' }, {} )
            ];
          },
          afterCss: function( el, container, offset ){
            return [
              $.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 110 + 110*offset )/3+70+'px', bottom: '40px' }, { } ),
              $.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset)/3 + 'px' }, {} )
            ];
          },
          currentCss: function( el, container ){
            return [
              $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 100 )/3+70+'px', bottom: '32px' }, { } ),
              $.jcoverflip.animationElement( el.find( 'img' ), { width: '67px' }, { } )
            ];
          }
	}).parent().hover(function(){ $(this).toggleClass('hover'); });
	setTimeout( function(){ jCoverflipNext( $('#right .recent') ); }, 3000 );



	// Navigation
	$('#header .nav > li:has(ul)').addClass('withsub');
	$('#header .nav li').hover(function(){
		$(this).toggleClass('hover');
		Cufon.refresh('#header .nav > li > a');
	});
	$('#header .nav li li:has(ul) > a').each(function(){
		$(this).html( '<span class="arrow">' + $(this).html() + '</span>' );
	});


});





////////////////////////
//
// News slider functions
//
function newsHandler(pause, animationDuration) {

	$('#news .items li').hover(function(){

		if (!$(this).hasClass('active')) {

			$(this).parent().children('li.active').removeClass('active');
			$(this).addClass('active');

			$('#news .images li.active').fadeOut(animationDuration, function(){
				$(this).removeClass('active');
				$('#news .images li').eq( $('#news .items li.active').index() ).fadeIn(animationDuration, function(){
					$(this).addClass('active');
				});
			});

			$('#news .arrow').animate({'top': $('#news .items li.active').index() * 88 + 1 + 'px'}, animationDuration);

		}

	}, function(){});
	
	$('#news .items').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	});

	setTimeout (function(){ newsNext(pause, animationDuration); }, pause);

}

function newsNext(pause, animationDuration) {

	if (!$('#news .items').hasClass('hover')) {

		if ($("#news .items li.active").hasClass('last')) {

			var index = 0;

		} else {

			var index = $('#news .items li.active').index() + 1;

		}

		$('#news .items li.active').removeClass('active');
		$('#news .items li').eq(index).addClass('active');
		$('#news .images li.active').fadeOut(animationDuration, function(){
			$(this).removeClass('active');
			$('#news .images li').eq(index).fadeIn(animationDuration, function(){
				$(this).addClass('active');
			});
		});
		$('#news .arrow').animate({'top': index * 88 + 1 + 'px'}, animationDuration);

	}

	setTimeout (function(){ newsNext(pause, animationDuration); }, pause);

}
//
// News slider functions
//
////////////////////////





////////////////////////
//
// Projects slider function
//
function projectNext(pause, animationDuration, click, back) {

	if ( click == true || ( click != true && !$('.lcol .slider ul').hasClass('hover') ) ) {

		if( back == true ) {

			var index = $('.lcol .slider ul').css('left').replace('px', '') * 1 + (95 * 3);
			if( index > 0 ) { index = Math.floor( $('.lcol .slider ul').children('li').size() * 95 / (95 * 3) ) * (95 * 3) * -1; }

		} else {

			var index = $('.lcol .slider ul').css('left').replace('px', '') - (95 * 3);
			if( index < -1 * $('.lcol .slider ul').children('li').size() * 95 ) {
				index = 0;
			}

		}

		$('.lcol .slider ul').animate({'left': index + 'px'}, animationDuration);

	}

	if ( click != true ) {

		setTimeout( function(){ projectNext(pause, animationDuration); }, pause );

	}

}
//
// Projects slider function
//
////////////////////////




////////////////////////
//
// jCoverFlip Next()
//
function jCoverflipNext(el) {

	if (!el.hasClass('hover')) {

		el.find('ul').jcoverflip('next');

	}

	setTimeout( function(){ jCoverflipNext(el); }, 3000 )

}
