if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
var viewportmeta = document.querySelectorAll('meta[name="viewport"]')[0];
if (viewportmeta) {
viewportmeta.content = 'width=device-width, minimum-scale=1.0, maximum-scale=1.0';
document.body.addEventListener('gesturestart', function() {
viewportmeta.content = 'width=device-width, minimum-scale=0.25, maximum-scale=1.6';
}, false);
}
}

var animating = false;

$(document).ready(function() {
	var part1 = "me";
	var part2 = "ianculshaw.co.uk";
	var part3 = "";
	$('#email').attr('href', 'mai' + 'lto:' + part1 + '@' + part2);
	
	adjustImages();
	
	$(window).resize(function() {
		adjustImages();
	});
	
	$('.image-gallery span').click(function(ev) {
		var itemNum = parseInt($(this).attr('data-number')) + 1;
		var theParents = $(this).parents();
		var navigation = $(theParents[3]).next();
		
		// Set the total of this gallery
		var galleryTotal = navigation.find('a').length;
		
		if(itemNum >= galleryTotal) {
			if(!animating) {
				animating = true;
				$(theParents[1]).animate({'left': '0px'}, 500, function() {
					animating = false;
				});
			}
			
			navigation.find('ul li a').each(function() { $(this).removeClass('current'); });
			navigation.find('a[data-number=0]').addClass('current');		
			
			return false;
		}
		
		var windowWidth = $(window).width();
		if(windowWidth < 480) {
			var galleryWidth = 300;
		} else 
		if (windowWidth >= 480 && windowWidth < 600) {
			var galleryWidth = 460;
		} else
		if(windowWidth >= 600 && windowWidth < 768) {
			var galleryWidth = 640;
		} else
		if(windowWidth > 768 && windowWidth < 992) {
			var galleryWidth = 740;
		} else
		if(windowWidth >= 992) {
			var galleryWidth = 960;
		} 
		
		var movement = (itemNum * galleryWidth)*-1;
		console.log('last');
		
		if(!animating) {
			animating = true;
			$(theParents[1]).animate({'left': movement + 'px'}, 500, function() {
				animating = false;
			});
		}
		
		
		
		
		
		navigation.find('ul li a').each(function() { $(this).removeClass('current'); });

		navigation.find('a[data-number='+(itemNum)+']').addClass('current');
		
	});
	
	$('.image-navigation ul li a').click(function(ev) {
		ev.preventDefault();
		var windowWidth = $(window).width();
		if(windowWidth < 480) {
			var galleryWidth = 300;
		} else 
		if (windowWidth >= 480 && windowWidth < 600) {
			var galleryWidth = 460;
		} else
		if(windowWidth >= 600 && windowWidth < 768) {
			var galleryWidth = 640;
		} else
		if(windowWidth > 768 && windowWidth < 992) {
			var galleryWidth = 740;
		} else
		if(windowWidth >= 992) {
			var galleryWidth = 960;
		} 
		
		var theParents = $(this).parents();
		var itemNum = $(this).attr('data-number');
		
		// Webkit 3d enabled
		/*if(Modernizr.csstransforms3d) {
			//$(theParents[2]).prev().addClass('ninety');
			var degs = 0;
			
			switch(itemNum) {
				case '1':
					degs = 90;
				break;
				case '2':
					degs = 180;
				break;
				case '3':
					degs = 270;
				break;
				default:
					degs = 0;
				break;
			}
			
			$('#galleryFocus').fadeIn(200).delay(500).fadeOut(200);
			
			$(theParents[2]).prev().find('.shape').css('-webkit-transform', 'rotateX('+degs+'deg)');
		} else {/**/
			// All other browsers
			var movement = (itemNum * galleryWidth)*-1;
			if(!animating) {
				animating = true;
				$(theParents[2]).prev().find('.stage .shape').animate({'left': movement + 'px'}, 500, function() {
					animating = false;
				});
			}
		//}
		
		$(this).parent().parent().each(function() { $(this).find('a').removeClass('current'); });
		
		$(this).addClass('current');
		
		
	});

});

function adjustImages() {
	// Replace the source's of the gallery images dependant on the width of the device
	var galleryWidth = 300;
	var windowWidth = $(window).width();
	console.log(windowWidth);
	if (windowWidth >= 480 && windowWidth <= 600) {
		console.log(480);
		var galleryWidth = 480;
		$('section.image-gallery').each(function(ev, el) {
			$(el).find('img').each(function(i, el) {
				el.src = el.src.replace('/l/', '/s/').replace('/m/', '/s/');
			});
			//var imgR = $(el).find('img')[0];
			//imgR.src = imgR.src.replace('/m/', '/l/');
		});
		
		

	} else
	if (windowWidth > 600 && windowWidth < 768) {
		console.log(640);
		var galleryWidth = 640;
		$('section.image-gallery').each(function(ev, el) {
			$(el).find('img').each(function(i, el) {
				el.src = el.src.replace('/l/', '/m/').replace('/s/', '/m/');
			});
			//var imgR = $(el).find('img')[0];
			//imgR.src = imgR.src.replace('/m/', '/l/');
		});
		
		// download complicated script
		// swap in full-source images for low-source ones
	} else
	if(windowWidth > 768) {
		console.log(960);
		var galleryWidth = 960;
		$('section.image-gallery').each(function(ev, el) {
			$(el).find('img').each(function(i, el) {
				el.src = el.src.replace('/m/', '/l/').replace('/s/', '/l/');
			});
		});
	}
	
	$('section.image-gallery').each(function(i, el) {
		var number = $(el).next().find('.current').attr('data-number');
		if(number > 0) {
			var equals = (galleryWidth/parseInt(number));
			$(el).find('.shape').css('left', -(equals) + 'px');
		}
	});
}
// chrome shipped without the time arg in m10
var timeundefined = false;
if (window.webkitRequestAnimationFrame){
    webkitRequestAnimationFrame(function(time){
        timeundefined = (time == undefined);
    });
}

// requestAnim shim layer by Paul Irish
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       || 
          window.webkitRequestAnimationFrame || 
          window.mozRequestAnimationFrame    || 
          window.oRequestAnimationFrame      || 
          window.msRequestAnimationFrame     || 
          function(/* function */ callback, /* DOMElement */ element){
            window.setTimeout(callback, 100);
          };
})();
	
var called = 1;	

// Ocanvas object
/*
var canvas = oCanvas.create({
	canvas: "#canvas"
});

var image = canvas.display.image({
	x: 80,
	y: 80,
	origin: { x: "center", y: "center" },
	image: "img/holding.png"
});

canvas.addChild(image);

function rotate120(obj) {
	obj.animate({
		rotation: obj.rotation + 120
	}, 1100, "ease-in-out", function () {
		canvas.redraw();
		setTimeout(function() {
			if(called == 3) {
				return;
			}
			rotate120(obj);
			called = called + 1;
		}, 300);
	});
}

rotate120(image);/**/




