customPodUrl = "://www.npr.org/buckets/podcasts/main/podlayer.php?";

/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code 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.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser doesn't support Ajax");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('txtSearch').value);
		searchReq.open("GET", '/buckets/podcasts/main/searchSuggest.php?search=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
	//setQuery(value);//disabled so user can choose whether to add or start a new search
	document.getElementById('txtSearch').value = value;
	document.getElementById('search_suggest').innerHTML = '';
	addTopic();
}
podUrl = "api.npr.org/query?action=Or&output=Podcast&apiKey=MDAxNzgwNDQ4MDEyMTQ4ODAwODZhNWFkZA001";
iFrameUrl = "http://api.npr.org/query?output=HTML&apiKey=MDAxNzgwNDQ4MDEyMTQ4ODAwODZhNWFkZA001&action=Or";
idString = "&id=";
searchString = "&searchTerm=";
ids = "";
searchs = "";

function addTopic(){
if(document.getElementById('txtSearch').value != ""){
	customPodSearch = document.getElementById('txtSearch').value;
		if(customPodSearch.search(/\(#/) != -1){
			var id = customPodSearch.split("(#")[1].split(")")[0];
			customPodcastNewTopic = '<span><input class="customPodInput" type="text" name="customPodTopic" value="' + customPodSearch + '" disabled="disabled" /> <input class="customPodButton" type="button" value="-" onclick="deletePodTopic(this.parentNode);" /><input name="customPodTopicId" type="hidden" value="' + id + '" /></span>';
		} else {
			customPodcastNewTopic = '<span><input class="customPodInput" type="text" name="customPodTopic" value="' + customPodSearch + '" disabled="disabled" /> <input class="customPodButton" type="button" value="-" onclick="deletePodTopic(this.parentNode);" /><input name="customPodSearchTerm" type="hidden" value="' + customPodSearch + '" /></span>';
		}
	document.getElementById('customPodBucketTopics').innerHTML = document.getElementById('customPodBucketTopics').innerHTML + customPodcastNewTopic; 
	document.getElementById('search_suggest').innerHTML = '';
	document.getElementById('txtSearch').value = '';
	createCustomPodQuery();
	}
}

function deletePodTopic(element){
element.parentNode.removeChild(element);
createCustomPodQuery();
}

var request = null;

	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	
	if (request == null){
		alert("There was an error creating the Ajax request object for your browser.");}

function ajaxGet(url,responseFunction){
	request.open("GET", url, true);
	request.onreadystatechange = function(){
		if(request.readyState == 4){
			responseFunction();
		}
	}
	request.setRequestHeader("Content-Type",
							"application/x-www-form-urlencoded");
	request.send(null);
}

empty = function(){}

abTestFeed = function(){
//	if(request.responseText == -1){request.responseText = "There are no stories that meet your search criteria.  To expand your search, add more topics to your feed or remove items that do not have id numbers next to them.";}
	customPodOutput = '<strong style="color:gray">Browse top stories from your feed:</strong>' + 
	'<br/><br/>' + 
	'<div id="customGraybox">' + 
	request.responseText + 
	'</div><br/>';
	document.getElementById("customPodPreview").innerHTML = customPodOutput;
	if(customPodSearchPrepend + customPodSearchString + customPodIdPrepend + customPodIdString == ""){document.getElementById("customPodPreview").innerHTML = ""; document.getElementById("subscribeTools").innerHTML = "";}
}

function createCustomPodQuery(){
customPodIdArray = [];
customPodSearchArray = [];
for(i=0; i<document.getElementsByName('customPodTopicId').length; i++){
	customPodIdArray[i] = document.getElementsByName('customPodTopicId')[i].value;
	}
for(i=0; i<document.getElementsByName('customPodSearchTerm').length; i++){
	customPodSearchArray[i] = document.getElementsByName('customPodSearchTerm')[i].value;
	}
customPodIdString = customPodIdArray.toString();
customPodSearchString = customPodSearchArray.toString().replace(/\,/g, " ");
if (customPodIdArray.length > 0){customPodIdPrepend = "&id=";} else {customPodIdPrepend = "";}
if (customPodSearchArray.length > 0){if (customPodIdArray.length > 0){customPodSearchPrepend = "&searchTerm=";} else {customPodSearchPrepend = "&searchType=mainText&searchTerm=";}} else {customPodSearchPrepend = "";}
if (document.getElementById('customPodTitle').value != ""){customPodTitlePrepend = "&title=";} else {customPodTitlePrepend = "";}
customPodRssQuery = "http" + customPodUrl + customPodSearchPrepend + customPodSearchString + customPodIdPrepend + customPodIdString + customPodTitlePrepend + document.getElementById('customPodTitle').value;
customPodItunesQuery = "itpc" + customPodUrl + customPodSearchPrepend + customPodSearchString + customPodIdPrepend + customPodIdString + customPodTitlePrepend + document.getElementById('customPodTitle').value;
//alert(customPodRssQuery);
//customPodAjax(customPodRssQuery);
ajaxGet("/buckets/podcasts/main/podresponse.php?" + customPodUrl + customPodSearchPrepend + customPodSearchString + customPodIdPrepend + customPodIdString + customPodTitlePrepend + document.getElementById('customPodTitle').value,abTestFeed);
	customSubLinks = '<a href="' + customPodItunesQuery + '" onClick="sa_onclick(this.href);"><img src="http://media.npr.org/images/podcasts/ui/itunes.gif" width="91" height="18" alt="" /></a>' + 
        '<!-- <a target="_blank" href="http://add.my.yahoo.com/rss?url=' + customPodRssQuery + '" onClick="sa_onclick(this.href);"><img src="http://media.npr.org/images/podcasts/ui/myyahoo.gif" width="91" height="18" alt="" /></a> -->' + 
    	'<a target="_blank" href="zune://subscribe/?NPR:' + document.getElementById('customPodTitle').value + '=' + customPodRssQuery + '"><img src="http://media.npr.org/images/podcasts/ui/zune_icon.jpg" width="49" height="16" class="zune" alt="Zune" /></a>' + 
		'<div class="spacer">&nbsp;</div><br>' + 
		'<p>Or, copy and paste this URL into a podcasting tool:</p>' + 
		'<div class="podfield">' + 
        '<a target="_blank" href="' + customPodRssQuery + '"><img height="14" width="27" alt="Subscription link" src="http://download.npr.org/anon.npr-www/chrome/icon_pod_chicklet.gif"/></a>' + 
        '<input type="text" onclick="this.select();" value="' + customPodRssQuery + '" id="podurl" size="35"/>' + 
        '</div>';
	document.getElementById("subscribeTools").innerHTML = customSubLinks;
}

