//
//  This script was created
//  by Mircho Mirev
//  mo /mo@momche.net/
//
//	:: feel free to use it BUT
//	:: if you want to use this code PLEASE send me a note
//	:: and please keep this disclaimer intact
//

function cRSSParser( hDiv )
{
	this.sName = cRSSParser.CS_NAME + cRSSParser.CN_COUNT++
	this.hObj = this.sName
	eval( this.hObj + ' = this' )
	this.init( hDiv )
}


cRSSParser.CS_NAME = "cRSSParser"
cRSSParser.CN_COUNT = 0
//	possible display types: 
// 	links		-	display links only
// 	descriptive	-	display description too

cRSSParser.CS_DEFAULT_DISPLAY = "descriptive"

cRSSParser.CS_LINK_TARGET = ""

//after this timeout an error will be displayed
cRSSParser.CN_TIMEOUT = 5000


cRSSParser.getDependencies = function()
{
	return cRSSParser.sDependencies
}

cRSSParser.getName = function()
{
	return cRSSParser.sName
}

cRSSParser.onLoad = function()
{
	return cRSSParser.getName()
}

cRSSParser.attachTo = function( hDiv )
{
	var hComponent = new cRSSParser( hDiv )
}

cRSSParser.prototype.init = function( hDiv )
{
	this.hDiv = hDiv
	this.hDiv.rssReader = this
	/*
	while( this.hDiv.hasChildNodes() )
	{
		this.hDiv.removeChild( this.hDiv.childNodes[ 0 ] )
	}
	*/
	
	this.onRssLoad = function(){}
	var sOnLoad = this.hDiv.getAttribute( 'onrssload' )
	if( sOnLoad != null && sOnLoad.length > 0 )
	{
		this.onRssLoad = new Function( sOnLoad )
	}
	this.hXMLHttp = XmlHttp.create()
	this.displayType = cRSSParser.CS_DEFAULT_DISPLAY
	this.linkTarget = cRSSParser.CS_LINK_TARGET
	var sAutoLoad = this.hDiv.getAttribute( 'rssauto' )
	if( sAutoLoad != null && sAutoLoad.length > 0 && !eval(sAutoLoad) )
	{
		//load later
	}
	else
	{
		this.load()
	}
}


cRSSParser.prototype.load = function()
{
	var hNewContainer = document.createElement( 'div' )
	var hContainer = getSubNodeByName( this.hDiv, 'div' )
	if( hContainer != null )
	{
		//alert(hContainer)
		this.hDiv.replaceChild( hNewContainer, hContainer )
	}
	else
	{
		this.hDiv.appendChild( hNewContainer )
	}
	hNewContainer.className = 'wait'

	var sDisplay = this.hDiv.getAttribute( 'rssdisplay' )
	if( sDisplay != null && sDisplay.length > 0 )
	{
		if( sDisplay == 'links' || sDisplay == 'descriptive' )
		{
			this.displayType = sDisplay
		}
	}
	
	var sLinkTarget = this.hDiv.getAttribute( 'rsstarget' )
	if( sLinkTarget != null && sLinkTarget.length > 0 )
	{
		this.linkTarget = sLinkTarget
	}
	
	var sAddress = this.hDiv.getAttribute( 'rss' )
	this.hXMLHttp.open( 'GET', sAddress, true )
	this.hXMLHttp.onreadystatechange = new Function( 'var sObjectName = "'+this.sName+'"; cRSSParser.readyStateChange( eval( sObjectName ) )' )
	this.hXMLHttp.send( null )
	this.hAbortTimeout = setTimeout( this.hObj+'.onTimeoutError()', cRSSParser.CN_TIMEOUT )
}

cRSSParser.prototype.setURL = function( sURL )
{
	this.hDiv.setAttribute( 'rss', sURL )
}

cRSSParser.prototype.getURL = function( sURL )
{
	return this.hDiv.getAttribute( 'rss' )
}

cRSSParser.readyStateChange = function( hThis )
{
	if( hThis.hXMLHttp.readyState == 4 )
	{
		var hError = hThis.hXMLHttp.parseError
		if( typeof hError == 'undefined' )
		{
			hError = new Object()
			hError.errorCode = 0
			hError.reason = ''
		}
		
		if ( !hThis.hXMLHttp.responseXML.documentElement || hThis.hXMLHttp.responseXML.documentElement.tagName == 'parsererror' )
		{
			hError.errorCode = 1
			hError.reason = "Error"
		}
		if( hError && hError.errorCode != 0 )
		{
			//alert( hError.reason )
		}
		else
		{
			clearTimeout( hThis.hAbortTimeout )
			var hWaitContainer = getSubNodeByName( hThis.hDiv, 'div' )
			var hContainer = hThis.createHTML( hThis.hXMLHttp.responseXML )
			hThis.hDiv.replaceChild( hContainer, hWaitContainer )
			hThis.onRssLoad()
		}
	}
}

cRSSParser.prototype.createHTML = function( hXML )
{
	var hNode = getSubNodeByName( hXML.documentElement, 'channel' )
	var hChildren = hNode.getElementsByTagName( 'item' )
	if( hChildren.length == 0 )
	{
		hChildren = hXML.documentElement.getElementsByTagName( 'item' )
	}
	
	var sLink = ""
	var sTitle = ""
	var sItems = ""
	var sResult = ""
	
	sTitle = getNodeText( getSubNodeByName( hNode, 'title' ) )
	sLink = getNodeText( getSubNodeByName( hNode, 'link' ) )
	var hContainer = document.createElement( 'div' )
	var hLink = document.createElement( 'a' )
	hLink.href = sLink
	hLink.target = this.linkTarget
	hLink.appendChild( document.createTextNode( sTitle ) )
	hContainer.appendChild( hLink )
	
	var hItemList = document.createElement( 'ul' )
	hContainer.appendChild( hItemList )

	for ( var i = 0; i < hChildren.length; i++ ) 
	{
		hItem = hChildren.item(i)
		if( hItem.nodeType == 1 )
		{
			if( hItem.nodeName == "item" )
			{
				hItemList.appendChild( this.processItem( hItem ) )
			}
		}
	}
	
	return hContainer
}

cRSSParser.prototype.onTimeoutError = function()
{
	this.hXMLHttp.abort()
	var hContainer = document.createElement( 'div' )
	hContainer.className = 'error'
	var hErrorNode = document.createTextNode( 'Error loading: '+this.getURL() )
	hContainer.appendChild( hErrorNode )
	var hWaitContainer = getSubNodeByName( this.hDiv, 'div' )
	this.hDiv.replaceChild( hContainer, hWaitContainer )
}

cRSSParser.prototype.processItem = function( sItemXML )
{
	var hChildren = sItemXML.childNodes
	var hItem = null

	var sTitle = ""
	var sLink = ""
	var sDesc = ""
	var sResult = ""
	
	var hItemNode = document.createElement( 'li' )
	var hItemLink = document.createElement( 'a' )
	var hItemDesc = document.createElement( 'div' )
	hItemLink.href = getNodeText( getSubNodeByName( sItemXML, 'link' ) )
	hItemLink.target = this.linkTarget
	hItemLink.appendChild( document.createTextNode( getNodeText( getSubNodeByName( sItemXML, 'title' ) ) ) )
	hItemNode.appendChild( hItemLink )

	sDesc = getNodeText( getSubNodeByName( sItemXML, 'description' ) )
	if( sDesc != "" && this.displayType == 'descriptive' )
	{
		hItemDesc.text = getNodeText( getSubNodeByName( sItemXML, 'description' ) )
		hItemNode.appendChild( hItemDesc )
	}

	return hItemNode 
}

cDomExtensionManager.register( new cDomExtension( document, [ "div@rss" ], cRSSParser.attachTo ) )
