var ExplainBubble = function(){
  this.link = Element.get("test");
	if(this.link){
	  this._bubble = new InfoBubble();
		this._bubble.bubbleWidth = 490;
		this._bubble.floatDistance = -125;
		this._bubble.driftDistance = 110;
		this._bubble.SetPoint("none");
		this._bubble.enableRollOutPop = false;
		this._bubble.title = "Declared Dividend";
		this._bubble.Init();
	  this._bubble.AttachBubble(document.getElementById("test"));
	  Events.add({element:this.link,type:"click",handler:this.ShowBubble,context:this});
	}
}
ExplainBubble.prototype.ShowBubble = function(evt, el){
	if(!this._bubble.IsBubbleShowing()){
		this._bubble.SetContent(this.GetExplainText());
		var pos = Element.getXY(el);
		this._bubble.MoveBubble(pos.x, pos.y, Element.getSize(el));
		this._bubble.BlowBubble();
	}
}
ExplainBubble.prototype.GetExplainText = function(){
	var text = [];
	text[0] = "The declared dividend shown represents the issuer's standard recurring dividend and does not reflect any special dividend.";
	var div = Element.create("div");
	var divText = Element.create("div", {id:"explainTextContainer"}, "", div);
	for(var i = 0; i < text.length; i++){Element.create("p", {className:"textBlock", style:"clear:both;"}, text[i], divText);}
	return div;
}
function InitExplainBubble(){expBubble = new ExplainBubble();}
Events.add({element: window,type: "load",handler: InitExplainBubble});

var calendarObj = function(){
	this.cb = new ContentBuffer();
	this.serializer = new Serializer();
	this.serializer.allowEncoding(0);
	this._bubble = new InfoBubble();
}
calendarObj.prototype.mouseOver = function(e, el, obj, location){
	var point = 'none'
	var offset = 0;
	var node = el;
	var x_pos = e.clientX - 350;
	var pos_y = null;//Element.getXY(el).y;
	var x_pos = Element.getXY(el.parentNode.parentNode.parentNode).x - 160;
	if(location == 'side_list') {
		var pos = Element.getXY(el);
		x_pos = 500;
	}
	else if(location == 'explaination') {
		var pos = Element.getXY(el);
		offset = pos.x - 0;
		point = 'top';
		node = el;
		x_pos = pos.x;
		pos_y = pos.y + 25;
		this._bubble.closeButton = false;
	}
	var json_obj = {};
	if(location != 'explaination'){json_obj = this.serializer.deserialize(obj);}
	if(!this._bubble.IsBubbleShowing()){
		if(location == 'side_list' || location == null){this._bubble.title = '<div class="view_def"><a onclick="calendar.showDefinition(event,\''+json_obj.definition+'\');return false;" href="javascript:void(0)">View Definition</a></div> <div style="margin-left:8px;"><b>' +json_obj.feedname + '-</b> ' + json_obj.released_on_f + '</div>';}
		if(location == 'front_page'){this._bubble.title = '<div style="margin-left:8px;"><b>' +json_obj.feedname + '-</b> ' + json_obj.released_on + '</div>';}
		if(location == 'explaination'){this._bubble.title = '<b>Explaination of IPO & SPO Tag Clouds</b>';}
		this._bubble.bubbleWidth = location != 'explaination' ? 475 : 300;
		this._bubble.floatDistance = 0;
		this._bubble.driftDistance = 0;
		this._bubble.SetPoint(point);
		this._bubble.enableRollOutPop = false;
		this._bubble.Init();
		this._bubble.AttachBubble(node);
		this._bubble.SetContent(this.GetContent(json_obj, location));
		this._bubble.MoveBubble(x_pos, pos_y);
		this._bubble.BlowBubble();
	}
	return false;
}
calendarObj.prototype.showDefinition = function(e , definition){
	var html = Element.create('div' , {className:'bubble_cont' , onclick:"calendar.cancelEvent(event)"}, 
			[
			  Element.create('div', {className:'highlights_header', style:'margin-left:8px;'}, ['<b>Definition</b>']),
				Element.create('p', {className:'', style:'padding:8px'} , definition)
			]);

	this._bubble.SetContent(html);
	try{e.stopPropagation();}
	catch(err){}
	try{e.cancelBubble();}
	catch(err){}
	return false;
}
calendarObj.prototype.mouseOut = function(el){this._bubble.PopBubble();}
calendarObj.prototype.cancelEvent = function(e){
	try{e.stopPropagation();}
	catch(err){}
	try{e.cancelBubble();}
	catch(err){}
	return false;
}
calendarObj.prototype.GetContent = function(obj ,index){
	if(!obj.concensus) obj.concensus = '';
	if(index == null || index == 'side_list') {
		var html = Element.create('div' , {className:'bubble_cont'}, 
			[
				Element.create('div', {className:'bubble_header'} , 
					[
						Element.create('div', {className:'released_data'}, 'Released on ' + obj.released_on_f + ' For ' + obj.released_for),
					 	Element.create('div', {className:''} , ['<b>'+obj.feedname+'</b>'])
					]
				),
				
				Element.create('div', {className:'data'} , 
					[
					 	Element.create('div', {className:'actual', style:'float:right'} , obj.actual),
						Element.create('div', {className:'actual'}, 'Actual')
					]
				),
				Element.create('div', {className:'data'} , 
					[
					 	Element.create('div', {className:'consensus', style:'float:right'} , '<span class="Consensus">' + obj.concensus + '</span>'),
						Element.create('div', {className:'consensus'}, '<span class="Consensus"><b>Consensus</b></span>')
					]
				),
				Element.create('div', {className:'data'} , 
					[
					 	Element.create('div', {className:'previous', style:'float:right'} , obj.previous),
						Element.create('div', {className:'previous'}, 'Previous')
					]
				),
				Element.create('div', {className:'highlights_cont'} , 
					[
					 	Element.create('div', {className:'highlights_header'} , ['<b>Highlights</b>']),
						Element.create('p', {className:''} , [obj.highlights])
					]
				)
			]
		);
	}
	else if(index == 'front_page') {
		var html = Element.create('div' , {className:'bubble_cont'}, 
			[
				Element.create('div', {className:'highlights_cont'} , 
					[Element.create('p', {className:''} , [obj.highlights])]
				)
			]
		);
	}
	else if(index == 'explaination') {
		var html = Element.create('div' , {className:'bubble_cont'}, 
			[
				Element.create('div', {className:'highlights_cont'} , 
					[
						Element.create('div', {className:'mn_header' , style:'margin-left:0px'} , 'This Week\'s IPO & SPO Highlights'),
						Element.create('p', {className:''} , ['These 15 symbols are arranged from the most recent deal to the oldest deal. The larger symbols represent a greater price yield and the smaller symbols shown are lower price yield in context to the other deals.<br/><br/>']),
						Element.create('div', {className:'mn_header' , style:'margin-left:0px'} , 'Most Viewed IPO & SPO Deals'),
						Element.create('p', {className:''} , ['These 20 symbols are arranged from most view to lease viewed by Scottrade users. The larger the symbol the more recent user views calculated.<br/><br/>']),
						Element.create('div', {className:'mn_disc' , style:'margin-left:0px'} , 'IPO & SPO data provided by MorningNotes®'),
					]
				)
			]
		);
	}
	return html;
}

var calendar = new calendarObj();

var earningsBubble = new InfoBubble();
var titleEl; var highEl; var lowEl; var conEl;var rightDiv;var vpScrollTop;
function InitEarningBubble(){
	vpScrollTop=Element.get("viewport").scrollTop;
	if(!vpScrollTop){vpScrollTop=0;}
	earningsBubble.bubbleWidth = 260;
  earningsBubble.closeButton = false;
  earningsBubble.enableRollOn = false;
  var mainDiv = Element.create('div',{className:"hasLayout"});
  titleEl = Element.create('div',{className:"bold hasLayout mb15",style:"color:#807B6A;"},'',mainDiv);
  var leftDiv = Element.create('div',{className:"floatLft pr10",style:"width:115px;border-right:1px solid #999;"},'',mainDiv);
  var row = Element.create('div', {className:"hasLayout mb5"},'',leftDiv);
  Element.create('div',{className:"floatLft"}, "High:", row);
  highEl = Element.create('div',{className:"floatRt bold"}, '', row);
  row = Element.create('div', {className:"hasLayout mb5"},'',leftDiv);
  Element.create('div',{className:"floatLft"}, "Low:", row);
  lowEl = Element.create('div',{className:"floatRt bold"}, '', row);
  row = Element.create('div', {className:"hasLayout mb5"},'',leftDiv);
  Element.create('div',{className:"floatLft"}, "Consensus:", row);
  conEl = Element.create('div',{className:"floatRt bold"}, '', row);
  rightDiv = Element.create('div', {className:"floatRt pl10", style:"width:115px;"},'',mainDiv);
  earningsBubble.Init();
  earningsBubble.SetContent(mainDiv);
  earningsBubble.AttachBubble(document.getElementsByTagName("body")[0]);
}
function ShowEarningsHover(el, symbol, date, high, low, consensus, analysts){
	titleEl.innerHTML = '<span>' + symbol + '</span> Earning Estimates <span style="font-weight:normal;">' + date + '</span>';
	highEl.innerHTML = high;
	lowEl.innerHTML = low;
	conEl.innerHTML = consensus;
	rightDiv.innerHTML = "Total number of<br />Analysts: <strong>" + analysts + "</span>";
	var xy = Element.getXY(el);
  var size = Element.getSize(el);
	earningsBubble.MoveBubble(xy.x, (xy.y + 15), size);
	earningsBubble.BlowBubble();
}
function HideEarningsHover(){earningsBubble.PopBubble();}
Events.add(window,"load",InitEarningBubble);