var multiBanner = false;
var ajaxSearchObject = getXmlHttpRequestObject();

function pageLoad()
{
	//get time,
	//if night: load dark banner
	//if day: load light banner
	
	var myDate = new Date();
	var myHour = myDate.getHours();
	
	if(multiBanner)
	{
		if(myHour >= 7 && myHour < 19)
		{
			document.getElementById('banner').src = "http://tensai-desu.org/bildungsroman/AnimeBanner.jpg";
		}
		else
		{
			document.getElementById('banner').src = "http://tensai-desu.org/bildungsroman/AnimeBannerALT.jpg";
		}
	}
	
	ajaxSearchObject = getXmlHttpRequestObject();
}

function searchSuggest()
{
	if (ajaxSearchObject.readyState == 4 || ajaxSearchObject.readyState == 0) 
	{
		var data = "phrase=" + escape(document.getElementById('searchTextBox').value);
		ajaxSearchObject.open("POST", "search_suggestions.php", true);
		ajaxSearchObject.onreadystatechange = handleSearchSuggest;
		ajaxSearchObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajaxSearchObject.send(data);
	}
}

function handleSearchSuggest() 
{
	if (ajaxSearchObject.readyState == 4) 
	{
		var suggestionNode = document.getElementById('suggestionDivNode');
		
		var searchBoxOffset = document.getElementById('searchTextBox').offsetLeft;
		var mainTableOffset = document.getElementById('mainTableNode').offsetLeft;
		var alignmentOffset = document.getElementById('suggestionAlignmentNode').offsetLeft;
		var offset = searchBoxOffset - (alignmentOffset - mainTableOffset);
		suggestionNode.setAttribute("style", "left: " + offset + "px;");
		
		//document.getElementById('hiddenSuggestionTable').setAttribute("class", "suggestion_visible_class");
		//document.getElementById('hiddenSuggestionTable').setAttribute("className", "suggestion_visible_class");
		suggestionNode.innerHTML = '';
		var suggestions = ajaxSearchObject.responseText.split("\n");
		
		setSuggestionBorderStyle(suggestions.length);
		/*
		if(suggestions.length > 1)
		{
			suggestionNode.setAttribute("class", "search_div_class search_div_black_border");
			suggestionNode.setAttribute("className", "search_div_class search_div_black_border search_div_ie_align");
		}
		else
		{
			suggestionNode.setAttribute("class", "search_div_class");
			suggestionNode.setAttribute("className", "search_div_class");
		}*/
		
		
		
		/*var divSuggestionId = "indexNum";
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div id = "' + divSuggestionId + '" onmouseover="setSuggestionDivStyle(this.id, 1)" ';
			suggest += 'onmouseout="setSuggestionDivStyle(this.id, 2)" ';
			suggest += 'onclick="selectSuggestion(this.innerHTML)" ';
			suggest += 'class="suggest_link_monochrome">' + suggestions.length + '</div>';
			suggestionNode.innerHTML += suggest;
		*/
		
		
		
		for(i=0; i < suggestions.length - 1; i++) 
		{
			var divSuggestionId = suggestions[i];
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div id = "' + divSuggestionId + '" onmouseover="setSuggestionDivStyle(this.id, 1)" ';
			suggest += 'onmouseout="setSuggestionDivStyle(this.id, 2)" ';
			suggest += 'onclick="selectSuggestion(this.innerHTML)" ';
			suggest += 'class="suggest_link_monochrome">' + suggestions[i] + '</div>';
			suggestionNode.innerHTML += suggest;
		}
		
		if(suggestions.length > 1)
		{
			suggestionNode.innerHTML += "<center><input type = 'Submit' name = 'innerSearchButton' value='Search' /></center>";
		}
		
		
	}
}

function selectSuggestion(suggestion)
{
	document.getElementById("searchTextBox").value = suggestion;
	document.getElementById("suggestionDivNode").innerHTML = '';
	setSuggestionBorderStyle(0);
}

function setSuggestionBorderStyle(rowParse)
{
	if(rowParse > 1)
	{
		document.getElementById('suggestionDivNode').setAttribute("class", "search_div_class search_div_black_border");
		document.getElementById('suggestionDivNode').setAttribute("className", "search_div_class search_div_black_border search_div_ie_align");
	}
	else
	{
		document.getElementById('suggestionDivNode').setAttribute("class", "search_div_class");
		document.getElementById('suggestionDivNode').setAttribute("className", "search_div_class");
	}
}

function setSuggestionDivStyle(id, isFocus)
{
	if(isFocus == 1)
	{
		document.getElementById(id).setAttribute("class", "suggest_link_highlight");
		document.getElementById(id).setAttribute("className", "suggest_link_highlight");
	}
	else
	{
		if(document.getElementById(id) != null)
		{
			document.getElementById(id).setAttribute("class", "suggest_link_monochrome");
			document.getElementById(id).setAttribute("className", "suggest_link_monochrome");
		}
	}
}

function getXmlHttpRequestObject() 
{
	if(window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		//alert("Your browser does not support XMLHTTP!");
	}
}

function setInputBoxStyle(id, isFocus)
{
	if(isFocus == 1)
	{
		document.getElementById(id).setAttribute("class", "input_focus_style");
		document.getElementById(id).setAttribute("className", "input_focus_style");
	}
	else
	{
		document.getElementById(id).setAttribute("class", "input_default_style");
		document.getElementById(id).setAttribute("className", "input_default_style");
	}
}

function castVote(voteCast, commentID, thumbsUpID, thumbsDownID, divRating)
{
	var xmlhttp;
	var data = "code=" + escape(commentID) + "&type=" + escape(voteCast);
	
	document.getElementById(thumbsUpID).setAttribute("onclick", "");
	document.getElementById(thumbsDownID).setAttribute("onclick", "");
	
	xmlhttp = getXmlHttpRequestObject();
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			var rating = parseInt(document.getElementById(divRating).innerHTML);
			if(voteCast == 1)
			{
				rating = rating + 1;
			}
			else
			{
				rating = rating - 1;
			}
			document.getElementById(divRating).innerHTML = rating;
			
			document.getElementById(thumbsUpID).setAttribute("onmouseover", "");
			document.getElementById(thumbsUpID).setAttribute("onmouseout", "");
			document.getElementById(thumbsUpID).setAttribute("class", "");
			document.getElementById(thumbsUpID).setAttribute("className", "");
			
			document.getElementById(thumbsDownID).setAttribute("onmouseover", "");
			document.getElementById(thumbsDownID).setAttribute("onmouseout", "");
			document.getElementById(thumbsDownID).setAttribute("class", "");
			document.getElementById(thumbsDownID).setAttribute("className", "");
			
			if(voteCast == 1)
			{
				thumbsUp(thumbsUpID, 1);
				thumbsDown(thumbsDownID, 0);
			}
			else
			{
				thumbsUp(thumbsUpID, 0);
				thumbsDown(thumbsDownID, 1);
			}
		}
	}

	xmlhttp.open("POST","cast_vote.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(data);
}

function thumbsUp(id, highlight)
{
	if(highlight == 1)
	{
		document.getElementById(id).src = "http://tensai-desu.org/bildungsroman/thumbs_up_green.gif";
	}
	else
	{
		document.getElementById(id).src = "http://tensai-desu.org/bildungsroman/thumbs_up_gray.gif";
	}
}

function thumbsDown(id, highlight)
{
	if(highlight == 1)
	{
		document.getElementById(id).src = "http://tensai-desu.org/bildungsroman/thumbs_down_red.gif";
	}
	else
	{
		document.getElementById(id).src = "http://tensai-desu.org/bildungsroman/thumbs_down_gray.gif";
	}
}

function markCommentAsSpam(commentID, div1, div2)
{
	var xmlhttp;
	var data = "code=" + escape(commentID);
	
	if(window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		//alert("Your browser does not support XMLHTTP!");
	}
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			document.getElementById(div1).innerHTML = "<br />";
			document.getElementById(div2).innerHTML = "Comment marked as spam.<br />";
			document.getElementById(div2).setAttribute("class", "text_spamlink_style");
			document.getElementById(div2).setAttribute("className", "text_spamlink_style");
		}
	}
	
	xmlhttp.open("POST","mark_spam.php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(data);
}

