/*=========================================================================
#
#    $Id: search_results.js,v 1.11 2007/03/15 19:00:46 acrosman Exp $
#
#=========================================================================
#
#    Copyright 2006 American Friends Service Committee
#
#===============================- GPL Header -=================================
#
#    This program is free software; you can redistribute
#    it and/or modify it under the terms of the GNU General Public License
#    as published by the Free Software Foundation; either version 2 of
#    the License, or (at your option) any later version.
#
#    This program is distributed in the hope that it
#    will be useful, but WITHOUT ANY WARRANTY; without even the implied
#    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#    See the GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with AFSC Vigil Registration System; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    
#========================================================================= */
/*
 * Replace the current image and link with the suggested inputs
 *
 */
function ReplaceImage(iid, new_link, new_img, portrait, display_name, citystate) {
	
	var image_holder=document.getElementById('photobg');
	var nameSpan = document.getElementById('photodisplayname');
	var locSpan = document.getElementById('photolocation');

	
	image_holder.innerHTML = '<a href="' +new_link + '"><img src="' + new_img + '" />';
	
	updateActions(iid);
	
	nameSpan.innerHTML = "<strong>" + display_name + "</strong>";
	locSpan.innerHTML = "<br />" + citystate;

}

function UpdateControls(type, page) {
	// Next update the prev/next links.
	var pagers = document.getElementById('paging');
	var more = areMore();
	var pagerMore = null;
	var pagerLess = null;
	var fillerText = null;
	
	if(page > 1) {
		pagerLess = document.createElement('a');
		pagerLess.setAttribute('href', 'javascript: onclick=UpdateThumbs(\'' + type + '\', ' + (page-1) + ')');
		pagerLess.setAttribute('id',"more");
		pagerLess.innerHTML = "Previous";
		
		if(more) {
			fillerText = document.createTextNode(' | ');
		}
	}
	if(more) {
		pagerMore = document.createElement('a');
		pagerMore.setAttribute('href','javascript: onclick=UpdateThumbs( \''+ type + '\', ' + '2)');
		pagerMore.setAttribute('id',"more");
		pagerMore.innerHTML = "Next";
	}
	
	
	pagers.innerHTML = '';
	if(pagerLess != null)
		pagers.appendChild(pagerLess);
	if(fillerText != null)
		pagers.appendChild(fillerText);
	if(pagerMore != null)
		pagers.appendChild(pagerMore);
	

}

function updateActions(iid) {
	// Update the report and TAF links for a new image
	var report_link = document.getElementById('rep_anchor');
	var taf_link = document.getElementById('mail_anchor');
	var fav_li = document.getElementById('favorite');
	var fav_link = document.createElement('a');
	
	// Update the fav link first
	var new_link_text = "javascript: onclick=sendVote('vote/" + iid +  "')";
	fav_link.setAttribute('href', new_link_text);
	fav_link.setAttribute('id', 'fav_anchor');
	fav_link.innerHTML="I like this one!";
	
	fav_li.innerHTML = '';
	fav_li.appendChild(fav_link);
	// Update just the href's on the other two anchors
	//report_link.setAttribute('href', 'index.php?q=report/' + iid);
	taf_link.setAttribute('href', 'taf/' + iid);
	
}

function updateVoting(){
	// Find the link to update
	var a = document.getElementById('fav_anchor');
	var linkattrs = a.attributes;
	
	var old_link_text = linkattrs.getNamedItem("href").value;
	
	var new_link_text = "javascript: onclick=sendVote('" + old_link_text +  "')";

	a.removeAttribute('href');	
	a.setAttribute('href', new_link_text);

}

window.onload=function() {
	
	//enableJS();	

}

// Enable all the links of the thumbnails to be JS enabled,
// and make the voting links JavaScript enabled as well.
function enableJS() {
	
	var thumbs = document.getElementById("resultcollection");
	var anchor = null;
	var input = null;
	var clickable = null;
	
	var result = null;
	var counter = 1;
	while (result = document.getElementById("result_" + counter) ) {
		
		anchor = result.getElementsByTagName("a");
		anchor= anchor[0]; // make sure I have to actual node
		
		input = anchor.firstChild;
		
		clickable = 'javascript: onclick=' + input.getAttribute('clickable');
		
		anchor.setAttribute('href', clickable);
		anchor.removeChild(anchor.firstChild);
		counter ++;
	}
	
	// Change vote link to be JS enabled.
	updateVoting();
}

function sendVote( voteURL ) {
	
	if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}

	if (x){
		x.onreadystatechange = function()			
		{		
			if (x.readyState == 4 && x.status == 200){
					el = document.getElementById('favorite');					
					el.innerHTML = "<span>Vote Processed</span>";
			}
		}
				
		x.open("GET", voteURL, true);	
		x.send(null);
	}

}

// ajaxManager is based on the ajaxManager function at: 
// http://dhtmlnirvana.com/ajax/ajax_tutorial/
function ajaxManager() {

	var args = ajaxManager.arguments;
	

	if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}

	if (x){
		x.onreadystatechange = function()			
		{		
			if (x.readyState == 4 && x.status == 200){
					el = document.getElementById(args[1]);					
					el.innerHTML = x.responseText;
					UpdateControls(args[2], args[3]);
			}			
		}
		
		//alert(args[0]);
		
		x.open("GET", args[0], true);	
		x.send(null);
	}

}

// Dreamweaver's popup window function:
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}