var active = 0;
var hidden = new Array();
var v = "null";
var oper2Array = new Array("null");
var tagsorterinnerheight = 0;
var tagsorteractive = 0;
var terms = 1;
var commentsorterinnerheights = new Array();
var commBoxArray = new Array();
var numReferences = 0;
var cooldown = 0;
function oper2(fac, id, height, restore, hide, release){
	if (active == 0){
/*		alert("Scaling procedure inactive, proceeding");*/
		for(i=0;i<oper2Array.length;i++){
/*			alert("Performing search for "+id+"! Current loop of index: "+i+", with id: "+oper2Array[i]);*/
			if(oper2Array[i]==id){
/*				alert(id+" found at index "+i+"! => Contracting: "+oper2Array[i]+" ("+id+") to a height of "+restore+" pixels");*/
				if (active == 0){
					oper2Array.splice(i, 1);
					for (j = 0; j<101; j++){
						setTimeout('compress('+'\''+id+'\''+','+j+','+height+','+restore+');',(2.5*fac*j));
					}
					active = 1;
					setTimeout('active = 0;expanded = 0', 250)
					if (hide >= 1) {
/*						alert("The object will be hidden after it has contracted to "+restore+" pixels");*/
						setTimeout("document.getElementById(\'"+id+"\').style.display = 'none';", 250);
					}
					return;
				}
			}else{
				if(i==(oper2Array.length-1)){
/*					alert("Entire array searched, "+id+" not found => Expanding: ("+id+") to a height of "+height+" pixels");*/
					oper2Array.push(id);
					for (j = 0; j < 101; j++) {
						setTimeout('expand('+'\''+id+'\''+','+j+','+height+','+restore+');',(2.5*fac*j));
					}
					active = 1;
					setTimeout('active = 0;expanded = 1', 250)
					document.getElementById(id).style.borderColor = "#CCCCCC";
					if (hide >= 1) {
						document.getElementById(id).style.display = 'block';
					}
					if (release >=1){
						setTimeout("document.getElementById(\'"+id+"\').style.height = 'auto';",250);
					}
					return;
				}
			}
		}
	}
}
function compress(id, j, height, restore){
	document.getElementById(id).style.height=(((height-restore)*(1-(j/100)))+restore)+'px';
}
function expand(id, j, height, restore){
	document.getElementById(id).style.height=(((height-restore)*((j/100)))+restore)+'px';
}
function showcaption(id){
	hidden = id;
	setTimeout('echohidden(\''+id+'\');', 250)
}
function hidecaption(id){
	document.getElementById(id).style.display='none';
	hidden = "null";
}
function echohidden(id){
	if(hidden==id){
			document.getElementById(id).style.display='block';
	}
}
function toggleLoginForm(action){
	if(navigator.userAgent.indexOf("MSIE 6.0") >= 0){
		document.getElementById("black").style.position="absolute";
		document.getElementById("black").style.height=document.body.clientHeight;
	}
	document.getElementById('black').style.display=action;
	document.getElementById('loginform').style.display=action;
	document.getElementById('loginform').style.left=((document.body.clientWidth/2)-320)+'px';
	if(action=="block"){
		window.scrollTo(0, 0);
	}
}
function toggleSignupForm(action){
	if(navigator.userAgent.indexOf("MSIE 6.0") >= 0){
		document.getElementById("black").style.position="absolute";
		document.getElementById("black").style.height=document.body.clientHeight;
	}
	document.getElementById('black').style.display=action;
	document.getElementById('RegForm').style.display=action;
	document.getElementById('RegForm').style.left=((document.body.clientWidth/2)-320)+'px';
	if(action=="block"){
		termsFrame.document.body.style.backgroundColor='#FFC8C8';
		document.getElementById('terms').style.outlineColor='#FF0000';
		document.getElementById('termsAccept').checked=0;
		window.scrollTo(0, 0);
	}
	document.getElementById('birthyear').value = "1970";
}
function menuEffects(){
	var menu2 = document.getElementById('menu2');
	if(document.getElementById('button1')){
		var button1 = document.getElementById('button1');
		var button1li = button1.getElementsByTagName('li');
	}
	var menu2li = menu2.getElementsByTagName('li');
	var ccwiki2 = document.getElementById('ccwiki');
	if(document.getElementById('header').addEventListener){
		if(document.getElementById('communityAnswer')){
			document.getElementById('communityAnswer').addEventListener('mouseover',function(){showcaption('communityAnswerOver');},false);
			document.getElementById('communityAnswer').addEventListener('mouseout',function(){hidecaption('communityAnswerOver');},false);
		}
		if(document.getElementById('communityQuestion')){
			document.getElementById('communityQuestion').addEventListener('mouseover',function(){showcaption('communityQuestionOver');},false);
			document.getElementById('communityQuestion').addEventListener('mouseout',function(){hidecaption('communityQuestionOver');},false);
		}
		for (i=0; i<menu2li.length; i++) {
			menu2li[i].addEventListener('click',function(){
				if(document.getElementById('activetab2')){
					document.getElementById('activetab2').id='null';
				}
			this.id='activetab2';
			},true);
		}
		if(document.getElementById('button1')){
			for (i=0; i<button1li.length; i++) {
				button1li[i].addEventListener('click',function(){
					if(document.getElementById('activetab')){
						document.getElementById('activetab').id='null';
					}
				this.id='activetab';
				},true);
			}
			var firefoxHideCaps = function(num, hx){
				num.addEventListener('mouseout',function(){
														 hidecaption('captionbuttonmenu'+hx);
														 },false);
			}
			var firefoxShowCaps = function(num, sx){
				num.addEventListener('mouseover',function(){
														  showcaption('captionbuttonmenu'+sx);
														  },false);
			}
			for (var i=0; i<button1li.length; i++) {
				i2=(button1li.length-i);
				firefoxShowCaps(button1li[i], i2);
				firefoxHideCaps(button1li[i], i2);
			}
		}
		if(document.getElementById('article') || document.getElementById('article2') || document.getElementById('favourite1')){
			var firefoxArticleRank = function(num){
				document.getElementById('stars'+(num+1)).addEventListener('mouseover',function(){showcaption('scores'+(num+1));},false);
				document.getElementById('stars'+(num+1)).addEventListener('mouseout',function(){hidecaption('scores'+(num+1));},false);
			}
			var firefoxCommunityMouseover = function(num){
				document.getElementById('communityMode'+(num+1)).addEventListener('mouseover',function(){showcaption('communityModeOver'+(num+1));},false);
				document.getElementById('communityMode'+(num+1)).addEventListener('mouseout',function(){hidecaption('communityModeOver'+(num+1));},false);
			}
			for(i=0;i<24;i++){
				if(document.getElementById('article'+(i+1)) || document.getElementById('favourite'+(i+1))){
					firefoxArticleRank(i);
				}
				if(document.getElementById('communityMode'+(i+1))){
					firefoxCommunityMouseover(i);
				}
			}
		}
		ccwiki2.addEventListener('mouseover',function(){showcaption('captioncc1')}, false);
		ccwiki2.addEventListener('mouseout',function(){hidecaption('captioncc1')}, false);
	}else if(document.getElementById('header').attachEvent){
		if(document.getElementById('communityAnswer')){
			document.getElementById('communityAnswer').onmouseover = function(){
				showcaption('communityAnswerOver');
			};
			document.getElementById('communityAnswer').onmouseout = function(){
				hidecaption('communityAnswerOver');
			};
		}
		if(document.getElementById('communityQuestion')){
			document.getElementById('communityQuestion').onmouseover = function(){
				showcaption('communityQuestionOver');
			};
			document.getElementById('communityQuestion').onmouseout = function(){
				hidecaption('communityQuestionOver');
			};
		}
		for (i=0; i<menu2li.length; i++) {
			menu2li[i].onclick=function(){if(document.getElementById('activetab2')){document.getElementById('activetab2').id='null'};this.id='activetab2'};
		}
		function ieHideCaps(num, hx){
				num.onmouseout=function(){
					hidecaption('captionbuttonmenu'+hx);
			}
		}
		function ieShowCaps(num, sx){
				num.onmouseover=function(){
					showcaption('captionbuttonmenu'+sx);
			}
		}
		if(document.getElementById('button1')){
			for (i=0; i<button1li.length; i++) {
				button1li[i].onclick=function(){if(document.getElementById('activetab')){document.getElementById('activetab').id='null'};this.id='activetab'};
				i2=(button1li.length-i);
				ieShowCaps(button1li[i], i2);
				ieHideCaps(button1li[i], i2);
			}
		}
		if(document.getElementById('article') || document.getElementById('article2') || document.getElementById('favourite1')){
				var ieArticleRank = function(num){
					document.getElementById('stars'+(num+1)).onmouseover = function(){
						showcaption('scores'+(num+1));
					}
					document.getElementById('stars'+(num+1)).onmouseout = function(){
						hidecaption('scores'+(num+1));
					}
				}
				var ieCommunityMouseover = function(num){
					document.getElementById('stars'+(num+1)).onmouseover = function(){
						showcaption('communityModeOver'+(num+1));
					}
					document.getElementById('stars'+(num+1)).onmouseover = function(){
						hidecaption('communityModeOver'+(num+1));
					}
					document.getElementById('communityMode'+(num+1)).addEventListener('mouseout',function(){hidecaption('communityModeOver'+(num+1));},false);
				}
			for(i=0;i<24;i++){
				if(document.getElementById('article'+(i+1))){
					ieArticleRank(i);
				}
				if(document.getElementById('communityMode'+(i+1))){
					ieCommunityMouseover(i);
				}
			}
		}
		ccwiki2.onmouseover=function(){showcaption('captioncc1');};
		ccwiki2.onmouseout=function(){hidecaption('captioncc1');};
	}
}
function fillTagSorter(stage){
	if(stage==1){
		processajax('/users/mypage/uid=0/preferences/ignore/list/list/ajax', 'ignoredTags', 'post', 'list', 'fillTagSorter');
	}else if(stage==2){
		processajax('/users/mypage/uid=0/preferences/highlight/list/list/ajax', 'highlightedTags', 'post', 'list');
	}
	tagsorterinnerheight = document.getElementById('tagsorterinner').offsetHeight;
}
function tagSorter(){
	if(tagsorteractive == 1){
		return false;
	}
	tagsorteractive = 1;
	setTimeout('tagsorteractive = 0',250);
	oper2(1, 'tagsorter', tagsorterinnerheight, 0, 1, 0);
	fillTagSorter(1);
	for(i=0;i<oper2Array.length;i++){
/*		alert("loop: "+(i+1)+", "+(i+1)+"= "+oper2Array[i]);
*/		if(oper2Array[i]=="tagsorterinner"){
/*			alert("match found at index "+i+" for "+oper2Array[i]);
*/			setTimeout(function(){document.getElementById('tagsorterouter').style.display='block';document.getElementById('tagsorterinner').style.display='none';/*alert("reached here");*/},250);
			oper2Array.splice(i, 1);
			return;
		}
		if((i+1)==oper2Array.length){
/*			alert("Finished searching with no matches found");
*/			setTimeout(function(){document.getElementById('tagsorterouter').style.display='none';document.getElementById('tagsorterinner').style.display='block';document.getElementById('tagsorter').style.height='auto';/*alert("reached here");*/},250);
			oper2Array.push('tagsorterinner');
			return;
		}
	}
}
function TagSorter2(){
	if(document.getElementById('tagsorter')){
		tagsorterinnerheight = document.getElementById('tagsorterinner').offsetHeight;
		document.getElementById('tagsorterinner').style.display='none';
		document.getElementById('tagsorterouter').style.display='block';
		document.getElementById('tagsorter').style.display='none';
	}
	for(i=0;i<25;i++){
		if(document.getElementById('commentson'+i)){
			commentsorterinnerheights.push(document.getElementById('commentson'+i).offsetHeight);
			document.getElementById('commentson'+i).style.display='none';
			document.getElementById('commentson'+i).style.height=0;
		}
	}
}
function commentSorter(i){
	oper2(1, 'commentson'+i, commentsorterinnerheights[i] ,0, 1, 0);
}
function activateAboutEdit(v){
	var v = document.getElementById('profaboutme').innerHTML;
	w = v.replace(/<[a-zA-Z\/][^>]*>/gi,"");
	x = w.replace("(Click to edit)", "");
	var regExp = /blah/;
	document.getElementById('profabout').innerHTML="<h2 style=\"color:#00B0B0;\">About Me</h2><p id=\"profaboutme\"><form action=\"\" id=\"aboutform\" method=\"post\"><textarea id=\"workingupdate\" style=\"width:460px;height:380px;font-size:12px; font-weight:ligther;\" name=\"data[User][about]\">"+x+"</textarea><input type=\"submit\" value=\"Save\" id=\"saveupdate\"/><input type=\"Button\" value=\"Cancel\" id=\"canacelupdate\" onclick=\"deactivateAboutEdit();\"/></form>"
}
function deactivateAboutEdit(){
	var v = document.getElementById('workingupdate').innerHTML;
	w = v.replace(/>/gi,"&gt;");
	x = w.replace(/</gi,"&lt;");
	z = x.replace(/\n/gi,"\n<br/>");
	document.getElementById('profabout').innerHTML="<h2 style=\"color:#00B0B0;\">About Me</h2><p onclick=\"activateAboutEdit();\" id=\"profaboutme\">"+z+"<span style=\"right:0;position:absolute;top:20px;background-color:#FFFFCC;\">(Click to edit)</span></p>"
}
function hide(id){
	document.getElementById(id).style.display='none';
}
function show(id){
	document.getElementById(id).style.display='block';
}
function filterMethod(){
	var loc = document.location.toString();
	loc2 = loc.replace(/^([_a-z0-9\-\./:]+)*([?]*)/,"");
	loc3 = loc2.replace("?","");
	loc4 = loc3.split("&");
	for(i=0;i<loc4.length;i++){
		if((loc4[i].split("=")[0])=="t"){
			if(loc4[i].split("=")[1]=="un"){
				if(document.getElementById('activetab2')){
					document.getElementById('activetab2').id="null";
					document.getElementById('menu2').getElementsByTagName('li')[1].id="activetab2";
				}
			}else if(loc4[i].split("=")[1]=="cu"){
				if(document.getElementById('activetab')){
					document.getElementById('activetab').id="null";
					document.getElementById('button1').getElementsByTagName('li')[5].id="activetab";
				}
			}else if(loc4[i].split("=")[1]=="ho"){
				if(document.getElementById('activetab')){
					document.getElementById('activetab').id="null";
					document.getElementById('button1').getElementsByTagName('li')[4].id="activetab";
				}
			}else if(loc4[i].split("=")[1]=="ne"){
				if(document.getElementById('activetab')){
					document.getElementById('activetab').id="null";
					document.getElementById('button1').getElementsByTagName('li')[3].id="activetab";
				}
			}else if(loc4[i].split("=")[1]=="we"){
				if(document.getElementById('activetab')){
					document.getElementById('activetab').id="null";
					document.getElementById('button1').getElementsByTagName('li')[2].id="activetab";
				}
			}else if(loc4[i].split("=")[1]=="mo"){
				if(document.getElementById('activetab')){
					document.getElementById('activetab').id="null";
					document.getElementById('button1').getElementsByTagName('li')[1].id="activetab";
				}
			}else if(loc4[i].split("=")[1]=="al"){
				if(document.getElementById('activetab')){
					document.getElementById('activetab').id="null";
					document.getElementById('button1').getElementsByTagName('li')[0].id="activetab";
				}
			}
		}
	}
}
function arraytest(){
	var myArray = new Array();  
	myArray.push(["test1"]);  
	myArray.push(["test2"]);  
	myArray.push(["test3"]);  
	for(i=0;i<myArray.length;i++){
		if(myArray[i]=="test1"){
			alert("Success");
		}
	}
}
/*AJAX-v*/
var str = "lol";
function getxmlhttp (){
	var xmlhttp = false;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
function processajax (serverPage, obj, getOrPost, str, multiple){
	xmlhttp = getxmlhttp();
	obj = document.getElementById(obj);
/*	alert("Script Starting");*/
	if (getOrPost == "get"){
/*		alert("\"GET\" method chosen");*/
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
/*			alert("Receiving Response");*/
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
/*				alert("Positive Response");*/
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	} else {
/*		alert("\"POST\" method chosen");*/
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
/*			alert("Receiving Response");*/
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
/*				alert("Positive Response");*/
				if(xmlhttp.responseText.indexOf("Error!")>=0){
					alert(xmlhttp.responseText);
				}else if((xmlhttp.responseText.indexOf("type:userTagList~")>=0) && ((obj.id == "highlightedTags" || obj.id == "ignoredTags"))){
					j = xmlhttp.responseText.substring(17, xmlhttp.responseText.length);
					obj.innerHTML = j;
					if(multiple == "fillTagSorter"){
						fillTagSorter(2);
					}
				}else if((xmlhttp.responseText.indexOf("type:tagSearchList~")>=0) && ((obj.id == "highlightDrop") || (obj.id == "ignoreDrop"))){
					j = xmlhttp.responseText.substring(22, xmlhttp.responseText.length);
					obj.innerHTML = j;
					if(multiple == "fillTagSorter"){
						fillTagSorter(2);
					}
				}else if(obj.id !== "highlightDrop" || obj.id !== "ignoreDrop" || obj.id !== "highlightedTags" || obj.id !== "ignoredTags"){
					j = xmlhttp.responseText;
					obj.innerHTML = j;
					if(multiple == "fillTagSorter"){
						fillTagSorter(2);
					}
				}
			}
		}
		xmlhttp.send(str);
	}
	return true;
}
function tagAutoComplete(ignoreOrHighlight){
	if(ignoreOrHighlight == "ignore"){
		field = "ignoredTagsField";
		otherDrop = "highlightDrop";
	}else{
		field = "highlightedTagsField";
		otherDrop = "ignoreDrop";
	}
	if(cooldown == 0 && document.getElementById(field).value !== ''){
		if(ignoreOrHighlight == "ignore"){
			target = "ignoreDrop";
			str = 'tag='+document.getElementById(field).value;
			str += '&type=ignore';
			cooldown = 1;
			setTimeout('cooldown = 0', 750);
			document.getElementById(otherDrop).innerHTML = "";
		}else{
			target = "highlightDrop";
			str = 'tag='+document.getElementById('highlightedTagsField').value;
			str += '&type=highlight';
			cooldown = 1;
			setTimeout('cooldown = 0', 750);
			document.getElementById(otherDrop).innerHTML = "";
		}
		processajax('/tagAutoComplete.php', target, 'post', str);
	}else if(cooldown == 0){
		document.getElementById('ignoreDrop').innerHTML = '';
		document.getElementById('highlightDrop').innerHTML = '';
	}
	if(ignoreOrHighlight == "blur"){
		document.getElementById('ignoreDrop').innerHTML = '';
		document.getElementById('highlightDrop').innerHTML = '';
	}
}
function setvalue(string, which){
	if(which == "ignore"){
		document.getElementById('ignoredTagsField').value = string;
	}else if (which == "highlight"){
	document.getElementById('highlightedTagsField').value = string;
	}
}
function ignoreTagAdd(){
	str_0 = document.getElementById('ignoredTagsField').value;
	if(str_0 == ''){
		alert('You must enter a tag to ignore!');
		return;
	}
	processajax('/users/mypage/uid=0/preferences/ignore/' + str_0 + '/add/ajax', 'ignoredTags', 'post', str_0);
	tagsorterinnerheight = document.getElementById('tagsorterinner').offsetHeight;
}
function highlightTagAdd(){
	str_0 = document.getElementById('highlightedTagsField').value;
	if(str_0 == ''){
		alert('You must enter a tag to highlight!');
		return;
	}
	processajax('/users/mypage/uid=0/preferences/highlight/' + str_0 + '/add/ajax', 'highlightedTags', 'post', str_0);
	tagsorterinnerheight = document.getElementById('tagsorterinner').offsetHeight;
}
function ignoreTagRemove(tag){
	processajax('/users/mypage/uid=0/preferences/ignore/' + tag + '/remove/ajax', 'ignoredTags', 'post', tag);
	tagsorterinnerheight = document.getElementById('tagsorterinner').offsetHeight;
}
function highlightTagRemove(tag){
	processajax('/users/mypage/uid=0/preferences/highlight/' + tag + '/remove/ajax', 'highlightedTags', 'post', tag);
	tagsorterinnerheight = document.getElementById('tagsorterinner').offsetHeight;
}
/*AJAX-^*/
function toggleTerms(){
	if(document.getElementById('termsAccept').checked){
		termsFrame.document.body.style.backgroundColor='#C8FFC8';
		document.getElementById('terms').style.outlineColor='#00FF00';
	}else{
		termsFrame.document.body.style.backgroundColor='#FFC8C8';
		document.getElementById('terms').style.outlineColor='#FF0000';
	}
}
function checkIfTheyAgreed(){
	return true;
/*	if(document.getElementById('termsAccept').checked==true){
		return true;
	}else{
		if(document.getElementById('RegForm').style.display=="block"){
			if(confirm("You must agree to the terms to Sign Up!")){
				document.getElementById('termsAccept').checked=true;
			}
		}
		return false;
	}*/
}
function addReference(){
	if (numReferences==0){
		document.getElementById('reference1').style.display="block";
		document.getElementById('referencesToAddHelper').style.display="none";
		numReferences = 2;
	}else{
		document.getElementById('referencesToAdd').innerHTML = document.getElementById('referencesToAdd').innerHTML + "<div id=\"reference" + numReferences + "\"> URL: <input type=\"text\" value=\"\" id=\"ReferenceURL\" name=\"data[Reference][url" + numReferences + "]\"/> Title: <input type=\"text\" value=\"\" id=\"ReferenceTitle\" name=\"data[Reference][title" + numReferences + "]\"/></div>";
		numReferences++;
	}
}