$(document).ready(function(){
	var oldSlide;
	
	/**
	 * Reward statistics graphs
	 */
	var toggleGraphs = function(id){
		if ($('#'+id).hasClass('selectedGraph'))
		{
			return false;
		}
		
		$('.selectedGraph').removeClass('selectedGraph');
		$('#'+id).addClass("selectedGraph");
		
		$("#downloadGraphs").toggle();
		$("#pointGraphs").toggle();
	}
	
	$("#showDownloadGraphs").click(function(){
		toggleGraphs(this.id);
		return false;
	});
	$("#showPointGraphs").click(function(){
		toggleGraphs(this.id);
		return false;
	});
	
	/**
	 * jQuery coda slider
	 */
	if ($(".scrollPanel").length)
	{
		var $panels = $('#rewardContainerText .scrollContainer > div');
		var $container = $('#rewardContainerText .scrollContainer');
		$panels.css({
			'float' : 'left',
			'position' : 'relative' // IE fix to ensure overflow is hidden
		});
		
		// calculate a new width for the container (so it holds all panels)
		$container.css('width', $panels[0].offsetWidth * $panels.length);
		
		// collect the scroll object, at the same time apply the hidden overflow
		// to remove the default scrollbars that will appear
		var $scroll = $('#rewardContainerText .scroll').css('overflow', 'hidden');
		
		// handle nav selection
		function selectNav() {
		  $(this)
		    .parents('div:first')
		      .find('a')
		        .removeClass('current')
		      .end()
		    .end()
		    .addClass('current');
		}
		
		$('#rewardContainer .sliderNav').find('a').click(selectNav);
		
		// go find the navigation link that has this target and select the nav
		function trigger(data) {
		  $("#rewardContainerIcon").find("img:visible").fadeOut(300);
			
		  var el = $('#rewardContainer .sliderNav').find('a[href$="' + data.id + '"]').get(0);
		  selectNav.call(el);
		  
	  	  $("#"+data.id+"-icon").fadeIn(300);
		}
		
		if (window.location.hash) {
		  trigger({ id : window.location.hash.substr(1) });
		} else {
		  $('div.sliderNav a:first').click();
		}
		
		// offset is used to move to *exactly* the right place, since I'm using
		// padding on my example, I need to subtract the amount of padding to
		// the offset.  Try removing this to get a good idea of the effect
		var offset = 0;
		
		
		var scrollOptions = {
		  target: $scroll, // the element that has the overflow
		  
		  // can be a selector which will be relative to the target
		  items: $panels,
		  
		  navigation: '.sliderNav a',
		  
		  // allow the scroll effect to run in both directions
		  axis: 'xy',
		  
		  prev: $("#rewardContainerLeft .hitbox"),
		  next: $("#rewardContainerRight .hitbox"),
		  
		  onAfter: trigger, // our final callback
		  
		  constant: false, // scroll faster if we scroll more elements at once
		  
		  offset: offset,
		  
		  interval: 30000,
		  
		  // duration of the sliding effect
		  duration: 500,
		  
		  // easing - can be used with the easing plugin: 
		  // http://gsgd.co.uk/sandbox/jquery/easing/
		  easing: 'swing'
		};
		
		// apply serialScroll to the slider - we chose this plugin because it 
		// supports// the indexed next and previous scroll along with hooking 
		// in to our navigation.
		$('#rewardContainerText').serialScroll(scrollOptions);
		
		// now apply localScroll to hook any other arbitrary links to trigger 
		// the effect
		$.localScroll(scrollOptions);
		
		// finally, if the URL has a hash, move the slider in to position, 
		// setting the duration to 1 because I don't want it to scroll in the
		// very first page load.  We don't always need this, but it ensures
		// the positioning is absolutely spot on when the pages loads.
		scrollOptions.duration = 1;
		$.localScroll.hash(scrollOptions);
	}
	
	// Fake a click on the first panel which'll start the auto slide
	$(".scrollContainer").trigger('goto', [0]);
	$(".scrollContainer").trigger('start');

	/**
	 * Artifical link emulation
	 */
	$(".slidePrev, .slideNext, .rewardButton, .testimonialLink").hover(function(){
		$(this).css({cursor:'pointer'});
	},
	function(){
		$(this).css({cursor:'auto'});
	});
	
	/**
	 * Next / Previous content slide links
	 */
	$(".slidePrev").bind("click", function(){
		if ($(".slide:visible").prev(".slide").length)
		{
			oldSlide = $(".slide:visible");
			
			$(".slide:visible").fadeOut(300, function(){
				$(oldSlide).prev(".slide").fadeIn(300);
				$(".slideCount").text($(oldSlide).prev(".slide").attr('id').replace(/slide/, '') + "/" + $(".slide").length);
			});
		}
		else
		{
			$(".slide:visible").fadeOut(300, function(){
				$(".slide:last").fadeIn(300);
				$(".slideCount").text($(".slide").length + "/" + $(".slide").length);
			});
		}
	});
	$(".slideNext").bind("click", function(){
		if ($(".slide:visible").next(".slide").length)
		{
			oldSlide = $(".slide:visible");
			
			$(".slide:visible").fadeOut(300, function(){
				$(oldSlide).next(".slide").fadeIn(300);
				$(".slideCount").text($(oldSlide).next(".slide").attr('id').replace(/slide/, '') + "/" + $(".slide").length);
			});
		}
		else
		{
			$(".slide:visible").fadeOut(300, function(){
				$(".slide:first").fadeIn(300);
				$(".slideCount").text("1/" + $(".slide").length);
			});
		}
	});
	
	/**
	 * More/Previous testimonial links
	 */
	$(".testimonialLink").live("click", function(){
		if ($(this).parent().attr('id') == 'testimonialOne')
		{
			$("#testimonialOne").fadeOut(300, function(){
				$("#testimonialTwo").fadeIn(300);
			});
		}
		else
		{
			$("#testimonialTwo").fadeOut(300, function(){
				$("#testimonialOne").fadeIn(300);
			});	
		}
	});
	
	/**
	 * Signup / Upload button
	 */
	$(".rewardButton").bind("click", function(){
		if ($(this).attr('id') == 'homeBtn')
		{
			document.location.href = "http://www.filefactory.com/";
		}
		else if ($(this).attr('id') == 'signupBtn')
		{
			document.location.href = "http://www.filefactory.com/member/signup.php";
		}
	});
});