/*=========================================================================
#
#    $Id: home_page_images.js,v 1.17 2007/03/05 21:53:42 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');
	
	if(portrait == 'P') {
		limit_str = 'height="300"';
	} else {
		limit_str = 'width="400"';
	}
	
	image_holder.innerHTML = '<a href="' +new_link + '"><img src="' + new_img + '" ' + limit_str + ' />';
	
	updateActions(iid);
	
	nameSpan.innerHTML = "<strong>" + display_name + "</strong>";
	locSpan.innerHTML = " - " + citystate;
	
	window.scrollTo(0,0);
	
}

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 + '\', ' + (page+1) + ')');
		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 UpdateThumbs(type, page) {
	
	var url = 'index.php?q=ajax/' + type + '/page/' + page;
	
	// First update the thumbs
	ajaxManager(url, 'resultcollection', type, page);
}

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('index.php?q=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', 'index.php?q=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() {
	
	// replace image links with jump menu
	var menuDiv = document.getElementById('new_menu');
	var linksP	= document.getElementById('old_menu');
	
	menuDiv.setAttribute('class', 'show');
	menuDiv.setAttribute('className', 'show');
	linksP.setAttribute('class', 'hide');
	linksP.setAttribute('className', 'hide');
	
	
	if(document.title == 'Friends for Peace | Recent Additions') {
		UpdateThumbs('recent', 1);
	} else if (document.title == 'Friends for Peace | Random') {
		UpdateThumbs('random', 1);
	} else {
		UpdateThumbs('popular', 1);
	}
	
	// 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);
	}

}

/*
 * Returns true if we expect there to be more results after this page
 */
function areMore() {
	var lastresult = document.getElementById('result_15');
	//alert("Checking: " + lastresult );
	
	
	if(lastresult != null ) {
		//alert("More to come");
		return true;
	} else {
		//alert("Last Page");
		return false;
	}
}

// Dreamweaver's popup window function:
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}