$(document).ready(function() {
		
	/* Home page fade effect */
	$('#prodimages').cycle({
		fx: 'fade',
		speed: 2000,
		timeout: 6000,
		delay: -2000,
		slideExpr: 'div'
	});
	
	/* Demo page slide */
	$('#demo_image').cycle({
		fx: 'scrollHorz',
		speed: 2000,
		timeout: 10000,
		next: '#demo_image',
		nowrap: 1,
		before: changeButton,
		after: showReplay
	});
	
	$('#demo_buttons img').each(function(index) {
		$(this).click(function() {
			$('.replay').css('visibility','hidden');
			$('#demo_image').cycle(index);
		});
	});
	
	/* Restart the demo! */
	$('.replay').click(function() {
		$('.replay').css('visibility','hidden');
		$('#demo_image').cycle( {
			fx: 	'scrollHorz',
			speed: 	2000,
			timeout: 5000,
			next: 	'#demo_image',
			nowrap: 1,
			before: changeButton,
			after: showReplay
		});
	});
	
	/* Form submit roll over */
	$('#submit').mouseover(function() {
		$(this).attr('src','/images/form/submit_roll_over.jpg');
	});
	$('#submit').mouseout(function() {
		$(this).attr('src','/images/form/submit.jpg');
	});
});

/* Change the selected button at the bottom of the demo */
function changeButton() {
	var selected = this.src.split('_');
	var d_folder = selected[0].split('/');
	d_folder = d_folder.pop();
	selected = selected[2].split('.');
	selected = selected[0];
	
	$.each($('#demo_buttons img'),function() {
		var src = $(this).attr('src');
		if( src.match(/demo\/(rd|a)m\//) ) {
			src = src.replace(/demo\/(rd|a)m\//,'demo/');
		}
		var src = src.replace(/_selected.gif/,'.gif');
		$(this).attr('src',src);
		
		if ( $(this).attr('src').match(selected) ) {
			var newSrc = '/images/demo/' + d_folder + '/' + selected + '_selected.gif';
			$(this).attr('src',newSrc);
		}
		
	});
}

/* Display the replay button */
function showReplay(currElement, nextElement, opts, isForward) {
	var index = opts.currSlide;
	if( index == 4 ) {
		$('.replay').css('visibility','visible');
	}
}