/*
 Author: Daniel Pavitt
 Code adpated from http://www.thechrisbox.com/
*/

$("document").ready(function() {
	
	/* -------------- preloading pictures ---------------- */
	var completePath = 'assets/images';
	$.preLoadImages([
		''+ completePath +'/project_1/gut_club_back_v8.jpg',
		''+ completePath +'/project_1/Gut_club_main_Poster_v7.png',
		''+ completePath +'/project_2/Gut_club_vis_V2.jpg',
		''+ completePath +'/project_3/death_vid_main_V5.jpg',
		''+ completePath +'/project_4/alluding_main_back_V2.jpg',
		''+ completePath +'/project_4/alluding_main_book_V2.png',
		''+ completePath +'/project_5/1095_main_V4.jpg',
		''+ completePath +'/project_6/liquid_type_main_V2.jpg',
		''+ completePath +'/project_7/CAF_main_V4.jpg',
		''+ completePath +'/project_8/whose_blood_back_v3.jpg',
		''+ completePath +'/project_8/whose_blood_main_poster_v3.png'
		
     ],function(){
		$('#preloader').fadeOut("slow", function(){
			$('body').css({
				'overflow': 'auto'
			});
			$('ul#side_links, ul#soci_links').fadeIn();
		});
    });
    
    /* -------------- JS Spinner ---------------- */
    var opts = {
	  lines: 8, // The number of lines to draw
	  length: 17, // The length of each line
	  width: 7, // The line thickness
	  radius: 15, // The radius of the inner circle
	  color: '#fff', // #rgb or #rrggbb
	  speed: 0.9, // Rounds per second
	  trail: 53, // Afterglow percentage
	  shadow: false // Whether to render a shadow
	};
	var target = document.getElementById('spinner');
	var spinner = new Spinner(opts).spin(target);
    
    $.fn.spin = function(opts) {
	  this.each(function() {
	    var $this = $(this),
	        data = $this.data();
	
	    if (data.spinner) {
	      data.spinner.stop();
	      delete data.spinner;
	    }
	    if (opts !== false) {
	      data.spinner = new Spinner($.extend({color: $this.css('color')}, opts)).spin(this);
	    }
	  });
	  return this;
	};
		
	/* -------------- link locations ---------------- */
	$('ul li a').stop().click(function(){
		var link = $(this).attr('title');
		$('html, body').stop().animate({
			scrollTop: $('#' + link).offset().top
		}, 2000);
	});
	

   	/* -------------- parallax scroller ---------------- */	
	$(window).scroll(function(e) {
		var delay = 0;
		var bg_speed = 0.8;
		var object_speed = 0.5; 			 		
		var container_offset = -600;
		var scroll_position = $(window).scrollTop();
		$("#main_content").stop().animate({ top : 1000 - scroll_position}, delay);
		if (scroll_position >= 0 && scroll_position <= 1280) {
			$("#gut_club_print .parallax_img img:first-child").stop().animate({ top: container_offset - (280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 0 && scroll_position <= 1100) {
			$("#gut_club_print .parallax_img img:last-child").stop().animate({ top: -400 - (280 * bg_speed) + (scroll_position * 0.5), right: - 1100 + (scroll_position * 0.5) }, delay);
		}
		if (scroll_position >= 1100 && scroll_position <= 2080) {
			$("#gut_club_print .parallax_img img:last-child").stop().animate({ top: -400 - (280 * bg_speed) + (scroll_position * 0.5)}, delay);
		}
		if (scroll_position > 1100) {
			$("#gut_club_print .parallax_img img:last-child").css({'right': -554.5});
		}
		if (scroll_position >= 580 && scroll_position <= 3280) {
			$("#gut_club_installation .parallax_img img:first-child").stop().animate({ top: container_offset - (1280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 1580 && scroll_position <= 4280) {
			$("#death_of_video .parallax_img img:first-child").stop().animate({ top: container_offset - (2280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 2580 && scroll_position <= 5280) {
			$("#alluding_to_illusions .parallax_img img:first-child").stop().animate({ top: container_offset - (3280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 2580 && scroll_position <= 3720) {
			$("#alluding_to_illusions .parallax_img img:last-child").stop().animate({ top: -600 - (3280 * bg_speed) + (scroll_position * bg_speed), left: 200 + (scroll_position * 1.2) - 5000}, delay);
		}
		if (scroll_position > 3720) {
			$("#alluding_to_illusions .parallax_img img:last-child").css({'left': -336});
		}
		if (scroll_position >= 3720 && scroll_position <= 5280) {
			$("#alluding_to_illusions .parallax_img img:last-child").stop().animate({ top: -600 - (3280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 3580 && scroll_position <= 6280) {
			$("#1095_days .parallax_img img:first-child").stop().animate({ top: container_offset - (4280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 4580 && scroll_position <= 7280) {
			$("#liquid_typography .parallax_img img:first-child").stop().animate({ top: container_offset - (5280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 5580 && scroll_position <= 8280) {
			$("#camberwell_arts_fest .parallax_img img:first-child").stop().animate({ top: container_offset - (6280 * bg_speed) + (scroll_position * bg_speed + 1)}, delay);
		}
		if (scroll_position >= 6580 && scroll_position <= 9280) {
			$("#whose_blood .parallax_img img:first-child").stop().animate({ top: container_offset - (7280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position >= 6580 && scroll_position <= 7960) {
			$("#whose_blood .parallax_img img:last-child").stop().animate({ top: -960 - (7280 * bg_speed) + (scroll_position * bg_speed), left: - 1550 + (scroll_position * bg_speed) - 5500}, delay);
		}
		if (scroll_position >= 7960 && scroll_position <= 9280) {
			$("#whose_blood .parallax_img img:last-child").stop().animate({ top: -960 - (7280 * bg_speed) + (scroll_position * bg_speed)}, delay);
		}
		if (scroll_position > 7960) {
			$("#whose_blood .parallax_img img:last-child").css({'left': -682});
		}	
	});
	
});
