/**
 *	fires when the DOM is loaded
 *
 */
$(document).ready(function() {
	// cufon init
	replaceFonts();
	
	// spotlight
	//spotLight();
	
	// companyfade
	setInterval('companyFade()', 5000);
	
	// small slide 
	setInterval('spotLightSmall()', 8000);
	
	// fancybox
	$('#movie').fancybox({
		'padding'		: 0,
		'autoScale'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'width'			: 800,
		'height'		: 474,
		'type'			: 'swf',
		'swf'			: {
			'wmode'				: 'transparent',
			'allowfullscreen'	: 'true',
		}
	});
});

/** 
 *	Activate cufon
 *
 */
function replaceFonts() {
	Cufon.replace(
		'.vision_sub, .frontslide h2, .frontslide h3, #menu a, #spotlight h2, .page h1, .page h2, #spotlight h3, #sidebar h2, #footer h3, .pagination div, .overview th, #spotlight-small .quote, .pdf, #spotlight-small h2.slogan', { 
			fontFamily: 'Rotis Sans',
			hover: true
		}
	);	
}

/**
 *	Slider for small spotlight
 *
 */
function spotLightSmall() {
	if ( $('#spotlight-small').length == 0 )
		return false;
	
	var current 		= $('#spotlight-small').find('.current');
	var currentLeft		= current.css('left');
	var next			= current.next('.quote');
	
	// reset on the end
	if ( next.length == 0 ) {
		next = $('#spotlight-small').find('.quote:lt(1)');
	}
	
	var new_left		= next.css('left');
	
	$(current).animate({
		left: '-780px'
	}, 1850, function() {
		$(current).removeClass('current');
		$(current).css('left', new_left);
	});
	
	$(next).animate({
		left: currentLeft
	}, 2200, function() {
		$(next).addClass('current');
	});
}

/** 
 *	Nivo slider
 *
 */
function spotLight() {
	$('#spotlight').nivoSlider({
		//slices:15,
		effect:'sliceDownLeft, sliceUpLeft',
		animSpeed:1500,
		pauseTime:5000,
		startSlide:1,
		directionNav:false, 
		directionNavHide:false, 
		controlNav:true,
		controlNavThumbs:false,
		controlNavThumbsFromRel:false, 
		controlNavThumbsSearch: '.jpg', 
		controlNavThumbsReplace: '_thumb.jpg',
		keyboardNav:true,
		pauseOnHover:true, 
		manualAdvance:false, 
		captionOpacity:1.0, 
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} 
	});
}

/**
 *	Rounds a number to nearest even
 *
 */
function roundToEven(n) {
	var even = Math.round(n);
	
	// substract 1
	if ( even%2 != 0 )
		even--;
	
	return even;
}

/**
 *	Fades out companies
 *
 */
function companyFade() {
	if ( $('.company').length == 0 )
		return false;
	
	var size 		= $('.company').length;
	var percentage 	= 10;
	var changes		= roundToEven(size / 100 * percentage);
	var swaps		= [];
	
	while( swaps.length < changes ) {
		var random = Math.floor( Math.random() *  size);
		
		if ($.inArray(random, swaps) == -1) {
			swaps.push(random);
		}
	}
	
	// swap by sections of two
	for(i=0; i<swaps.length; i=i+2) {
		swapFadeImages(i, i+1, swaps);
	}
}

function swapFadeImages(i, j, swaps) {
	var fadeTime	= 2000;
	var fade_i 		= '#company_' + swaps[i];
	var fade_j 		= '#company_' + swaps[j];
	var link_i		= $(fade_i + ' a').attr('href');
	var link_j		= $(fade_j + ' a').attr('href');
	var img_i		= $(fade_i + ' img');
	var img_j		= $(fade_j + ' img');
	
	$(img_i).fadeOut(fadeTime, function() {
		$(fade_i + ' a').attr('href', link_j);
		$(fade_i + ' img').detach();
		img_j.appendTo( $(fade_i + ' a') )
		
		$(img_i).fadeIn(fadeTime);
	});
	
	$(img_j).fadeOut(fadeTime, function() {
		$(fade_j + ' a').attr('href', link_i);
		$(fade_j + ' img').detach();
		img_i.appendTo( $(fade_j + ' a') )
		
		$(img_j).fadeIn(fadeTime);
	});

}
