var qy = new Object();
if(window.location.search && window.location.search.length > 0)
{for (var i = 0;p = window.location.search.split('&')[i]; i++) {if (/\??(.*)=(.*)/.test(p)) qy[dCd(RegExp.$1)] = dCd(RegExp.$2)}}
var pURL = "http://pipes.yahoo.com/pipes/pipe.run?_id=";
var eURL = "&_render=json&_callback=pipeCallback";
var dateBase = pURL + "46054878f859d825eb1e17e8fa2a9c91" + eURL;
var pLHBase  = pURL + "45cdbddcc534897283a1a3a20a062cea" + eURL;
var pHLBase  = pURL + "82fb50186edb24b3258d34f01f1e97a9" + eURL;
var reloadBoolean = true;
function setType(type)
{
	if(type == "forsale")
	{
		var forsaleSelect = gElem('forsaleSelect');
		forsaleSelect.style.display = "block";
		var communitySelect = gElem('communitySelect');
		communitySelect.style.display = "none";
		gElem("minmaxprice").style.display = '';
		gElem("sortbyrow").style.display = '';
	}
	else if(type == "community")
	{
		var forsaleSelect = gElem('forsaleSelect');
		forsaleSelect.style.display = "none";
		var communitySelect = gElem('communitySelect');
		communitySelect.style.display = "block";
		gElem("minmaxprice").style.display = "none";
		gElem("sortbyrow").style.display = "none";
	}
}
function cElem(na){return document.createElement(na)}
function gElem(na){return document.getElementById(na)}
function dCd(myS){return decodeURIComponent(myS)}
function a(o,ao){o.appendChild(ao)}
function displayWoot(param)
{
	if(param.value.items[0] != undefined && param.value.items[0].title != "" )
	{
		var wootItem = param.value.items[0];
		var wootDiv = gElem("wootbanner");
		var link = cElem("a");
		link.target = "_blank";
		link.href = "http://www.woot.com/";
		var title = cElem("span");
		title.innerHTML = wootItem.myTitle;
		a(link,title);
		
		var link2 = cElem("a");
		link2.target = "_blank";
		link2.className = "mobile_hidden";
		link2.href = "http://www.woot.com/";
		var wootImg = cElem("img");
		wootImg.src = wootItem.imgurl;
		wootImg.border = "0";
		wootImg.style.cssFloat = "right";
		wootImg.style.styleFloat = "right";
		a(link2,wootImg);
			
		var link3 = cElem("a");
		link3.target = "_blank";
		link3.href = "http://www.woot.com/";
		var wootImg2 = cElem("img");
		wootImg2.src = wootItem.imgurl;
		wootImg2.border = "0";
		wootImg2.style.cssFloat = "left";
		wootImg2.style.styleFloat = "left";
		a(link3,wootImg2);
		
		
		var intro = cElem("span");
		intro.style.fontWeight = "bold";
		intro.innerHTML = "Todays woot!<br/>";
		
		a(wootDiv,link2);
		a(wootDiv,link3);
		a(wootDiv,intro);
		a(wootDiv,link);
		a(wootDiv,cElem("br"));
	
		var unorderedList = cElem("ul");
	
		var listItem = cElem("li");
		var blogLink = cElem("a");
		blogLink.href = wootItem.link;
		blogLink.target = "_blank";
		blogLink.innerHTML = "Item's blog entry";
		
		a(listItem,blogLink);
		a(unorderedList,listItem);
		
		if(wootItem.enclosure != undefined)
		{
			listItem = cElem("li");
			var media = cElem("a");
			media.href = wootItem.enclosure.url;
			media.target = "_blank";
			media.innerHTML = "Today's Podcast Link";
			
			a(listItem,media);
			a(unorderedList,listItem);
		}
		var centerDiv = cElem("div");
		centerDiv.style.width="320px";
		centerDiv.style.textAlign = "left";
		centerDiv.style.paddingLeft = "100px";
		var centerElem = cElem("center");
		a(centerElem,centerDiv);
		a(centerDiv,unorderedList);
		
		a(wootDiv,centerElem);
	}
}
function doCall()
{
	var city = gElem("city").value;
	var hasPic = gElem("hasPic").checked;
	hasPic ? hasPic = 1 : hasPic = 0
	
	var search = gElem("search").value;
	var typeOfSearch = gElem("searchSubject").value;
	var cAbb = "";
	var maxAsk = "";
	var minAsk = "";
	var sortValue = "date"; // default to "date"
	if(typeOfSearch == "forsale")
	{
		cAbb = gElem("forsaleSelect").value;
		
		//only do min and max for forsale type searches
		maxAsk = gElem("maxAsk").value;
		minAsk = gElem("minAsk").value;
		
		//only do sorting by price for forsale type searches
		var myform = gElem("myform");
		for (var i = 0; i < myform.sort.length; i++) 
		{
			 if (myform.sort[i].checked==true)
			 {
				sortValue = myform.sort[i].value;
			 }
		}
	}
	else if (typeOfSearch == "community")
	{
		cAbb = gElem("communitySelect").value;
	}
	var searchType = gElem("searchType").checked;
	searchType ? searchType = "T" : searchType = "";
	
	var baseURL;
	switch(sortValue)
	{
		case "date":
		{
			baseURL = dateBase;
			break;
		}
		case "price_hl": 
		{
			baseURL = pHLBase;
			break;
		}
		case "price_lh": 
		{
			baseURL = pLHBase;
			break;
		}
		default:
		{
			baseURL = dateBase;
			break;
		}
	}

   var loc = document.location + "";
   
   loc = loc.substr(0,loc.indexOf("search.html")+11);
   
   var url = loc + "?&search=" + search + "&city=" + city + "&hasPic=" + hasPic + ( typeOfSearch == "forsale" ? "&maxAsk=" + maxAsk + "&minAsk=" + minAsk : "" ) + "&cAbb=" + cAbb + "&srchType=" + searchType + "&type=" + typeOfSearch + "&sort=" + sortValue;
   
   location.href=url;	
}
function doJson()
{
	var city = gElem("city").value;
	var hasPic = gElem("hasPic").checked;
	hasPic ? hasPic = 1 : hasPic = 0
	
	var search = gElem("search").value;
	var typeOfSearch = gElem("searchSubject").value;
	var cAbb = "";
	var maxAsk = "";
	var minAsk = "";
	var sortValue = "date"; // default to "date"
	if(typeOfSearch == "forsale")
	{
		cAbb = gElem("forsaleSelect").value;
		
		//only do min and max for forsale type searches
		maxAsk = gElem("maxAsk").value;
		minAsk = gElem("minAsk").value;
		
		//only do sorting by price for forsale type searches
		var myform = gElem("myform");
		for (var i = 0; i < myform.sort.length; i++) 
		{
			 if (myform.sort[i].checked==true)
			 {
				sortValue = myform.sort[i].value;
			 }
		}
	}
	else if (typeOfSearch == "community")
	{
		cAbb = gElem("communitySelect").value;
	}
	var searchType = gElem("searchType").checked;
	searchType ? searchType = "T" : searchType = "";
	
	var baseURL;
	switch(sortValue)
	{
		case "date":
		{
			baseURL = dateBase;
			break;
		}
		case "price_hl": 
		{
			baseURL = pHLBase;
			break;
		}
		case "price_lh": 
		{
			baseURL = pLHBase;
			break;
		}
		default:
		{
			baseURL = dateBase;
			break;
		}
	}

	var url = baseURL + "&city=" + city + "&hasPic=" + hasPic + ( typeOfSearch == "forsale" ? "&maxAsk=" + maxAsk + "&minAsk=" + minAsk : "" ) + "&search=" + search + "&cAbb=" + cAbb + "&srchType=" + searchType;
	
	var elem = gElem('jsondiv');
	var script = cElem('script');
	script.type = 'text/javascript';
	script.src = url;
	a(elem,script);
}
function pipeCallback(param)
{
	var oDiv = gElem("result");
	oDiv.innerHTML = "&nbsp;";
	
	var oDiv2 = gElem("lastResults");
	oDiv2.innerHTML = "&nbsp;";
	
	if(param.value.items[0] != undefined && param.value.items[0].title != "" )
	{
		var total = param.value.items.length;
	
		var mainLinks = cElem("div");
		
		var text = total == 1 ? total + " Result Found:" : total + " Results Found:";
		oDiv.innerHTML = text; 
		a(oDiv,cElem("br"));
		
		var i = 0;
		for (i = 0; i < total && i < 7; i++)
		{
			doTheWork(oDiv,i,mainLinks,param.value.items[i])
		}
		
		for (i = i; i < total; i++)
		{
			doTheWork(oDiv2,i,mainLinks,param.value.items[i])
		}		
		var topSpan = cElem('span');
		topSpan.innerHTML = "<br/><a href='#top' style='font-weight:bold;text-decoration:none'>Back To Top</a><br/><br/>";
		a(oDiv2,topSpan);
		mainLinks.innerHTML = "<b><u>CraigsList.org Searches Used:</u></b><br/>" + mainLinks.innerHTML;
		a(oDiv2,mainLinks);
		
		var footerDiv = gElem("adbriteDIV_footer")
		if(total > 0) {	footerDiv.className = "show_not_mobile mobile_hidden";}
		else{ footerDiv.className = "noshow_not_mobile mobile_hidden";}
		
		var middleMobileAdDiv = gElem("mobileAdMiddle")
		if(total > 0) {	middleMobileAdDiv.className = "show_mobile";}
		else{ middleMobileAdDiv.className = "noshow_mobile";}
	}
	else
	{
		oDiv.innerHTML = "No Results Found. Please Try Again.";
		gElem("adbriteDIV_footer").style.display = "none";
	}
}

function doTheWork(oDiv, i, mainLinks, currentItem)
{
            var elementDiv = cElem("div");
			elementDiv.style.borderTop= "solid black 2px";
			elementDiv.style.marginTop = "5px";	
			
			var mainLinkURL = currentItem.mainlink;
			var mainLinksSeen = mainLinks.innerHTML;
			if(mainLinkURL != undefined && mainLinkURL != "" && mainLinksSeen.indexOf(mainLinkURL.substring(7,mainLinkURL.indexOf(".c"))) == -1)
			{
				mainLinksSeen = mainLinksSeen + mainLinkURL.substring(7,mainLinkURL.indexOf(".c"));
				var mainLink = cElem("a");
				mainLink.href = mainLinkURL;
				var city = gElem("city").value;
				if(city.indexOf(",") == -1)
				{
					mainLink.innerHTML = "Link to search on CraigsList.org";
				}
				else
				{
					mainLink.innerHTML = "Search " + mainLinkURL.substring(7,mainLinkURL.indexOf(".c")) + " on CraigsList.org";
				}
				mainLink.style.fontWeight = "bold";
				mainLink.style.textDecoration = "none";
				mainLink.target = "_blank";
				a(mainLinks,mainLink);
				a(mainLinks,cElem('br'));
			}
			
			var ahref = cElem("a");
			ahref.href = "javascript:showHideContentDiv("+i+")";
			ahref.innerHTML = currentItem.title;
			ahref.style.fontWeight = "bold";
			ahref.style.textDecoration = "none";
			ahref.style.fontSize = "15px";
			ahref.title = "Expand / Collapse";
			
			var itemLink = cElem("a");
			itemLink.target = "_blank";
			itemLink.href = currentItem.link;
			itemLink.innerHTML = "&#9774;";
			itemLink.title = "View listing on CraigsList.org";
			itemLink.style.fontWeight = "bold";
			itemLink.style.textDecoration = "none";
			itemLink.style.cssFloat = "right";
			itemLink.style.styleFloat = "right";
			
			var header = cElem("div");
			a(header,ahref);
			header.style.width = "100%";
			header.style.backgroundColor = "lightblue";
			header.style.fontSize = "15px";
			header.innerHTML = "<span style='font-size:15px;font-weight:bold'>" + (i+1) + ". </span>" + header.innerHTML + "   &nbsp;<i style='font-size:11px'>(&nbsp;" + currentItem.thedate + "&nbsp;)</i>";
			a(header,itemLink);
			a(elementDiv,header);
			
			var contentDiv = cElem("div");
			contentDiv.style.fontSize = "12px";
			contentDiv.style.overflow = "auto";
			contentDiv.id = "content_"+i;
			contentDiv.style.display="none";
			contentDiv.style.visibility="hidden";
			
			if(currentItem.description != undefined && currentItem.description != "")
			{
				var description = cElem("div");
				description.style.fontSize = "12px";
				
				var preHTML = "";
				if(currentItem.replyto[0] != undefined && currentItem.replyto[0].content != undefined && currentItem.replyto[0].content != "")
				{
					preHTML = "Reply To: " + currentItem.replyto[0].content + "<hr/>";
				}
				
				description.innerHTML = preHTML + currentItem.description;
				description.style.overflow = "auto";
				description.style.margin = "3px";
				a(contentDiv,description);
			}
			if(currentItem.images != undefined && currentItem.images != "" && currentItem.images.length > 0)
			{
				var pictures = cElem("div");
				pictures.style.fontSize = "12px";
				pictures.innerHTML = currentItem.images[0].content;
				pictures.style.overflow = "auto";
				pictures.style.margin = "3px";
				a(contentDiv,pictures);
			}
			
			a(elementDiv,contentDiv);
			a(oDiv,elementDiv);
}

function showHideContentDiv(num)
{
	var locElem = gElem("content_"+ num);
	var style = locElem.style;
	var dis = style.display;
	if(dis=="block"){
		style.display="none";
		style.visibility="hidden";
	}else{
		style.display="block";
		style.visibility="visible";
	}
}