var deactiveLinks = new Array(3)
var onMC = new Array(3)

function vote() {
	var vote = new votePopup()
}

function deactivateLinks() {
	return true;
	var wrapper = document.getElementById("popupWindow")
	var links = wrapper.getElementsByTagName('a');
	for (i=1;i<links.length;i++) {
		deactiveLinks[i-1] = links[i].href
		onMC[i-1] = links[i].onclick
		links[i].removeAttribute('href')
		links[i].removeAttribute('onclick')
	}
}

function votePopup() {
	// This is object Javascript.
	this.popupWindow = document.getElementById("popupWindow")
	if (!this.popupWindow) {
		var vBody = document.getElementsByTagName("body")[0]
		var vNode = document.createElement("div")
		    vNode.style.width="400px"
		    vNode.style.height="300px"
		    vNode.style.position="absolute"
		    vNode.style.top="50%"
		    vNode.style.left="50%"
		    vNode.style.zIndex="2147483647"
		    vNode.style.marginLeft="-200px"
		    vNode.style.marginTop="-150px"
		    vNode.style.backgroundImage="url(images/parchment.png)"
		    vNode.style.display=""
		    vNode.id='popupWindow'
		vBody.appendChild(vNode)
		this.popupWindow=document.getElementById("popupWindow")
		addClose("window");
		populateVoteWindow(this.popupWindow)
	}	
	
	// Define the prototypes, to reduce namespace pollution.
	if (typeof(_votePopupDefined) == "undefined") {
		_votePopupDefined = true
	}
}

function populateVoteWindow(voteWindow) {
	var ajax = createAJAX()
	var date = new Date;
	var seconds = Math.floor(date.getTime()/1000)
	ajax.open("GET", "votingWindow.php?query=populate&ts="+seconds, true)
	ajax.onreadystatechange = function() {
		if ((ajax.readyState == 4) && (ajax.status == 200)) {
			if (ajax.responseText == "close") {
				document.getElementById("popupWindow").innerHTML=''
				hidePopup()
			} else if(ajax.responseText == "continue") {
				return true
			} else {
				addHTML(ajax.responseText)
				deactivateLinks();
			} 
		} else if ((ajax.readyState == 4) && (ajax.status != 200)) {
			hidePopup();
		}
	}
	ajax.send(null)	
}

function addClose(type) {
	if (type == "window") {
	addHTML('<div style="height:15px;border:0;margin:0;padding:0;width:400px;"><div style="margin:0;padding:0;text-align:right;"><a href="#" onclick="hidePopup();return false"><img style="width:41px;height:15px;border:0;margin:0:border:0" src="images/voteClose.gif" /></a></div></div>')
		//addHTML('<div style="background-color:#0011FF;height:15px;border:0;margin:0;padding:0;border-bottom:1px solid #0000AA;width:400px;"><div style="margin:0;padding:0;text-align:right;"><a href="#" onclick="hidePopup();return false"><img style="width:41px;height:15px;border:0;margin:0:border:0" src="images/close.gif" /></a></div></div>')
	}
}
	
function addHTML(html) {
	var popupWindow=document.getElementById("popupWindow")
	var temp = popupWindow.innerHTML
	popupWindow.innerHTML = temp + html
}

function hidePopup() {
	var popupWindow = document.getElementById("popupWindow");
	popupWindow.style.display="none"
}
	
function createAJAX() {
        var obj = false;
        try {
                obj = new XMLHttpRequest();
        } catch (e) {
                try {
                        obj = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                        try {
                                obj = new ActiveXObject("Msxml2.XMLHTTP");
                        } catch (e) {
                                alert("It appears that your browser has no AJAX support. Consider upgrading to firefox 3!");
                        }
                }
        }
        return obj;
}

function voteFor(url) {
	var username = document.getElementById("voterUsername")
	if (username.value == "") {
		username.style.backgroundColor="#CC0000"
		username.select();
		username.focus();
		return false;
	}
	recordVote(username.value, url)
}

function recordVote(username, voted) {
	var ts = new Date
	var url = "votingWindow.php?query=castvote"
	    url = url + "&acct=" + username
	    url = url + "&ts=" + ts.getTime()
	    url = url + "&vote=" + voted
	var obj = createAJAX()
	obj.open("GET", url, true);
	obj.onreadystatechange=function() {
		if (obj.readyState == 4) {
			if (obj.responseText == "close") {
				document.getElementById("voteContents").innerHTML='';
				hidePopup();
			} else if (obj.responseText != "continue") {
				document.getElementById("voteContents").innerHTML=obj.responseText
			}
		}
	}
	obj.send(null)	
}

