/* This is from http://marc.theaimsgroup.com/?l=php-general&m=112198633625636&w=2*/
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if (browser == "Microsoft Internet Explorer")
        ro = new ActiveXObject("Microsoft.XMLHTTP");
	else
        ro = new XMLHttpRequest();
    return ro;
}

var http = createRequestObject();

function doRate(xtraid, rateValue) {
    http.open('get', 'index.php?a=xtras&do=rate&html=1&xtra_id=' + xtraid + '&rating=' + rateValue);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() {
    if (http.readyState != 4)
    	return;

	var response = http.responseText;
	var update = new Array();
        
	if (response.indexOf("|" != -1)) {
		update = response.split("|");
		document.getElementById(update[0]).innerHTML = update[1];
	}
}

function confirmReport(comment_id, xtra_id) {
	var msg = "You are about to notify the Xtras mods that this comment is inappropriate. Are you sure you wish to report it?";
	if (confirm(msg))
		window.location = "index.php?a=xtras&do=report_comment&comment_id=" + comment_id + "&xtra_id=" + xtra_id;
}

function confirmDelete(comment_id) {
	var msg = "You are about to delete a comment. Are you sure you wish to continue?"
	if (confirm(msg))
		window.location = "index.php?a=manage&do=del_comment&comment_id=" + comment_id;
}

function swap(id, bigFile, smallFile) {
	var image = document.getElementById(id);
	var curFile = image.src.substring(image.src.lastIndexOf('/')+1);
	var swapImage = new Image();
	
	if (curFile == bigFile) {
		document.getElementById("mag_" + id).innerHTML = "Expand";
		swapImage.src = "http://www.adiumxtras.com/images/pictures/" + smallFile;
	} else {
		document.getElementById("mag_" + id).innerHTML = "Contract";
		swapImage.src = "http://www.adiumxtras.com/images/pictures/" + bigFile;
	}
	
	image.src = swapImage.src;
}

var oldInfo = new Array(5);

function showhighlight (id, number) {
	var full = new Image();
	full.src = "http://www.adiumxtras.com/images/star_full_hi.png";
	for (number; number > 0; number--) {
		oldInfo[number] = new Image();
		oldInfo[number].src = document.getElementById("xtra" + id + "_" + number).src;
		document.getElementById("xtra" + id + "_" + number).src = full.src;
	}
}

function killhighlight (id, number) {
	for (number; number > 0; number--)
		document.getElementById("xtra" + id + "_" + number).src = oldInfo[number].src;
}

// This portion "inspired" by http://www.bartelme.at/scripts/common.js

var isSafari = (navigator.userAgent.indexOf("WebKit") != -1);

if (!isSafari) {
	// Preload!
	alternateSearch = new Image();
	alternateSearch.src = "http://www.adiumxtras.com/images/searchbkg_on.png";
}

function search_clearValue(element, name) {
	if (!isSafari) {
		var search = document.getElementById(element);
		if (search.value == 'Search ' + name) {
			search.value='';
			search.style.color = "#000000";
		}
		document.getElementById(element + "Form").style.background = "url(http://www.adiumxtras.com/images/searchbkg_on.png) no-repeat";
	}
}

function search_setValue(element, name) {
	if (!isSafari) {
		var search = document.getElementById(element);
		if (search.value == '') {
			search.style.color = "#888888";
			search.value = 'Search ' + name;
		}
		document.getElementById(element + "Form").style.background = "url(http://www.adiumxtras.com/images/searchbkg.png) no-repeat";
	}
}

function search_handleSubmit(element, name) {
	if (!isSafari)
		return;
	// Avoid blank dropdown entries by just redirecting them.
	if (document.getElementById(element).value == '') {
		if (element == "search")
			window.location = 'index.php?a=search';
		else
			window.location = 'index.php?a=' + element + '&do=search';
	} else
		document.getElementById(element + "f").submit();
}


function makeSearch(element, name) {
	var searchInput = document.getElementById(element);
	if (isSafari) {
		searchInput.setAttribute("type", "search");
		searchInput.setAttribute("autosave", "adiumxtras" + element);
		searchInput.setAttribute("results", "10");
		searchInput.setAttribute("placeholder", "Search " + name);
		searchInput.setAttribute("onsearch", "search_handleSubmit('" + element + "', '" + name + "');");
		if (searchInput.value == "Search " + name)
		searchInput.value = '';
	} else {
		var searchForm = document.getElementById(element + "Form");
		searchForm.style.top = "1px";
		searchForm.style.width = "154px";
		
		searchInput.style.border = "0";
		searchForm.style.background = "url(http://www.adiumxtras.com/images/searchbkg.png) no-repeat";
		searchInput.style.background = "none";
		if (searchInput.value == 'Search ' + name)
			searchInput.style.color = "#888888";
		//Somehow, the following line messes up camino.
		//searchInput.style.height = "25px";
		searchInput.style.fontSize = "11px";
		searchInput.style.width = "138px";
		searchInput.style.padding = "6px 0 4px 8px";
	}		
}

function replyComment (xtra_id, parent_id) {
	var former = document.getElementById("reply");
	// They've hit reply already. Remove the last one.
	if (former) {
		var parentNodeId = former.parentNode.getAttribute("id");
		former.parentNode.removeChild(former);
		// It seems they hit reply twice. Remove it the second time and end.
		if (parentNodeId == "c" + parent_id)
			return;
	}

	// Let's shorten the variable a bit. It's a lot to type/see!
	var comment = document.getElementById("c" + parent_id);
	iH = document.createElement("DIV");
	iH.setAttribute("id", "reply");
	
	var form = document.createElement("FORM");
	form.setAttribute("method", "post");
	form.setAttribute("action", "index.php");
	
	var hiddenNames = new Array("xtra_id", "parent_id", "a", "do");
	var hiddenValues = new Array(xtra_id, parent_id, "xtras", "add_comment");
	
	for (var x = 0; x < hiddenNames.length; x++) {
		var input = document.createElement("INPUT");
		input.setAttribute("name", hiddenNames[x]);
		input.setAttribute("type", "hidden");
		input.setAttribute("value", hiddenValues[x]);
		form.appendChild(input);
	}
	
	var textarea = document.createElement("TEXTAREA");
	textarea.setAttribute("cols", 60);
	textarea.setAttribute("rows", 8);
	textarea.setAttribute("name", "comment");
	textarea.setAttribute("style", "margin-left: 5px; width: 95%;");
	form.appendChild(textarea);
	
	var submit = document.createElement("INPUT");
	submit.setAttribute("type", "submit");
	submit.setAttribute("value", "Add Comment");
	form.appendChild(submit);
	
	iH.appendChild(form);
	comment.appendChild(iH);
}