$(function(){
	

	function doSomething ( )
	{
	  // (do something here)
	  console.log($(window).scrollTop())
	}
	//setInterval ( doSomething, 150 );
	
	
	$('.stories') 
		.after('<div id="StoryNav">') 
		.cycle({ 
		    fx:     'scrollUp', 
		    speed:  800, 
		    timeout: 0, 
		    pager:  '#StoryNav' 
		});
	
	
	/* NAVIGATION */
	
	$('#nav a').bind('click',function(event){
		var $anchor = $($(this).attr('href'));

		$('#nav a').removeClass('selected');
		$(this).addClass('selected');
		
		var titleTopOffset =   parseInt($anchor.find('.inner-title').css('background-position-y'),10);
		
		if(titleTopOffset) {
			var topPos = $anchor.offset().top + titleTopOffset - 70;
		} else {
			var topPos = $anchor.offset().top + 70;
		}
		var topDistance = Math.abs($(window).scrollTop() - topPos);
		
		/*
		console.log("scrollTop");
		console.log($(window).scrollTop());
		console.log("anchor.offset");
		console.log($anchor.offset().top);
		*/
		//alert(topDistance);
		if(topDistance > 5500) {
			scrollSpeed =  topDistance*0.8;
			scrollEasing = 'easeInOutSine';
			//console.log("Long'un");
		} else {
			scrollSpeed = 2000;
			scrollEasing = 'easeInOutQuart';
			//console.log("Short'un");
		}
		$('html, body').stop().animate({
			scrollTop: topPos
		},scrollSpeed,scrollEasing);
		/*
		if you don't want to use the easing effects:
		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top
		}, 1000);
		*/
		event.preventDefault();
	});
	
	
	// SHOW PRODUCTS
	var productShow = 1;
	$("#product-2").animate({"backgroundPositionX": "600%", "opacity": 0},0);
	$('#buyNav a').bind('click',function(event){
		event.preventDefault();

		textDiv = "#product-" + productShow + "-text";
		productDiv = "#product-" + productShow;
		//alert(productDiv);
		$(textDiv).fadeOut();
		$(productDiv).animate({"backgroundPositionX": "-300%", "opacity": 0},700);
		
		if(productShow == 1) {
			productShow = 2;
		} else {
			productShow = 1;
		}
		textDiv = "#product-" + productShow + "-text";
		productDiv = "#product-" + productShow;
		
		//alert(productDiv);
		
		$(textDiv).delay(400).fadeIn();
		$(productDiv).animate({"backgroundPositionX": "600%", "opacity": 0},0).animate({"backgroundPositionX": "50%", "opacity": 1},800);		
	})
	
	var movedMug = false;
	$("#inner2b").data('initBgX',$("#inner2b").css("backgroundPositionX"));
	$("#inner2c").data('initBgX',$("#inner2c").css("backgroundPositionX"));
	$("#inner2d").data('initBgX',$("#inner2d").css("backgroundPositionX"));
	$("#about").bind('inview', function (event, visible) {
		if (visible == true) {
			/*$(window).bind('scroll', function(){
				if($(window).scrollTop() > 1470 && $(window).scrollTop() < 1900) {*/
					if(movedMug == false) {
						$("#inner2b").css({"backgroundPositionX":$("#inner2b").data('initBgX')}).stop(true,false).delay(2050).animate({"backgroundPositionX":-3000}, 2500);
						$("#inner2c").css({"backgroundPositionX":$("#inner2c").data('initBgX')}).stop(true,false).delay(2050).animate({"backgroundPositionX":-3000}, 2500);
						$("#inner2d").css({"backgroundPositionX":$("#inner2d").data('initBgX')}).stop(true,false).delay(1700).animate({"backgroundPositionX":150}, 1300, 'easeOutBack');
						movedMug = true;
					}
				/*} 
			});*/
		} else {
			movedMug = false;
		}
	});
	
	
	var inViewId;
	$(".page").bind('inview', function (event, visible) {
		var whichNav = '#nav li a[href="#' + $(this).attr('id') + '"]';
		
		$('#nav li a').removeClass('selected');
		if (visible == true) {
			
			$(whichNav).addClass('selected');
				//console.log($(this).attr('id'));
			
			inViewId = $(this).attr('id');
		
		}else{
			//$(whichNav).removeClass('selected');
		}	
	});


	//BOXES ON SHELF
	var haveBoxesMovedIn = false;
	$("#stockists").bind('inview', function (event, visible) {
		if (visible == true) {
			if(	haveBoxesMovedIn == false) {
				$("#stockists .box").each(function(index) {
					if(!$(this).data('initBgX')) {
						$(this).data('initBgX',$(this).css("backgroundPositionX"));
					}
					$(this).css({"backgroundPositionX":"2000px"});
					$(this).stop(true,false).delay(1400+index*200).animate({"backgroundPositionX":$(this).data('initBgX')}, 800);
				});
				haveBoxesMovedIn = true;
			}
		} else {
			if(	haveBoxesMovedIn == true) {
				$("#stockists .box").css({"backgroundPositionX":"2000px"});
				haveBoxesMovedIn = false;
			}
		}
	});
	

	// TICKETS ROUND CUP
	
	function getInternetExplorerVersion() {
	    var rv = -1; // Return value assumes failure.
	    if (navigator.appName == 'Microsoft Internet Explorer') {
	        var ua = navigator.userAgent;
	        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
	        if (re.exec(ua) != null)
	            rv = parseFloat(RegExp.$1);
	    }
	    return rv;
	}
	var ieVer = getInternetExplorerVersion();
	var safeToFadePNG = true;
    if (ieVer > -1 && ieVer < 9.0)
            safeToFadePNG = false;
    
   
	
	if(safeToFadePNG) {
		var haveTicketsMovedIn = false;
		$("#tokens .ticket").animate({opacity: 0},0);
		$("#tokens").bind('inview', function (event, visible) {
			if (visible == true) {
				if(	haveTicketsMovedIn == false) {
					$("#tokens .ticket").each(function(index) {
						//console.log('getting tokens');
						$(this).stop(true,true).delay(1400+index*150).animate({opacity: 1},0);
					});
					haveTicketsMovedIn = true;
				}
			} else {
				if(	haveTicketsMovedIn == true) {
					$("#tokens .ticket").animate({opacity: 0},0);
					haveTicketsMovedIn = false;
				}
			}
		});
	}
	
	
	// LOADING MESSAGES
	$('#loadMessages li').css({opacity: 0.0});
	var $active;
	var $next; 
	$('#loadMessages').css({display: "block"});
	$('#loadMessages li:first').css({opacity: 1});
	function messageSwitch() {
	    $active = $('#loadMessages li.active');
	
	    if ( $active.length == 0 ) $active = $('#loadMessages li:first');

		if($active.next().length) {
			$next =  $active.next();
			//console.log('IS A NEXT');
		} else {
			$next =  $('#loadMessages li:first');
			//console.log('NO NEXT');
		}
	
	    $active.addClass('last-active');
	    
	    //console.log('Active ' + $active.text());
	    //console.log('Next ' + $next.text());
	
	    $active.stop(true,false).animate({opacity: 0.0}, 400);
	    $next.stop(true,false).delay(400).animate({opacity: 1.0}, 400, function() {
	            $active.removeClass('active last-active');
	            $next.addClass('active');
	    });
	}

	var loadMessageInterval = window.setInterval( messageSwitch, 3000 );
	







	$("a.popupLink").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	400, 
		'speedOut'		:	400, 
		'width'			:	860,
		'height'		:	'90%',
		'overlayShow'	:	true,
		'padding'		:	0
	});

	/* main background image. moves against the direction of scroll*/
	$('.page').scrollParallax({
		'speed': -0.2,
		'axis': 'y'
	});


	/* two additional samples inside about, both moving with direction of scroll*/
	$('.page .inner-lev1').scrollParallax({
		'speed': 1,
		'axis': 'y'
	});	
	$('.page .inner-lev2').scrollParallax({
		'speed': 3,
		'axis': 'y'
	});
	$('.page .inner-lev3').scrollParallax({
		'speed': 7,
		'axis': 'y'
	});
	
	/*******************PAGE 2*******************
	*/
	$('#about').scrollParallax({
		'speed': -0.2,
		'axis': 'y',
		'direction': 'down'
	});
	$('#about .inner-bg').scrollParallax({
		'speed': -0.2,
		'axis': 'y',
		'direction': 'down'
	});

	$('#about .inner-lev1').scrollParallax({
		'speed': 0.6,
		'axis': 'y',
		'direction': 'down'
	});	
	$('#about .inner-lev2').scrollParallax({
		'speed': 1.2,
		'axis': 'y',
		'direction': 'down'
	});
	$('#about .inner-lev3').scrollParallax({
		'speed': 1.2,
		'axis': 'y',
		'direction': 'down'
	});
	
	/*******************PAGE 3*******************
	*/
	$('#blend').scrollParallax({
		'speed': -0.2,
		'axis': 'y',
		'direction': 'down'
	});

	
	/*******************PAGE 4*******************
	*/
	$('#stockists').scrollParallax({
		'speed': -0.2,
		'axis': 'y'
	});
	$('#stockists .box').scrollParallax({
		'speed': -0.2,
		'axis': 'y',
		'direction': 'left'
	});
	$('#stockists .content').scrollParallax({
		'speed': -0.25,
		'axis': 'y'
	});
	
	/*******************PAGE 5*******************
	*/
	$('#buy').scrollParallax({
		'speed': -0.2,
		'axis': 'y',
		'direction': 'down'
	});

	$('#buy .product').scrollParallax({
		'speed': -0.4,
		'axis': 'y'
	});
	/*******************PAGE 6*******************
	*/
	$('#tokens').scrollParallax({
		'speed': -0.4,
		'axis': 'y'
	});

	$('#tokens .inner-lev1').scrollParallax({
		'speed': -0.8,
		'axis': 'y',
		'direction': 'down'
	});
	$('#tokens .ticket').scrollParallax({
		'speed': -0.4,
		'axis': 'y'
	});
	/*******************PAGE 7*******************
	*/
	$('#contact').scrollParallax({
		'speed': -0.4,
		'axis': 'y'
	});

	$('#contact .content').scrollParallax({
		'speed': -1,
		'axis': 'y'
	});
});
