// Slideshow Variables, 28 Jun 2008
var speed = 3000;
var pause = false;
var images = new Array();
var photoID = 0;

// Functions Display/Hide Users' Comments, 23 May 2008
function toggleAddForm(page) {
	if (isDisplay('postform' + page) == 'none') {
		value1 = '';
		value2 = '<a class=\\"button\\" href=\\"javascript:toggleAddForm(\'' + page + '\')\\">Hide Post Form</a>';
	} else {
		value1 = 'none';
		value2 = '<a class=\\"button\\" href=\\"javascript:toggleAddForm(\'' + page + '\')\\">Post a Comment</a>';
	}
	element1 = 'document.getElementById("postform' + page + '")';
	element2 = 'document.getElementById("postform' + page + 'Show")';
	eval( element1 + '.style.display = "' + value1 + '";');
	eval( element2 + '.innerHTML = "' + value2 + '";');
}
function toggleComments(date, page) {
	if (isDisplay('comments' + page) == 'none') {
		value1 = '';
		value2 = '<a class=\\"button\\" href=\\"javascript:toggleComments(\'' + date + '\', \'' + page + '\')\\">';
		value2 += 'Hide Comments</a>';
	} else {
		value1 = 'none';
		value2 = '<a class=\\"button\\" href=\\"javascript:';
		if (date == 'constr') value += 'toggleComment(\'' + date + '\', \'' + page;
		else value2 += 'callAjax(\'posts\', \'' + date + '\', \'' + page;
		value2 += '\')\\">Display Comments</a>';
	}
	element1 = 'document.getElementById("comments' + page + '")';
	element2 = 'document.getElementById("comments' + page + 'Show")';
	eval( element1 + '.style.display = "' + value1 + '";');
	eval( element2 + '.innerHTML = "' + value2 + '";');
}
function isDisplay(name) {
	element = 'document.getElementById("' + name + '")';
	eval( 'value = ' + element + '.style.display');
	return value;
}
function toggleDiv(name) {
	if (isDisplay(name) == 'none') value = '';
	else value = 'none';
	element = 'document.getElementById("' + name + '")'
	eval( element + '.style.display = "' + value + '";');
}

// Slideshow Functions, 28 Jun 2008
function setPause() {
	pause = (pause) ? false : true;
	if (!pause) autoNext();
	document.getElementById("pause").innerHTML = (pause) ? 'Play' : 'Pause'; // 24 Jul 2008
}
function manuNext() { // Manually Go to Next Photo
	document.getElementById("pause").innerHTML = 'Play'; // 24 Jul 2008
	pause = true;
	nextFoto();
}
function autoNext() { // Automatically Load Next Photo
	if (pause) return;
	nextFoto();
	if (!pause) setTimeout('autoNext()', speed);
}
function moveFast() { // Make Slideshow Faster
	speed -= (speed > 1000) ? 1000 : 0;
	document.getElementById("speed").innerHTML = (speed / 1000) + " seconds"; // 24 Jul 2008
}
function moveSlow() { // Make Slideshow Slower
	speed += (speed < 5000) ? 1000 : 0;
	document.getElementById("speed").innerHTML = (speed / 1000) + " seconds"; // 24 Jul 2008
}
function nextFoto() { // Load Next Photo, 24 Jul 2008
	try {
		var stage = document.getElementById("stage");
	} catch (e) { return; }
	if (++photoID == images.length) photoID = 0;
	stage.src = images[photoID];
}
function prevFoto() { // Manually Go to Previous Photo, 24 Jul 2008
	try {
		var stage = document.getElementById("stage");
	} catch (e) { return; }
	if (--photoID == 0) photoID = images.length;
	stage.src = images[photoID];
	pause = true;
	document.getElementById("pause").innerHTML = 'Play';
}

// AJAX Initialisation, 07 Jul 2008
var ajax = false;
try {
	ajax = new XMLHTTPRequest();
} catch (e) {
	try {
		ajax = new ActiveXObject('Msml2.XMLHTTP');
	} catch (e) {
		try {
			ajax = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (e) {
			ajax = false;
		}
	}
}
// 08 Jul 2008
function callAjax(request, date, page) {
	switch (request) {
		case 'posts':
			if (noAJAX(date, 'comments')) return; // 28 Jul 2008
			toggleComments(date, page); break;
		case 'entry':
			if (noAJAX(date, page)) return;
			toggleDiv('page' + page); break;
	}
	var query = '&request=' + request + '&date=' + date + '&page=' + page;
	ajax.open('GET', 'ajax-' + date + (new Date().getTime()) + query);
	ajax.onreadystatechange = function() {
		responseAjax(request, date, page);
	}
	ajax.send(null);
}
// 08 Jul 2008
function responseAjax(request, date, page) {
	if (ajax.readyState == 4 && ajax.status == 200) {
		// alert(ajax.responseText);
		switch (request) {
			case 'posts':
				try {
					document.getElementById('comments' + page).innerHTML = ajax.responseText;
				} catch (e) {
					alert(ajax.responseText);
					document.getElementById('comments' + page).innerHTML = e.name + " Error, Sorry... " + e.message;
				}
				break;
			case 'entry': document.getElementById('page' + page).innerHTML = ajax.responseText; break;
		}
	} else if (ajax.readyState == 4) {
		alert(ajax.status + '\n' + ajax.statusText);
	} else {
		switch (request) {
			case 'posts': document.getElementById('comments' + page).innerHTML = "Loading... " + (4-ajax.readyState); break;
			case 'entry': document.getElementById('page' + page).innerHTML = "Loading... " + (4-ajax.readyState); break;
		}
	}
}
// 09 Jul 2008
// Same as callAjax('entry',date,page) but will also return footer table
// For use on '/all' page...
function showEntry(date, page) {
	if (noAJAX(date, page)) return;
	toggleDiv('page' + page);
	var query = '&request=entry&all=true&date=' + date + '&page=' + page;
	ajax.open('GET', 'ajax=' + (new Date().getTime()) + query);
	ajax.onreadystatechange = function() {
		responseAjax('entry', date, page);
	}
	ajax.send(null);
}
// 09 Jul 2008
// Same as callAjax('entry',date,page) but will also return footer table
// Replaces one Entry with another
function changeEntry(date, page) {
	if (noAJAX(date, page)) return;
	element = document.getElementById('page');
	element.style.display = '';
	var query = '&request=entry&single=true&date=' + date + '&page=' + page;
	ajax.open('GET', 'ajax-' + date + (new Date().getTime()) + query);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) {
			// alert(request + ':\n' + ajax.responseText);
			document.getElementById('page').innerHTML = ajax.responseText;
		} else if (ajax.readyState == 4) {
			alert(ajax.status + '\n' + ajax.statusText);
		} else {
			document.getElementById('page').innerHTML = "Loading... " + (4 - ajax.readyState);
		}
	}
	ajax.send(null);
}
// 28 Sep 2008
// Loads Entry Page List and replaces old list
function entryIntro(date) {
	if (noAJAX(date, '')) return;
	element = document.getElementById('entry');
	element.style.display = '';
	var query = '&request=list&date=' + date;
	ajax.open('GET', 'ajax-' + date + '=' + (new Date().getTime()) + query);
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4 && ajax.status == 200) {
			// alert(request + ':\n' + ajax.responseText);
			document.getElementById('entry').innerHTML = ajax.responseText;
		} else if (ajax.readyState == 4) {
			alert(ajax.status + '\n' + ajax.statusText);
		} else {
			document.getElementById('entry').innerHTML = "Loading... " + (4 - ajax.readyState);
		}
	}
	ajax.send(null);
}

// 11 Jul 2008
// Turn a date string into a path string
function changeDate(date) {
	date = date.split(' ').join('/');
	var tst = /^\d{4}\/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/\d\d?$/i;
	if (tst.test(date)) return date;
	tst = /^\d\d?\/(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/\d{4}$/i;
	if (tst.test(date)) {
		date = date.split('/');
		temp = strNum(date[0]);
		date[0] = date[2];
		date[2] = temp;
		return date.join('/');
	}
	tst = /^\d\d?\/\d\d?\/\d{4}$/i;
	if (tst.test(date)) {
		date = date.split('/');
		temp = date[0];
		date[0] = strNum(date[2]);
		date[1] = strMon(date[1]);
		date[2] = temp;
		return date.join('/');
	}
	tst = /^\d{4}\/\d\d?\/\d\d?$/i;
	if (tst.test(date)) {
		date = date.split('/');
		date[1] = strMon(date[1]);
		date[2] = strNum(date[2]);
		return date.join('/');
	}
}

// 11 Jul 2008
function strMon(mon) {
	mon = parseInt(mon, 10);
	switch (mon) {
		case 1: return 'Jan';
		case 2: return 'Feb';
		case 3: return 'Mar';
		case 4: return 'Apr';
		case 5: return 'May';
		case 6: return 'Jun';
		case 7: return 'Jul';
		case 8: return 'Aug';
		case 9: return 'Sep';
		case 10: return 'Oct';
		case 11: return 'Nov';
		case 12: return 'Dec';
	} return mon;
}
// 11 Dec 2008
function strNum(num) {
	num = parseInt(num, 10); // Need to be sure we're working with an Int first (Noticed problem turning '09' to '009'), 22 May 2009
	if (num < 0 || num > 9) return "" + num;
	return "0" + num;
}
// 11 Jun 2008
// If !ajax, change location rather than attempt to use AJAX...
function noAJAX(date, page) {
	if (!ajax) {
		url = '/' + changeDate(date);
		if (page == 'comments') url += '?comments';
		window.location = url;
		return true;
	} return false;
}

// 15 Dec 2008
function countryCode(country) {
	switch(country) {
		case 'South Korea':
		case 'Korea': return 'kr';
		case 'France': return 'fr';
		case 'United Kingdom':
		case 'UK': return 'gb';
		case 'United States':
		case 'US': return 'us';
		default: return false;
	}
}
// 16 Dec 2008
function adminCode(area) {
	switch(area) {
		case 'North Carolina':
		case 'N Carolina': return 'NC';
		default: return area;
	}
}

// 12 Mar 2009
function vote(pic, link) {
	// TODO: Upgrade to jQuery's AJAX
	if ($) $.ajax('ajax?time=' + (new Date().getTime()) + '&request=vote&pic=' + pic + '&link=' + link);
	else if (ajax) {
		ajax.open('GET', 'ajax?time=' + (new Date().getTime()) + '&request=vote&pic=' + pic + '&link=' + link);
		ajax.onreadystatechange = function() {
			// if (ajax.readyState == 4 && ajax.status == 200) alert(ajax.responseText);
			return;
		}
		ajax.send(null);
	} else alert("Your browser does not support Ajax :(");
}

// 8 Apr 2009
function updateWebcam() {
	if (!document.getElementById('webcamimg')) return false;
	img = document.getElementById('webcamimg');
	time = document.getElementById('webcamtime');
	tmp = new Date();
	img.src = 'webcam.jpg?' + tmp.getTime();
	line = ((tmp.getHours() < 10) ? '0' : '') + tmp.getHours() + 'h' + ((tmp.getMinutes() < 10) ? '0' : '') + tmp.getMinutes() + ' ';
	line += tmp.getDate() + ' ' + strMon(tmp.getMonth()+1) + ' ' + tmp.getFullYear();
	time.innerHTML = line;
	setTimeout("updateWebcam();", 180000);
}
if (location.href.indexOf('index.php') > -1 || location.href == 'http://yodas.ws/' || location.href.indexOf('webcam') > -1)
	setTimeout("updateWebcam();", 5000);

// Put default values on function arguments, 28 Sep 2011
// From http://parentnode.org/javascript/default-arguments-in-javascript-functions/
Function.prototype.defaults = function() {
	var _f = this;
	var _a = Array(_f.length-arguments.length).concat(Array.prototype.slice.apply(arguments));
	return function() {
		return _f.apply(_f, Array.prototype.slice.apply(arguments).concat(
			_a.slice(arguments.length, _a.length)
		));
 	}
}

/****************
 * 2011 Redesign
 ***************/

// Detect Mobile Browser, 19 Oct 2011
function isMobile() {
	var ua = navigator.userAgent;
	var mobileBrowsers = ['WP7','Windows CE','Android','iPad','iPhone','Symbian','Vodafone','nook','webOS'];
	mobileBrowsers.push('palmOS','Palm','WMS PIE','Blackberry','Mobile','Cricket','Kindle','MOT-','Nokia','SAMSUNG');
	for (i in mobileBrowsers) { // Detect Mobile Browsers
		if (ua.indexOf(mobileBrowsers[i]) != -1) return true;
	}
	return false;
}

// Do Nothing Function, 24 Sep 2011
function blank() {
	$('#main').html('Under Construction');
	$('nav > a').removeClass('active');
}
// TODO: Load Twitter Feed
function loadTwitter(link) {
	$('#main').html('loading...').css('cursor', 'wait');
	$('#main').load("test/twitterCatch.php?format=html", function() {
		$('#main').css('cursor', 'auto');
		$('nav > a').removeClass('active');
		$(link).addClass('active');
	});
}
// Load Entry via AJAX, 24 Sep 2011
var loadEntry = function (date, link) {
	if (link != null && link.indexOf('#') == -1) link = '#' + link;
	$('#main').html('loading...').css('cursor', 'wait');
	$.get("ajax-" + date + '&request=entry&date=' + date, function(data, status, xhr) {
		$('#main').css('cursor', 'auto').html(data);
		$('nav > a').removeClass('active');
		if (link != null) $(link).addClass('active');
		// TODO: HTML5 History Manipulation
	});
}.defaults(null);

// Toggle Social Menu, 3 Dec 2011
function toggleSocialMenu() {
// Ugh, Chrome won't call this function!
//	var socialMenu = document.getElementById('socialMenu');
//	if (socialMenu.style.display == 'inline-block') socialMenu.style.display = 'none';
//	else socialMenu.style.display = 'inline-block';
//	$('#socialMenu').toggle();
}
