/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/

// Speed of the automatic slideshow
var slideshowSpeed = 8000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"title" : "Freestyle semi custom kit.",
		"image" : "rhk_1.jpg",
		"url" : "#",
		"secondline" : "All New Design"
	}, {
		"title" : "Craig Anderson #100 picking up his GOLD chrome kit, 2010.",
		"image" : "rhk_9.jpg",
		"url" : "#",
		"secondline" : "Happy Customer!"
	}, {
		"title" : "Ultra high quality Italian plastics now available!",
		"image" : "rhk_35.jpg",
		"url" : "#",
		"secondline" : "Genuine Ractech Plastics"
	}, {
		"title" : "We’ve got you covered!",
		"image" : "rhk_36.jpg",
		"url" : "#",
		"secondline" : "JNR Bike Templates"
	}, {
		"title" : "Team Moto wanted to stand out, so naturally they called us…",
		"image" : "rhk_17.jpg",
		"url" : "#",
		"secondline" : "Full Custom Dealer Design"
	}, {
		"title" : "Customise your kit with a wide variety of industry logos.",
		"image" : "rhk_25.jpg",
		"url" : "#",
		"secondline" : "Want The Factory Look?"
	}, {
		"title" : "Berry Sweet Lucas Oils 2011 Team kit.",
		"image" : "rhk_37.jpg",
		"url" : "#",
		"secondline" : "Standing Out On The Race Track"
	}, {		
		"title" : "Tech semi custom kit.",
		"image" : "rhk_2.jpg",
		"url" : "#",
		"secondline" : "All New Design"
}, {
		"title" : "We take great pride in seeing our hard work on display!",
		"image" : "rhk_10.jpg",
		"url" : "#",
		"secondline" : "Proud Kodak Moments "
}, {
		"title" : "Not after a conventional or standard design? Excellent, we love a challenge!",
		"image" : "rhk_18.jpg",
		"url" : "#",
		"secondline" : "Think Outside The Box"
}, {
		"title" : "Give us a call for a quick turn around on your next background order!",
		"image" : "rhk_8.jpg",
		"url" : "#",
		"secondline" : "Thinking About Racing This Year?"
}, {
		"title" : "For a fast red head.",
		"image" : "rhk_16.jpg",
		"url" : "#",
		"secondline" : "Red Plate"
}, {
		"title" : "Professional design for a passionate company.",
		"image" : "rhk_38.jpg",
		"url" : "#",
		"secondline" : "Full Custom Dealer Design"
}, {
		"title" : "The Insure My Ride bike stood out in the crowd!",
		"image" : "rhk_24.jpg",
		"url" : "#",
		"secondline" : "Design Request : Attention Grabbing"
}, {
		"title" : "Pink Anarchy semi custom kit.",
		"image" : "rhk_3.jpg",
		"url" : "#",
		"secondline" : "All New Design"
}, {
		"title" : "Give you ride the Custom CMYK design look to really stand out!",
		"image" : "rhk_11.jpg",
		"url" : "#",
		"secondline" : "Don't want to be a sheep?"
}, {	
		"title" : "Ando’s Husqvarna kit 2010.",
		"image" : "rhk_26.jpg",
		"url" : "#",
		"secondline" : "Tested By Craig Anerson"
}, {	
		"title" : "Choose our reflective GOLD Metallic Chrome Option for a flawless BLING LOOK.",
		"image" : "rhk_19.jpg",
		"url" : "#",
		"secondline" : "GOLD Chrome Option"
}, {
		"title" : "Craig Anderson from Round 1 of the 2011 Rockstar the MX Nationals.",
		"image" : "rhk_7.jpg",
		"url" : "#",
		"secondline" : "RHK Grafix In Full Flight"
}, {
		"title" : "You can customise any of the kit colours, sponsor logos, rider number/name and background colours.",
		"image" : "rhk_27.jpg",
		"url" : "#",
		"secondline" : "Customise Any Design"
}, {	
		"title" : "2011 MX Nationals Pro Lites rider Kade Mosig.",
		"image" : "rhk_23.jpg",
		"url" : "#",
		"secondline" : "@kadeMosig48"
}, {
		"title" : "Shift semi custom kit.",
		"image" : "rhk_4.jpg",
		"url" : "#",
		"secondline" : "All New Design"
}, {
		"title" : "Full custom design for Team KTM Bunbury in 2010.",
		"image" : "rhk_12.jpg",
		"url" : "#",
		"secondline" : "Bunbury KTM"
}, {
		"title" : "Choose our reflective SILVER Metallic Chrome option for a flawless BLING LOOK.",
		"image" : "rhk_20.jpg",
		"url" : "#",
		"secondline" : "SILVER Chrome Option"
}, {	
		"title" : "Ruffling a few feathers by riding a Honda CRF450R at the Coolum MX Nats in 2010.",
		"image" : "rhk_6.jpg",
		"url" : "#",
		"secondline" : "Ando Back On A HONDA"
}, {	
		"title" : "Choose from our wide range of Standard, Racer & Team background designs.",
		"image" : "rhk_14.jpg",
		"url" : "#",
		"secondline" : "Backgrounds From $79.95"
}, {
		"title" : "2010 MX Nationals Under 19's Rider Sam Southy.",
		"image" : "rhk_22.jpg",
		"url" : "3",
		"secondline" : "Toowoomba Honda Racing"
}, {
		"title" : "Ando and his hot Italian ride featured in a Dirt Action article in 2010.",
		"image" : "rhk_32.jpg",
		"url" : "3",
		"secondline" : "Dirt Action #133 Photo"
}, {
		"title" : "Shadow semi custom kit.",
		"image" : "rhk_5.jpg",
		"url" : "3",
		"secondline" : "All New Design"
}, {
		"title" : "Because we can!",
		"image" : "rhk_34.jpg",
		"url" : "#",
		"secondline" : "Rocking It OLD SCHOOL"
}, {
		"title" : "Transmoto.com.au full custom kit.",
		"image" : "rhk_15.jpg",
		"url" : "#",
		"secondline" : "Transmoto TE510 Kit"
}, {	
		"title" : "All sponsor logos featured on our kits are just examples, you can choose to remove or change them.",
		"image" : "rhk_29.jpg",
		"url" : "#",
		"secondline" : "To Make This Clear!"
}, {
		"title" : "Jason Chapman’s 1995 KTM 250 EXC project bike.",
		"image" : "rhk_30.jpg",
		"url" : "#",
		"secondline" : "European Classic"
}, {
		"title" : "RHK Custom Grafix test riders: Luke Arbon, Billy Mackenzie & Cody Mackie.",
		"image" : "rhk_31.jpg",
		"url" : "#",
		"secondline" : "Kawasaki Racing Team 2010"
}, {
		"title" : "We also do neckbrace sticker kits, helmet ID kits and more.",
		"image" : "rhk_13.jpg",
		"url" : "#",
		"secondline" : "Accessories!"
}, {
		"title" : "We were proud to see and support the return of ASHCAN to the Australia MX scene.",
		"image" : "rhk_21.jpg",
		"url" : "#",
		"secondline" : "The Return Of Kim Ashkenazi"
}
];



$(document).ready(function() {
		
	// Backwards navigation
	$("#back").click(function() {
		stopAnimation();
		navigate("back");
	});
	
	// Forward navigation
	$("#next").click(function() {
		stopAnimation();
		navigate("next");
	});
	
	var interval;
	$("#control").toggle(function(){
		stopAnimation();
	}, function() {
		// Change the background image to "pause"
		$(this).css({ "background-image" : "url(images/btn_pause.png)" });
		
		// Show the next image
		navigate("next");
		
		// Start playing the animation
		interval = setInterval(function() {
			navigate("next");
		}, slideshowSpeed);
	});
	
	
	var activeContainer = 1;	
	var currentImg = 0;
	var animating = false;
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		if(direction == "next") {
			currentImg++;
			if(currentImg == photos.length + 1) {
				currentImg = 1;
			}
		} else {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	var stopAnimation = function() {
		// Change the background image to "play"
		$("#control").css({ "background-image" : "url(images/btn_play.png)" });
		
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("next");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
