var request = false;
try {
	request = new XMLHttpRequest();
} catch (trymicrosoft) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (othermicrosoft) {
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			request = false;
		}
	}
}

function switchVersions() {
	var versionNumber = document.getElementById("switchVersion").value;

	var url = 'http://edcommunity.apple.com/ali/story.php?itemID=' + itemID + "&version=" + versionNumber;

	window.location = url;
}

var movingVars = new Array();
var prevOffset = new Array(0,0)

function truncater(item,len,link) {
	var text = item.innerHTML;
		
	var test_length = len+125;
		
	if (text.length <= test_length) {
		return;
	} else {
		var new_text = text.substr(0,len);
		
		var key = len;
		
		while(new_text.charAt(key) != " ") {
			key--;
		}

		new_text = text.substr(0,key) + "...";
		
		var newText = document.createElement("span");
		newText.innerHTML = new_text;
		newText.className = 'blog_short_text';

		var pNode = item;

		while(pNode.className != "blog_par") {
			pNode = pNode.parentNode;
		}
		
//		item.style.display = 'none';
		var oNode = item;
		item.parentNode.removeChild(item);
				
		if (link == "") {
			var openUp_link = document.createElement("a");
		} else {
			link.parentNode.removeChild(link);
			var openUp_link = document.createElement("a");			
		}

		openUp_link.onclick = function() { openUp(openUp_link,len); return false; }
		openUp_link.href = '#';
		openUp_link.className = 'blog_more_link';
		
		var str = "Read more";
		var readmore = document.createTextNode(str);		
		openUp_link.appendChild(readmore);
		
		pNode.style.display = "block";
		newText.style.display = "inline";
		oNode.style.display = "none";

		pNode.appendChild(oNode);
		pNode.appendChild(newText);
		pNode.appendChild(openUp_link);
	}
}

function openUp(item,len) {	
	var pNode = item;
	
	while(pNode.nodeName != "A") {
		pNode = pNode.parentNode;
	}
	
	var prevNode = pNode;
	
	while (prevNode.nodeName != "SPAN") {
		prevNode = prevNode.previousSibling;
	}

	var killNode = prevNode;
	
	prevNode = prevNode.previousSibling;

	while (prevNode.nodeName != "SPAN") {
		prevNode = prevNode.previousSibling;
	}
	
	prevNode.style.display = 'inline';

	killNode.parentNode.removeChild(killNode);

	var str = "Collapse";
	var readless = document.createTextNode(str);
	item.appendChild(readless);
	item.removeChild(item.firstChild);
	
	item.onclick = function() { truncater(prevNode,len,item); return false; }
	
	return false;
}

function advance_content(area,action,id,offset,max,searchType) {
	if (movingVars[0]) { return false; }
	if (action == 'next' && offset == max) { return false; }

	var theArea = document.getElementById('submissionthumb');
	theArea.sliding = null;
		
	movingVars[0] = action;
	movingVars[1] = id;
	movingVars[2] = 'submissionthumb';
	movingVars[3] = area;
	movingVars[4] = searchType;

	if (searchType == 'i') {
		var url = 'search.php?output=XML&institution=' + id + '&results=1&offset=' + offset + '&order=' + area;
	} else {
		var url = 'search.php?output=XML&owner=' + id + '&results=1&offset=' + offset + '&order=' + area;
	}

	// Use offset to determine next button - make that button spin
		
	request.open("GET",url,true);
	request.onreadystatechange = newResults;
	request.send(null);
}

function newResults() {
	if (request.readyState == 4) {
		var response = request.responseXML;
		
		var id = response.getElementsByTagName("id")[0].firstChild.data;

		try {
			var thumb = response.getElementsByTagName("thumb")[0].firstChild.data;
		} catch (err) {
			var thumb = '';
		}

		var title = response.getElementsByTagName("fulltitle")[0].firstChild.data;
		var offset = response.getElementsByTagName("offset")[0].firstChild.data;
		var authorID = response.getElementsByTagName("authorID")[0].firstChild.data;
		var totalresults = response.getElementsByTagName("totalresults")[0].firstChild.data;
		
		var thumbSrc = document.createElement("img");
		thumbSrc.src = thumb;
		
		if (movingVars[0] == "next") {
			thumbSrc.setAttribute("style","left: 90px");		
			thumbSrc.xpos = 90;
		} else {
			thumbSrc.setAttribute("style","left: -90px");		
			thumbSrc.xpos = -90;		
		}
		
		thumbSrc.setAttribute("width","90px");
		thumbSrc.setAttribute("height","60px");
		thumbSrc.setAttribute("id","newMover");
		thumbSrc.setAttribute("style","top: 0px");
		thumbSrc.setAttribute("style","position: absolute");
		thumbSrc.className = 'submissionthumb';
		thumbSrc.ypos = 0;
				
		var area = document.getElementById("submissionnav").getElementsByTagName("div")[0];
		area.appendChild(thumbSrc);
		
		if (movingVars[0] == "next") {
			slideElement(movingVars[2],-90,0,2);
			slideElement("newMover",0,0,2);	
		} else {
			slideElement(movingVars[2],90,0,2);
			slideElement("newMover",0,0,2);	
		}

		var linkTitle = document.getElementById("submissionnav").getElementsByTagName("a")[0];		
		
		var newLink = document.createElement("a");		
		newLink.href = 'item.php?itemID=' + id;
		newLink.innerHTML = title;
		
		linkTitle.parentNode.replaceChild(newLink,linkTitle);
		
		var next = offset;
		var prev = offset - 2;
		var thisNumber = offset;
		if (thisNumber == 0) { thisNumber = 1; }
			
		if (prev < 0) { prev = totalresults - 1; }
		if (next == totalresults) { next = 0; }
		
		var prevButton = document.getElementById("submissionnav").getElementsByTagName("a")[1];
		var nextButton = document.getElementById("submissionnav").getElementsByTagName("a")[2];
		prevButton.href = "javascript:advance_content('recent','prev','" + movingVars[1] + "','" + prev + "','" + totalresults + "','" + movingVars[4] + "')";
		nextButton.href = "javascript:advance_content('recent','next','" + movingVars[1] + "','" + next + "','" + totalresults + "','" + movingVars[4] + "')";
		
		var submissioncount = document.getElementById("submissioncount");
		submissioncount.innerHTML = offset + " of " + totalresults;
	}
}

function slideElement(elementId,x,y,inc) {
	if (!document.getElementById) return false;
	if (!document.getElementById(elementId)) return false;

	var element = document.getElementById(elementId);
	
	if (element.sliding) clearTimeout(element.sliding);
		
	if (!element.xpos) element.xpos = 0;
	if (!element.ypos) element.ypos = 0;

	if (element.xpos == x && element.ypos == y) { 
		if (elementId == 'newMover') { 
			element.setAttribute("id",movingVars[2]); 
			element.setAttribute("style","position: relative;");
			movingVars = new Array();
		} else {
			var area = element.parentNode;
			area.removeChild(element);
		} 
		
		return true;
	};

	if (element.xpos > x) {

		var dist = Math.ceil((element.xpos-x)/inc);
		element.xpos = element.xpos - dist;

	}

	if (element.xpos < x) {

		var dist = Math.ceil((x-element.xpos)/inc);
		element.xpos = element.xpos + dist;

	}

	if (element.ypos > y) {

		var dist = Math.ceil((element.ypos-y)/inc);
		element.ypos = element.ypos - dist;

	}

	if (element.ypos < y) {

		var dist = Math.ceil((y-element.ypos)/inc);
		element.ypos = element.ypos + dist;

	}

	element.style.left = element.xpos+'px';
	element.style.top = element.ypos+'px';

	element.sliding = setTimeout('slideElement("'+elementId+'",'+x+','+y+','+inc+')',10);
}

function updateRating() {
	var ratingValue = document.getElementById("ratingForm").value;
			
	if (ratingValue != "") {
		var url = '/ali/rate.php?itemID=' + itemID + '&rating=' + ratingValue;

		request.open("GET",url,true);
		request.onreadystatechange = processRating;
		request.send(null);
	}

	killDiv();
}

function processRating() {
	if (request.readyState == 4) {
		var response = request.responseXML;
			
		var updated_rating = response.getElementsByTagName("updated_rating")[0].firstChild.data;
		var new_score = response.getElementsByTagName("newscore")[0].firstChild.data;
		var rating_width = response.getElementsByTagName("rating_width")[0].firstChild.data;
		var ratings = response.getElementsByTagName("ratings")[0].firstChild.data;
				
		var stars = document.getElementById("stars");
		stars.style.background = "url(/ali/images/stars/stars_full_change.gif) no-repeat top left";
		stars.style.width = rating_width + "px";
		
		var info = document.getElementById("rating_info");

		if (ratings > 1) { var suffix = 's'; } else { var suffix = ''; }

		info.innerHTML = updated_rating + " of 5, " + ratings + " rater" + suffix;
		info.style.padding = "2px 2px 2px 2px";
		info.style.background = '#d0d0d0';
	}
}

function rating(evt) {
	var divID = "ratingBox";	
	
	if (document.getElementById(divID)) {
		killDiv();
	} else {		
		var evt  = (evt) ? evt : ((event) ? event : null);
		var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

		var tempX = evt.pageX;
		var tempY = evt.pageY;

		var commentDiv = document.createElement("div");
		
		commentDiv.setAttribute("id","ratingBox");
		commentDiv.className = "floating_rating_box";
		commentDiv.style.top = tempY - 40 + "px";
		commentDiv.style.left = tempX + "px";
				
		var ratingSelect = document.createElement("select");
		ratingSelect.setAttribute("name","ratingForm");
		ratingSelect.setAttribute("id","ratingForm");
		ratingSelect.onchange=updateRating;
		ratingSelect.options[0] = new Option("choose rating...","");
		ratingSelect.options[1] = new Option("no stars","0");
		ratingSelect.options[2] = new Option("1 star","1");
		ratingSelect.options[3] = new Option("2 stars","2");
		ratingSelect.options[4] = new Option("3 stars","3");
		ratingSelect.options[5] = new Option("4 stars","4");
		ratingSelect.options[6] = new Option("5 stars","5");

		commentDiv.appendChild(ratingSelect);
		
		document.getElementsByTagName("body")[0].appendChild(commentDiv);
	}
	
	return false;
}

function favorites(node) {
	Effect.toggle('pclist','blind',{ duration:.25 });
	$(node).toggleClassName("favorite_active");
	
	return false;
}

function add_pc(node) {
	var href = node.href;
		
	new Ajax.Request(href,{
		method: 'GET',
		onComplete: function (transport) {
			var response = transport.responseXML;
			var output = response.getElementsByTagName("add")[0].firstChild.data;
			
			if (output == "yes") {
				$(node).innerHTML = "Added to " + node.name;
			} else {
				$(node).innerHTML = "Removed from " + node.name;
			}
		}
	});

	return false;
}

function addFavorite() {
	var url = "/ali/processors/add_favorites.php?itemID=" + itemID;

	request.open("GET",url,true);
	request.onreadystatechange = processFavorite;
	request.send(null);
	
	return false;
}

function processFavorite() {
	if (request.readyState == 4) {
		var response = request.responseXML;
				
		var add = response.getElementsByTagName("add")[0].firstChild.data;
		var favLink = document.getElementById("favorites");
		var favPar = favLink.parentNode;
		var favLinkText = favLink.getElementsByTagName("span")[0];
		
		if (add == "yes") {
			favLink.setAttribute("style","background: url(/ali/images/kill_favorite.png) no-repeat 11px 1px;");
			favLinkText.innerHTML = "This item has been added to your favorites.";
			favLinkText.setAttribute("style","background: #d0d0d0;");			
		} else {
			favLink.setAttribute("style","background: url(/ali/images/add_favorite.png) no-repeat 11px 1px;");
			favLinkText.innerHTML = "This item has been removed from your favorites.";		
			favLinkText.setAttribute("style","background: #d0d0d0;");
		}		
	}
}

function killDiv() {
	var ratingBox = document.getElementById("ratingBox");
	document.getElementsByTagName("body")[0].removeChild(ratingBox);
}

////// ADD KEYWORD /////////////////////////////////////////////////////////////////////////////////
function newKeyword(evt,item) {
	var evt  = (evt) ? evt : ((event) ? event : null);	
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	
	if (evt.keyCode == 13 || evt.keyCode == 3) {
		var pNode = node.parentNode;
	
		while (pNode.getAttribute("id") != "tags") {
			pNode = pNode.parentNode;
		}

		var newKeyword = item.value;

		var spans = pNode.getElementsByTagName("span");
		var x = spans.length - 1;
				
		// Make sure that this isn't a duplicate
		for (i=0;i<x;i++) {
			if (spans[i].getElementsByTagName("a")[0].innerHTML == newKeyword) {
				return false;
			}
		}
		
		var url = '/ali/builder/builder_processor.php?itemID=' + itemID + "&tag=" + encodeURIComponent(newKeyword) + "&action=addTag";
				
		request.open("GET",url,true);
		request.onreadystatechange = function() { newKeywordProcesor(item); }
		request.send(null);			
	}

	return false;
}
function newKeywordProcesor(node) {
	if (request.readyState == 4 && request.responseText != "failed" && request.responseText != "") {
		var pNode = node.parentNode;
	
		while (pNode.getAttribute("id") != "tags") {
			pNode = pNode.parentNode;
		}
		
		var newKeyword = node.value;

		var spans = pNode.getElementsByTagName("span");
		var x = spans.length - 1;
				
		var tagSpan = document.createElement("span");
		var tagLink = document.createElement("a");
		tagLink.href = "search.php?keyword=" + newKeyword;
		tagLink.innerHTML = newKeyword;
				
		tagSpan.appendChild(tagLink);
				
		pNode.insertBefore(tagSpan,spans[x]);
		
		node.value = "";
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
