
	function Indices() {
		this._sort = "changepct";
		this._sortasc = false;
		this._data = [];
		this._region = null;

		this.commonDataFields = [{n:"name",l:"Name",align:"alignLeft"},{n:"delayed",l:"D",align:"alignLeft"},{n:"last",l:"Current",align:"alignRight"},{n:"changepct",l:"Change",align:"alignRight"},{n:"volume",l:"Volume",align:"alignRight"}]

		this.smartChartDiv = null;
		this.smartChartWidth = 210;
		this.smartChartHeight = 140;

		this.rolloverMouse = getMouse(); // from includes/jslib/mouse.2.js
	
	}

	Indices.prototype.init = function() {
		this.region(Element.get('lastregion').value || "0");
		this.getData();

		this._sortLinks = Element.parseSelector("table#table-indicesdata thead a");

	}

	Indices.prototype.region = function() {
		if (arguments[0]) { this._region = arguments[0] }
		return this._region;
	}


	Indices.prototype.data = function() {
		if (arguments[0]) { var newData = arguments[0]; this._data = new ArraySortable(); for (var i=0;i<newData.length;i++) { this._data.push(newData[i]); } }
		return this._data;
	}


	Indices.prototype.getData = function(region) {
		
		try { hideHelp(); } catch(e) { }
		region = region || this.region();
		this.region(region);

		Element.setDisplay("showExchange",region != 0 ? "none" : "block");
		Element.setDisplay("lima",region != 2 ? "none" : "block");

		Common.contentBufferAbortRequests();
		Common.loadContentBuffer({
			page:"indicesBuffer.asp",
			context:this,
			data:{
				region: this.region()
				,sort:this._sort
			},
			onload:function() { this.draw() },
			onerror:this.handleError
		});
	}

	Indices.prototype.draw = function() {

		var data = this.data();

		var tbody = Element.get("tbody");

		Element.removeChildNodes(tbody);

		data.sortByField(this._sort,this._sortasc,true);
		//recurseObject({o:data,i:1,l:2,clear:1});

		var len = data.length;
		for (var i=0; i<len;i++) {
			if (data[i].bridgeSymbol.match(/NYA$/)) {
				var vol = Element.get("volumeNYSE");
				if (vol) { Element.setHTML(vol, data[i].volume_f); }
			} else if (data[i].bridgeSymbol.match(/COMP$/)) {
				var vol = Element.get("volumeNASDAQ");
				if (vol) { Element.setHTML(vol, data[i].volume_f); }
			} else if (data[i].bridgeSymbol.match(/XAX$/)) {
				var vol = Element.get("volumeAMEX");
				if (vol) { Element.setHTML(vol, data[i].volume_f); }
			}

			if (i == 0) {
				// give Help id to first one
				data[i].name_f = data[i].name_f.replace('<img','<img id="rollIndice" ');
			}
			var tr = Element.create("tr",{},"",tbody);

			for (var x=0;x<this.commonDataFields.length;x++) {
				
				var cellClass = (this.commonDataFields[x].n == this._sort) ? "currentsymbol ":"";
				cellClass += ((this.commonDataFields[x].align?this.commonDataFields[x].align:" alignLeft"));
				var td = Element.create("td",{"class":cellClass, width:x?"10%":"60%"},(data[i][this.commonDataFields[x].n+"_f"]||data[i][this.commonDataFields[x].n]),tr);
			}
		}


			for (var i in this._sortLinks) {
				Element.removeClass(this._sortLinks[i],"sortasc");
				Element.removeClass(this._sortLinks[i],"sortdsc");
			}
	
			Element.addClass("sort"+this._sort,(this._sortasc?"sortasc":"sortdsc"));



	}

	Indices.prototype.sort = function(sortby) {

		if (arguments[0]) { 
			if (this._sort == sortby) {
				this._sortasc = !this._sortasc;
			}
			else {
				this._sort = sortby;
			}

			this.draw();
		}
		else { return {sort:this._sort,sortasc:this._sortasc} }
	}


	// called when expanding industry
	Indices.prototype.showChartRollover = function(argsObj) {

		var title = '';
		var subTitle = '';
		argsObj.realtime = argsObj.realtime || false;

		if (argsObj.name) {
			title += unescape(argsObj.name) + ' ' + argsObj.country;
		} else {
			if (argsObj.displaySymbol) {
				title = ((argsObj.company)?unescape(argsObj.company)+' ' : argsObj.displaySymbol);
				//subTitle = ((argsObj.company)?' '+argsObj.displaySymbol : '');
				subTitle = ((argsObj.company)?' '+unescape(argsObj.exchange)+':'+argsObj.displaySymbol : '');
			} else {
				title = argsObj.symbol;
			}
		}
	
		// doesn't exist, create element
		if (!this.smartChartDiv) {
			this.smartChartDiv = Element.create("div",{id:"smartChartRollover"},[
				Element.create("div",{id:"smartChartRolloverTitle",className:"moduleHeader nocorner"},""),
				Element.create("div",{id:"smartChartRolloverBody"},[
					Element.create("div",{id:"smartChartRolloverQuote"}),
					Element.create("img",{id:"smartChartImage"}),
					Element.create("img",{id:"smartChartImageError",src:(gGlobalPath+"images/chartrollbuffer_error.gif"),className:"none"})
				]),
				Element.create("br",{className:"clearing"})
			],document.body);
	
	
		}

		this.rolloverMouse.callback = this.trackMouse;

	
		// populate title
		Element.setHTML("smartChartRolloverTitle",title);
		Element.setHTML("smartChartRolloverQuote","");
	
		// set next to link
		var linkPos = Element.getXY(argsObj.link);
		var chartTop = linkPos.y-30;
		// adjust vertical position
		// if (chartTop+180 is over bottom, adjust for difference
	
		Element.setXY(this.smartChartDiv,linkPos.x+25,chartTop)
		Element.setVisibility(this.smartChartDiv,"visible");
		Element.setDisplay("smartChartImageError","none")
	
		var chartPath = this.buildChartPath(argsObj.width, argsObj.historical) + argsObj.symbol;
		smartChartImg = Element.get("smartChartImage");
		smartChartImg.setAttribute("src", chartPath);
		Element.setDisplay("smartChartImage","none")
		smartChartImg.onload = function() { Element.setDisplay("smartChartImage","block") }
		smartChartImg.onerror = function() { Element.setDisplay("smartChartImageError","block") }	

		Common.contentBufferAbortRequests();
		Common.loadContentBuffer({
			page:gGlobalPath + 'markets/overview/chartRollBuffer.asp?symbol=' + argsObj.symbol + "&realtime=" + argsObj.realtime,
			context:this,
			data:{
				region: this.region()
			},
			responseType:"text/html",
			onload:function() { Element.setHTML("smartChartRolloverQuote",arguments[0].getResult()); },
			onerror:this.handleError
		});
	}


	
	Indices.prototype.hideRolloverChart = function() {
		this.rolloverMouse.callback = function() { }
		if (this.smartChartDiv) {
			Element.setVisibility(this.smartChartDiv,"hidden");
		}
	}

	
	Indices.prototype.trackMouse = function() {
	
		pt = new pointObj(this.x, this.y);

		if (pt.x > (gCurrPageWidth / 2) + 10) {
			pt.x -= 20;
			pt.x -= Indices.smartChartWidth;
		} else {
			pt.x += 20
		}
	
		pt.y -= (Indices.smartChartHeight / 2) - 10;
	
		if (window.innerHeight) {
			var docHeight = window.innerHeight + document.body.scrollTop
		} else {
			var docHeight = document.body.clientHeight + document.body.scrollTop
		}
	
		if (pt.y + Indices.smartChartHeight + 60 > docHeight) {
			pt.y = docHeight - Indices.smartChartHeight - 60;
		} else if (b.isMajor && pt.y < document.body.scrollTop) {
			pt.y = document.body.scrollTop;
		}
		
		Indices.smartChartDiv.style.left = pt.x;
		Indices.smartChartDiv.style.top  = pt.y;
	
	}

	Indices.prototype.buildChartPath = function(w, historical) {
		var url = buildAbsolutePath('/common/charts/smartChart.asp') + '?width='
		if (w) {
			url += w;
			this.smartChartWidth = w;
		} else {
			url += this.smartChartWidth;
		}
		url += '&height=' + this.smartChartHeight;
		if (historical) {
			var n = new Date();
			n.setDate(n.getDate() - 5);
			var d = n.getMonth() + 1 + '-' + n.getDate() + '-' + n.getFullYear();
	
			url += '&histDate=' + d;
		} else {
			url += '&ruleOverride=isWeekend';
		}
		url += '&symbol=';
		return url;
	}

	Indices = new Indices();



	loadBuffer.add("Indices.init()");
