//
// General
//

	function getLazyReference(obj)
	{
		if (obj.id)
		{
			return obj;
		}
		else
		{
			return document.getElementById(obj);
		}
	}

	// actually add an event listener, regardless of browser
	function addListener(obj, eventType, listenerFunction)
	{
		if (!obj)
		{
			alert("bad object for " + eventType + ", obj:" + obj + ", fn:" + listenerFunction + ", caller:" + addListener.caller);
			return;
		}
		if (obj.addEventListener)
		{
			obj.addEventListener(eventType, listenerFunction, true);
			
		}
		else if (obj.attachEvent)
		{
			var success = obj.attachEvent("on" + eventType, listenerFunction);
		}
	}	

	function GetSelectValue(box)
	{
		if (!box.selectedIndex)
		{
			box = document.getElementById(box);
		}
		
		return box.options[box.selectedIndex].value;
	}

//
// Discussion Comments
//

	var commentSocketFrame;

	function InitCommentSocket()
	{	
		if (commentSocketFrame == null)
		{
			commentSocketFrame = document.createElement("IFRAME");
			commentSocketFrame.style.visibility = "hidden";
			document.body.appendChild(commentSocketFrame);
		}
	}
	
	function SendSocketRequest(request)
	{
		InitCommentSocket();
		commentSocketFrame.src = request;
	}

	function ViewComments(commentID)
	{
		var divComment = document.getElementById("divComment" + commentID)
		
		var request = "/WebServices/CommentService.aspx?commentID=" + commentID + "&commentsOnly=1";
		SendSocketRequest(request)
	}
	
	function CommentServiceCallback(commentID)
	{
		var divComment = document.getElementById("divComment" + commentID);
		var commentSocketWindow = commentSocketFrame.contentWindow || commentSocketFrame.contentDocument.defaultView;

		divComment.innerHTML = commentSocketWindow.document.body.innerHTML;
	}
	
	
	
//
// low-bandwidth mode	
//
	function loadFinished()
	{
		//var slowdiv = document.getElementById("slowconnection");
		//if (slowdiv && window.loadStart)
		if (window.loadStart)
		{
			var slowUrl = location.href;
			if (slowUrl.indexOf("?") > -1)
			{
				slowUrl += "&slow="
			}
			else
			{
				slowUrl += "?slow="
			}
			
			if (window.isSlow)
			{
					addInfoBar('This is low bandwidth mode, so maps are disabled. <a href="' + slowUrl + '0">switch back to fast mode</a>?');
			}
			else
			{
				var elapsedTicks = new Date() - loadStart;
				if (elapsedTicks > 8000)
				{
					// 8 seconds is long enough for a scoring bullride.  We aught to be able to load a single page in that time.
					addInfoBar('Wow, this connection is slow!  Wanna <a href="' + slowUrl + '1">switch to low bandwidth mode</a>?');
				}
			}
		}
		
	}
	
	function addInfoBar(message)
	{
		var infobar = document.createElement("DIV");
		infobar.className = "infobar";
		infobar.innerHTML = message;
		if (document.body && document.body.childNodes.length > 0)
		{
			document.body.insertBefore(infobar, document.body.childNodes[0]);
		}
	}
	
	if (window.isSlow)
	{
		loadFinished();
	}
	else
	{
		addListener(window, "load", loadFinished);
	}

//
// Voting
//
	
	function VoteUp(commentID)
	{
		var request = "/WebServices/VoteService.aspx?commentID=" + commentID;
		SendSocketRequest(request)
		
		//window.open(request, "aaa");
	}
	
	function VoteImageUp(imageID)
	{
		var request = "/WebServices/VoteService.aspx?imageID=" + imageID;
		SendSocketRequest(request)
		
		//window.open(request, "aaa");
	}

	function VoteServiceCallback(id)
	{
		var spanVote = document.getElementById("spanVote" + id);
		if (spanVote)
		{
			spanVote.innerHTML = "<b>Cool.  You've voted for it!<b>";
		}
	}
	
//
// Compliments
//
Vote = {listenerSet:false}

Vote.Send = function()
{
	var text = escape(document.getElementById("txtCompliment").value)
	var commentID = document.getElementById("hdTargetCommentID").value
	var imageID = document.getElementById("hdTargetImageID").value
	
	LoadContentService.SendVote(commentID, imageID, Vote.index+1, text, Vote.onVoted);
	
	Vote.commentID = commentID;
	Vote.imageID = imageID;
	
	if (commentID > 0)
	{
		Vote.itemID = commentID;
		//Vote.isComment = true;
	}
	else
	{
		Vote.itemID = imageID;
		//Vote.isComment = true;
	}
}

Vote.Open = function(commentID)
{
	Vote.Close();
	LoadContentService.LoadComplimentEdit(commentID, 'divCompliment' + commentID)
}

Vote.Close = function()
{
	var div = document.getElementById("divComplimentBox");
	while (div)
	{
		div.parentNode.removeChild(div);
		div = document.getElementById("divComplimentBox");
	}
}

Vote.onVoted = function()
{
	//alert("voted");
	Vote.Close();
	
	var scoreBox = document.getElementById("divScoreHolder" + Vote.itemID);
	if (scoreBox)
	{
		//scoreBox.scrollIntoView();
		LoadContentService.LoadComplimentScore(Vote.commentID, Vote.imageID, scoreBox.id)
	}
	
}

Vote.onIndexChanged = function(list, index)
{
	Vote.index = index;
	
	indicator = document.getElementById("spriteSend");
	indicator.style.marginLeft = (-20 * index) + "px";
	//alert(index)
}

Vote.onClick = function(e)
{
	var target = e.target ? e.target : e.srcElement;
	var depth = 0;
	while (target && ++depth < 5)
	{
		if (target.id == "divComplimentBox")
		{
			// we're over the compliment box.  don't close it!
			return;
		}
		target = target.offsetParent;
	}
	
	return Vote.Close()
}

Vote.onOpen = function()
{
	if (!Vote.listenerSet)
	{
		addListener(document.body, "click", Vote.onClick)
		Vote.listenerSet = true;
	}
	
	Vote.buttons = new RadioButtonList("rbVote1","rbVote2","rbVote3","rbVote4","rbVote5","rbVote6","rbVote7","rbVote8","rbVote9","rbVote10");
	Vote.buttons.onChange = Vote.onIndexChanged;
	Vote.buttons.setSelectedIndex(7);

}

Vote.onScoreLoad = function()
{
	//alert('reloaded score');
}


// 
// This is a set of toolbar buttons that act like a radio list.
// Hand it a list anchor ids: new RadioButtonList("link1", "link2", "link3");
//
function RadioButtonList()
{
	this.links = new Array();
	for (var a=0; a<arguments.length; a++)
	{
		var link = getLazyReference(arguments[a]);
		link.jref = this;
		link.checked = false;
		link.index = this.links.length;
		this.links[this.links.length] = link;

		addListener(link, "click", this.click);
	}
	this.setSelectedIndex(0);
	
	this.onChange = null;
}
RadioButtonList.prototype.click = function(e)
{
	//var target = e.target ? e.currentTarget : e.srcElement;
	var target = e.target ? e.target : e.srcElement;
	var depth = 0;
	while (target.tagName != "A" && depth++ < 5)
	{
		// for some reason, it's the IMG that registers the click, not the link itself
		target = target.parentNode;
	}
	target.jref.setSelectedIndex(target.index);
	target.blur();
}
RadioButtonList.prototype.render = function()
{
	for (var a=0; a<this.links.length; a++)
	{
		var link = this.links[a];
		if (link.checked)
		{
			link.className = "selected";
		}
		else
		{
			link.className = "";
		}
	}
}
RadioButtonList.prototype.setSelectedIndex = function(index)
{
	for (var a=0; a<this.links.length; a++)
	{
		var link = this.links[a];
		link.checked = false;
	}
	this.selectedIndex = index;
	this.links[index].checked = true;
	this.render();
	
	if (this.onChange != null)
	{
		this.onChange(this, this.selectedIndex);
	}
}
	

//
// Admin
//

	function commentEdit_mouseOver(id)
	{
		var obj = document.getElementById(id);
		if (obj && obj.style)
		{
			obj.style.originalBorder = obj.style.border;
			obj.style.border = "1px dashed #cccccc";
		}
	}

	function commentEdit_mouseOut(id)
	{
		var obj = document.getElementById(id);
		if (obj && obj.style)
		{
			//obj.style.border = obj.style.originalBorder;
			obj.style.border = "1px solid #cccccc";
		}
	}
	
	function adminEdit(id)
	{
		window.open("/Cramhole/Admin/Element_Edit.aspx?elementID=" + id);
	}

	function openPreviewWindow()
	{
		var previewWindow = window.open("", "preview", "width=840,height=800,scrollbars");
		previewWindow.focus();
	}
