var votes = new Array();
var currentPage;
var searchField = null;

function initialize() {
	if (cookieExists("votes")) {
		votes = getCookie("votes").split("|");
	}

	loadGrid(1);
}

function checkReturn(evt,object) {
	var keynum;
	if (window.event) { //IE
		keynum = evt.keyCode
	} else if (evt.which) { //Netscape/Firefox/Opera
		keynum = evt.which
	}
	
	if (keynum == 13) {
		searchGrid(object.getAttribute("id"));
	}
}
function searchGrid(field) {
	//Check to see if this page is being called from view.asp, if so, store search and redirect to index.asp
	if (location.href.indexOf("view.asp") != -1) {
		var value = document.getElementById(field).value;
		if (value.length > 0) {
			location.href = "/Gallery/?" + field + "=" + escape(value);
		}
	} else {
		//Clear search text from the non-active field
		document.getElementById((field == "title" ? "last" : "title")).value = "";
		
		//Load results into grid
		searchField = field;
		loadGrid(1);
	}
}

function loadGrid(page) {
	//Handle support of passing this from IE to fix variable scope issue
	if (typeof(page) == "object") {
		var pageID = parseInt(page.firstChild.data);
		page = pageID;
	}
	currentPage = page;
	
	var http = new HttpClient("GET",true,false,"text");
	http.callback = function(text) {
		var target = document.getElementById("grid");
		emptyElement(target);
		
		zeroResults = false;
		
		//Hide 0 results message and top 5
		document.getElementById("zero_results").style.display = "none";
		
		var data = eval("("+text+")");
		if (data.entries.length == 0) {
			//Search returned 0 results, show message and top 5
			zeroResults = true;
			document.getElementById("zero_results").style.display = "block";
		} else {
			for (i = 0; i < data.entries.length; i++) {
				//Build table row
				if (i % 5 == 0) {
					var tr = document.createElement("tr");
				}
				
				//Build table cell
				var td = document.createElement("td");
				td.setAttribute("class","thumb_bg");
				td.setAttribute("className","thumb_bg");
				td.setAttribute("align","center");
				td.setAttribute("valign","top");
				
				//Build inner table
				var inner_table = document.createElement("table");
				var inner_tbody = document.createElement("tbody");
				
				//Build photo row
				var photo_row = document.createElement("tr");
				var photo_cell = document.createElement("td");
				photo_cell.setAttribute("align","center");
				photo_cell.setAttribute("valign","middle");
				photo_cell.setAttribute("height","130");
				var a = document.createElement("a");
				a.setAttribute("href","/gallery/view.asp?id=" + data.entries[i].id + "&title=" + data.entries[i].title);
				var img = document.createElement("img");
				img.setAttribute("src","/entries/" + data.entries[i].thumbnail);
				img.setAttribute("border","0");
				a.appendChild(img);
				photo_cell.appendChild(a);
				photo_row.appendChild(photo_cell);
				inner_tbody.appendChild(photo_row);
				
				//Build title row
				var title_row = document.createElement("tr");
				var title_cell = document.createElement("td");
				title_cell.setAttribute("align","center");
				title_cell.setAttribute("valign","bottom");
				title_cell.setAttribute("height","20");
				var a = document.createElement("a");
				a.setAttribute("href","/gallery/view.asp?id=" + data.entries[i].id + "&title=" + data.entries[i].title);
				a.setAttribute("class","entry_title");
				a.setAttribute("className","entry_title");
				a.appendChild(document.createTextNode(data.entries[i].title));
				title_cell.appendChild(a);
				title_row.appendChild(title_cell);
				inner_tbody.appendChild(title_row);
				
				//Build button row
				var button_row = document.createElement("tr");
				var button_cell = document.createElement("td");
				button_cell.setAttribute("align","center");
				var button = document.createElement("img");
				button.setAttribute("id",data.entries[i].id);
				/*if (votes.indexOf(""+data.entries[i].id+"") != -1) {
					button.setAttribute("src","/gallery/imgs/btn_vote_disable.jpg");
				} else {
					button.setAttribute("src","/gallery/imgs/btn_vote.jpg");
					button.setAttribute("class","pointer");
					button.setAttribute("className","pointer");
					if (document.all) {
						button.onclick = function(){vote(this,'grid');};
					} else {
						button.setAttribute("onclick","vote(this,'grid');");
					}
				}*/
				button.setAttribute("src","/gallery/imgs/btn_vote_disable.jpg");
				button_cell.appendChild(button);
				button_row.appendChild(button_cell);
				inner_tbody.appendChild(button_row);
				
				//Build votes row
				var votes_row = document.createElement("tr");
				var votes_cell = document.createElement("td");
				votes_cell.setAttribute("align","center");
				votes_cell.setAttribute("class","redvotes");
				votes_cell.setAttribute("className","redvotes");
				var span = document.createElement("span");
				span.setAttribute("id", "votes_"+data.entries[i].id);
				span.appendChild(document.createTextNode(data.entries[i].votes));
				votes_cell.appendChild(span);
				votes_cell.appendChild(document.createTextNode(" Votes"));
				votes_row.appendChild(votes_cell);
				inner_tbody.appendChild(votes_row);
				
				//Attach inner table
				inner_table.appendChild(inner_tbody);
				td.appendChild(inner_table);
				
				//Attach table cell
				tr.appendChild(td);
				
				//Attach table row
				if (i % 5 == 4 || i == (data.entries.length - 1)) {
					target.appendChild(tr);
				}
			}
		}
		
		//Build pagination controls
		var pagination = document.getElementById("pagination");
		emptyElement(pagination);
		
		//Previous
		if (!zeroResults) {
			if (currentPage > 1) {
				var img = document.createElement("img");
				img.setAttribute("align","absmiddle");
				img.setAttribute("src","/imgs/previous.png");
				img.setAttribute("class","pointer");
				img.setAttribute("className","pointer");
				img.setAttribute("border","0");
				if (document.all) {
					var temp = currentPage - 1;
					img.onclick = function(){loadGrid(temp);};
				} else {
					img.setAttribute("onclick","loadGrid("+(currentPage - 1)+");");
				}
				pagination.appendChild(img);
				
				var a = document.createElement("a");
				if (document.all) {
					var temp_previous = currentPage - 1;
					a.onclick = function(){loadGrid(temp_previous);};
				} else {
					a.setAttribute("onclick","loadGrid("+(currentPage - 1)+");");
				}
				a.appendChild(document.createTextNode("Previous"));
				pagination.appendChild(a);
				pagination.appendChild(document.createTextNode(" | "));
			} else {
				/*var img = document.createElement("img");
				img.setAttribute("src","/imgs/previous.png");
				pagination.appendChild(img);
				pagination.appendChild(document.createTextNode("Previous | "));*/
			}
		}
		
		//Page numbers
		for (j = 1; j <= parseInt(data.pages); j++) {
			if (j == currentPage) {
				pagination.appendChild(document.createTextNode((j > 1 ? " | " : "")));
				var span = document.createElement("span");
				span.setAttribute("class","current");
				span.setAttribute("className","current");
				span.appendChild(document.createTextNode(j));
				pagination.appendChild(span);
			} else {
				pagination.appendChild(document.createTextNode((j > 1 ? " | " : "")));
				var a = document.createElement("a");
				if (document.all) {
					//Since IE is ultra-lame and doesn't understand variable scope, pass this instead of a page number
					a.onclick = function(){loadGrid(this);};
				} else {
					a.setAttribute("onclick","loadGrid("+j+");");
				}
				a.appendChild(document.createTextNode(j));
				pagination.appendChild(a);
			}
		}
		
		pagination.appendChild(document.createTextNode(" "));
		
		//Next
		if (!zeroResults) {
			if (currentPage < parseInt(data.pages) && data.entries.length > 0) {
				pagination.appendChild(document.createTextNode(" | "));
				var a = document.createElement("a");
				if (document.all) {
					var temp = currentPage + 1;
					a.onclick = function(){loadGrid(temp);};
				} else {
					a.setAttribute("onclick","loadGrid("+(currentPage + 1)+");");
				}
				a.appendChild(document.createTextNode("Next"));
				pagination.appendChild(a);
				
				var img = document.createElement("img");
				img.setAttribute("align","absmiddle");
				img.setAttribute("src","/imgs/next.png");
				img.setAttribute("class","pointer");
				img.setAttribute("className","pointer");
				img.setAttribute("border","0");
				if (document.all) {
					var temp_next = currentPage + 1;
					img.onclick = function(){loadGrid(temp_next);};
				} else {
					img.setAttribute("onclick","loadGrid("+(currentPage + 1)+");");
				}
				pagination.appendChild(img);
			} else {
				/*pagination.appendChild(document.createTextNode(" | Next"));
				var img = document.createElement("img");
				img.setAttribute("src","/imgs/next.png");
				pagination.appendChild(img);*/
			}
		}
	}
	var url = "/ajax/data.asp?action=view&page="+page;
	if (searchField == "title" || searchField == "last") {
		var value = document.getElementById(searchField).value;
		if (value.length > 0) {
			url += "&"+searchField+"="+value;
		}
	}
	http.makeRequest(url,null);
}

var currentID;
var currentFrom;
function vote(object,from) {
	currentID = object.getAttribute("id");
	currentFrom = from;
	
	//Enforce 5 vote limit
	if (votes.length <= 5) {
		var http = new HttpClient("GET",true,false,"text");
		http.callback = function(text) {
			var data = eval("("+text+")");
			
			//Determine how to update UI based on where user is voting from
			if (currentFrom == "inner") {
				//Update vote counter
				var counter = document.getElementById("inner_votes_"+currentID);
				emptyElement(counter);
				counter.appendChild(document.createTextNode(data.votes));
			
				//Disable vote button
				object.setAttribute("src","/gallery/imgs/btn_large_vote_disable.jpg");
				object.onclick = "";
				object.setAttribute("class","");
				object.setAttribute("className","");
			} else {
				//Update vote counter
				var counter = document.getElementById("votes_"+currentID);
				emptyElement(counter);
				counter.appendChild(document.createTextNode(data.votes));
			
				//Disable vote button
				object.setAttribute("src","/gallery/imgs/btn_vote_disable.jpg");
				object.onclick = "";
				object.setAttribute("class","");
				object.setAttribute("className","");
			}
			
			//Update cookie
			votes.push(currentID);
			setCookie("votes",votes.join("|"),tomorrow,"/");
		}
		http.makeRequest("/ajax/data.asp?action=vote&id="+object.getAttribute("id"),null);
	} else {
		//Display voted too much message
		alert("You have already voted 5 times today. Please come back tomorrow.");
	}
}
