var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19324934-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

var menuLinks = [
	{
		name: 'Top',
		uri: '/'
	},
	{
		name: 'YTClock',
		uri: '/projects/ytclock/'
	},
	{
		name: 'Helicopter Project',
		uri: '/projects/helicopter/'
	},
	{
		name: 'Other Projects',
		uri: '/projects/others/'
	},
	{
		name: 'Photos',
		uri: '/photos/'
	},
	{
		name: 'Videos',
		uri: '/videos/'
	},
	{
		name: 'Blog',
		uri: '/blog/'
	}
];
	
var langLinks = [
	{
		name: 'US-English',
		img: '/scripts/flags/en.png',
		uri: 'javascript:setLanguage(\'en\')'
	},
	{
		name: '日本語',
		img: '/scripts/flags/ja.png',
		uri: 'javascript:setLanguage(\'ja\')'
	}
];

var isMobile = !!navigator.userAgent.match(/(iPhone|iPod|iPad|Android)/i);
var isLargeMobile = !!navigator.userAgent.match(/iPad/i);

function animateOpacity(element, from, to, diff, interval) {
	if (isMobile) {
		element.style.opacity = to;
		return;
	}
	
	from += diff;
	if (diff > 0 && from >= to || diff < 0 && from <= to) {
		from = to;
	} else if (diff != 0) {
		setTimeout(animateOpacity, interval, element, from, to, diff, interval);
	}
	element.style.opacity = from;
}

function setLanguage(lang) {
	var date = new Date();
	date.setTime(date.getTime() + 365 * 24 * 60 * 60 * 1000);
	document.cookie = 'lang=' + lang + ';path=/;expires=' + date.toGMTString();
	location.reload();
}

function addMenu() {
	var body = document.getElementsByTagName('body')[0];
	if (body) {	
		try {
			var mul, mli, ma;
			
			if (isMobile) {
				body.className += ' mobile';
				if (isLargeMobile) {
					body.className += ' large_mobile';
				}
			}
			
			var mdiv = document.createElement('nav');
			mdiv.setAttribute('id',
					isMobile ? 'mobile_navigation' : 'navigation');
					
			
			mul = document.createElement('ul');
			mul.setAttribute('id', 'nav_list');
			for (var i = 0; i < menuLinks.length; i++) {
				var li = document.createElement('li');
				var a = document.createElement('a');
				a.setAttribute('href', menuLinks[i].uri);
				a.appendChild(document.createTextNode(menuLinks[i].name));
				if (document.location.pathname == menuLinks[i].uri) {
					var liAttr = document.createAttribute('class');
					liAttr.nodeValue = 'current_page';
					li.setAttributeNode(liAttr);
				}
				li.appendChild(a);
				mul.appendChild(li);
			}
			addLanguageBar(mul);
			mdiv.appendChild(mul);
			
			body.appendChild(mdiv);
			animateOpacity(mdiv, 0.0, 1.0, 0.1, 20);
			
			var clockScript = document.createElement('script');
			clockScript.setAttribute('type', 'text/javascript');
			clockScript.setAttribute('src', '/scripts/ytclock.js');
			body.appendChild(clockScript);
						
			var menuStyle = document.createElement('link');
			menuStyle.setAttribute('rel', 'stylesheet');
			menuStyle.setAttribute('type', 'text/css');
			menuStyle.setAttribute('href', '/styles/navigation.css');
			document.getElementsByTagName('head')[0].appendChild(menuStyle);
		} catch (e) {}
	} else {
		setTimeout(addMenu, 200);
	}
}

function addLanguageBar(navList) {
	var li = document.createElement('li');
	li.setAttribute('id', 'lang');
	var img = document.createElement('img');
	img.setAttribute('src', '/scripts/flags/flag.png');
	img.setAttribute('width', '16');
	img.setAttribute('height', '11');
	li.appendChild(img);

	var langUl = document.createElement('ul');
	var langLi = document.createElement('li');
	
	for (var i = 0; i < langLinks.length; i++) {
		var langLi = document.createElement('li');
		var a = document.createElement('a');
		a.setAttribute('href', langLinks[i].uri);
		a.appendChild(document.createTextNode(langLinks[i].name + ' '));
		var img = document.createElement('img');
		img.setAttribute('src', langLinks[i].img);
		img.setAttribute('width', '16');
		img.setAttribute('height', '11');
		a.appendChild(img);
		langLi.appendChild(a);
		langUl.appendChild(langLi);
	}

	li.appendChild(langUl);
	navList.appendChild(li);
}

addMenu();

if (isMobile) {
	var ytclock = null;
	var navigation = null;
	var marginTop = 30;
	var marginBottom = 15;
	var ytclockTopTarget = 0;
	var navTopTarget = marginTop;
	
	if (navigator.userAgent.match(/iPad/i)) {
		(function updateViewport() {
			viewport = document.getElementsByName('viewport')[0];
			if (viewport) {
				viewport.setAttribute('content', 'width=900');
			} else {
				setTimeout(updateViewport, 100);
			}
		})();
	}

	function onScroll() {
		if (!navigation || !ytclock) {
			return false;
		}
		
		ytclockTopTarget = window.innerHeight + window.pageYOffset
				- 128 - marginBottom;

		var height = navigation.offsetHeight;
		if (height + marginTop + 128 < window.innerHeight) {
			navTopTarget = window.pageYOffset + marginTop;
		} else {
			if (navigation.offsetTop + height + marginBottom * 2  + 128
					< window.pageYOffset + window.innerHeight) {
				navTopTarget = window.pageYOffset + window.innerHeight
						- height - marginBottom * 2 - 128;
			} else if (navigation.offsetTop - marginTop > window.pageYOffset) {
				navTopTarget = window.pageYOffset + marginTop;
			}
		}
		
		if (ytclockTopTarget < navTopTarget + height + marginBottom) {
			ytclockTopTarget = navTopTarget + height + marginBottom;
		}

		ytclock.style.top = ytclockTopTarget + 'px';
		navigation.style.top = navTopTarget + 'px';
	}
	
	window.onscroll = onScroll;
	window.onload = onScroll;
	
	(function getElements() {
		//body = document.getElementById('body');
		ytclock = document.getElementById('ytclock');
		navigation = document.getElementById('mobile_navigation');
		if (ytclock && navigation) {
			onScroll();
			setInterval(onScroll, 2000);
		} else {
			setTimeout(getElements, 100);
		}
	})();
}

