var globalSiteURL = 'http://forum.aw8.net/';
var AJAXfile = 'f7';
var loadingImg = '<img src="http://oxygen.aw8.net/v3-images/loading.gif" align="absmiddle" border="0" />';
var currentScroll = 0;
var defaultDocTitle = 'awForum . v75 . chat angelically';

// for front page
var frontPage = '';

function backtoPosition() {
	pageScroll(0,currentScroll);
	// clear any secret value reservoir
	if ($('threadReservoir')) {
		$('threadReservoir').update('');
	}
}

function postLabel(action) {
	showIt('postitArea');
	setPosition('postit_L','postIt',-105,-5);
	$("postIt").update('<img src="http://oxygen.aw8.net/v3-images/postit.png" width="72" height="162" />');
	if ($('threadReservoir')) {
		if ($('threadReservoir').innerHTML!=='') {
			if ($('postitArea').style.display=='none' || action=='on') {
				new Effect.Appear($('postIt'),{duration:0.3});
			} else {
				hideIt('postitArea');
				new Effect.Fade($('postIt'),{duration:0.2});
			}
		} else {
			alert('You cannot mark this thread.');
		}
	} else {
		alert('You have not opened any thread yet.');
	}
}

function addPostIt() {
	if ($('postitArea').style.display=='none') {
		useTool('markUnread');
		postLabel('on');
	} else {
		useTool('markRead');
		postLabel('off');
	};
}

function addThreadMark() {
	if ($('threadReservoir')) {
		if ($('threadReservoir').innerHTML!=='') {
			useTool('addMark');
		} else {
			alert('Please choose and open a thread first.');
		}
	} else {
		alert('Please choose and open a thread first.');
	}
}

function setSearch(searchArea,target,thisColor) {
	$('SSPS_post').style.color = '';
	$('SSPS_member').style.color = '';
	$('SSPS_address').style.color = '';
	$('SSPS_bookmark').style.color = '';
	$(target).style.color = thisColor;
	document.searchForm.searchArea.value = searchArea;
	$('searchTitle').update('Search for '+searchArea);
}

// listing and setting actions
// global functions
var page = 1;

// for oxygen and setting pages...
function showColor(thisBox,colorValue) {
	var targetBox = $(thisBox);
	var targetColor = $(colorValue);
	if (targetColor.value.length==6) {
		targetBox.style.backgroundColor = targetColor.value;
	}
}
// search fields
function checkSearchField() {
	if (document.searchForm.searchArea.value=='') {
		alert('Please choose what you want to search for first in the list above the input.');
		return false;
	} else {
		return true;
	}
}

var goToforum = '';
// used when the user posts, and then goes back to the forum list
// and this will force reload the page...
var forumUpdated='false';

function backToList(forumname) {
	openArea("threadListArea",forumname);
	$("threadInfo").hide();
	backtoPosition();
	History.set('fh', 's_'+forumname);
	if ($("forumTitle")) {
		document.title = defaultDocTitle+' . '+$("forumTitle").innerHTML;
	}
}

function openArea(thatArea,forumName) {
	hideIt('threadListArea');
	hideIt('threadMain');
	hideIt('composeNew');
	hideIt('postIt');
	popOutBox('pBox1');
	popOutBox('pBox2');
	if (thatArea=='threadListArea') {
		var thisThreadForum;
		showIt(thatArea);
		hideIt('adminTools');
		// clear the whole thread area
		$("threadMain").update();
		// save forum name in JS
		if ($("threadForum")) {
			thisThreadForum = $("threadForum").innerHTML;
		}
		if (forumUpdated=='true' || thisThreadForum!==goToforum) {
			if (forumName) {
				goToforum = forumName;
				getForum(forumName);
			}
		}
		if ($("threadForumName")) {
			document.title = defaultDocTitle+' . '+$("threadForumName").innerHTML;
		} else {
			document.title = defaultDocTitle;
		};
	} else {
		showIt(thatArea);
	}
}

function getForum(forumname,pageNum) {
	var loadContents;
	if (forumname=='prev') {
		if (page>1) {
			page = parseFloat(page)-1;
		} else {
			page = 1;
		}
	} else if (forumname=='next') {
		if (page>=pageNum) {
			page = 1;
		} else {
			page = parseFloat(page)+1;
		}
	} else {
		if (pageNum) {
			page = pageNum;
		} else {
			page = forumname;
		}
	}
	// for loading a new forum group
	if (pageNum=='1') {
		goToforum = forumname;
	}
	// for loading a new group or just to turn pages
	if (goToforum!=='') {
		loadContents = goToforum+'_'+page;
	} else {
		loadContents = page;
	};
	new Ajax.History.Request(globalSiteURL+'f7-ajax-forum.php', {
		method: 'GET',
		parameters: { a: loadContents },
		history: {
			id: 'fh',
			state: 's_'+forumname,
			cache : true,
			onStateChange: function(state) {
			}
		},
		onCreate: function() {
			$("loadingLine").show();
			$("loadingLine").update('Retrieving forum thread list, please wait...');
			popOutBox('pBox1'); popOutBox('pBox2');
		},
		onSuccess: function(transport) {
			$("loadingLine").hide();
			openArea('threadListArea');
			$("threadListContents").update(transport.responseText);
			fadeListItems($("fadeItems").innerHTML);
			if ($("forumInfoHTML")) {
				$("forumInfo").update($("forumInfoHTML").innerHTML);
				$("forumInfo").show();
				$("forumInfoHTML").update();
			}
			document.title = defaultDocTitle+' . '+$("forumTitle").innerHTML;
			// restore the forum update check due to reply posting...
			forumUpdated = 'false';
		}
	});
}
function checkList(folder) {
	if ($("threadListContents").innerHTML=='') {
		getForum(folder,1);
	}
}

var statusID = '';
var nestID_g = ''; // for scrolling in getThread function
var loadFrom = '';
var compactMode = '';

function checkPostit() {
	if ($("postItCommand")) {
		if ($("postItCommand").innerHTML=='true') {
			if ($("postItCommandColor").innerHTML=='yellow') {
				$("postIt").update('<img src="http://oxygen.aw8.net/v3-images/postit-yellow.png" width="72" height="162" />');
			} else {
				$("postIt").update('<img src="http://oxygen.aw8.net/v3-images/postit.png" width="72" height="162" />');
			}
			showIt("postitArea");
			setPosition('postit_L','postIt',-105,-5);
			new Effect.Appear($('postIt'),{duration:0.3});
		}
	}
}

function showForumInfo() {
	// this is for getThread only...
	// forum list one has its own script to show forum info
	if ($("forumInfoHTML_T")) {
		$("forumInfo").update($("forumInfoHTML_T").innerHTML);
		$("forumInfo").show();
		$("forumInfoHTML_T").update('');
	}
}

function showThread(thisText) {
	$("postIt").update('');
	$("threadMain").update(thisText);
	showForumInfo();
	// update document title
	if ($("threadTitle")) {
		document.title = defaultDocTitle+' . '+$("threadTitle").innerHTML;
	}
	// main part
	if ($("globalNotice")) {
		$('globalNotice').hide();
	};
	if (loadFrom=='fromThread') {
		if (compactMode=='true') {
			// loading compact mode, do nothing but scroll to top
			jumpToPageTop();
		}
		openArea('threadMain');
	} else if (loadFrom=='fromStretch') {
		openArea('threadMain');
	} else if (loadFrom=='fromOthers') {
		openArea('threadMain'); 
	} else {
		hideIt(statusLine2); showIt(statusLine1);
		openArea('threadMain');
		// record the position first
		if (window.pageYOffset!=='undefined') {
			currentScroll = window.pageYOffset;
		};
		// mark up that box
		itemOn('thread_'+statusID+'_box');
	};
	if (compactMode!=='true') {
		checkPostit();
	};
	if (nestID_g!=='') {
		jumpToAnchor('threadMid');
	} else {
		jumpToPageTop();
	};
	initLightbox();
	nestID_g = ''; // clear it
	compactMode = '';
	loadFrom = '';
	if ($("currentForum")) {
		if ($("currentForum").innerHTML!=='') {
			goToforum = $("currentForum").innerHTML;
		};
	};
	expandInput;
}
function getThread(forum,threadID,nestID,thisFrom,thisCompactMode) {
	hideIt('postIt');
	popOutBox('pBox1');
	popOutBox('pBox2');
	if (threadID=='') {
		alert('You have reached the end of the listing, or the thread number is invalid. Please try again.');
	} else {
		statusID = threadID;
		nestID_g = nestID;
		statusLine1 = 'status1_'+statusID;
		statusLine2 = 'status2_'+statusID;
		compactMode = thisCompactMode;
		if (thisFrom=='fromThread') {
			loadFrom = 'fromThread';
			if ($("threadMain")) {
				new Effect.Fade("threadMain", { duration:0,to:0.35 });
			};
		} else if (thisFrom=='false' || thisFrom=='fromStretch') {
			loadFrom = 'fromStretch';
		} else if (thisFrom=='fromOthers') {
			loadFrom = 'fromOthers';
		} else if (thisFrom=='fromSticky') {
			loadFrom = 'fromSticky';
		} else {
			loadFrom = 'fromList';
		}
		new Ajax.History.Request(globalSiteURL+'f7-ajax-thread.php', {
			method: 'GET',
			parameters: {
				forum: forum,
				id: threadID,
				nestID: nestID,
				compact: compactMode
			},
			 history: {
				 id: 'fh',
				 state: 's_'+forum+'.'+threadID,
				 cache : true,
				 onStateChange: function(state) {
				 }
			 },
			onCreate: function() {
				popOutBox('pBox1'); popOutBox('pBox2');
				if (loadFrom=='fromStretch') {
					$("composeStatus").update('Retrieving your newly posted thread...');
					jumpToPageTop();
				} else if (loadFrom=='fromThread') {
					showIt("hoverLoading");
				} else if (loadFrom=='fromOthers') {
					showIt('globalNotice'); hideIt('threadMain'); hideIt('threadListArea'); hideIt('composeNew');
					jumpToPageTop();
				} else if (loadFrom=='fromList') {
					hideIt(statusLine1); showIt(statusLine2);
					$(statusLine2).innerHTML = 'Retrieving thread data...';
					jumpToPageTop();
				} else if (thisFrom=='fromSticky') {
					$("sticky_"+forum+"_"+threadID).update('Opening...');
				};
			},
			onLoading: function() {
				if (loadFrom=='fromList') {
					$(statusLine2).innerHTML = 'Rebuilding data outline...';
				}
			},
			onSuccess: function(transport) {
				if (loadFrom=='fromThread') {
					if ($("threadMain")) {
						new Effect.Appear("threadMain", { duration:0 });
					};
				};
				hideIt("hoverLoading");
				showThread(transport.responseText);
			}
		});
		if ($("uS_"+forum+threadID)) {
			if ($("uS_"+forum+threadID).className!=='through') {
				$("uS_"+forum+threadID).className = 'through';
				updateUnreadList();
			};
		};
	}
}
function updateUnreadList() {
	var currentUnread = checkThroughItems("unreadBoxList");
	if (currentUnread==0 || currentUnread=='') {
		$("unread_notiID").hide();
	} else {
		$("unread_notiID").update(currentUnread);
	};
}

// for top control buttons
function upButton() {
	if ($("threadForum")) {
		backToList($('threadForum').innerHTML);
	} else if (goToforum!=='') {
		getForum(goToforum,1);
	}
}
function leftButton(action) {
	if (action=='click') {
		if ($("threadReservoir")) {
			if ($("threadReservoir").innerHTML!=='' && $("threadForum").innerHTML!=='') {
				// when it is currently reading a thread
				getThread($("threadForum").innerHTML,$("threadPrevID").innerHTML,'','fromThread');
			};
		} else if (goToforum!=='') {
			// when it is currently viewing the forum
			getForum('prev');
		};
	} else if (action=='mouseOver') {
		if ($("threadReservoir")) {
			// when it is currently reading a thread
			popUpBox('popupBox1','aPrev',$("threadPrevSubject").innerHTML,'popupBox2');
		} else if (goToforum!=='') {
			// when it is currently viewing the forum
			popUpBox('popupBox1','aPrev','Previous Page','popupBox2');
		};
	} else if (action=='mouseOut') {
		popOutBox('popupBox1');
	};
}
function rightButton(action) {
	if (action=='click') {
		if ($("threadReservoir")) {
			// when it is currently reading a thread
			getThread($("threadForum").innerHTML,$("threadNextID").innerHTML,'','fromThread');
		} else if (goToforum!=='') {
			// when it is currently viewing the forum
			getForum('next');
		};
	} else if (action=='mouseOver') {
		if ($("threadReservoir")) {
			if ($("threadReservoir").innerHTML!=='') {
				// when it is currently reading a thread
				popUpBox('popupBox2','aNext',$("threadNextSubject").innerHTML,'popupBox1');
			};
		} else if (goToforum!=='') {
			// when it is currently viewing the forum
			popUpBox('popupBox2','aNext','Next Page','popupBox1');
		};
	} else if (action=='mouseOut') {
		popOutBox('popupBox2');
	};
}

// expand replies
function expandThread(forum,threadID) {
	if (threadID=='') {
		alert('There is no such a thread for expanding.');
	} else {
		new Ajax.Request(globalSiteURL+'f7-ajax-thread-expand.php', {
			method: 'GET',
			parameters: {
				forum: forum,
				id: threadID
			},
			onCreate: function() {
				$("expandArea").update('<div align="center" style="margin:0 10px;">Retrieving the hidden stack of posts...</div>');
			},
			onSuccess: function(transport) {
				hideIt("expandArea");
				$("expandArea").update(transport.responseText);
				new Effect.Appear("expandArea",{duration: 0.5});
				hideIt("expandButton");
				initLightbox();
			}
		});
	}
}

// for expanding nested comments
function expandNested(thisForum,threadID,nestID) {
	var entryPt = 'newEntryPoint_'+nestID;
	var processPt = 'newEntryProcess_'+nestID;
	var expandButton = 'expandButton_'+nestID;
	new Ajax.Request(globalSiteURL+"f7-ajax-forum-expandNested.php", {
		method: 'GET',
		parameters: {
			forum: thisForum,
			threadID: threadID,
			nestID: nestID
		},
		onCreate: function() {
			showIt(processPt);
		},
		onSuccess: function(transport) {
			$(entryPt).hide();
			$(processPt).hide();
			changeStyle(expandButton,''); // change it back to no stack image
			var newbox=document.createElement("div");
			$(entryPt).appendChild(newbox);
			$(newbox).update(transport.responseText);
			new Effect.Appear(entryPt,{duration: 0.5});
			initLightbox();
			entryPt = ''; processPt = ''; expandButton = ''; shadeBottom = '';
		}
	});
}

function killStar(thisStar) {
	$(thisStar).innerHTML = '';
}

function getComposeBox(forum) {
	$("composeNew").innerHTML = '';	
	openArea('composeNew'); showIt('composeNewLoading');
	// clear the thread area to avoid conflicts
	$('threadMain').innerHTML = '';
	if (forum=='this_forum') {
		forum = goToforum;
	}
	new Ajax.Request(globalSiteURL+'f7-ajax-compose.php', {
		method: 'GET',
		parameters: { forum: forum },
		onCreate: function() {
			showIt('composeNewLoading');
		},
		onSuccess: function(transport) {
			hideIt('threadMain'); hideIt('threadListArea'); hideIt('composeNewLoading'); hideIt('composeNew');
			$("composeNew").update(transport.responseText);
			new Effect.Appear("composeNew",{duration: 0.5});
			jumpToPageTop();
		}
	});
}
function useTool(action,values) {
	if (action=='snapBack') {
		var thisAgree = confirm('Are you sure to apply snapBack and go back to the past? This will reveal back any read threads!');
	} else {
		var thisAgree = 'true';
	}
	if (thisAgree) {
		if (action=='snapBack') {
			new Ajax.Request(globalSiteURL+'f7-ajax-tool.php', {
				method: 'POST',
				parameters: {
					action: action,
					values: $('snapBackMenu').value
				},
				onCreate: function() {
					showIt("processProgress");
					$("processProgress").update('Processing...');
				},
				onSuccess: function(transport) {
					alert(transport.responseText);
					hideIt("processProgress");
					$("processProgress").update('');
				}
			});
		} else {
			new Ajax.Request(globalSiteURL+'f7-ajax-tool.php', {
				method: 'POST',
				parameters: {
					action: action,
					values: $('threadReservoir').innerHTML
				},
				onCreate: function() {
					if (action=='addMark') {
						new Effect.Fade('tmBox',{duration:0,to:0.3});
					};
				},
				onSuccess: function(transport) {
					if (action=='addMark') {
						new Effect.Appear('tmBox',{duration:0});
						$("tmBox").update(transport.responseText);
					};
				}
			});
		}
	}
}
function processThread(thisLocation,inForum) {
	new Ajax.Request(globalSiteURL+'f7-ajax-processThread.php', {
		method: 'GET',
		parameters: {
			location: thisLocation,
			inForum: inForum
		},
		onCreate: function() {
			if (thisLocation=='submenu') {
				$("unreadFunction").hide();
				$("unreadProcess").update('<span class="red">Processing...</span>');
			} else {
				$("processProgress").update('Processing...');
			};
		},
		onSuccess: function(transport) {
			if (thisLocation=='submenu') {
				$("unreadFunction").show();
				$("unreadProcess").hide();
				if (transport.responseText=='error') {
					$("unreadProcess").update('Error in processing, please try again.');
				} else {
					$("unreadBoxList").update(transport.responseText);
				};
			} else {
				$("processProgress").update(transport.responseText);
			};
		}
	});
}

function markLogin(location) {
	var markConfirm = confirm('This will mark current time as your last logged time, and will clean all unread threads except those in your stack. Are you sure to continue?');
	if (markConfirm) {
		new Ajax.Request(globalSiteURL+'f7-ajax-markLogin.php', {
			method: 'GET',
			onCreate: function() {
				if (location=='submenu') {
					hideIt("unreadFunction");
					$("unreadProcess").update('<span class="red">Processing...</span>');
				} else {
					$("processProgress").update('Processing...');
				};
			},
			onSuccess: function(transport) {
				if (location=='submenu') {
					showIt("unreadFunction");
					$("unreadProcess").update(transport.responseText);
				} else {
					$("processProgress").update(transport.responseText);
				};
			}
		});
	};
}

function getGBentry(owner,thisID) {
	jumpToAnchor('gbTop');
	hideIt("actionPanel");
	hideIt("composeboxList");
	$("GBThread").update('<div style="margin:30px;" class="contents red" align="center">Processing request...</div>');
	new Ajax.Request(globalSiteURL+'f7-ajax-member-gb.php', {
		method: 'GET',
		parameters: {
			getOwner: owner,
			getID: thisID
		},
		onCreate: function() {
			if (window.pageYOffset!=='undefined') {
				currentScroll = window.pageYOffset;
			};
			$("GBThread").update('<div style="margin:30px;" class="contents red" align="center">Retrieving threaded guestbook entry...</div>');
			new Effect.Fade('GBListArea',{duration:0.1,from:1,to:0.3});
		},
		onSuccess: function(transport) {
			hideIt("GBListArea");
			showIt("GBThread");
			$("GBThread").update(transport.responseText);
			setFormValue(document.memberGB.nestID,thisID);
		}
	});
}

function getMoreGBentry(owner,page) {
	new Ajax.Request(globalSiteURL+'f7-ajax-member-gb.php', {
		method: 'GET',
		parameters: {
			getOwner: owner,
			page: page
		},
		onCreate: function() {
			$("loadingMore").update('<span class="red">Getting older posts...</span>');
		},
		onSuccess: function(transport) {
			$('getMore_'+page).update(transport.responseText);
		}
	});
}

function getThreadmarks(thisForum,pageNum) {
	if (pageNum) {
		page = pageNum;
		forumName = thisForum;
	} else {
		page = thisForum;
		forumName = '';
	}
	if (forumName=='') {
		getLine = page;
	} else {
		getLine = forumName+'_'+page;
	};
	new Ajax.Request(globalSiteURL+'f7-ajax-threadmarker.php', {
		method: 'GET',
		parameters: { a: getLine },
		onCreate: function() {
			$("introText").update(loadingImg+' <strong class="red">Constructing list...</strong>');
		},
		onSuccess: function(transport) {
			$("threadListContents").update(transport.responseText);
			$("contentTitleSide").update($("pageButtonHTML").innerHTML);
			$("pageButtonHTML").update(''); // clean it to avoid duplications of IDs for popup messages
			fadeListItems($("fadeItems").innerHTML);
		}
	});
}

function getRSS() {
	if (goToforum!=='') {
		window.location = 'http://forum.aw8.net/awForum.xml?'+goToforum;
	} else {
		window.location = 'http://forum.aw8.net/awForum.xml';
	}
}

function emailThread() {
	if (emailCheck(document.searchBar.searchField.value)) {
		if (document.searchBar.searchField.value=='') {
			return prompt("Please enter your name first:");
		}
		new Ajax.Request(globalSiteURL+'f7-ajax-emailthread.php', {
			method: 'POST',
			parameters: {
				emailAddress: document.searchBar.searchField.value,
				forum: document.searchBar.searchForum.value,
				threadID: document.searchBar.threadID.value
			},
			onCreate: function() {
				$("ThreadSearchProgress").update('Emailing the thread to the person you wrote, please wait...');
			},
			onSuccess: function(transport) {
				if (transport.responseText=='done') {
					$("ThreadSearchProgress").update('Success!');
				} else {
					alert(transport.responseText);
					$("ThreadSearchProgress").update('Error. Please try again.');
				}
			}
		});
	}
}
		
function checkSearch() {
	if (document.searchBar.original.value==document.searchBar.searchField.value) {
		alert("Please enter at least one keyword to search.");
		return false;
	} else {
		if (document.searchBar.action.value=='search') {
			return true;
		} else {
			if (emailCheck(document.searchBar.searchField.value)) {
				emailThread();
				return false;
			} else {
				return true;
			}
		}
	}
}

// set thread sorting
function setThreadSort(thisValue) {
	new Ajax.Request(globalSiteURL+'f7-ajax-tool.php', {
		method: 'POST',
		parameters: {
			action: 'setThreadSort',
			values: thisValue
		},
		onSuccess: function(transport) {
			if (transport.responseText=='done') {
				$("setThreadSortProgress").update('Setting saved.');
			} else {
				alert(transport.responseText);
			};
		}
	});
}

// attachment
function loadAttachment(thisFile,targetArea) {
	new Ajax.Updater(targetArea, globalSiteURL+'f7-ajax-attachment.php', {
		method: 'GET',
		parameters: {
			attachFile: thisFile
		},
		onCreate: function() {
			$(targetArea).update('<span class="red">Retrieving image...</span>');
		}
	});
}

// for sending smilies to shoutbox
function sendToShoutbox(word) {
	originalWord = $('shoutLine').value;
	if (originalWord=='') {
		newWord = word;
	} else {
		newWord = originalWord+' '+word;
	}
	$('shoutLine').value = newWord;
	$('shoutLine').focus();
}

// set front page
function changeSetting(thisSetting,thisValue) {
	new Ajax.Request(globalSiteURL+'f7-ajax-settings.php', {
		method: 'POST',
		parameters: {
			thisSetting: thisSetting,
			thisValue: thisValue
		},
		onCreate: function() {
			$("frontSetting").update('Saving...');
		},
		onSuccess: function(transport) {
			if (transport.responseText=='done') {
				$("frontSetting").update('Setting saved.');
				window.setTimeout("Effect.Fade('frontSetting')",5000);
				$("saveFrontPage").checked = false;
			} else {
				showIt('frontSetting');
				$("frontSetting").update('<acronym title="'+ajaxReq.responseText+'">Cannot save your preference.</acronym>');
			}
		}
	});
}

// hide smilies in the thread list
var threadSmilies = 'on';
function toggleSmilies() {	var theseDIVs = document.getElementsByClassName('threadSmilie');
	for (var i=0; i<theseDIVs.length; i++){
		if (threadSmilies=='on') {
			theseDIVs[i].style.display = 'none';
		} else {
			theseDIVs[i].style.display = '';
		};
	}
	if (threadSmilies=='on') {
		threadSmilies = 'off';
	} else {
		threadSmilies = 'on';
	};
}

// for forum info menu - FACBEOOK LINKAGE
function fbs_click() {
	u=location.href;
	t=document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}