var profNavCurrent = "info";
function profNavSwap(profNavNew, profQs) {
	if (profNavCurrent != "") {
		if ($("wProfNav" + profNavCurrent.charAt(0).toUpperCase() + profNavCurrent.substr(1).toLowerCase())) {
			$("wProfNav" + profNavCurrent.charAt(0).toUpperCase() + profNavCurrent.substr(1).toLowerCase()).className = "off";
		}
	}
	if ($("wProfNav" + profNavNew.charAt(0).toUpperCase() + profNavNew.substr(1).toLowerCase())) {
		$("wProfNav" + profNavNew.charAt(0).toUpperCase() + profNavNew.substr(1).toLowerCase()).className = "on";
	}
	$("wProfBody").innerHTML = "<div class=\"wait\"><img src=\"/images/wait_general.gif\" /></div>";
	new Ajax.Request("/data/prof_content.php?pid=" + profId + "&pp=" + profNavNew + "&" + profQs, {onSuccess: profNavDraw, evalScripts: true});
	profNavCurrent = profNavNew;
}

function profNavDraw(t) {
	$("wProfBody").innerHTML = t.responseText;
	
	if (profNavCurrent == "posts") {
		postLoadIntoFrame("postFrameList","ax=drawlist&cid=&uid=" + userId);
	} else if (profNavCurrent == "contact") {
		if (profMapReady == 1) {
			mapLoad();
			mapAddress(profMapAddress,profMapTag);
		} else {
			$("profLivemap").style.display = "none";
		}
	}
}

function profNavAutoload() {
	var tempLocation = location.hash;
	tempLocation = tempLocation.replace("?","");
	if (tempLocation != "" && tempLocation != "#") {
		tempLocation = tempLocation.replace("#","");
		if (tempLocation.indexOf("-") > -1) {
			tempLocationArray = tempLocation.split("-");
			profNavSwap(tempLocationArray[0], "");
			profEditLoad(tempLocationArray[1], "");
		} else {
			profNavSwap(tempLocation, "");
		}
	} else {
		profNavSwap("info", "");
	}
}

function profUpdatesShow() {
	$("updatesControl").style.display = "none";
	new Effect.BlindDown("updatesMore", {queue: {position: "end", scope: "drawer"}, duration: 0.5});
}

function profEditHover (profSegment, profMode) {
	if (profMode == 1) {
		$(profSegment+"Editbox").style.display = "block";
	} else {
		$(profSegment+"Editbox").style.display = "none";
	}
}

var profEditStatus = 0;

function profEditLoad (profSegment, profQs) {
	new Ajax.Request("/data/prof_edit.php?pid=" + profId + "&ps=" + profSegment + "&pa=edit&" + profQs, {onSuccess: profEditDraw});
}

function profEditDraw (t) {
	$("wPane").innerHTML = t.responseText;
	if (BrowserDetect.browser == "Explorer") {
		$("wPane").style.display = "block";
	} else {
		new Effect.Appear("wPane", {queue: {position: "end", scope: "pane"}, duration: 0.25});
	}
	profEditStatus = 0;
}

function profEditProcess (profSegment) {
	new Ajax.Request("/data/prof_edit.php", {onSuccess: profEditProcessOut, parameters: Form.serialize(document.profEdit)});
}

function profEditProcessOut (t) {
	profEditStatus = 0;
	profEditCancel();
	tmpMessage = t.responseText;
	if (tmpMessage == "//reload//") {
		window.location.reload(false);
	} else {
		alertBubbleAdd(tmpMessage);
		profNavSwap(profNavCurrent, "");
	}
}

function profEditChanged () {
	profEditStatus = 1;
}

function profEditCancel () {
	if (profEditStatus == 1) {
		if (window.confirm("By canceling without saving, your changes will be lost. If you are okay with this, please click OK. To return to your edits, click Cancel.")) {
			new Effect.Fade("wPane", {queue: {position: "end", scope: "pane"}, duration: 0.25});
			profEditStatus = 0;
		}
	} else {
		new Effect.Fade("wPane", {queue: {position: "end", scope: "pane"}, duration: 0.25});
	}
}

function profEditWyntkCount() {
	tmpValue = document.profEdit.profEditWyntk.value;
	tmpLength = tmpValue.length;
	tmpLength = 140 - tmpLength;
	if (tmpLength < 0) {
		tmpValue = tmpValue.substring(0,140);
		tmpLength = 0;
		document.profEdit.profEditWyntk.value = tmpValue;
	}
	if (tmpLength == 1) {
		tmpOutput = "1 character remaining.";
	} else {
		tmpOutput = tmpLength + " characters remaining.";
	}
	if (tmpLength <= 10) {
		tmpOutput = "<span style=\"color:#f00;\">" + tmpOutput + "</span>";
	}
	$("profEditWyntkCount").innerHTML = tmpOutput;
}

function profEditPop (profUrl) {
	window.open(profUrl, 'mu', 'height=600,width=700');
}

var profFollowId = 0;
function profFollow(profId) {
	profFollowId = profId;
	new Ajax.Request("/data/prof_follow.php?pid=" + profId, {onSuccess: profFollowDraw});
}
function profFollowDraw(t) {
	tmpResponse = t.responseText;
	if (tmpResponse == "//followed//") {
		$("followTrigger").innerHTML = "<a href=\"#\" onclick=\"profFollow(" + profFollowId + ");return false;\"><img src=\"/images/icon_remove.png\" /></a> <a href=\"#\" onclick=\"profFollow(" + profFollowId + ");return false;\">Stop following</a>";
		alertBubbleAdd("You are now following this professional.");
	} else {
		$("followTrigger").innerHTML = "<a href=\"#\" onclick=\"profFollow(" + profFollowId + ");return false;\"><img src=\"/images/icon_follow.png\" /></a> <a href=\"#\" onclick=\"profFollow(" + profFollowId + ");return false;\">Follow this professional</a>";
		alertBubbleAdd("You are no longer following this professional.");
	}
}

var map = null;
var geocoder = null;

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("profLivemap"));
		
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
	}
}

function mapAddress(address,mark) {
	if (geocoder) {
		geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					alert(address + " cannot be found.");
				} else {
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					marker.openInfoWindowHtml(mark);
				}
			}
		);
	}
}

function mapDrivingDirections() {
	var destination = profMapAddress;
	saddr = prompt("Enter your zip code to view driving directions.","");
	destination = destination.replace(" ","+");
	window.open("http://maps.google.com/maps?saddr=" + saddr + "&daddr=" + destination, "googleMap");
}