// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
 
// when set to true, display detailed error messages
var showErrors = true;

var myPipeData = "";
 
// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
 
// will store the reference to the XMLHttpRequest object
var xmlHttp;
// this should work for all browsers except IE6 and older
try
{
// try to create XMLHttpRequest object
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// assume IE6 or older
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
 
// try every prog id until one works
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
{
 
try {
// try to create XMLHttpRequest object
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
} 
catch (e) {} // ignore potential error
}
}
 
// return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
 
}
 
// function that displays an error message
function displayError($message)
{
// ignore errors if showErrors is false
if (showErrors)
{
// turn error displaying Off
showErrors = false;
// display error message
alert("Error encountered: \n" + $message);
}
}
 
// Retrieve titles from a feed and display them
//function getFeed(feedLink, feed)
function pumpPipe(feed)
{
// only continue if xmlHttp isn't void

if (xmlHttp)
{
// try to connect to the server
try
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
/* Get number of feeds and loop through each one of them to
change the class name of their container (<li>). */
//var numberOfFeeds = document.getElementById("feedList").childNodes.length;
 
//for (i = 0; i < numberOfFeeds; i++)
//document.getElementById("feedList").childNodes[i].className = "";
// Change the class name for the clicked feed so it becomes // highlighted
//feedLink.className = "active";
// Display "Loading..." message while loading feed
document.getElementById("mainNewsCont").style.display = "none";
document.getElementById("loading").style.display = "block";
// Call the server page to execute the server-side operation

//Stane
//This actually calls php script that produces the response

params = "feed=" + feed;
//xmlHttp.open("POST", "pump.php", true);
xmlHttp.open("GET", "cache/" + feed, true);
xmlHttp.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = handleHttpGetFeeds;
xmlHttp.send(params);
}
else
{
// if connection was busy, try again after 1 second
//setTimeout("getFeed('" + feedLink + "', '" + feed + "');", 1000);
setTimeout("pumpPipe('" + feed + "');", 1000);
}
}
// display the error in case of failure
catch (e)
{
displayError(e.toString());
}
}
}
 
// function that retrieves the HTTP response
function handleHttpGetFeeds()
{
// continue if the process is completed
if (xmlHttp.readyState == 4)
{
// continue only if HTTP status is "OK"
if (xmlHttp.status == 200)
{
try
{
displayFeed();
}
catch(e)
{
// display error message
displayError(e.toString());
}
}
else
{
displayError(xmlHttp.statusText);
}

}
}
// Processes server's response
function displayFeed(where_to_put_it)
{
// read server response as text, to check for errors
var response = xmlHttp.responseText;
// server error?
if (response.indexOf("ERRNO") >= 0
//|| response.indexOf(" error:") >= 0
|| response.length == 0)
throw(response.length == 0 ? "Void server response." : response);

// hide the "Loading..." message upon feed retrieval
document.getElementById("loading").style.display = "none";


var titlesContainer = document.getElementById("mainNewsDesc");
//Parse output //try and cache might be problematic
var myPipeOutput = "";

if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/. test(response)) {
myPipeData = eval('(' + response + ')');
	
	//Output Pipe header
myPipeOutput =  "<div class=\"pipe_head\"><div class=\"pipe_title\"><a href=\"" + myPipeData.value.link + "\">"+  myPipeData.value.title +"</a></div><div class=\"pipe_desc\">" + myPipeData.value.description + "</div></div>";

titlesContainer.innerHTML = myPipeOutput;
myPipeOutput = "";
//Start the items section of the output
myPipeOutput += "<div class=\"pipe_items\">";
var i = 0;
while(myPipeData.value.items[i]!=null)
{
	
	myPipeOutput +="<div class=\"main_news_item\" id=\"" + i + "\" onmouseover=\"showDeep(this)\" onmouseout=\"hideHigh(this)\">";
	myPipeOutput +="<div class=\"main_item_title\"><a target=\"_blank\" href=\""+myPipeData.value.items[i].link+"\">"+myPipeData.value.items[i].title+"</a></div>";
	
	//Flickrs
		var j = 0;
		if(myPipeData.value.items[i].flickr[j]!=null)
		{
			myPipeOutput += "<p>";
			//Add to which divider this is pumped
			while((myPipeData.value.items[i].flickr[j]!=null)&&(j<5))
			{
				myPipeData.value.items[i].flickr[j].description = myPipeData.value.items[i].flickr[j].description.replace(/width='160'/g,"");
				myPipeData.value.items[i].flickr[j].description = myPipeData.value.items[i].flickr[j].description.replace(/height='240'/g,"");
				myPipeData.value.items[i].flickr[j].description = myPipeData.value.items[i].flickr[j].description.replace(/&amp;/g,"&");
				myPipeData.value.items[i].flickr[j].description = myPipeData.value.items[i].flickr[j].description.replace(/&quot;/g,"\"");
				myPipeData.value.items[i].flickr[j].description = myPipeData.value.items[i].flickr[j].description.replace(/&lt;/g,"<");
				myPipeData.value.items[i].flickr[j].description = myPipeData.value.items[i].flickr[j].description.replace(/&gt;/g,">");
				myPipeData.value.items[i].flickr[j].description = myPipeData.value.items[i].flickr[j].description.replace(/&#39;/g,"\'");
				myPipeOutput += "<a target=\"_blank\" href=\""+myPipeData.value.items[i].flickr[j].link+"\">"+myPipeData.value.items[i].flickr[j].description+"</a>";
				j++;
			}
			myPipeOutput +="<br/><font size='1'><img src=\"icon/flickr.png\" alt=\"wikipedia\"/> Random from Flickr:</font>";
			myPipeOutput += "</p>";
		}
		
	myPipeOutput +="<div class=\"main_item_desc\">"+myPipeData.value.items[i].description+"</div>";
	myPipeOutput +="</div>";
	i++;
}



//Close Items divider
myPipeOutput += "</div>";
}
else
{
	 myPipeOutput="Problems whith pipe, plumber on the way. Please pick another news feed";
}

titlesContainer = document.getElementById("mainNewsCont");
titlesContainer.innerHTML = myPipeOutput;
titlesContainer.style.display = "block";

getDeep(0);

}

function hideHigh(currentItem){
	currentItem.style.backgroundColor="";
}

function showDeep(currentItem){
	
	var indi = currentItem.id;
	currentItem.style.backgroundColor="#F0F0F0";
	document.getElementById("my_tabla").style.marginTop = currentItem.offsetTop - 0 + "px";
	document.getElementById("mainNewsCont").style.marginBottom = document.getElementById("my_tabla").offsetHeight - 0 +"px";	
	getDeep(indi);
	
}

//This actually displays text in the right coulumn
function getDeep(indi){
				
		
		var myPipeOutput = "<table border='0' cellspacing='0' cellpadding='0'><tr>"
								
		var k =0;
		var j = 0;
		if(myPipeData.value.items[indi].wiki[j]!=null)
		{
			k++;
			myPipeOutput +="<td bgcolor='#E0E0E0' width='150px' valign='top'>";
			//Open wikis divider
			myPipeOutput += "<div id=\"wikis\" class=\"deep_items\" style=\"display:block\">";
			myPipeOutput +="<p><img src=\"icon/wikipedia.png\" alt=\"wikipedia\"/> Wikipedia:</p>";
			while(myPipeData.value.items[indi].wiki[j]!=null)
			{
				
				//Filter unneeded text
				myPipeData.value.items[indi].wiki[j].title = myPipeData.value.items[indi].wiki[j].title.replace(/- Wikipedia, the free encyclopedia/g,"");
				myPipeData.value.items[indi].wiki[j].title = myPipeData.value.items[indi].wiki[j].title.replace(/- Wikipédia/g,"");
				myPipeData.value.items[indi].wiki[j].title = myPipeData.value.items[indi].wiki[j].title.replace(/- Vikipedija/g,"");
				
				if((NoRepeats(indi, j, 0)==1)&&(philter(myPipeData.value.items[indi].wiki[j].title)==1))
				{
					//Add to which divider this is pumped
					myPipeOutput += "<div class=\"deep_item\">";
					myPipeOutput += "<div class=\"deep_item_title\"><a target=\"_blank\" href=\""+myPipeData.value.items[indi].wiki[j].link+"\">"+myPipeData.value.items[indi].wiki[j].title+"</a></div>";
					//myPipeOutput += "<div class=\"deep_item_title\"><a target=\"_blank\" href=\""+myPipeData.value.items[indi].wiki[j].link+"\">"+mytitle+"</a></div>";
					myPipeOutput += "</div>";
				}
				j++;
			}
			//Close flickr divider
			myPipeOutput += "</div>";
			myPipeOutput +="</td>";
		}
		
		
		
		
		j = 0;
		if(myPipeData.value.items[indi].technorati[j]!=null)
		{
			k++;
			myPipeOutput +="<td bgcolor='#E6E6E6' width='150px' valign='top'>";
			//Open wikis divider
			myPipeOutput += "<div id=\"technoratiblogs\" class=\"deep_items\" style=\"display:block\">";
			myPipeOutput +="<p><img src=\"icon/technorati.png\" alt=\"technorati\"/> Blogs on Technorati:</p>";
			while(myPipeData.value.items[indi].technorati[j]!=null)
			{
				//Add to which divider this is pumped
				myPipeData.value.items[indi].technorati[j].title = myPipeData.value.items[indi].technorati[j].title.replace(/: See what people are saying right now on Technorati/g,"");
				if((NoRepeats(indi, j, 2)==1)&&(philter(myPipeData.value.items[indi].technorati[j].title)==1))
				{
					myPipeOutput += "<div class=\"deep_item\">";
					myPipeOutput += "<div class=\"deep_item_title\"><a target=\"_blank\" href=\""+myPipeData.value.items[indi].technorati[j].link+"\">"+myPipeData.value.items[indi].technorati[j].title+"</a></div>";
					myPipeOutput += "</div>";
				}
				j++;
			}
			//Close flickr divider
			myPipeOutput += "</div>";
			myPipeOutput +="</td>";
		}
		
		
		
		
		j = 0;
		if(myPipeData.value.items[indi].vizu[j]!=null)
		{
			k++;
			myPipeOutput +="<td bgcolor='#ECECEC' width='150px' valign='top'>";
			//Open wikis divider
			myPipeOutput += "<div id=\"vizupolls\" class=\"deep_items\" style=\"display:block\">";
			myPipeOutput +="<p><img src=\"icon/vizu.png\" alt=\"vizu\"/> Vizu Polls:</p>";
			while((myPipeData.value.items[indi].vizu[j]!=null)&&(philter(myPipeData.value.items[indi].vizu[j].title)==1))
			{
				//Add to which divider this is pumped
				myPipeData.value.items[indi].vizu[j].title = myPipeData.value.items[indi].vizu[j].title.replace(/Vizu:/g,"");
				myPipeData.value.items[indi].vizu[j].title = myPipeData.value.items[indi].vizu[j].title.replace(/Vizu/g,"");
				if(NoRepeats(indi, j, 1)==1)
				{
					myPipeOutput += "<div class=\"deep_item\">";
					myPipeOutput += "<div class=\"deep_item_title\"><a target=\"_blank\" href=\""+myPipeData.value.items[indi].vizu[j].link+"\">"+myPipeData.value.items[indi].vizu[j].title+"</a></div>";
					myPipeOutput += "</div>";
				}
				j++;
			}
			//Close flickr divider
			myPipeOutput += "</div>";
			myPipeOutput +="</td>";
		}
		
		
		
		j = 0;
		if(myPipeData.value.items[indi].urban[j]!=null)
		{
			k++;
			myPipeOutput +="<td bgcolor='#F0F0F0' width='150px' valign='top'>";
			//Open wikis divider
			myPipeOutput += "<div id=\"urbans\" class=\"deep_items\" style=\"display:block\">";
			myPipeOutput +="<p><img src=\"icon/urbandictionary.png\" alt=\"urbandictionary\"/> Urban Dictionary:</p>";
			while(myPipeData.value.items[indi].urban[j]!=null)
			{
				//Add to which divider this is pumped
				myPipeData.value.items[indi].urban[j].title = myPipeData.value.items[indi].urban[j].title.replace(/Urban Dictionary:/g,"");
				if((NoRepeats(indi, j, 3)==1)&&(philter(myPipeData.value.items[indi].urban[j].title)==1))
				{
					myPipeOutput += "<div class=\"deep_item\">";
					myPipeOutput += "<div class=\"deep_item_title\"><a target=\"_blank\" href=\""+myPipeData.value.items[indi].urban[j].link+"\">"+myPipeData.value.items[indi].urban[j].title+"</a></div>";
					myPipeOutput += "</div>";
				}
				j++;
			}
			//Close flickr divider
			myPipeOutput += "</div>";
			myPipeOutput +="</td>";
		}
		
		myPipeOutput +="</tr>";
		myPipeOutput +="<tr><td colspan='"+k+"'>";
		myPipeOutput +="<div class='footer'>";
		myPipeOutput +="DISCLAIMER: The Content of presented news items and the selection of associated articles is created entirely by the respective news providers and Yahoo Pipes service. The owner of this site declines any responsibility for potential obscene, insulting or otherwise disturbing material, or association or combination of one or more items.";
		myPipeOutput +="</div>";
		myPipeOutput +="</td></tr>";
		myPipeOutput +="</table>";
		document.getElementById("my_tabla").innerHTML = myPipeOutput;
		document.getElementById("my_tabla").width=k*150+"px";
}

//Don't print repeating titles
function NoRepeats(indi, currentj, wha)
{
	var norepeat = 1;
	switch(wha)
	{
	case 0:  
	  for(var i = 0; i < currentj; i++)
	  {
		  if((myPipeData.value.items[indi].wiki[i].title == myPipeData.value.items[indi].wiki[currentj].title))
		  {
			  norepeat = 0;
		  }
	  }
	  break    
	case 1:
	  for(var i = 0; i < currentj; i++)
	  {
		  if((myPipeData.value.items[indi].vizu[i].title == myPipeData.value.items[indi].vizu[currentj].title))
		  {
			  norepeat = 0;
		  }
	  }
	  break
	case 2:
	  for(var i = 0; i < currentj; i++)
	  {
		  if((myPipeData.value.items[indi].technorati[i].title == myPipeData.value.items[indi].technorati[currentj].title))
		  {
			  norepeat = 0;
		  }
	  }
	  break
	case 3:
	  for(var i = 0; i < currentj; i++)
	  {
		  if((myPipeData.value.items[indi].urban[i].title == myPipeData.value.items[indi].urban[currentj].title))
		  {
			  norepeat = 0;
		  }
	  }
	  break
	default:
	  
	}
	return norepeat;
}
//Truncate unneaded data

function philter(currentTitle)
{
	var oket = 1;
	var pattern =/www./;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
	
	var pattern =/fr.wikipedia/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
			
	var pattern =/fi.wikipedia/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
	
	var pattern =/technorati.com/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
			
	var pattern =/Discuter:/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
			
	var pattern =/Talk:/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
			
	var pattern =/User:/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
			
	var pattern =/Technorati Profile for/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
			
	var pattern =/vizu.com/;
	if(currentTitle.search(pattern)!=-1)
			oket = 0;
			
	return oket;
}