
$(document).ready(function() {
	jQuery(window).hashchange();
});

jQuery(window).hashchange(function(){
	
	var hash = location.hash.replace(/#\//,'');
	var hashSplit = hash.split('/');
	var xml = hashSplit[0];
	var path = hashSplit.join('/');
	
	if(xml == 'video') {
		var videoObj = '<div id="videobox">';
		if($.browser.mozilla){
			videoObj += "<video controls='controls' src='publish/html/roche.ogv' poster='publish/html/roche.jpg'></video>";
		} else if ($.browser.msie && $.browser.version < 9 ) {
			videoObj += '<object ';
					videoObj += 'width="320" height="304" ';
					videoObj += 'type="video/x-ms-asf" ';
					videoObj += 'url="publish/html/roche.wmv" data="publish/html/roche.wmv" ';
					videoObj += 'classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" ';
					videoObj += 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" ';
					videoObj += 'standby="Loading Microsoft Windows Media Player components..."';
				videoObj += '>';
				videoObj += '<param name="url" value="publish/html/roche.wmv" />';
				videoObj += '<param name="filename" value="roche.wmv" />';
				videoObj += '<param name="uimode" value="mini" />';
				videoObj += '<param name="showstatusbar" value="false">';
				videoObj += '<param name="autosize" value="true" />';
				videoObj += '<param name="playcount" value="true" />';
				videoObj += '<param name="allowchangedisplaysize" value="true"> ';
				videoObj += '<param name="autostart" value="false">';
				videoObj += '<param name="showcontrols" value="true">';
				videoObj += '<param name="clicktoplay" value="true">';
			videoObj += '</object>';
		} else{
			videoObj += '<video width="320" height="240" controls="controls" poster="publish/html/roche.jpg" src="publish/html/roche.mp4"></video>';
		}
		videoObj += '</div>';
		$('#content').html(videoObj);
		setActiveState(path.split('/'));
		return;
	}
	$.get("publish/html/getContent.php?path=" + path,{},
		function(data){
			var obj = jQuery.parseJSON(data);
			var fullPathArr = obj['path'] ? obj['path'].split('/') : [];
			if(obj['path'] != '') window.location.href = "#/" + obj['path'] + "/";
			if(obj['page']) { showContentPage(obj['page']); }
			else if(obj['overview']) { showContentOverview(fullPathArr,obj['overview']); }
			else if(obj['image']) { showContentImage(fullPathArr,obj); }
			if(obj['subNav']) showSubNav(fullPathArr,obj['subNav']);
			else showSubNav([],[])
			setActiveState(fullPathArr);
		}
	);
});

function setActiveState(path) {
	if(path.length > 1) {
		pathstr = '#/' + path.join('/') + '/';
		$('a').removeClass('active');
		$("a").each(function() {
			searchstr = $(this).attr('href');
			if(searchstr != '#/' && pathstr.match(searchstr) != null) $(this).addClass("active");
		});
	}
}

function showSubNav(path,arr) {
	if(arr.length == 0 || path.length < 2) {
		$('#navSub').html('');
		return;
	}
	var pathArr = path;
	var nav = '';
	for(var i=0;i<arr.length;i++) { nav += '<a href="#/'+pathArr[0]+'/'+pathArr[1]+'/'+arr[i]+'/">'+arr[i]+'</a> ' };
	$('#navSub').html(nav);
}
function showContentOverview(path,obj) {
	var content = '<div id="overview">';
	for(var i=0;i<obj.length;i++) {
		content += '<div class="item"><a href="#/' + path.join('/') + '/' + obj[i] + '/"><img src="../extdoc/generated/' + obj[i] + '_s.jpg" /></a></div>';
	}
	content += '</div>';
	$('#content').html(content);
}
function showContentImage(path,obj) {
	parentpath = path.clone();
	parentpath.pop();
	var content = '<div id="detail">';
	content += '<img src="publish/extdoc/generated/' + obj['image'] + '" />';
	content += '<div id="hoverbox">';
	if(obj['prev']) content += '<a href="#/' + parentpath.join('/') + '/' + obj['prev'] + '/" class="step prev"></a>';
	if(obj['next']) content += '<a href="#/' + parentpath.join('/') + '/' + obj['next'] + '/" class="step next"></a>';
	content += '</div">';
	content += '</div>';
	$('#content').html(content);
	
	
	var ua = navigator.userAgent.toLowerCase();
	var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || isAndroid) {
		
		$('#content #hoverbox').addClass('mobile');
		$('#content #hoverbox').mouseenter(function(){
			delayedHoverEffect();
		});
		window.setTimeout('hideMobileArrows()',5000);
		
	} else {
		$('#content #hoverbox').mouseenter(function(){
			delayedHoverEffect();
		});
		$('#content #hoverbox').mouseover(function(){
			delayedHoverEffect();
		});	
	}
}

function hideMobileArrows() {
	$('#content #hoverbox').removeClass('mobile');
}

function delayedHoverEffect() {
	$('#content #hoverbox').addClass('step');
}

function showContentPage(obj) {
	var content = '<div id="page">';
	if(obj['image']) {
		content += '<img src="publish/extdoc/generated/' + obj['image'] + '"';
		if(obj['title']) {
			content += ' alt="' + obj['title'] + '"';
		}
		content += ' />';
	}
	content += '<p>' + obj['text'] + '</p>';
	content += '</div><div class="clear"></div>';
	$('#content').html(content);
}

Array.prototype.clone = function () {
	var arr1 = new Array(); 
	for (var property in this) { arr1[property] = typeof (this[property]) == 'object' ? this[property].clone() : this[property]; }
	return arr1;
}


/* ################################## JSON ################################## */

function array2json(arr) {
	//Converts the given data structure to a JSON string.
	//Argument: arr - The data structure that must be converted to JSON
	//http://www.openjs.com/scripts/data/json_encode.php
	var parts = [];
	var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');
	for(var key in arr) {
		var value = arr[key];
		if(typeof value == "object") { //Custom handling for arrays
			if(is_list) parts.push(array2json(value)); /* :RECURSION: */
			else parts[key] = array2json(value); /* :RECURSION: */
		} else if (typeof value == "function") { /* do nothing for functions */
		} else {
			var str = "";
			if(!is_list) str = '"' + key + '":';
			//Custom handling for multiple data types
			if(typeof value == "number") str += value; //Numbers
			else if(value === false) str += 'false'; //The booleans
			else if(value === true) str += 'true';
			else str += '"' + value + '"'; //All other things
			parts.push(str);
		}
	}
	var json = parts.join(",");
	if(is_list) return '[' + json + ']';//Return numerical JSON
	return '{' + json + '}';//Return associative JSON
}

/* ################################## // JSON ################################## */

