//
//  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 sDivTitle = sTitle + "_lista_noticias"

/***/
	var hImg1 = document.createElement( 'img' )
	hImg1.src = 'images/ver.gif'
	hImg1.id = "imgUpper" + sDivTitle
	hImg1.setAttribute( 'onclick', "Oculta('" + sDivTitle + "');" )
	hContainer.appendChild( hImg1 )
	
	var hImg2 = document.createElement( 'img' )
	hImg2.src = 'images/nover.gif'
	hImg2.id = "imgDown" + sDivTitle
	hImg2.setAttribute( 'onclick', "Muestra('" + sDivTitle + "');" )	
	hImg2.style.display = 'none' 
	hContainer.appendChild( hImg2 )    
/***/ 
 
    var hLink = document.createElement( 'a' )
    hLink.href = sLink
    hLink.target = this.linkTarget
    hLink.appendChild( document.createTextNode( sTitle ) )
    hContainer.appendChild( hLink )





/***
	var hDivItemList = document.createElement( 'div' )
	hDivItemList.id = sDivTitle
	hDivItemList.class = 'right_content'
    hContainer.appendChild( hDivItemList )
	
	
    var hItemList = document.createElement( 'ul' )
	//hItemList.id = sTitle + "_lista_noticias"
	hDivItemList.appendChild( hItemList )
    //hContainer.appendChild( hItemList )

/***/
    var hItemList = document.createElement( 'ul' )
	hItemList.id = sDivTitle
    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 sPhotoImg = ""   ///PhotoImg management, for gti-gallerys
	
	

    var hItemNode = document.createElement( 'li' )
    var hItemLink = document.createElement( 'a' )
    var hItemDesc = document.createElement( 'div' )
	//hItemDesc.class = 'content'
	
	var hImg = document.createElement( 'img' )
	

	var table = document.createElement("table")
	/**/
	table.setAttribute("cellspacing", "0")
	table.setAttribute("cellpadding", "0")
    table.setAttribute("border", "0")
	/**/
	//table.setAttribute("margin", "0 5px 0 0")
	//table.setAttribute("style", "border: 0; cellspacing: 0; padding: 0; margin-left: 5px")	
	table.marginBottom = "0"
	table.marginTop = "0"
	table.marginLeft = "5px"
	table.marginRight = "5px"	
	table.paddingBottom = "0"
	table.paddingTop = "0"
	table.paddingLeft = "5px"
	table.paddingRight = "5px"	

    var tbody = document.createElement("tbody")
    var y = 0
    var tr = document.createElement("tr")
    var td = document.createElement("td")
	
	var sAux = getNodeText( getSubNodeByName( sItemXML, 'title' ) )


/***/
	var hImg1 = document.createElement( 'img' )
	hImg1.src = 'images/ver.gif'
	sAux = 'div' + sAux
	hImg1.id = "imgUpper" + sAux
	hImg1.setAttribute( 'onclick', "Oculta('" + sAux  + "');" )
	hImg1.style.display = 'none' 	
	
	td.appendChild( hImg1 )
	tr.appendChild(td)
	
	td = document.createElement("td")
	//td.style.marginRight='5px'; 
	var hImg2 = document.createElement( 'img' )
	hImg2.src = 'images/nover.gif'
	//sAux = "div" + sAux
	hImg2.id = "imgDown" + sAux
	hImg2.setAttribute( 'onclick', "Muestra('" +  sAux  + "');" )	


	td.appendChild( hImg2 )
	tr.appendChild(td)
/***/

	td = document.createElement("td")
    hItemLink.href = getNodeText( getSubNodeByName( sItemXML, 'link' ) )
    hItemLink.target = this.linkTarget
	hItemLink.align = 'center'

	td.appendChild( hItemLink )
	tr.appendChild(td)
	tbody.appendChild(tr)
	
	
	

	

/**/

	sPhotoImg = getNodeText( getSubNodeByName( sItemXML, 'photo:imgsrc') )
	if (sPhotoImg != "" && this.displayType == 'descriptive')
	{
		hImg.src = sPhotoImg
		hImg.align = 'center'
/*		hCol.appendChild( hImg )
///		hCol2.appendChild( hItemLink )
		hCol2.appendChild( document.createTextNode( getNodeText( getSubNodeByName( sItemXML, 'title' ) ) ) )
   		hRow.appendChild( hCol )
		hRow.appendChild( hCol2 )
		//hItemNode.appendChild( hTable )
		*/
	    hItemLink.appendChild( hImg )	
		//hImg1.appendChild( hImg )	
		//hItemLink.appendChild( hTable )
	}
	/*
	else {
		hItemLink.appendChild( document.createTextNode( getNodeText( getSubNodeByName( sItemXML, 'title' ) ) ) )
		//hItemNode.appendChild( hItemLink )
	}
*/




    hItemLink.appendChild( document.createTextNode( '  ' + getNodeText( getSubNodeByName( sItemXML, 'title' ) ) ) )
    //hItemNode.appendChild( hItemLink )
	

    sDesc = getNodeText( getSubNodeByName( sItemXML, 'description' ) )
	

    if( sDesc != "" && this.displayType == 'descriptive' )
    {

		hItemLink.title = sDesc;
        //hItemDesc.text = sDesc;
		
		//var nodeDesc = document.createContextualFragment( sDesc );		
		//var nodeDesc = document.createElement( sDesc );
		
		
		var nodeDesc = document.createTextNode( sDesc );
		
		
		hItemDesc.appendChild( nodeDesc )
		

		//hItemDesc.id = 'div' + sAux
		hItemDesc.id = sAux
		hItemDesc.style.display = 'none'

		hItemDesc.style.color = 'navy'
        //hItemNode.appendChild( hItemDesc )
		
		/***/
		tr = document.createElement("tr")
		td = document.createElement("td")
        tr.appendChild(td)
		td = document.createElement("td")
        tr.appendChild(td)
		td = document.createElement("td")
		td.appendChild( hItemDesc )
        tr.appendChild(td)
		tbody.appendChild(tr)
		/***/
    }

	

    table.appendChild(tbody)
    hItemNode.appendChild(table)


    return hItemNode
}

cDomExtensionManager.register( new cDomExtension( document, [ "div[rss]" ], cRSSParser.attachTo ) )

