var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
$(document).ready(function(){
	
	//SETUP
	links = $("#globe ul li").clone();
	$("#globe").after($("#globe ul:first")).next().addClass("shown");
	$(".shown a").after ("<span>(view moments)</span>");
	
	//link to sometimes page
	$(".shown span, .shown a").css("cursor","pointer").click(function(){
		window.location.href = "/sometimes"+$(this).parent().children("a").attr("href");
	})
	
	$("#globe").after("<ul class='hotspots first'></ul>");
	$(".hotspots").html(links);
	$(".hotspots").after($(".hotspots").clone().removeClass("first").addClass("second"));
	$("#globe").prepend('<img class="bg" src="/wp-content/themes/ob/images/globe_bg.png">');
	$("#globe").prepend('<img class="fg" src="/wp-content/themes/ob/images/globe_fg.png">');
	$("#globe").after('<img class="mask" src="/wp-content/themes/ob/images/mask.png">');
	$(".shown li:not(:first)").fadeOut();
	$(".hotspots a").each(function(){
		topval = $(this).attr("data-topval");
		leftval = $(this).attr("data-leftval");
		$(this).css("top", topval).css("left", leftval).css("z-index",3000).css("zoom",1);
	});
	
	interaction = false;
	mcount = 1;
    minterval = setInterval(function(){
				if(interaction!=true){
					$('.shown li:nth-child('+mcount+')').fadeOut(1000);
			        mcount = mcount+1;
			        if(count>3) {
			            mcount = 1;
			            $('.shown li:nth-child('+mcount+')').fadeIn(3000);
			        }else{
			            $('.shown li:nth-child('+mcount+')').fadeIn(3000);
			            }
				} //end interaction
        
        }, 3000); //minterval

	// Uses http://plugins.jquery.com/project/Pause-Resume-animation
	// ANIMATION

	var bg = $('.bg'),
	    hotspot1 = $('.hotspots.first'),
	    hotspot2 = $('.hotspots.second'),
	    fg = $('.fg'),
	    globe = $('#globe');
		animspeed = 15000;
	if(agentID) {
		animspeed = 30000;
	}
	
	if($("#globe").hasClass("static")){
		bg.css('right', '0px');
		hotspot1.css('left', '0px');
		hotspot2.css('left', '321px');
		fg.css('left', '0px');
	} else {

	var globeAnimation = function () {

	  globe.data('complete', false);

		bg.css('right', '0px').animate({
		    right: '-311px'
		  }, animspeed, 'linear', function () {
		    // Animation complete.
		  });

		hotspot1.css('left', '0px').animate({
		    left: '-311px'
		  }, animspeed, 'linear', function () {
		    // Animation complete
		  });

		hotspot2.css('left', '321px').animate({
		    left: '0'
		  }, animspeed, 'linear', function () {
		    // Animation complete
		  });

		fg.css('left', '0px').animate({
		    left: '-311px'
		  }, animspeed, 'linear', function () {
		    globe.data('complete', true);
		  });

	};

	var animationCompleteListener = window.setInterval(function () {
		if (globe.data('complete')) {
			globeAnimation();
		}
	}, 200);

	globeAnimation();

	//INTERACTION
	$(".hotspots a").click(function(){
		if(agentID) {
			hotspot1.pause();
			hotspot2.pause();
			fg.pause();
			bg.pause();
	       	setTimeout(function(){
				hotspot1.resume();
				hotspot2.resume();
				fg.resume();
				bg.resume();
				}, 3000);
			moment = $(this).attr("href");
			$(".shown li").fadeOut();
			$(".shown li a[href="+moment+"]").parent().fadeIn();
			}
		event.stopPropagation();
		return false;
	});


	$(".hotspots a").hover(
		
					function(){
						interaction = true;
						
						if (!agentID) {
						hotspot1.pause();
						hotspot2.pause();
						fg.pause();
						bg.pause();
						moment = $(this).attr("href");
						$(".shown li").fadeOut();
						$(".shown li a[href="+moment+"]").parent().fadeIn();
						}
					},
					
					function(){
						if (!agentID) {
						hotspot1.resume();
						hotspot2.resume();
						fg.resume();
						bg.resume();
						}
					});
	$("#globe").mouseout(function(){
		if(!agentID) {
		$(".shown li").fadeOut();
		}
		
	});
	
	} //end if static
});
