﻿if (typeof(KG) == "undefined") 
    Type.registerNamespace("KG");

KG.NewsItem = function(id, title,item,uri,target) {
    this._id = id;
    this._title = title;
    this._item = item;
    this._uri = uri;
    this._target = target;
    
    
}

KG.NewsItem.prototype = {

    getId: function() {
        return this._id;
    },

    getTitle: function() {
        return this._title;
    },
    
    getItem: function() {
        return this._item;
    },
        
    getURI: function() {
        return this._uri;
    },
    
    getTarget: function() {
        return this._target;
    },

    dispose: function() {
        //alert('bye ' + this.getName());
    }
}
KG.NewsItem.registerClass('KG.NewsItem', null, Sys.IDisposable);


////////////////////////////////////////////////////////////////////////////////////////////
// end KG.NewsItem Prototype
////////////////////////////////////////////////////////////////////////////////////////////



var _newsItem = null;
var _timer = null;
var _interval = 5000;


function pageLoad(sender, eventArgs)
{
    InitializeComponent();    
}

function InitializeComponent()
{
    var v = $get('span_newsitem');
    if(v!=null)
    {
        if( $get('span_newsitem').innerHTML == 'Rastelli Foods');
            GetNewsItem();
            
        Watcher();
    }
}

function Watcher()
{
    if(_newsItem == null)
    {
        _newsItem = new KG.NewsItem(-1,'Rastelli Foods', 'RastStore2008','http:www.rastellis.com');
        _timer = setTimeout("Watcher()",_interval);
        
        //alert('new newsitem' + _newsItem.getValue());
    }
    else
    {
        if(_timer !=null)
            clearTimeout(_timer);
            
        GetNewsItem();
        //alert(_newsItem.getValue());
    }
}

function GetNewsItem()
{
    //NewsItemService.GetNewsItem(_newsItem.getId(),GetItems_Complete,onError);
    NewsItemService.GetNewsItem(GetItems_Complete,onError);
}

function GetItems_Complete(item)
{
    //alert(item.ID);
    
    if(item.ID == -1)
    {
         _newsItem = new KG.NewsItem(-1,'Rastelli Foods', 'RastStore2008','http:www.rastellis.com','');
         GetNewsItem();
        //_timer = setTimeout("Watcher()",_interval);
        return;
    }
        
        
    if(item != null)
    {
        _newsItem = new KG.NewsItem(item.ID,item.Title,item.Item,item.URI,item.Target);
        
        $get('span_newsitem').innerHTML = "<a href='" + _newsItem.getURI() + "' target='" + _newsItem.getTarget() + "'>" + _newsItem.getTitle() + "</a>";
        
        _timer = setTimeout("Watcher()",_interval);
    }
}

function onError(error)
{
    // ok try again
    //alert(error.get_message());
    _timer = setTimeout("Watcher()",_interval);
}

// cookie to persist the current news item
/*function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}*/




Sys.Application.notifyScriptLoaded();