//Javascript file for dynamically adding Channels to Rez

// Provide the XMLHttpRequest class for IE 5.x-6.x:
if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
  try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
  try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
  try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
  throw new Error( "This browser does not support XMLHttpRequest." )
};

function addChannelDirectoryLink(channelId, directoryId, channelName)
{
	postdata = "channel_id="+channelId+"&directory_id="+directoryId;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", "/rez/eiontest/channel_link.php?type=add&directory=1", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", postdata.length);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4)
		{
			channel_links = document.getElementById('channel_links');
			channel_links.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(postdata);
}

//adds a topic to a channel (channel to a topic?) using Ajax
function addChannelLink(channelId, topicId, channelName)
{
	//if (isTopicInChannel(channelId))
	//	return false;

	postdata = "channel_id="+channelId+"&topic_id="+topicId;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", "/rez/eiontest/channel_link.php?type=add", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", postdata.length);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4)
		{
			channel_links = document.getElementById('channel_links');
			channel_links.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(postdata);
}

//checks whether a topic has a particular channel associated with it
//returns true if it does, false if it doesnt
function isTopicInChannel(channelId)
{
	var channel_links = document.getElementById('channel_links');
	if (channel_links)
	{
		anchors = channel_links.getElementsByTagName('A');
		channels = new Array();
		for (i in anchors)
		{
			if (!anchors[i].href)
				continue;
			channel_number = anchors[i].href.match("rez_display\\.php\\?c="+channelId+"$");
			if (channel_number)
				return true;
		}
	}
	return false;
}

function refreshChannelLinks(topicId)
{
	var postdata = "topic_id="+topicId;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", "/rez/eiontest/channel_link.php", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", postdata.length);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4)
		{
			channel_links = document.getElementById('channel_links');
			channel_links.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(postdata);
}

//removes a topic from a channel (a channel from a topic?) using Ajax
function removeChannelLink(channelId, topicId)
{
	postdata = "channel_id="+channelId+"&topic_id="+topicId;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", "/rez/eiontest/channel_link.php?type=remove", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", postdata.length);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4)
		{
			channel_links = document.getElementById('channel_links');
			channel_links.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send(postdata);
}

//Adds a brand new channel name
function addNewChannel(newChannelName, topicId)
{
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("POST", "/rez/eiontest/channel_link.php?type=new", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4)
		{
			channel_links = document.getElementById('channel_links');
			channel_links.innerHTML = xmlhttp.responseText;
			document.getElementById('output').innerHTML = '';
		}
	}
	xmlhttp.send("channel_name="+newChannelName+"&topic_id="+topicId);
}

var lastChannelName = "";
//Searches for channels using a specified name, and displays the channels in a list
function searchChannels(channelName, idToShowFoundChannels, topicId)
{
	if (lastChannelName == channelName)
		return;
	lastChannelName = channelName;
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open("GET", "/rez/eiontest/channels_like.php?name="+channelName+"&topic_id="+topicId, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4)
		{
			objectToShowFoundChannels = document.getElementById(idToShowFoundChannels);
			objectToShowFoundChannels.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.send("");
}

var channelChangeTimeout = 0;
function channelSearchChanged(channelName, idToShowFoundChannels, topicId)
{
	if (channelChangeTimeout)
		clearTimeout(channelChangeTimeout);
	
	channelChangeTimeout = setTimeout("searchChannels('"+channelName+"', '"+idToShowFoundChannels+"', '"+topicId+"');", 100);
}

//handles keypresses, eg up and down, for going through the list of channels to add
function channelHandleKey(e, idWithFoundChannels)
{
	if (!e) var e = window.event;
	var key = e.keyCode || e.which;
	
	output = document.getElementById(idWithFoundChannels);
	anchors = output.getElementsByTagName("a");
	if (output.currentIndex == undefined)
		output.currentIndex = -1;
	
	if (key == 13)
	{
		//Enter key
		anchors[output.currentIndex].click();
		output.innerHTML = "";
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	} else {
		if (key == 38)
		{
			//Up key
			output.currentIndex--;
			if (output.currentIndex < 0)
				output.currentIndex = 0;
		} else if (key == 40)
		{
			//Down key
			output.currentIndex++;
			if (output.currentIndex >= anchors.length)
				output.currentIndex = anchors.length - 1;
		}
		for (i=anchors.length-1;i>=0;i--)
		{
			anchors[i].style.background = "transparent";
		}
		if (anchors.length && output.currentIndex >= 0)
			anchors[output.currentIndex].style.background = "yellow";
	}
}
