
site = {
	playerControls : {
		onPlay: function() {
			site.slider.stopShow();
			site.playing = true;
		},
		onEnded: function() {
			if (!site.playing) {
				site.slider.startShow();
			}
			site.playing = false;
		}
	},
	playing : false
}

$(document).ready(function(){
	var pieces = $('#ticker').find('.text-piece');

	if (pieces.length > 1) {
		setTimeout(function(){fadeNext(pieces);pieces=null}, 3500);
	}
	
	
	var flash = $('.vjs-flash-fallback');
	var slider = $('#slider').bxSlider({
		mode: 'fade',
		pager: 'true',
		pause: 5000,
		auto: true
		//autoHover: true,
	});
	$('.bx-prev, .bx-next').hover(function(){
		$(this).stop(true, true).fadeTo(250, 1);
	}, function(){
		$(this).stop(true, true).fadeTo(250, 0);
	});
	
	$('.slider-wrapper').hover(function(){
		slider.stopShow();
	}, function(){
		if (!site.playing) {
			slider.startShow();
		}
	});
	var pager = $('.bx-pager').detach();
	pager.appendTo('.slider-wrapper');
	$('.slide').show();
	site.slider = slider;
	$('.video-wrapper').click(function(){
		slider.stopShow();
	});
	

	var players = VideoJS.setup("All", {
		//useBuiltInControls: true,
	});
	
	$(players).each(function(i,e){
		e.onPlay(site.playerControls.onPlay);
		e.onPause(site.playerControls.onEnded);
		e.onEnded(site.playerControls.onEnded);
	});
});

function fadeNext(pieces, index, options) {
	var i = (index || 0) * 1,
		j = (i + 1) % pieces.length,
		settings = $.extend({
			duration : 300,
			interval: 3500
		}, options),
		duration = settings.duration,
		interval = settings.interval;
	
	$(pieces[i]).fadeOut(duration);
	$(pieces[j]).hide().delay(duration).fadeIn(duration);
	setTimeout(function(){fadeNext(pieces,j);pieces=null;j=null}, interval + 2 * duration);
}
