$(document).ready(function() {
	// time related parameters
	var landing_idle_time = 2500; 
	var blackbox_loop_interval = 6000;
	var customer_loop_interval = 10000;
	
	// add mouseover effects to image block.
	for (var n=1; n<=7; n++)
	{
		mouseEffect(n);
	}

	// add fadein/fadeout effects to customer block
	$('.customer').attr('style', 'display:block');
	setInterval("$('.customer-wrapper').slideLoop()", customer_loop_interval);

	// fadein layer1

	var blackbox_wrapper_time = blackbox_loop_interval * $('.blackbox').length;

	$.idleTimer(landing_idle_time);

	$(document).bind("idle.idleTimer", function(){
		$('#layer1').delay(landing_idle_time).fadeIn(function(){
			setInterval("$('.blackbox-wrapper-inner').slideLoop()", blackbox_loop_interval);
			$(this).find('.close').click(function(){
				$('#layer1').remove();
			});
		}).delay(blackbox_wrapper_time).fadeOut();

		$.idleTimer('destroy');
	});

	//get a wide div holding all tickers inline
	var mqwidth = 0;
	$('#marquee li').each(function(i, e){
		mqwidth += $(e).outerWidth(true);
	});

	$('#marquee ul').attr('width', mqwidth);

	// use jquery simpleScroll for smooth marquee scroll
	$('#marquee ul').simplyScroll({
		autoMode: 'loop',
		frameRate: 24
	});

});

$.fn.delay = function(duration){
	$(this).animate({dummy: 1}, duration);
	return this;
}

$.fn.slideLoop = function()
{
	var $active = $(this).children('.active'); 
	
	//build a circular link list.
	if ( $active.length == 0 ) $active = $(this).children(":last");
	var $next =  $active.next().length ? $active.next() : $(this).children(":first");
	
	$active.addClass('last-active');
	
	$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 500, function() {
		$active.removeClass('active last-active');
	});
}

function mouseEffect(n)
{
	var img = '.img-0' + n;
	var mssg = '.mssg-0' + n;

	var image_off = $(img).attr("src");
	var image_on = image_off.replace(/\.jpg/, "_on.jpg");

	$(img).mouseenter(function(){
		$(img).attr("src", image_on);
		$(mssg).show();
	}).mouseleave(function(){
		$(img).attr("src", image_off);
		$(mssg).hide();
	});
}
