var ticker_shortnewsBox = {

	// global xmlhttprequest object
	xmlHttp: false,

	/** AJAX functions **/

	// constants
	REQUEST_GET        : 0,
	REQEST_POST        : 2,
	REQUEST_HEAD       : 1,
	REQUEST_XML        : 3,
	
	curentTime		   : 0,



	/**
	 * instantiates a new xmlhttprequest object
	 *
	 * @return xmlhttprequest object or false
	 */
	getXMLRequester: function( )
	{
     var xmlHttp = false;
            
     // try to create a new instance of the xmlhttprequest object        
     try
     {
        // Internet Explorer
        if( window.ActiveXObject )
        {
            for( var i = 10; i; i-- )
            {
                try
                {
                    // loading of a newer version of msxml dll (msxml3 - msxml5) failed
                    // use fallback solution
                    // old style msxml version independent, deprecated
                    if( i == 2 )
                    {
                        this.xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
                    }
                    // try to use the latest msxml dll
                    else
                    {
                        
                        this.xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
                    }
                    break;
                }
                catch( excNotLoadable )
                {                        
                    this.xmlHttp = false;
                }
            }
        }
        // Mozilla, Opera und Safari
        else if( window.XMLHttpRequest )
        {
            this.xmlHttp = new XMLHttpRequest();
        }
     }
     // loading of xmlhttp object failed
     catch( excNotLoadable )
     {
        this.xmlHttp = false;
     }
     return this.xmlHttp ;
	},


	/**
 	* sends a http request to server
	* @param strSource, String, datasource on server, e.g. data.php
	* @param strData, String, data to send to server, optionally
 	* @param intType, Integer,request type, possible values: REQUEST_GET, REQUEST_POST, REQUEST_XML, REQUEST_HEAD default REQUEST_GET
 	* @param strData, Integer, ID of this request, will be given to registered event handler onreadystatechange', optionally
 	* @return String, request data or data source
	*/
	sendRequest: function( strSource, strData, intType, intID )
	{
     if( !strData )
        strData = '';

     // default type (0 = GET, 1 = xml, 2 = POST )
     if( isNaN( intType ) )
        intType = 0; // GET

     // previous request not finished yet, abort it before sending a new request
     if( this.xmlHttp && this.xmlHttp.readyState )
     {
        this.xmlHttp.abort( );
        this.xmlHttp = false;
     }
        
     // create a new instance of xmlhttprequest object
     // if it fails, return
     if( !this.xmlHttp )
     {
        this.xmlHttp = this.getXMLRequester( );
        if( !this.xmlHttp )
            return;
     }
    
    // parse query string
    if( intType != 1 && ( strData && strData.substr( 0, 1 ) == '&' || strData.substr( 0, 1 ) == '?' ) )
        strData = strData.substring( 1, strData.length );

    // data to send using POST
    var dataReturn = strData ? strData : strSource;
    
     switch( intType )
     {
        case 1:    // xml
            strData = "xml=" + strData;
        case 2: // POST
            // open the connection 
            this.xmlHttp.open( "POST", strSource, true );
            this.xmlHttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
            this.xmlHttp.setRequestHeader( 'Content-length', strData.length );
            break;
        case 3: // HEAD
            // open the connection 
            this.xmlHttp.open( "HEAD", strSource, true );
            strData = null;
            break;
        default: // GET
            // open the connection 
            var strDataFile = strSource + (strData ? '?' + strData : '' );
            this.xmlHttp.open( "GET", strDataFile, true );
            strData = null;
     }
    
     // set onload data event-handler
     this.xmlHttp.onreadystatechange = new Function( "", "ticker_shortnewsBox.processResponse(" + intID + ")" ); ;

     // send request to server
     this.xmlHttp.send( strData );    // param = POST data
    
     return dataReturn;
	},
    

	processResponse: function( intID )
	{
     // status 0 UNINITIALIZED open() has not been called yet.
     // status 1 LOADING send() has not been called yet.
     // status 2 LOADED send() has been called, headers and status are available.
     // status 3 INTERACTIVE Downloading, responseText holds the partial data.
     // status 4 COMPLETED Finished with all operations.
     switch( this.xmlHttp.readyState )
     {
        // uninitialized
        case 0:
        // loading
        case 1:
        // loaded
        case 2:
        // interactive
        case 3:
            break;
        // complete
        case 4:    
            // check http status
            if( this.xmlHttp.status == 200 )    // success
            {
                this.processData( this.xmlHttp, intID );
            }
            // loading not successfull, e.g. page not available
            else
            {
                if( window.handleAJAXError )
                    handleAJAXError( this.xmlHttp, intID );
                else
                    alert( "ERROR\n HTTP status = " + this.xmlHttp.status + "\n" + this.xmlHttp.statusText ) ;
            }
     }
	},

	processData: function( xmlHttp, intID )
	{  
	 document.getElementById('tdl_live').className = '';
	 document.getElementById('tdl_short').className = '';
	 document.getElementById('tdl_upd').className = '';
  
     this.curentTime = parseFloat( xmlHttp.responseXML.getElementsByTagName('curentTime')[0].firstChild.nodeValue ); 
	 
	 this.parseUpdates ( xmlHttp.responseXML.getElementsByTagName('updates')[0]   );
	 this.parseShotnews( xmlHttp.responseXML.getElementsByTagName('shortnews')[0] );
	 this.parseTicker  ( xmlHttp.responseXML.getElementsByTagName('ticker')[0]    );
	},

	fetch_unix_timestamp: function()
	{
	  return parseInt(new Date().getTime().toString().substring(0, 10))
	},
	
	setCookie: function(name, value, expiresin, path, domain, secure) 
	{
	 if (expiresin<1)
	   expiresin=1;
  
	  now=new Date();
	  expires=new Date(now.getTime()+expiresin*86400000);

	  var curCookie = name + "=" + escape(value) +
	      ((expires) ? "; expires=" + expires.toGMTString() : "") +
	      ((path)    ? "; path=" + path : "") +
	      ((domain)  ? "; domain=" + domain : "") +
	      ((secure)  ? "; secure" : "");
	  document.cookie = curCookie;
	},

	getCookie: function(name) 
	{
	  var dc = document.cookie;
	  var prefix = name + "=";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  } else
	    begin += 2;
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	},

	strpad: function(val)
	{
	  return (!isNaN(val) && val.toString().length==1)?"0"+val:val;
	},

	unix_timestamp_to_human: function(source)
	{
	  var d = new Date(source*1000);
	  return this.strpad(d.getDate())+'.'+this.strpad(d.getMonth()+1)+'.'+d.getFullYear()+' '+this.strpad(d.getHours())+':'+this.strpad(d.getMinutes());
	},

	parseTicker: function( xml )
	{
	 var ausgabe  = '';
	 var utime    = this.getCookie('InfoBoxliveLastReadet'); 
	 var setBlink = false;
 
	 var items = xml.getElementsByTagName('item');
 
	 for (var i = 0; i < items.length; ++i)
	 {
	  var text = items[i].getElementsByTagName('description')[0].firstChild.nodeValue;
	  var date = items[i].getElementsByTagName('pubdate')[0].firstChild.nodeValue;
	  var url  = items[i].getElementsByTagName('link')[0].firstChild.nodeValue; 
      
	  if (parseFloat(date)>parseFloat(utime))
	  {
	   var hightligt = 'class="hightligt"';
	   setBlink = true;
	  } else {
	   var hightligt = '';
	  }
  
	  ausgabe += '<div id="contents" '+hightligt+'>';
	  ausgabe += '<a target="_blank" href="'+url+'">'+text+'</a><br />'+this.unix_timestamp_to_human(date);
	  ausgabe += '</div>';
	 } 
	 
	 if (setBlink==true && document.getElementById('tdl_live').style.display != 'block')
	 {
	  document.getElementById('tdl_live').className = 'hasNew';
	  this.showBox('live', true);
	 }	
 
	 document.getElementById('obj_live').innerHTML = ausgabe;
	},

	parseShotnews: function( xml )
	{
	 var ausgabe = '';
	 var utime   = this.getCookie('InfoBoxshortLastReadet');
	 var setBlink = false;

	 var items = xml.getElementsByTagName('item');

	 for (var i = 0; i < items.length; ++i)
	 {  
	  var text = items[i].getElementsByTagName('description')[0].firstChild.nodeValue;
	  var date = items[i].getElementsByTagName('pubdate')[0].firstChild.nodeValue;
	  var url  = items[i].getElementsByTagName('link')[0].firstChild.nodeValue; 
	  var isinticker  = items[i].getElementsByTagName('isinticker')[0].firstChild.nodeValue;

	  if (parseFloat(date)>parseFloat(utime))
	  {     
	   var hightligt = 'class="hightligt"';
	   
	   if (isinticker!='1')
	    setBlink = true;
	  } else {
	   var hightligt = '';
	  }
  
	  ausgabe += '<div id="contents" '+hightligt+'>';
	  ausgabe += '<a target="_blank" href="'+url+'">'+text+'</a><br />'+this.unix_timestamp_to_human(date);	
	  ausgabe += '</div>';
	 }
 
	 if (setBlink==true && document.getElementById('tdl_short').style.display != 'block')
	 {
	  document.getElementById('tdl_short').className = 'hasNew';
	  this.showBox('short', true);
	 } 
  
	 document.getElementById('obj_short').innerHTML = ausgabe;
	},

	parseUpdates: function( xml )
	{
	 var ausgabe = '';
	 var utime   = this.getCookie('InfoBoxupdLastReadet');
	 var setBlink = false;

	 var items = xml.getElementsByTagName('item');

	 for (var i = 0; i < items.length; ++i)
	 {  
	  var titel = items[i].getElementsByTagName('titel')[0].firstChild.nodeValue;
	  var date  = items[i].getElementsByTagName('pubdate')[0].firstChild.nodeValue;
	  var url   = items[i].getElementsByTagName('link')[0].firstChild.nodeValue; 
  
	  if (parseFloat(date)>parseFloat(utime))
	  {
	   var hightligt = 'class="hightligt"';
           setBlink = true;
	  } else {
	   var hightligt = '';
	  }
  
	  ausgabe += '<div id="contents" '+hightligt+'>';
	  ausgabe += '<a target="_blank" href="'+url+'">'+titel+'</a><br />'+this.unix_timestamp_to_human(date);
	  ausgabe += '</div>';
	 }
  
	 if (setBlink==true && document.getElementById('tdl_upd').style.display != 'block')
	  document.getElementById('tdl_upd').className = 'hasNew';
 
	 document.getElementById('obj_upd').innerHTML = ausgabe;
	},

	showBox: function(obj, hideBlink)
	{
	 document.getElementById('tdl_live').style.fontWeight  = 'normal';	
	 document.getElementById('tdl_short').style.fontWeight = 'normal';
	 document.getElementById('tdl_upd').style.fontWeight   = 'normal';
	 document.getElementById('tdl_'+obj).style.fontWeight  = 'bold';
	 
	 if (hideBlink!=true)
	  document.getElementById('tdl_'+obj).className = '';

	 document.getElementById('obj_live').style.display  = 'none';
	 document.getElementById('obj_short').style.display = 'none';
	 document.getElementById('obj_upd').style.display   = 'none';
	 document.getElementById('obj_'+obj).style.display  = 'block';
      
	 if (this.curentTime>0)
	  this.setCookie('InfoBox'+obj+'LastReadet', this.curentTime, 60);
	}
}


ticker_shortnewsBox.sendRequest('/rss/ticker_shortnews_updates_box.xml');
window.setInterval("ticker_shortnewsBox.sendRequest('/rss/ticker_shortnews_updates_box.xml')", 120000);
//ticker_shortnewsBox.sendRequest('http://www.live-wintersport.com/ticker_shortnews_updates_box.php');
//window.setInterval("ticker_shortnewsBox.sendRequest('http://www.live-wintersport.com/ticker_shortnews_updates_box.php')", 120000);

