var MarketCurrent={
	Logg:[],
	New:[],
	LoggedNewLen:0,
	Interval:null,

	Init:function () {		
		var aIds=readCookie('mc_ids') ? readCookie('mc_ids').split(',') : [];
		
		this.New=aIds;
		this.LoggedNewLen=this.New.length;
		this.Logg=this.GetIds();

		if (this.New.length) this.DisplayInitTitle();
		setTimeout(function () { this.LoadMarketCurrents(); }.bind(this),45000);
	},

	GetIds:function () {
		var aIds=[];
		
		$$('ul.market_currents_mafne_wrapper li').each(
			function(e){
				aIds.push(e.id);
			}
		);

		return aIds;
	},

	Logger:function (Ids) {
		this.New.push(Ids);
		this.Logg=this.GetIds();
	},

	LoggNew:function (aNew) {
		this.New=this.New.concat(aNew);

		createCookie('mc_ids',this.New.join(','));
	},

	GetDiff:function () {
		var aDiff;

		if (this.Logg.length) if (aDiff=this.Disect(this.Logg, this.GetIds())) this.LoggNew(aDiff);
		this.Logg=this.GetIds();

		if (this.New.length && this.New.length > this.LoggedNewLen) {
			this.LoggedNewLen=this.New.length;
			this.DisplayDiffTitle();
		}
	},

	DisplayDiffTitle:function () {
		var Count=0;
		
		clearInterval(this.Interval);
		
		this.Interval=setInterval(function () {
			if (document.title.indexOf('new Market Current') > -1) document.title='Stock Market News, Opinion & Analysis, Investing Ideas -- Seeking Alpha';
			else document.title=this.New.length+' new Market Current'+(this.New.length==1 ? '' : 's')+'! Stock Market News, Opinion & Analysis, Investing Ideas -- Seeking Alpha';
		
			if (++Count==6) {
				clearInterval(this.Interval);
				document.title=this.New.length+' new Market Current'+(this.New.length==1 ? '' : 's')+'! Stock Market News, Opinion & Analysis, Investing Ideas -- Seeking Alpha';
			}
		}.bind(this),1000)
	},

	DisplayInitTitle:function () {
		document.title=this.New.length+' new Market Current'+(this.New.length==1 ? '' : 's')+'! Stock Market News, Opinion & Analysis, Investing Ideas -- Seeking Alpha';
		
		this.LoggedNewLen=this.New.length;
	},

	DisplayDiff:function () {
		var aTemp=[];

		if (!this.New.length) return;
		aTemp=this.New.concat();
		this.New=[];

		clearInterval(this.Interval);
		document.title='Stock Market News, Opinion & Analysis, Investing Ideas -- Seeking Alpha';
		
		try {
			aTemp.each(function (e) { if ($(e)) new Effect.Highlight(e,{duration:4.0}); });
		}
		catch (e) {}

		this.LoggedNewLen=0;
		eraseCookie('mc_ids');
	},

	Disect:function (aOld, aNew) {
		aDiff=[];

		aNew.each(function (e) { if (aOld.indexOf(e)==-1) aDiff.push(e); });

		return aDiff.length ? aDiff : null;
	},

	LoadMarketCurrents:function () {
		new Ajax.Request('/memcached/bbbb?ie='+Math.random(), {
			method: 'get',
			onSuccess: function(transport){
				$('latest_market_currents').innerHTML=transport.responseText;
				init_link_mafne_on_array('market_currents',$$('.market_currents_mafne_wrapper a'));

				this.GetDiff();
			}.bind(this)		
		});

		setTimeout(function () { this.LoadMarketCurrents(); }.bind(this),45000);
	}
}

Event.observe(document,'mousemove',function () { MarketCurrent.DisplayDiff.call(MarketCurrent); });