

function locationChangedId(id,name) {
	var locationId = document.getElementById("contentLocationId");
	
	if(locationId!=null) {
		document.getElementById("contentStart").value = 0;
		locationId.value = id;
		var cU = document.getElementById("contentUrl");
		var cA = document.getElementById("contentAction");
		if(cU!=null) {
			cU = cU.value;
		}
		if(cA!=null) {
			cA=cA.value;
		}
		document.getElementById('searchLocation').value=name;
		document.getElementById('searchLocationId').value=id;
		
		switchTabLinks(locationId.value);
		loadContent(null,cU,cA);
		
		
		var efpl = document.getElementById("exploreFrontPageLink");
			if(efpl!=null) {
			efpl.href = 'today?id='+id;
		}
	}
}

function locationChangedNear(force) {
	var nearString = document.getElementById("contentNearString");
	
	if(nearString!=null || force) {
		document.getElementById("contentStart").value = 0;
		var cU = document.getElementById("contentUrl");
		var cA = document.getElementById("contentAction");
		if(cU!=null) {
			cU = cU.value;
		}
		if(cA!=null) {
			cA=cA.value;
		}

		loadContent(null,cU,cA);
		
	}
}



function locationChanged(lpin) {
	var locationId = document.getElementById("contentLocationId");
	
	//YAHOO.log("hi guys2:"+locationId.value+" lpin.value:"+lpin.value);

	if(locationId!=null) {
		document.getElementById("contentStart").value = 0;
		//YAHOO.log("hi guys3: new value:"+lpin.value);
		if(lpin!=null) {
		locationId.value = lpin.value;
		}
		var cU = document.getElementById("contentUrl");
		var cA = document.getElementById("contentAction");
		if(cU!=null) {
			cU = cU.value;
		}
		if(cA!=null) {
			cA=cA.value;
		}
		switchTabLinks(locationId.value);
		loadContent(null,cU,cA);
		
		
		var efpl = document.getElementById("exploreFrontPageLink");
		if(efpl!=null && lpin!=null) {
			efpl.href = 'today?id='+lpin.value;
			efpl.innerHTML = 'Front Page for '+lpin.getAttribute('locationTitle');
		}
	}
}

var CURRENT_START_OFFSET = 0;

function loadContent(previousNext,theurl,action) {
	resetSlide();
	hideSlideshow();
	
	var div = document.getElementById("new");
	div.innerHTML = "<img src=\"images/ajax-loader.gif\" alt=\"loading...\"/>";

/*	
	var tO = document.getElementById("tagOptions");
	if(tO!=null) {
		tO.innerHTML = "<img src=\"images/ajax-loader.gif\" alt=\"loading...\"/>";
	}
	*/
	var url = "explore";
	if(theurl!=null) {
		url = theurl;
	} else {
		var cu = document.getElementById("contentUrl");
		var ca = document.getElementById("contentAction");
		if(cu!=null) {
			url = cu.value;
		}
		if(ca!=null) {
			action = ca.value;
		}
		
	}
	//YAHOO.log("LCBABY");
	var locationId = document.getElementById("contentLocationId").value;
	var nearString = document.getElementById("contentNearString").value;
	
	var type = document.getElementById("contentType").value;
	var last = document.getElementById("contentLast").value;
	var sort = document.getElementById("contentSort").value;
	
	
	var tagList = "";
	var count = 0;
	for(mrr in allTagCheckboxes) {
		if(allTagCheckboxes[mrr].checked) {
			if(count!=0) {
				tagList = tagList + ",";
			}
			tagList = tagList+allTagCheckboxes[mrr].value;
			count = count+1;
		}
	}
	
	
	var increm = 25;
	if(document.getElementById("contentIncrement")!=null) {
		increm = parseInt(document.getElementById("contentIncrement").value);
	}
	//YAHOO.log("LOADING:"+locationId);
	var newStart;
	if(previousNext=='next') {
		newStart = last;
	} else if(previousNext=='previous') {
		newStart = 	parseInt(document.getElementById("contentStart").value)-increm;
		if(newStart<0) {
		 newStart=0;
		}
	} else {
		newStart = parseInt(document.getElementById("contentStart").value)-1;
		if(newStart<0) {
		 newStart=0;
		}
	}

	CURRENT_START_OFFSET = newStart;
	
	var creatorId = -1;
	var creatorIdStr = document.getElementById("contentCreatorId");
	if(creatorIdStr!=null) {
	
		creatorIdStr = creatorIdStr.value;
	}
	if(creatorIdStr!=null && creatorIdStr!='') {
		creatorId = parseInt(creatorIdStr);
	}

	var columnId = -1;
	var columnIdStr = document.getElementById("contentColumnId");
	if(columnIdStr!=null) {
		columnIdStr = columnIdStr.value;
	}
	if(columnIdStr!=null && columnIdStr!='') {
		columnId = parseInt(columnIdStr);
	}

	var relatedId = -1;
	var relatedIdStr = document.getElementById("contentRelatedId");
	if(relatedIdStr!=null) {
		relatedIdStr = relatedIdStr.value;
	}
	if(relatedIdStr!=null && relatedIdStr!='') {
		relatedId = parseInt(relatedIdStr);
	}

	var tagCreatorId = -1;
	var tagCreatorIdStr = document.getElementById("contentTagCreatorId");
	if(tagCreatorIdStr!=null) {
		tagCreatorIdStr = tagCreatorIdStr.value;
	} 
	
	if(tagCreatorIdStr!=null && tagCreatorIdStr!='') {
		tagCreatorId = parseInt(tagCreatorIdStr);
	}

	var tagMax = document.getElementById("contentTagMax");
	if(tagMax!=null) {
		tagMax=tagMax.value;
	} else {
		tagMax=-1;
	}
	
	//YAHOO.log("START:"+newStart);
	if(action==null) {
		action = "contentJax";
	}
//	YAHOO.log("ZZZurl:"+url+" action:"+action+" CREATORID:"+creatorId+" locId:"+locationId);
	var params = "";
	if(locationId!=-1) {
		params = params+"id="+locationId;
	} 

	if(nearString!=null && nearString!='') {
		params = params+"&near="+escape(nearString);
	}
	
	var contentRadius = document.getElementById('contentRadius');
	if(contentRadius!=null) {
		params = params+"&contentRadius="+escape(contentRadius.value);
	}
	
	if(type!=-1) {
		params = params+"&type="+type;
	}
	if(newStart>0) {
		params = params+"&start="+newStart;
		params = params+"&offset="+newStart;
	}
	
	params = params +"&sort="+sort;
	
	if(tagList!='') {
		params = params+"&tagList="+tagList;
	}
	
	if(creatorId!='-1') {
		params = params+"&creatorId="+creatorId;
	}
	if(tagMax!=30) {
		params = params+"&contentTagMax="+tagMax;
	}
	if(tagCreatorId!='-1') {
		params = params+"&tagCreatorId="+tagCreatorId;
	}
	
	if(columnId!='-1') {
		params = params+"&columnId="+columnId;
	}

	if(relatedId!='-1') {
		params = params+"&relatedId="+relatedId;
	}
	
	var hideVotes = document.getElementById("hideFilter");
	if(hideVotes!=null && hideVotes.value!='') {
		params = params + "&hideFilter="+hideVotes.value;
	}

	var filter = document.getElementById("contentQualityFilter");
	if(filter!=null && filter.value!='') {
		params = params + "&filter="+filter.value;
	}
	
	var global = document.getElementById("isGlobalView");
	if(global!=null && global.value!='') {
		params = params + "&global="+global.value;
		if(global.value=='true') {
			document.getElementById('nearControls').style.display = 'none';
			document.getElementById('exploreFilterBox').style.backgroundColor = '#CCCCFF;';
		} else if(global.value=='false') {
			document.getElementById('nearControls').style.display = 'block';
			document.getElementById('exploreFilterBox').style.backgroundColor = 'lightBlue';
		}
	}
	
	
	var latLng = document.getElementById("contentLatLng");
	if(latLng!=null && latLng.value!='') {
		params = params + "&latLng="+latLng.value;
	}
	
	
	
	var showVoteButtons = document.getElementById("showVoteButtons");
	
	if(showVoteButtons!=null) {
		//YAHOO.log("svb:"+showVoteButtons.checked);
		if(showVoteButtons.checked) {
			showVoteButtons = true;
			params = params + "&showVotes=true";		
		}
	}

	var contentRadius = document.getElementById("contentRadius");
	
	if(contentRadius!=null) {
		params = params + "&radius="+contentRadius.value;		
	}



	var blogView = document.getElementById("contentColumnView");
	
	if(blogView!=null) {
		//YAHOO.log("svb:"+showVoteButtons.checked);
		if(blogView.checked) {
			params = params + "&blogView=true";		
		}
	}

	var listView = document.getElementById("contentListView");
	
	if(listView!=null) {
		if(listView.checked) {
			params = params + "&listView=true";		
		}
	}



	var needsPhoto = document.getElementById("contentNeedsPhoto");
	
	if(needsPhoto!=null) {
		if(needsPhoto.checked) {
			params = params + "&needsPhoto=true";		
		}
	}


	
	var sshow = document.getElementById('slideshowButton');
	if(sshow!=null) {
		if(showVoteButtons!=true && (type==69 || type==44 || type==45)) {
			sshow.style.display = 'inline';	
		} else {
			sshow.style.display = 'none';
		}
	}
	
	
	var query = document.getElementById("contentQuery");
	if(query!=null && query.value!=null && query.value!='' && query.value!='Search here....') {
		params = params + "&query="+query.value;
		//alert('addin the param:'+query.value);
		//showDiv('queryFilter');
	} else {
//		hideDiv('queryFilter');
		query = null;
	}
	var useTags = document.getElementById("contentUseTags");
	if(useTags!=null && useTags.value!='true') {
		params = params + "&useTags="+useTags.value;
	}
	
	var useFields = document.getElementById("contentUseFields");
	if(useFields!=null && useFields.value!='true') {
		params = params + "&useFields="+useFields.value;
	}
	
	var useTitles = document.getElementById("contentUseTitles");
	if(useTitles!=null && useTitles.value!='true') {
		params = params + "&useTitles="+useTitles.value;
	}
	
	
    var req = initRequest(url);
	 var isloc = isLoc(type);
    
    makeAMap();
    
	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseLoadContentResponse(req.responseText,previousNext,newStart,increm);
            	goAnchor('pagerBarTop');
	        } else if(req.status==12019)  {
	        
	        } else {
				alert("Error sending search:"+req.status);
	        }
	    }
	};
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send("action="+action+"&"+params);
    
    var rssLink = document.getElementById('rssLink');
    if(rssLink!=null) {
	    rssLink.href = "explore?action=rss&"+params;
	}
}

var CONTENT_TOTAL_COUNT = null;

function parseLoadContentResponse(thehtml,previousNext,newStart,increm) {
	var div = document.getElementById("new");
	div.innerHTML = thehtml;
	
	
	var lastInput = document.getElementById("contentLast");
	var startInput = document.getElementById("contentStart");

	var totalCount = document.getElementById("updatedTotalCount").value;
	
	var newTitle = document.getElementById("updatedTitle");
	if(newTitle!=null) {
		newTitle = newTitle.innerHTML;
		var cTitle = document.getElementById("contentHeaderTitle");
		if(cTitle!=null) {
			cTitle.innerHTML=newTitle;
		}
		newTitle.innerHTML = '';
	}
		
		
	//YAHOO.log("new tc:"+totalCount);
	CONTENT_TOTAL_COUNT = parseInt(totalCount);
	document.getElementById("contentTotalCount").value = totalCount;
	var newLast;
	
		newLast = parseInt(newStart)+increm;
		var np = document.getElementById("nextPageButton");
		var np2 = document.getElementById("nextPageButton2");
		if(np!=null && np2!=null) {
			if(newLast>=totalCount) {
				newLast = totalCount;
				np.style.display = 'none';
				if(np2!=null) {
					np2.style.display = 'none';
				}
			} else {
				np.style.display = 'inline';
				if(np2!=null) {
					np2.style.display = 'inline';
				}
			}
		

			var pb = document.getElementById("previousPageButton");
			var pb2 = document.getElementById("previousPageButton2");
			if(newStart<increm) {
				pb.style.display = 'none';
				if(pb2!=null) {
					pb2.style.display = 'none';
				}
			} else {
				pb.style.display = 'inline';
				if(pb2!=null) {
					pb2.style.display = 'inline';
				}
				
			}
		
		}
	
	startInput.value = newStart;
	lastInput.value = newLast;
	
	var lastSpan = document.getElementById("lastSpan");
	var startSpan = document.getElementById("startSpan");
	var totalCountSpan = document.getElementById("totalCountSpan");


	var lastSpan2 = document.getElementById("lastSpan2");
	var startSpan2 = document.getElementById("startSpan2");
	var totalCountSpan2 = document.getElementById("totalCountSpan2");

	
	

	if(lastSpan!=null) {	
		lastSpan.innerHTML = newLast;
	}
	
	if(startSpan!=null) {
		if(totalCount=='0') {
			startSpan.innerHTML = '0';
		} else {
			startSpan.innerHTML = parseInt(newStart)+1;
		}
		totalCountSpan.innerHTML = totalCount;
	}

	if(lastSpan2!=null) {
		lastSpan2.innerHTML = newLast;
		if(totalCount=='0') {
			startSpan2.innerHTML = '0';
		} else {
			startSpan2.innerHTML = parseInt(newStart)+1;
		}
		totalCountSpan2.innerHTML = totalCount;
	}
	if(maps!=null) {
		deleteAllMarkers('mainLocMap');
	}

	var tO = document.getElementById("tagOptions");
	var utO = document.getElementById("updatedTagOptions");
	var ufc = document.getElementById("updatedFullContext");
	var upc = document.getElementById("updatedPlaceCard");
	var uzb = document.getElementById("updatedZoomBox");
	
	if(ufc!=null) {
		var fc = document.getElementById("fullContext");
		if(fc!=null) {
			fc.innerHTML = ufc.innerHTML;
			ufc.innerHTML = '';
			var atags = fc.getElementsByTagName("A");
			for(n in atags) {
				sweetTitles.checkAndAdd(atags[n]);
			}
		}
	}
	
	if(upc!=null) {
		var pc = document.getElementById("placeCard");
		if(pc!=null) {
			pc.innerHTML = upc.innerHTML;
			upc.innerHTML = '';
			var atags = pc.getElementsByTagName("A");
			for(n in atags) {
				sweetTitles.checkAndAdd(atags[n]);
			}
		}
	}
	
	if(uzb!=null) {
		 var zb = document.getElementById("zoomBox");
		 if(zb!=null) {
		 	zb.innerHTML = uzb.innerHTML;
		 	uzb.innerHTML = '';
		 }
	}
		 	
	
	var atags = div.getElementsByTagName("A");
	for(n in atags) {
		sweetTitles.checkAndAdd(atags[n]);
	}
	
	
	if(utO!=null) {
		tO.innerHTML = utO.innerHTML;
		utO.innerHTML = '';
	}
			
	var whd = document.getElementById("newjax");
	if(whd!=null) {
		whd.onclick();
	} else {
		}

	var whde = document.getElementById("newjaxEntries");
	if(whde!=null) {
		whde.onclick();
	} else {
		}


}



var allTagCheckboxes = new Array();

function addTagFilter(tag) {
	if(tag==null || tag=='') {
		return;
	}
	if(allTagCheckboxes[tag]!=null) {
		allTagCheckboxes[tag].checked = !allTagCheckboxes[tag].checked;
		clickTagFilter(tag,allTagCheckboxes[tag]);
		return;
	} 
	var clist = document.getElementById("tagChecklistEntries");
	var clistBox = document.getElementById("tagChecklistBox");
	clistBox.style.display='block';
	
	var newDiv = document.createElement("span");
	newDiv.className = "tagFilter";
	var input = document.createElement("input");
	input.setAttribute("type","checkbox");
	input.setAttribute("checked","true");
	input.setAttribute("value",tag);
	input.onclick = function() {
		clickTagFilter(tag,input);
	};
	newDiv.appendChild(input);
	var txt = document.createTextNode(tag);
	newDiv.appendChild(txt);		

	clist.appendChild(newDiv);
	input.checked = true;
	allTagCheckboxes[tag]=input;
	checkTagFilter(tag);
}	

function checkTagFilter(tag) {
	//YAHOO.log("CHECKITYO:"+tag);
	locationChangedNear(true); 
	
}

function clickTagFilter(tag,input) {
	if(allTagCheckboxes[tag]==null) {
		allTagCheckboxes[tag] = input;
	}
	if(input.checked) {
		checkTagFilter(tag);
	} else {
		uncheckTagFilter(tag);
	}
}

function uncheckTagFilter(tag) {
locationChangedNear(true);
}

function clearAllFilters() {
	for(cb in allTagCheckboxes) {
		allTagCheckboxes[cb].checked = false;
	}
	allTagCheckboxes = new Array();
	var tcl = document.getElementById('tagChecklistEntries');
	hideDiv('tagChecklistBox');
	tcl.innerHTML = '';
	var lpin = document.getElementById('searchLocation');
	if(lpin != null && lpin.listener) {
		lpin.listener();
	} 
}

function switchTabLinks(newLocId) {

	var fpage = document.getElementById("todayTabLink");
	var newQ = document.getElementById("newTabLink");
	var content = document.getElementById("contentTabLink");
	var places = document.getElementById("placesTabLink");
	
	if(fpage!=null) {
		fpage.href = 'today?id='+newLocId;
	}
	
	if(newQ!=null) {
		newQ.href = 'entity?action=newQueue&locationId='+newLocId;
	}
	
	if(content!=null) {
		content.href = 'location?action=articles&id='+newLocId;
	}
	
	if(places!=null) {
		places.href = 'location?action=placeExplorer&id='+newLocId;
	}
	
}
	
var imageList = null;
var tipdivList = null;
var listLength;
var currentList;
var slideID;
var opacityID;
var stopped = false;
var recurID;


function resetSlide() {
	clearInterval(slideID);
	clearInterval(recurID);
	stopit();
	imageList = null;
	tipdivList = null;
	listLength = null;
	currentList = null;
	stopped = true;
}
	

function getWideUrl(url) {
	var offset = url.indexOf('_M_D_100.png');
	var size = 600;
	if(offset==-1) {
		offset = url.indexOf('_M_D_50.png');
		size = 400;
		if(offset!=-1) {
			return url.substring(0,offset) + "_M_D_"+size+".png";
		}
	}
	if(offset!=-1) {
		return url.substring(0,offset) + "_M_W_"+size+".png";
	}
	
	if(url.indexOf('places48.png')!=-1) {
		url = url.substring(0,url.indexOf('images/'))+'images/places400x300.png';
	} else if(url.indexOf('events48.png')!=-1) {
		url = url.substring(0,url.indexOf('images/'))+'images/events400x300.png';
	} else if(url.indexOf('48.png')!=-1) {
		url = url.substring(0,url.indexOf('images/'))+'images/articles400x300.png';
	}
	return url;
}

function getIdFromUrl(url) {
	var offset = url.indexOf('static/');
	if(offset!=-1) {
		var str = url.substring(offset+7);
		offset = str.indexOf("_M_W");
		if(offset==-1) {
			offset = str.indexOf("_M_D");
			if(offset==-1) {
				offset = str.indexOf("_V_M_W");
			}
		}
		str = str.substring(0,offset);
		return str;
	}
}




function fadeOutIn(opac,fadein,cbk) {
	var bd = document.getElementById("new");
	if(bd==null) {
		bd = document.getElementById("bigImage");
	}
	
	if(opac==null) {
		opac = 100;
	}
	var passed = parseInt(opac);

	if(!fadein) {
		var newOpac = parseInt(passed-10);
		if ( newOpac > 0 ) {
			bd.style.opacity = '.'+newOpac;
			bd.style.filter = "alpha(opacity:"+newOpac+")";
			slideID = window.setTimeout(function() {
				fadeOutIn(newOpac,fadein,cbk);
			},30);
		}
		else { 
			bd.style.opacity = '0.0';
			bd.style.filter = "alpha(opacity:0)";
			if(cbk!=null) {
				cbk();
			}
		}
	} else {
		var newOpac = parseInt(passed+10);
		if ( newOpac < 100 ) {
			bd.style.opacity = '.'+newOpac;
			bd.style.filter = "alpha(opacity:"+newOpac+")";
			slideID = window.setTimeout(function() {
				fadeOutIn(newOpac,fadein,cbk);
			},30);
		}
		else { 
			bd.style.opacity = '1.0';
			bd.style.filter = "alpha(opacity:100)";
			if(cbk!=null) {
				cbk();
			}
		}
	}
	
}
function timerFade(time,div,hide) {
	window.setTimeout(function() {
		fadeOutInDiv(null,false,null,div,hide);
	},time);
}

function fadeOutInDiv(opac,fadein,cbk,div,hide) {
		var lowOpacity = 0;
		if(hide==30) {
			hide = false;
			lowOpacity = 30;
		}
		if(opac==null) {
			opac = 100;
		}
		if(div==null) {
			return;
		}
		var passed = parseInt(opac);
	
		if(!fadein) {
			var newOpac = parseInt(passed-10);
			if ( newOpac > lowOpacity ) {
				div.style.opacity = '0.'+newOpac;
				div.style.filter = "alpha(opacity:"+newOpac+")";
				slideID = window.setTimeout(function() {
					fadeOutInDiv(newOpac,fadein,cbk,div,hide);
				},30);
			}
			else { 
				div.style.opacity = '0.'+lowOpacity;
				div.style.filter = "alpha(opacity:"+lowOpacity+")";
				if(hide) {
					div.style.display = 'none';
				}
				if(cbk!=null) {
					cbk();
				}
			}
		} else {
			var newOpac = parseInt(passed+10);
			if ( newOpac < 100 ) {
				div.style.opacity = '.'+newOpac;
				div.style.filter = "alpha(opacity:"+newOpac+")";
				slideID = window.setTimeout(function() {
					fadeOutInDiv(newOpac,fadein,cbk,div,hide);
				},30);
			}
			else { 
				div.style.opacity = '1.0';
				div.style.filter = "alpha(opacity:100)";
				if(cbk!=null) {
					cbk();
				}
			}
		}
}

function hideSlideshow() {
	var bd = document.getElementById("new");
	var newbuffer = document.getElementById("newbuffer");
	var controls = document.getElementById('slideshowControls');
	if(controls!=null) {
		controls.style.display = 'none';
	}
	if(newbuffer!=null) {
		bd.innerHTML = newbuffer.innerHTML;
		newbuffer.innerHTML = '';
	}
	imageList = null;
	tipDivList = null;
	idList = null;
}
var loopy = false;
function initImageList() {
	var bd = document.getElementById("new");
	if(bd==null) {
		bd = document.getElementById("photoset");
		loopy = true;
	}

	var newbuffer = document.getElementById("newbuffer");
	if(newbuffer==null) {
	//	newbuffer = document.createElement('div');
		//newbuffer.style.display = 'none';
	}
	var controls = document.getElementById("slideshowControls");
	if(controls!=null) {
		controls.style.display = 'block';
	}
	/* We have to copy over the contents so the tips and stuff are there! */
	/* Clear this on any content load! */
	if(newbuffer!=null) {
		newbuffer.innerHTML = bd.innerHTML;
	}
	
	imageList = new Array();
	tipDivList = new Array();
	idList = new Array();
	var div = document.createElement('div');
	if (bd != null) {
		div.innerHTML = bd.innerHTML;
	}
	
	if(newbuffer!=null) {
		bd.innerHTML = "<div id=\"container\" style=\"text-align:left;padding-bottom:500px;\"><a href=\"\" onclick=\"pauseplay();return false;\"><img border=\"0\" id=\"theimage\"/></a></div>";
	}
	
	// new / miniThumb/ a / img
	for(var i=0;i<div.childNodes.length;i++) {
		var di = div.childNodes[i];
		if(di.tagName=='DIV' || di.tagName=='SPAN') {
			var thea = di.childNodes[0];
			if(thea!=null) {
				if(thea.getAttribute) {
					var tipdivid = thea.getAttribute("tipdivid");
					var theimg = thea.childNodes[0].src;
					var entityId = thea.getAttribute("entityId");

					if(theimg!=null) {
						idList.push(entityId);
						imageList.push(theimg);
						if(tipdivid!=null) {
							tipDivList.push(tipdivid);

						}
						
						//YAHOO.log("di:"+theimg);
					}
				}
			}
		}
	}
	listLength = imageList.length;
	currentList = -1;
	if(loopy) {
		currentList = 0;
	}
}	


function toggleSlideControls() {
	var controls = document.getElementById('slideshowControls');
	if(controls.style.display=='block') {
		hideSlideShow();
	} else {
		showSlideShow();
	}	
}	

function showSlideShow() {
	var controls = document.getElementById('slideshowControls');
	controls.style.display = 'block';
	move(0);
}

var finished = false;

function doit(reset,getReady,repeat) {
	finished = false;
	if(imageList==null) {
		initImageList();
	}
	if(reset) {
		clearInterval(slideID);
		clearInterval(recurID);
		stopped = false;
	}
	if(stopped==false) {
		currentList = currentList+1;
		if(currentList>=listLength) {
			finished = true;
			currentList=0;
			go(0,false);
			return;
		}
	}
	if(!getReady) {
		go(currentList,true);
	}
}

function pauseplay() {
	if(stopped==false) {
		stopit();	
	} else {
		doit(true);
	}
}

function go(currentList,again,force) {

		var url = getWideUrl(imageList[currentList]);
		var tipdivid = tipDivList[currentList];
		var entityId = idList[currentList];
		
		var theimg = document.getElementById("theimage");
		if(theimg==null) {
			theimg = document.getElementById("bigImage");
		}
		var infosauce = document.getElementById("infosauce");
		if(!stopped || force) {
			// Fade out
			// Swap the img and the helpers
			
			var newimg = new Image();
			newimg.onclick = function() {
				self.location = ''+entityId;
			};
			newimg.onload = function() {
				opacityID =  fadeOutIn(100,false, function() {
					// Fade in
					theimg.src = newimg.src;
					theimg.onclick = newimg.onclick;
					
					var oldEntityId = theimg.getAttribute("entityId")
					theimg.setAttribute("entityId",entityId);
					
					var thumbSpan = document.getElementById("thumbSpan"+entityId);
					thumbSpan.style.borderBottomColor = "#3366CC";
					
					var oldThumbSpan = document.getElementById("thumbSpan"+oldEntityId);
					oldThumbSpan.style.borderBottomColor = "white";

					
					var isVid = url.indexOf('_V_')!=-1;
					if(isVid) {
		//				theimg.setAttribute('width',400);
						theimg.style.width = '400px';
					} else {
						theimg.style.width = 'auto';
					}	
					var contentStart = CURRENT_START_OFFSET;
					var contentLast = document.getElementById('contentLast');
					var totalCount = CONTENT_TOTAL_COUNT;
					if(CONTENT_TOTAL_COUNT==null) {
						CONTENT_TOTAL_COUNT = parseInt(document.getElementById('contentTotalCount').value);
					}
					
					if(contentLast!=null) {
						contentLast = contentLast.value;
					} else {
						contentLast = 0;
					}
					contentLast =parseInt(contentLast);
					contentStart = (parseInt(currentList)+parseInt(contentStart))+1;
					//YAHOO.log("contentStart3:"+contentStart);
					if(infosauce!=null) {
						
						infosauce.innerHTML = '';
												//YAHOO.log('cs:'+contentStart+' contentLast:'+contentLast+' tc:'+totalCount);
						infosauce.innerHTML = infosauce.innerHTML + '</div>';
					}
					//YAHOO.log("gonna fade in");
					if(!stopped || force) {
						opacityID = fadeOutIn(0,true,function() { 
							if(again && !stopped) {
								var slideInput = document.getElementById('slideInterval');
								var interval = 2200;
								if(slideInput!=null) {
									interval = parseInt(slideInput.value);
								}
								recurID = window.setTimeout(function() { 
									doit();
								},interval);
							}
						});
					}
					if(infosauce!=null) {
						var tipdiv = document.getElementById(tipdivid);
						if(tipdiv!=null) {
							for(var x=0;x<tipdiv.childNodes[0].childNodes.length;x++) {
								//YAHOO.log("child["+x+"]:"+tipdiv.childNodes[0].childNodes[x].className);
								if(tipdiv.childNodes[0].childNodes[x].className == 'thumbStuff') {
									infosauce.innerHTML = infosauce.innerHTML + tipdiv.childNodes[0].childNodes[x].innerHTML;
								}
							}
						}
					}
					var finishMsg = null;
					if(contentStart==contentLast && contentLast<totalCount) {
						finishMsg = '<div style=\"margin-top:10px;margin-bottom:20px;\"><strong>This page is finished, but there are more results. Click next page above for more.</<strong></div>';
					} else if (contentStart==1 && finished==true) {
						finishMsg = '<a style="border-bottom:0px;" href="" onclick="currentList=-1;doit(true);return false;">Restart...</a>';
					}
					
					if(contentLast>0) {
						var ssi = document.getElementById('slideshowInfo');
						if(ssi!=null) {
							if(finishMsg!=null) {
								ssi.innerHTML = finishMsg;
							} else {
								ssi.innerHTML = '';
							}
							ssi.innerHTML = ssi.innerHTML + ' '+(contentStart+'/'+contentLast);
						} else {
							infosauce.innerHTML = infosauce.innerHTML +  '<div class=\"slideshowInfo\">Item '+contentStart+'/'+contentLast+' on this page</div>'
						}
					} 
				});
			};
			
				
			newimg.src = url;
			
		}
		// Sleep and do it again

}
function move(howmuch) {
	if(imageList==null) {
		initImageList();
	}
	if(howmuch==null) {
		howmuch = 1;
	}
	clearInterval(slideID);
	clearInterval(recurID);
	//YAHOO.log("currentList:"+currentList+" howmuch:"+howmuch+" (c+h):"+(currentList+howmuch));
	currentList = parseInt(currentList+howmuch);
	if(currentList<0) {
		currentList = 0;
		if(howmuch!=0) {
			return;
		}
	}
	if(currentList>=listLength) {
		currentList = listLength-1;
		if(howmuch!=0) {
			return;
		}
	}
	//YAHOO.log("going:"+currentList);
	go(currentList,false,true);
}


function stopit() {
	var bd = document.getElementById("new");
	if(bd==null) {
		bd = document.getElementById("bigImage");
	}
	stopped = true;
	if(slideID!=null) {
		clearInterval(slideID);
	}
	if(opacityID!=null) {
		clearInterval(opacityID);
	}
	
	bd.style.opacity = '1.0';
	bd.style.filter = "alpha(opacity:100)";
}


function switchType(type,href) {
	var dd = document.getElementById('typeDropdown');
	if(dd!=null) {
		hideDropdown('typeDropdown','typeLink');
	}
	
	var action = document.getElementById("contentAction");
	if(action!=null) {
		action = action.value;
		if(action=="contentJax" || action=="newQueuejax") {
			document.getElementById('contentType').value = type;
			document.getElementById('contentStart').value = 0;
			document.getElementById('contentLast').value = 0;
			loadContent();
			var csel = document.getElementById('classifiedSelect');
			if(csel!=null) {
				if(type!=19) {
					csel.style.display = 'none';	
				} else {
					csel.style.display = 'inline';
				}
			}
			
			var isloc = isLoc(type);
				
			var lsel = document.getElementById('locationSelect');
			if(lsel!=null) {
				if(isloc) {
					lsel.style.display = 'inline';	
				} else {
					lsel.style.display = 'none';
				}
			}
			
			var searchSelect = document.getElementById('searchType');

			if(searchSelect!=null) {			
				for (i = searchSelect.length - 1; i>=0; i--) {
					if(searchSelect.options[i].value==type) {
						searchSelect.options[i].selected = true;
					} else {
						searchSelect.options[i].selected = false;
					}
	     		}
     		}
     		
			var sshow = document.getElementById('slideshowButton');
			if(sshow!=null) {
				if(type==69 || type==44 || type==45) {
					sshow.style.display = 'inline';	
				} else {
					sshow.style.display = 'none';
				}
			}
			
			var nsort = document.getElementById('normalSort');
			var usort = document.getElementById('userSort');
			var esort = document.getElementById('eventSort');
			var fesort = document.getElementById('feedEntrySort');
			
			
			var tagContainer = document.getElementById('tagContainer');
			var tagFilterContainer = document.getElementById('tagFilterContainer');
			var tagFilterInput = document.getElementById('tagFilterInput');
			var filvote = document.getElementById('filterAndVote');
			//YAHOO.log("*********type:"+type);
			if(usort!=null) {
				if(type==37) {
					usort.style.display = 'inline';
					nsort.style.display = 'none';
					esort.style.display = 'none';
					fesort.style.display = 'none';
					filvote.style.display = 'none';
					
					//YAHOO.log("***peoplesort");
					tagContainer.style.display = 'none';
					tagFilterContainer.style.display = 'none';
					
					/*
					var filterColumn = document.getElementById('filterColumn');
					filterColumn.style.display = 'none';
					
					var contentColumn = document.getElementById('new');
					contentColumn.style.width='880px';
					*/
				} else {
					usort.style.display = 'none';
					fesort.style.display = 'none';
					nsort.style.display = 'inline';
					filvote.style.display = 'inline';
					//YAHOO.log("***normalsort");
					tagContainer.style.display = 'block';
					tagFilterContainer.style.display = 'block';
					if(type==12) {
						esort.style.display = 'inline';
						nsort.style.display = 'none';
						// Make upcoming unavailable and switch away from it if necessary
					} else if(type==-2) {
						fesort.style.display = 'inline';
						esort.style.display = 'none';
						nsort.style.display = 'none';
					} else {
						esort.style.display = 'none';
						nsort.style.display = 'inline';
						// make sure upcoming is available
					}
					/*
					var filterColumn = document.getElementById('filterColumn');
					filterColumn.style.display = 'block';
					
					var contentColumn = document.getElementById('new');
					contentColumn.style.width='600px';
					*/
					
				}
				
				if(type==73) {
					filvote.style.display = 'none';
					tagContainer.style.display = 'none';
					tagFilterContainer.style.display = 'none';
				}
				
			}
			makeSubtabsInactive();
			if(href!=null) {
					var typebox = href.parentNode;
					if(typebox.className =='typebox' || typebox.className=='typeboxhover') {
						typebox.className = 'typeboxselected';
						typebox.oldClass = 'typeboxselected';
					}
					var typeIndicator = document.getElementById('typeIndicator');
					if(typeIndicator!=null) {
						typeIndicator.innerHTML = href.innerHTML;
					}
			}
			return false;
		}
	}
	return true;
}
function isLoc(typeStr) {
	var isloc = false;
	var type = parseInt(typeStr);
	switch(type) {
		case 1:
		case 6:
		case 7:
		case 30:
		case 31:
		case 32:
		case 47:
		case 48:		
		case 54:
		case 56:
		case 57:
		case 58:
		case 61:		
		case 62:
		case 63:
		case 65:
		case 74:
		case 75:
		case 77:
		case 78:		
		case 79:
		case 80:
		case 81:
		case 82:						
		isloc = true;
		break;
	}
	//YAHOO.log("isloc:"+isloc+" type:"+type);
	return isloc;
}
function makeSubtabsInactive() {
	var subtabBar = document.getElementById('typeBar');
	if(subtabBar!=null) {
	for(var i=0;i<subtabBar.childNodes.length;i++) {
		if(subtabBar.childNodes[i].tagName=="DIV") {
			if(subtabBar.childNodes[i].className=='typeboxselected' || subtabBar.childNodes[i].className=='typeboxhover') {
				subtabBar.childNodes[i].className = 'typebox';
			}
		}
	}
	}
}

function makeAMap() {
	var mapper = document.getElementById('mainLocMap');
	if(mapper!=null) {
		mapper.className = 'bigGoogleMap';
	}
	var resultBox = document.getElementById('new');
	if(resultBox!=null) {
	/*
		resultBox.style.height='300px';
		resultBox.style.overflow='auto';
		*/
	}

}

function hideAMap() {
	var mapper = document.getElementById('mainLocMap');
	if(mapper!=null) {
		mapper.className = 'hiddenDiv';
	}
	var resultBox = document.getElementById('new');
	if(resultBox!=null) {
		//resultBox.style.height='auto';
		//resultBox.style.overflow='hidden';
	}
}

function doQuery() {
	if(document.getElementById('contentQuery')!=null) {
		document.getElementById('contentQuery').value=document.getElementById('extraQueryBox').value;
		switchType(document.getElementById('contentType').value)
		return false;
	} else {
		return true;
	}
}


function loadGrapes(locationId,sort) {
 	var grapeLoader = document.getElementById('grapeLoader');
 	grapeLoader.style.display = 'block';

 	var grapeDiv = document.getElementById('grapes');
 	grapeDiv.style.display = 'none';
 	
 	if(sort==null) {
 		sort = 'atf';
 	}
 	
 	if(sort=="atf" && document.getElementById("atfGrapeSort")!=null) {
 		document.getElementById("atfGrapeSort").style.fontWeight = 'bold';
  		document.getElementById("newestGrapeSort").style.fontWeight = 'normal';
 	} else if(document.getElementById("atfGrapeSort") != null) {
 		document.getElementById("atfGrapeSort").style.fontWeight = 'normal';
  		document.getElementById("newestGrapeSort").style.fontWeight = 'bold';
 	
 	}

	var url = 'api?action=grapes&id='+locationId+'&sort='+sort;
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseLoadGrapesResponse(req.responseText);
	        }  else if(req.status==12019) { 
	        
	        } else {
				alert("Error loading newstips:"+req.status);
	        }
	    }
	};
	req.open("GET", url, true);
    req.send(null);
}

function parseLoadGrapesResponse(thehtml) {
 	var grapeDiv = document.getElementById('grapes');
 	grapeDiv.innerHTML = thehtml;
 	var grapeLoader = document.getElementById('grapeLoader');
 	grapeLoader.style.display = 'none';
 	grapeDiv.style.display = 'block';
 	
}    


function loadEntries(locationId,feedId,sort) {
 	var entryLoader = document.getElementById('entryLoader');
 	entryLoader.style.display = 'block';

 	var feedEntriesDiv = document.getElementById('feedEntries');
 	feedEntriesDiv.style.display = 'none';
 	
 	if(sort==null) {
 		sort = 'atf';
 	}
 	
 	if(sort=="atf" && document.getElementById("atfEntrySort")!=null) {
 		document.getElementById("atfEntrySort").style.fontWeight = 'bold';
  		document.getElementById("newestEntrySort").style.fontWeight = 'normal';
 	} else if(document.getElementById("atfEntrySort") != null) {
 		document.getElementById("atfEntrySort").style.fontWeight = 'normal';
  		document.getElementById("newestEntrySort").style.fontWeight = 'bold';
 	
 	}

	var url = 'api?action=feedEntries&feedId='+feedId+'&locationId='+locationId+'&sort='+sort;
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseLoadEntriesResponse(req.responseText);
	        }  else if(req.status==12019) { 
	        
	        } else {
				alert("Error loading posts:"+req.status);
	        }
	    }
	};
	req.open("GET", url, true);
    req.send(null);
}

function parseLoadEntriesResponse(thehtml) {
 	var feedEntriesDiv = document.getElementById('feedEntries');
 	feedEntriesDiv.innerHTML = thehtml;
 	aTags = feedEntriesDiv.getElementsByTagName('a');
	  for(e in aTags) {
			  sweetTitles.checkAndAdd(aTags[e]);
	  }
 	var entryLoader = document.getElementById('entryLoader');
 	entryLoader.style.display = 'none';
 	feedEntriesDiv.style.display = 'block';
}    


    
function applaudGrape(entityId,flag,noFlagResult,thevent) {
	//YAHOO.log("thevent:"+thevent+" wevent:"+window.event+' isc:'+isComplex);
	var grapeVoteDiv = document.getElementById('grape'+entityId);
	grapeVoteDiv.innerHTML = '<img src="images/ajax-loader-small.gif"/>';
	
	applauseFeedback('Sending vote...',thevent);
	var url = "tag";
	var params = "action=applaudBury&id="+entityId;
	if(flag==true) {
		url = "tag";
		params = "action=applaudBury&id="+entityId+"&flag=true";
	}
	
    var req = initRequest(url);
	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseGrapeApplauseResponse(req.responseXML,entityId,flag,noFlagResult);

	        }  else if(req.status==12019) { 
	        
	        } else {
				alert("Error sending vote:"+req.status);
	        }
	    }
	};
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send(params);
}

function parseGrapeApplauseResponse(xml,entityId,flag,noFlagResult) {
	var grapeVoteDiv = document.getElementById('grape'+entityId);
	var replyTag = xml.getElementsByTagName("reply")[0];
	
	var atfCount = 0;
	var flagCount = 0;
	
	var msg = null;
	if(replyTag!=null) {
		var appTag = replyTag.getElementsByTagName("applause");
		if(appTag!=null) {
			atfCount = appTag[0].childNodes[0].nodeValue;
		}
		
		var flagTag = replyTag.getElementsByTagName("flags");
		if(flagTag!=null) {
			flagCount = flagTag[0].childNodes[0].nodeValue;
		}
	} else {
		msg = xml.getElementsByTagName("error")[0].childNodes[0].nodeValue;	
	}
   	applauseFeedback(msg,null);
   	var anotify = document.getElementById('applauseFeedback');
	timerFade(2500,anotify,true);
	
	
	
	var html = "";
	if(flag==true) {
		html = html +atfCount+' <img alt="vote above the fold" class="middleAlign" src="images/above30fade.png"/>';
		
		if(!noFlagResult) {
			html = html +'<br/> '+ flagCount+ ' <img class="middleAlign" src="images/flag.png"/>';
		}
		grapeVoteDiv.innerHTML = html;
	} else {
		html = html +atfCount+' <img class="middleAlign" src="images/abovefold.png"/>';
		
		if(!noFlagResult) {
			html = html +'<br/> '+flagCount+' <img class="middleAlign" src="images/flag30fade.png"/>';
		}
		grapeVoteDiv.innerHTML = html;
	}
}


/* Applaud Feed Entry nonsense */



function applaudEntry(entryId,thevent) {
	var entryVoteDiv = document.getElementById('entry'+entryId);
	var oldEntryHtml = entryVoteDiv.innerHTML; 
	entryVoteDiv.innerHTML = '<img src="images/ajax-loader-small.gif"/>';
	applauseFeedback('Sending vote...',thevent);
	var url = "tag";
	var params = "action=applaudEntry&id="+entryId;
    var req = initRequest(url);
	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseEntryApplauseResponse(req.responseXML,entryId,oldEntryHtml,thevent);

	        }  else if(req.status==12019) { 
	        
	        } else {
				alert("Error sending vote:"+req.status);
	        }
	    }
	};
	
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send(params);
}

function parseEntryApplauseResponse(xml,entryId,oldEntryHtml,thevent) {
	var entryVoteDiv = document.getElementById('entry'+entryId);
	var replyTag = xml.getElementsByTagName("reply")[0];
	var atfCount = 0;
	var msg = null;
	if(replyTag!=null) {
		var appTag = replyTag.getElementsByTagName("applause");
		if(appTag!=null) {
			atfCount = appTag[0].childNodes[0].nodeValue;
		}
		msg = xml.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		var html = "";
		html = html +atfCount+' <img class="middleAlign" src="images/abovefold.png"/> ';
		entryVoteDiv.innerHTML = html;
	} else {
		msg = xml.getElementsByTagName("error")[0].childNodes[0].nodeValue;
		entryVoteDiv.innerHTML = oldEntryHtml;		
	}
   	applauseFeedback(msg,null);
   	var anotify = document.getElementById('applauseFeedback');
	timerFade(2500,anotify,true);
	
	
}



/* Lets deal with editorial coments and all that */

function loadComments(entityId,type,scrollToEnd) {
 	var commentsLoader = document.getElementById('commentsLoader');
 	
 	var sort = document.getElementById("commentSort");
 	if(sort!=null) {
 		sort = sort.value;
 	}
 	
 	commentsLoader.style.display = 'inline';

 	var commentsDiv = document.getElementById('comments');
// 	commentsDiv.style.display = 'none';
 	
 	if(type==null) {
 		type = document.getElementById("commentType");
 		if(type==null) {
 			type = 'normal';
 		} else {
 			type = type.value;
 		} 
 	 } else {
 	 	var ctype = document.getElementById("commentType");
 	 	ctype.value = type;
 	 }
 	
 	var editorial = false;

	if(document.getElementById("normalCommentsLink")!=null) { 
	 	if(type=="normal") {
	 		document.getElementById("normalCommentsLink").style.fontWeight = 'bold';
	  		document.getElementById("editorialCommentsLink").style.fontWeight = 'normal';
	  		document.getElementById("allCommentsLink").style.fontWeight = 'normal';
	  		editorial = false;
	 	} else if(type=="editorial"){
	 		document.getElementById("normalCommentsLink").style.fontWeight = 'normal';
	  		document.getElementById("editorialCommentsLink").style.fontWeight = 'bold';
	  		document.getElementById("allCommentsLink").style.fontWeight = 'normal';
	  		editorial = true;
	 	} else if(type=="all") {
	 		document.getElementById("normalCommentsLink").style.fontWeight = 'normal';
	  		document.getElementById("editorialCommentsLink").style.fontWeight = 'normal';
	  		document.getElementById("allCommentsLink").style.fontWeight = 'bold';
	  		editorial = false;
	 	}
	}
	
	var url = 'api?action=comments&id='+entityId+'&type='+type;
	if(sort!=null) {
		url = url + "&sort="+sort;
	}
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseLoadCommentsResponse(req.responseText,scrollToEnd,editorial);
	        } else {
				alert("Error fetching comments:"+req.status);
	        }
	    }
	};
	req.open("GET", url, true);
    req.send(null);
}

function parseLoadCommentsResponse(thehtml,scrollToEnd,editorial) {
 	var commentDiv = document.getElementById('comments');
 	commentDiv.innerHTML = thehtml;
 	var commentsLoader = document.getElementById('commentsLoader');
 	commentsLoader.style.display = 'none';
 //	commentDiv.style.display = 'block';
 	
	var atags = commentDiv.getElementsByTagName("A");
	for(n in atags) {
		sweetTitles.checkAndAdd(atags[n]);
	}
	if(scrollToEnd) {
		location.hash = '#commentForm';
	}
	var cEdit = document.getElementById("cEditorial");
	if(cEdit!=null) {
		if(editorial) {	
			cEdit.checked = true;
		} else {
	  		cEdit.checked = false;
	  	}
  	}
}    

function sendComment(draft,thevent) {
	// entityId
	// atf
	// watch
	// private
	// editorial
	// body

	var params = "action=addComment";
	var entityId = null;
	
	var cEntityId = document.getElementById('cEntityId');
	if(cEntityId!=null) {
		params = params + "&id="+cEntityId.value;
		entityId = cEntityId.value;
	}

	var cAtf = document.getElementById('cAtf');
	if(cAtf!=null && cAtf.checked) {
		params = params + "&applaud="+cAtf.value;
	}
	
	var cWatch = document.getElementById('cWatch');
	if(cWatch!=null && cWatch.checked) {
		params = params + "&watch="+cWatch.value;
	}
	
	var cPrivate = document.getElementById('cPrivate');
	if(cPrivate!=null && cPrivate.checked) {
		params = params + "&private="+cPrivate.value;
	}
	
	var cNickname = document.getElementById('cNickname');
	var nickname = null;
	if(cNickname!=null && cNickname.value!=null && cNickname.value!='') {
		params = params + "&nickname="+cNickname.value;
		nickname = cNickname.value;
	}
	
	var cUrl = document.getElementById('cUrl');
	if(cUrl!=null && cUrl.value!=null) {
		params = params + "&url="+cUrl.value;
	}
	
	var cEmail = document.getElementById('cEmail');
	if(cEmail!=null && cEmail.value!=null) {
		params = params + "&email="+cEmail.value;
	}
	
	
	var cSubject = document.getElementById('cSubject');
	if(cSubject!=null) {
		params = params + "&subject="+cSubject.value;
	}
	
	var cEditorial = document.getElementById('cEditorial');
	var editorial = null;
	if(cEditorial!=null && cEditorial.checked) {
		params = params + "&editorial="+cEditorial.value;
		editorial = true;
	}
	
	var cBody = document.getElementById('commentText');
	var body = null;
	if(cBody!=null && cBody.value!=null && cBody.value!='') {
		params = params + "&body="+escape(cBody.value);
		body = escape(cBody.value);
	}
	
	if(draft=='true') {
		params = params + "&submitDraft.x=1";
	}
	
	var anon = document.getElementById('cAnon');
	if(anon!=null && anon.value=='true') {
	// anon, nickname required
		if(nickname==null) {
			applauseFeedback('Nickname is required since you aren\'t logged in!',thevent);
			var anotify = document.getElementById('applauseFeedback');
			timerFade(2500,anotify,true);
			return;
		}
	}
	if(body==null) {
		applauseFeedback('Your comment is empty! Please type in something before submitting.',thevent);
		var anotify = document.getElementById('applauseFeedback');
		timerFade(2500,anotify,true);
		return;
	
	}
	applauseFeedback('Sending comment...',thevent);
	
	var url = 'api';
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseAddCommentsResponse(entityId,req.responseText,editorial);
	        } else {
				alert("Error adding comment:"+req.status);
	        }
	    }
	};
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send(params);
}

	
function parseAddCommentsResponse(entityId,text,editorial) {
	applauseFeedback('Comment Sent.',null);
	var anotify = document.getElementById('applauseFeedback');
	timerFade(2500,anotify,true);
	
	if(editorial) {
		loadComments(entityId,"editorial",true);
	} else {
		loadComments(entityId,"normal",true);
	}
}

	
function parseTipResponse(text,locationId,place) {
	/*
	var tlr = document.getElementById('tipLineResult');
	tlr.style.display = 'block';
	tlr.innerHTML = text;
	*/
	applauseFeedback('Tip posted!',null);
	var anotify = document.getElementById('applauseFeedback');
	timerFade(2500,anotify,true);
	
	var tipBody = document.getElementById('tipBody');
	tipBody.value = '';
	
	var tipId = document.getElementById("tipId");
	if(tipId==null) {
		tipId = document.createElement("input");
		tipId.setAttribute("type","hidden");
		tipId.setAttribute("id","tipId");
		tipId.id = 'tipId';
		document.getElementsByTagName('body')[0].appendChild(tipId);
	}
	
	tipId.value = text;
		
	
	loadGrapes(locationId,'newest');
	// show map if user selected 'map' radio button
	if (place == 'map') {
		//var div = document.getElementById('editLocationDiv');		
		//showPopWinDiv(div, 500, 500, null);
		//showPopWin('http://localhost:222/alchemy3/editLocationPopup.jsp?hideTip=true&prefix=editLocationDiv&showEdit=true&noload=true', 500, 500, null);
		showMiniPopup('editLocationDiv','tipBody');
	}
}

function postTip(tipline,event) {
		
	var params = "action=addTip";

	// from radio is "anonymous" or "user"
	// place radio is "map" or "${location.entityId}"
	
	var theFrom = 'anonymous';
	if (tipline.from != null) {
		var j=tipline.from.length;
		for (i=0; i<j; i++){
			if(tipline.from[i].checked) { 
				theFrom = tipline.from[i].value
			}
		}
	}
	
	var thePlace = null;
	if (tipline.place != null) {
		j=tipline.place.length;
		for (i=0; i<j; i++){
			if(tipline.place[i].checked) 
				thePlace = tipline.place[i].value
		}
	}
		
	var cBody = document.getElementById('tipBody');
	if(cBody!=null && cBody.value != '' && cBody.value != 'Type your tip here') {
		params = params + "&body="+cBody.value;
	} else {
		applauseFeedback('Please enter some text before you click post newstip',event);
		var anotify = document.getElementById('applauseFeedback');
		timerFade(2500,anotify,true);
		return;
	}
		
	if(theFrom == 'anonymous') {
		params = params + "&anonymous=true";
	} 
	
	// 1 is Earth entityId
	var locationId = 1;
	if (thePlace !== 'map') {
		params = params + "&locationId="+thePlace;
		locationId = thePlace;
	}
	
	var url = 'api';
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseTipResponse(req.responseText,locationId,thePlace);
	        } else {
				alert("Error sending tip:"+req.status);
	        }
	    }
	};
	applauseFeedback('Sending newstip',event);
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send(params);
}

function sendTip(anonymous) {
	// anonymous
	// body


	var params = "action=addTip";
	
	
	var cBody = document.getElementById('tipBody');
	if(cBody!=null && cBody.value != '') {
		params = params + "&body="+cBody.value;
	} else {
		return;
	}
		
	if(anonymous == 'true') {
		params = params + "&anonymous=true";
	}	
	
	var loc = document.getElementById('tipLocationId');
	var locationId = 1;
	if(loc!=null) {
		params = "&locationId="+loc.value;
		locationId = loc.value;
	}
	
	var url = 'api';
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseTipResponse(req.responseText,locationId);
	        } else {
				alert("Error sending tip:"+req.status);
	        }
	    }
	};
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send(params);
}



function parseUpdateTipLocationResponse(text,locationId) {
	var tlr = document.getElementById('tipLineResult');
	tlr.style.display = 'block';
	/*tlr.innerHTML = text;*/
	loadGrapes(1,'newest');
	AC_unload();
}


function doSaveGeotag() {

	if(updateTipLocation()) {
		document.getElementById('editLocationDiv').style.display='none';
	}
}

function updateTipLocation() {
	// anonymous
	// body

	var params = "action=updateTipLocation";
	
	
	var cTipId = document.getElementById('tipId');
	if(cTipId!=null) {
		params = params + "&tipId="+cTipId.value;
	}
	
	
	var loc = document.getElementById('editLocationDivlocationId');
	if(loc==null || loc.value==null ) {
		return false;
	}
	var locationId = 1;
	if(loc!=null) {
		params = params+"&locationId="+loc.value;
		locationId = loc.value;
	}
	
	var url = 'api';
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseUpdateTipLocationResponse(req.responseText);
	        } else {
				alert("Error updating tip location:"+req.status);
	        }
	    }
	};
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send(params);
    return true;
}


function makeMarkerDraggable(prefix) {
	var gmap = maps[prefix+'locSearchMap'];
	var marker = gmarkers[mainMarkerId[prefix+'locSearchMap']];
	var marker2 = selGmarkers[mainMarkerId[prefix+'locSearchMap']];
	marker.enableDragging();
	marker2.enableDragging();
	
	var latlngDiv = document.getElementById(prefix+'selectionLocationLatLng');

	var latInput = document.getElementById(prefix+'locationLat');
	var lngInput = document.getElementById(prefix+'locationLng');
	
		
	GEvent.addListener(marker,"dragend", function() {
	        			//self.location = url;
						latlngDiv.innerHTML = '<strong>Updated Lat/Lng from map point:</strong> '+marker.getPoint().lat()+'/'+marker.getPoint().lng();
						latInput.value = marker.getPoint().lat();
						lngInput.value = marker.getPoint().lng();
						var preciseLoc = document.getElementById(prefix+'preciseLoc');
						if(preciseLoc!=null) {
							preciseLoc.value = 'true';
						} else {
							latlngDiv.innerHTML = latlngDiv.innerHTML + '<input type="hidden" name="'+prefix+'preciseLoc" value="true">';
						}
	        		});
	        		GEvent.addListener(marker2,"dragend", function() {
	        			//self.location = url;
	        			latlngDiv.innerHTML = '<strong>Updated Lat/Lng from map point:</strong> '+marker.getPoint().lat()+'/'+marker.getPoint().lng();
	        			latInput.value = marker.getPoint().lat();
						lngInput.value = marker.getPoint().lng();
						var preciseLoc = document.getElementById(prefix+'preciseLoc');
						if(preciseLoc!=null) {
							preciseLoc.value = 'true';
						} else {
							latlngDiv.innerHTML = latlngDiv.innerHTML + '<input type="hidden" name="'+prefix+'preciseLoc" value="true">';
						}
	        		});
}


function parseLocSearchResponse(text,initial,prefix,hideResults) {

	var lsr = document.getElementById(prefix+'locSearchResults');
	lsr.innerHTML = text;

	
	var lat = document.getElementById(prefix+'locationLat');
	if(lat!=null) {
		lat = lat.value;
	}
	var lng = document.getElementById(prefix+'locationLng');
	if(lng!=null) {
		lng = lng.value;
	}

	if(!initial) {	
		var selLoc = document.getElementById(prefix+'selectionLocationUpdate');
		var selLocResults = document.getElementById(prefix+'selectionLocationResult');
		if(selLoc!=null) {
			selLocResults.innerHTML = selLoc.innerHTML;
			selLoc.innerHTML = '';
		}
	} else {
		var selLocResults = document.getElementById(prefix+'selectionLocationResult');
		if(selLocResults!=null) {
			selLocResults.innerHTML = '';
		}
	}
	if(initial || hideResults) {
		lsr.innerHTML = '';
	}
	
	if(initial) {
		var originalLat = document.getElementById('originalLat');
		var originalLng = document.getElementById('originalLng');
		if(originalLat!=null) {
			lat = originalLat.value;
		}
		if(originalLng!=null) {
			lng = originalLng.value;
		}
	}
	if(lat!=null && lng!=null) {	
		GoogleMap(lat,lng,prefix+'locSearchMap',lat+","+lng,true);
	} else {
	
	}
	makeMarkerDraggable(prefix);
	
}


function doLocSearch(initial,prefix,address,hideResults) {
	// anonymous
	// body

	var params = "action=locSearch";
	
	if(prefix==null) {
		prefix = '';
	} else {
		params = params+"&prefix="+prefix;
	}
	
	if(address) {
		params = params+"&address=true";
	}
	
	var cLocation = document.getElementById(prefix+'locSearch');
	if(cLocation!=null) {
		params = params + "&location="+escape(cLocation.value);
	}
	
	var url = 'api';
    var req = initRequest(url);
 	req.onreadystatechange = function() {
	    if (req.readyState == 4) {
	        if (req.status == 200) {
	            parseLocSearchResponse(req.responseText,initial,prefix,hideResults);
	        } else {
				alert("Error searching for location:"+req.status);
	        }
	    }
	};
	req.open("POST", url, true);
    req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    req.send(params);
}

function loadFresh() {
	document.getElementById('contentLast').value=0;
	document.getElementById('contentStart').value=0;
	document.getElementById('contentSort').value=this.value;
	loadContent();
}

function clearTipline(textArea) {
	if (textArea.value=='Type your tip here') { 
		textArea.value = '';
	}
}

function onUrlPreviewChanged(name) { 
	var link = document.getElementById('link'+name);
	var text = document.getElementById('friendly'+name);
	var preview = document.getElementById('previewLink'+name);
	
	if(link.value!=null && link.value!='' && link.value!='http://') {
		if(text.value!=null && text.value!='') {
			preview.innerHTML = '<div>preview: <a href="'+link.value+'">'+text.value+'</a></div>'+
							    '<div>code: ['+link.value+' '+text.value+']</div>';		
		} else {
			preview.innerHTML = 'preview: <a href="'+link.value+'">'+link.value+'</a>'+
							    '<div>code: ['+link.value+']</div>';
		}
	} else {
		preview.innerHTML = '<em>please enter a web link</em>';
	}
}