// When the document is ready, load all the javascript.
$(document).ready(function() {
	var firstLoad = true;
	// Hide everything but first 
	$('div.jquerytitle').hide();
	$('div.jquerytitle.one').show();
	$('div.jquerydescription').hide();
	$('div.jquerydescription.one').show();
	$('div.jquerydescription_button').hide();
	$('div.jquerydescription_button.one').show();
	
	// Set up URLs
	$('div.jquerydescription_button.one').click(function() {
		window.location = "http://google.com";
	});
	$('div.jquerydescription_button.two').click(function() {
		window.location = "http://yahoo.com";
	});
	$('div.jquerydescription_button.three').click(function() {
		window.location = "http://msn.com";
	});
	$('div.jquerydescription_button.four').click(function() {
		window.location = "http://youtube.com";
	});
	
	
	// Load slideshow cycles
	$('div.jquerymain_image').cycle({
		speed: 9000,
		startingSlide: 0,
		before: onBefore
	});
	
	// Runs when the slide show starts to change
	function onBefore() {
		if(firstLoad) {
			firstLoad = false;
			return;
		}
		// 'this' refers to the image
		var slide = this.className;		
		
		$('div.jquerytitle').fadeOut('slow');
		$('div.jquerytitle.'+slide).fadeTo(500, 1).fadeIn('slow');
		
		$('div.jquerydescription').fadeOut('slow');
		$('div.jquerydescription.'+slide).fadeTo(600, 1).fadeIn('slow');
		
		$('div.jquerydescription_button').fadeOut('slow');
		$('div.jquerydescription_button.'+slide).fadeTo(600, 1).fadeIn('slow');
		
		$('div.jquerybutton').removeClass('selected');
		$('div.jquerybutton.'+slide).addClass('selected');
		
	}
	
	// Set up the buttons.
	$('div.jquerybutton.one').click(function() {
		if($('div.jquerydescription:animated').size() != 0) return;
		if($('div.jquerybutton.one').hasClass('selected')) return;
		$('div.jquerymain_image').cycle(0);
		$('div.jquerybutton').removeClass('selected');
		$('div.jquerybutton.one').addClass('selected');
	});
	
	$('div.jquerybutton.two').click(function() {
		if($('div.jquerydescription:animated').size() != 0) return;
		if($('div.jquerybutton.two').hasClass('selected')) return;
		$('div.jquerymain_image').cycle(1);
		$('div.jquerybutton').removeClass('selected');
		$(this).addClass('selected');
	});
	
	$('div.jquerybutton.three').click(function() {
		if($('div.jquerydescription:animated').size() != 0) return;
		if($('div.jquerybutton.three').hasClass('selected')) return;
		$('div.jquerymain_image').cycle(2);
		$('div.jquerybutton').removeClass('selected');
		$(this).addClass('selected');
	});
	
	$('div.jquerybutton.four').click(function() {
		if($('div.jquerydescription:animated').size() != 0) return;
		if($('div.jquerybutton.four').hasClass('selected')) return;
		$('div.jquerymain_image').cycle(3);
		$('div.jquerybutton').removeClass('selected');
		$(this).addClass('selected');
	});
	
});
