var webpath = "http://stagevu.com";
var userimgpath = "http://stagevu.com/img/userimg";

function toggle(id) {
	$("#" + id).toggle();
}

function slidetoggle(id) {
	$("#" + id).slideToggle("slow");
}

var advshown = false;
function togglesearch() {
	if (advshown) $("#advsearch").fadeOut(1000);
	else $("#advsearch").fadeIn(1000);
	
	advshown = !advshown;
}

var loadingshow = false;
function loading() {
	if (loadingshow == false) {
		$("#loading").fadeIn("normal");
		window.loadingshow = true;
	} else {
		$("#loading").fadeOut("normal");
		window.loadingshow = false;
	}
}

var noticeshow = false;
var noticetimeout = null;
function notice(message, color) {
	if (message != null) $("#notice").html(message);
	if (color != null) $("#notice").css("background-color", color);
	if (window.noticetimeout != null) {
		window.clearTimeout(window.noticetimeout);
		window.noticetimeout = window.setTimeout('window.noticetimeout = null; window.notice();', 5000);
	} else if (noticeshow == false) {
		$("#notice").fadeIn("slow");
		window.noticeshow = true;
		window.noticetimeout = window.setTimeout('window.noticetimeout = null; window.notice();', 5000);
	} else {
		$("#notice").fadeOut("slow");
		window.noticeshow = false;
	}
}

function localtime(elemid, utc) {
	function numtomonth(monthnum) {
		switch (monthnum) {
			case 0: return "January"; break;
			case 1: return "February"; break;
			case 2: return "March"; break;
			case 3: return "April"; break;
			case 4: return "May"; break;
			case 5: return "June"; break;
			case 6: return "July"; break;
			case 7: return "August"; break;
			case 8: return "September"; break;
			case 9: return "October"; break;
			case 10: return "November"; break;
			case 11: return "December"; break;
		}
	}
	
	function ampm(hour) {
		return (hour < 12 ? 'am' : 'pm');
	}
	
	function to12hr(hour) {
		var hr12 = hour % 12;
		if (hour == 0) hr12 = 12;
		return hr12;
	}
	
	d = new Date(utc);
	d += d.getTimezoneOffset() * 60 * 1000;
	$("#" + elemid).text(numtomonth(d.getMonth()) + ' ' + d.getDate() + ', ' + d.getFullYear + ' ' + two12hr(d.getHours) + ':' + d.getMinutes + ':' + d.getSeconds + ' ' + ampm(d.getHours));
}

function toggleads() {
	$(".ad").toggle();
}

function logincheck(idpostfix) {
	if (typeof idpostfix == 'undefined') idpostfix = '';
	var loginmessage = new Array();
	if ($("#un" + idpostfix).val().length == 0) loginmessage.push('The username is required');
	if ($("#pw" + idpostfix).val().length == 0) loginmessage.push('The password is required');
	var logintext = loginmessage.join('<br />');
	if (logintext.length == 0) {
		loading();
		$("#loginform" + idpostfix + " input").attr("disabled", "disabled");
		return true;
	} else {
		$("#loginmessage" + idpostfix).html("<span style='color: #f84'>" + loginmessage + "</span>");
		return false;
	}
}

function logindone(data, idpostfix) {
	if (typeof idpostfix == 'undefined') idpostfix = '';

	loading();
	$("#loginform" + idpostfix + " input").removeAttr("disabled");
	$("#loginmessage" + idpostfix).html(data.message);

	if (data.success) {
		var pattern = new RegExp('login', 'i');
		if (location.href.match(pattern)) location.href = webpath;
		else location.reload(true);
	}
}

var userdisplay = false;
function toggleuser() {
	if (!userdisplay)
		$("#user").fadeIn("slow");
	else $("#user").fadeOut("slow");
	
	userdisplay = !userdisplay;
}

function toggleusermenu(action) {
	if (action == 'show') $("#usermenu").show();
	else if (action == 'hide') $("#usermenu").hide();
	else if (action == 'toggle') $("#usermenu").toggle();
}

function fadereplace(imageid, newimageurl) { //Replaces an image via fadeout/fadein
	var img = new Image();
	img.src = newimageurl + "?randomquerystring=" + Math.round(Math.random() * 1000000);
	$("#" + imageid).fadeOut("slow", function () {
		$("#" + imageid).after(img).next().css("display", "none").css("vertical-align", "middle").fadeIn("slow");
		$("#" + imageid).attr("id", "oldpicture").next().attr("id", imageid);
		$("#oldpicture").remove();
	});
}

function logout() {
	function done(data, status) {
		if (data.success === true) {
			window.notice(data.message, '#090');
			var pattern = new RegExp('login', 'i');
			if (location.href.match(pattern)) location.href = webpath;
			else location.reload(true);
		} else {
			window.notice(data.message, '#900');
		}
		window.loading();
	}
	window.loading();
	$.post(window.webpath + "/ajax/logout.php", {}, done, "json");
}

$(document).ready(function() {
	$('#loginform').ajaxForm({
		beforeSubmit: function() { return logincheck(''); },
		dataType: 'json',
		success: function(data) { logindone(data, ''); }
	});
	$('#loginform2').ajaxForm({
		beforeSubmit: function() { return logincheck('2'); },
		dataType: 'json',
		success: function(data) { logindone(data, '2'); }
	});
	$("#user").fadeTo(0, 0.8);
	$("#usermenu").fadeTo(0, 0.8);
});
