(function() {
	var symbol_lookup = new SymbolSearch();
		symbol_lookup.setCSSSelector('symbolSearch', 'symbolSearchHidden3');
		symbol_lookup.setFormElements('symbol_lookup', '_symbol');
		symbol_lookup.setDestinationURL("");
		symbol_lookup.setRequestURL("../../symbolLookup/getSymbols.asp");
		symbol_lookup.setRequestor(new ContentBuffer());
	
	SymbolSearch.prototype.go = function(symbol, isFund){
		if (symbol) {
			this.query = symbol;
			this.elInput.value = symbol;
		}
		this.clearResults();
	};
	
}());

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){
	//el.onclick = null;
	
	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));
		
		/*
		if(location == 'front_page') {
			var bubble_height = Element.getSize(this._bubble._mainDiv).height;
			pos_y = -50;
			//x_pos += 180;
		} 
		else if(location == null) {
			//x_pos += 180;
		}
		*/
		
		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;
}

calendarObj.prototype.init = function(e, el){
	e.cancel();
	Events.add({
		element:Element.get('sector_btn'),
		type:'click',
		handler:function (e, el) {
			e.cancel();
			var list = Element.get('sector_list');
			
			if(list.style.display == 'block') {
				list.style.display = 'none';
				Events.remove(this.addEvent);	
			}
			else {
				list.style.display = 'block';
				
				this.addEvent = Events.add(document.body, 'click', function (e) {
					list.style.display = 'none';
				});		
			} 
		}
	})
	
	Events.add({
		element:Element.get('aq_btn'),
		type:'click',
		handler:function (e, el) {
			e.cancel();
			var list = Element.get('aq_list');
			
			if(list.style.display == 'block') {
				list.style.display = 'none';
				Events.remove(this.addEvent);	
			}
			else {
				list.style.display = 'block';
				
				this.addEvent = Events.add(document.body, 'click', function (e) {
					list.style.display = 'none';
				});		
			} 
		}
	})
	
	Events.add({
		element:Element.get('date_range'),
		type:'click',
		handler:function (e, el) {
			e.cancel();
			var list = Element.get('date_range_cont');
			
			if(list.style.display == 'block') {
				list.style.display = 'none';
				Events.remove(this.addEvent);	
			}
			else {
				list.style.display = 'block';
				
				this.addEvent = Events.add(document.body, 'click', function (e) {
					list.style.display = 'none';
				});		
			} 
		}
	})
	
	Events.add({
		element:Element.get('search_Dividends'),
		type:'click',
		handler:function (e, el) {
			e.cancel();
			var url = 'search.asp';
			var symbol_search = Element.get('symbol_search').value;
			var date_range = Element.get('buffer_range').value;
			var sel_date = Element.get('sel_date').value;
			var sel_date_js = Element.get('sel_date_js').value;
			calendar.data = {search_symbol: symbol_search, section: 'Dividends', range:date_range, sel_date:sel_date, sel_date_js:sel_date_js};
			calendar.Buffer(url, function(cp) {
				var result = cp.getResult();
				if(result) {
					Element.get('search_cont').style.display = 'block';
					Element.get('search_cont').innerHTML = result;
				
					Events.add({
						element:Element.get('exit_btn'),
						type:'click',
						handler:function (e, el) {
							Element.get('search_cont').style.display = 'none';
							Element.get('search_cont').innerHTML = '';
						}
					});
				}
			});
		}
	});
	Events.add({
		element:Element.get('search_Earnings'),
		type:'click',
		handler:function (e, el) {
			e.cancel();
			var url = 'search.asp';
			var symbol_search = Element.get('symbol_search').value;
			var date_range = Element.get('buffer_range').value;
			var sel_date = Element.get('sel_date').value;
			var sel_date_js = Element.get('sel_date_js').value;
			calendar.data = {search_symbol: symbol_search, section: 'Earnings', range:date_range, sel_date:sel_date, sel_date_js:sel_date_js};
			calendar.Buffer(url, function(cp) {
				var result = cp.getResult();
				if(result) {
					Element.get('search_cont').style.display = 'block';
					Element.get('search_cont').innerHTML = result;
					
					Events.add({
						element:Element.get('exit_btn'),
						type:'click',
						handler:function (e, el) {
							Element.get('search_cont').style.display = 'none';
							Element.get('search_cont').innerHTML = '';
						}
					});
					
				}
			});
		}
	});
	
	Events.add({
		element:Element.get('search_IPO & SPO Deals'),
		type:'click',
		handler:function (e, el) {
			e.cancel();
			var url = 'search.asp';
			var symbol_search = Element.get('symbol_search').value;
			var date_range = Element.get('buffer_range').value;
			var sel_date = Element.get('sel_date').value;
			var sel_date_js = Element.get('sel_date_js').value;
			calendar.data = {search_symbol: symbol_search, section:'IPO & SPO Deals' , range:date_range, sel_date:sel_date, sel_date_js:sel_date_js};
			calendar.Buffer(url, function(cp) {
				var result = cp.getResult();
				if(result) {
					Element.get('search_cont').style.display = 'block';
					Element.get('search_cont').innerHTML = result;
					
					Events.add({
						element:Element.get('exit_btn'),
						type:'click',
						handler:function (e, el) {
							Element.get('search_cont').style.display = 'none';
							Element.get('search_cont').innerHTML = '';
						}
					});
					
				}
			});
		}
	});
	
}
	
calendarObj.prototype.AddSymbol = function(el, index){
	if(!index) {
		var symbol = Element.get('_symbol').value;
	    var url = 'addBuffer.asp';
		this.data = {add_symbol: symbol};
		this.cb.abortRequests();
		this.Buffer(url, function(cp) {
			var result = cp.getResult();
			if(result) {
				if (result == 'valid') {
					location.href = 'calendar.asp?tab=mmevents&';
				}
				else if(result == 'invalid') {
					alert('Symbol ' + symbol + ' is not valid');
				}
			}
		});	
	}

	else if(index == 'SearchAdd') {
		var symbol = Element.get('symbol_search').value;
	    var url = 'addBuffer.asp';
		this.data = {add_symbol: symbol};
		this.cb.abortRequests();
		this.Buffer(url, function(cp) {
			var result = cp.getResult();
			if(result) {
				if (result == 'valid') {
					alert(symbol + ' added to My Market Events');
				}
				else if(result == 'invalid') {
					alert('Symbol ' + symbol + ' is not valid');
				}
			}
		});	
	}
}

calendarObj.prototype.RemoveItem = function(symbol){
    var url = 'removeBuffer.asp';
	this.data = {remove_symbol: symbol};
	this.cb.abortRequests();
	this.Buffer(url, function(cp) {
		var result = cp.getResult();
		if(result) {
			if (result == 'removed') {
				location.href = 'calendar.asp?tab=mmevents';
			}
		}
	});	
}

calendarObj.prototype.Buffer = function(url, callback){
	this.cb.load({
		url:url,
		context:this,
		data:this.data,
		onload:callback
	});
}

calendarObj.prototype.changeRange = function(el) {
	var current = Element.get('buffer_range').value;
	var new_value = el.innerHTML;
	
	Element.get('date_range').innerHTML = '<img class="expand_icon" border="0" src="../../common/img/downArrow.gif"/>' + new_value;
	el.innerHTML = current;
	
	Element.get('buffer_range').value = new_value;
}

calendarObj.prototype.mouseInSelector = function(el){
	el.style.backgroundColor = '#EEEEEE';
}

calendarObj.prototype.mouseOutSelector = function(el){
	el.style.backgroundColor = 'white';
}

var calendar = new calendarObj();

Events.add({
	element:window,
	type:'load',
	handler: function(e, el){
		calendar.init(e ,el);
		
		var h_height = Element.getSize('highlights').height;
		if(h_height > 525) {
			Element.get('viewport').style.height = h_height + 'px';	
		}
		else {
			Element.get('viewport').style.height = '525px';	
		}
		//var scrollTop = Element.get('viewport').scrollTop;
		window.scroll(0,0);
		setTimeout('window.scroll(0,0)',400);
	}
});

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);