// Index chart object
// Copyright (c) 2001, Just Information Corp. All rights reserved.
//
// Usage: automatic pull realtime Index chart (GIF) for TSE, OTC, or STK
//	  Client should create a IndexChartObj in the document and call it's genTag method 
//	  eg. 	<script language="javascript><!--
//	     	var gTSE = new IndexChartObj('twsi', 310, 200, 30000, 'TSE');
//		gTSE.genTag();
//		//--></script>
//
//	  parameter: id: image object id in the document
//		     w:  width of the gif chart
//		     h:  height of the gif chart
//		     feq: refresh period in minsecond
//		     market: 'TSE'/'OTC'/'BOTH'/StkID
//
//	 note: the cache directory of the gif was /rt/gif, and the backend asp program
//	       is gifIndexChart.asp
//
function IndexChartObj(id, w, h, feq, market)
{
	this.id = id;	
	var sId = '' + market;
	sId = sId.toLowerCase();	
	if (sId == 'both') {
		this.market = 'TSE';
		this.both = true;
		this.baseUrl = 'http://money.hinet.net/rt1/gif/gifindexchart.djgif?w=' + w + '&h=' + h;		
	} else if (sId == 'otc') {
		this.market = 'OTC';
		this.both = false;
		this.baseUrl = 'http://money.hinet.net/rt1/gif/gifindexchart.djgif?w=' + w + '&h=' + h;		
	} else if (sId == 'tse') {
		this.market = 'TSE';
		this.both = false;
		this.baseUrl = 'http://money.hinet.net/rt1/gif/gifindexchart.djgif?w=' + w + '&h=' + h;		
	} else {
		this.market = market;
		this.both = false;
		this.baseUrl = 'http://money.hinet.net/rt1/gif/gifstkchart.djgif?w=' + w + '&h=' + h;
	}
	this.change = changeId;
	this.geturl = Geturl;
	this.w = w;
	this.h = h;
	this.feq = feq;
	this.bStarted = false;
	this.oImg = new Image(w, h);
	this.reload = reload;
	this.getnext = getNext;
	this.getalt = getAlt;
	this.start = Start;
	this.genTag = genTag;
	this.timerid = 0;
	eval('document.o' + id + ' = this;');
}

function changeId()
{
	if (this.both) {
		if (this.bStarted)
			clearTimeout(this.timerid);
		if (this.market == 'TSE')
		{
			this.market = 'OTC';
		}
		else
		{
			this.market = 'TSE'
		}
		if (document.all)
			document.images[this.id].alt = this.getalt();			
		this.reload();
	}
}

function getAlt(bFirst)
{
	if (this.id) {
		if (this.market == 'TSE') {
			if (document.layers && bFirst)
				return (this.both) ? '切換市場' : '加權即時走勢';
			return (this.both) ? '點一下切換櫃檯指數' : '加權即時走勢';
		} else if (this.market == 'OTC') {
			if (document.layers && bFirst)
				return (this.both) ? '切換市場' : '櫃檯指數即時走勢';
			return (this.both) ? '點一下切換加權指數' : '櫃檯即時走勢';
		} else {
			return '' + this.market + '即時走勢圖';
		}
	}
}
function getNext()
{
	if (this.id) {
		if (document.layers)
			this.timerid = setTimeout(ReloadImg, this.feq, this.id);
		else
			this.timerid = setTimeout('ReloadImg("' + this.id + '")', this.feq);
	}
}

function Geturl()
{
	var sUrl;
	if (this.id) {
		if ( this.market == "TSE" || this.market == "OTC")
			sUrl = this.baseUrl + '&id=' + this.market;
		else
			sUrl = this.baseUrl + '&id=' + this.id;
		return sUrl;
	}
}	

function reload()
{
	if (this.id) {
		var sUrl = this.geturl();
		this.oImg.src = sUrl;
		this.oImg.onLoad = ImgLoaded(this);
	}
}

function Start()
{
	if (this.id && this.bStarted == false) {	
		this.bStarted = true;
		this.getnext();	
	}
}

function genTag()
{
	if (this.id) {
		var sContent = '<img name="' + this.id + '" border="0" hspace="0" vspace="0" width="' + this.w + '" height="' + this.h + '" src="' + this.geturl() + '" alt="' + this.getalt(true) + '" onload="eval(\'document.o' + this.id + '.start()\')">';
		if (this.both == true) 
			sContent = '<a href="javascript:eval(\'document.o' + this.id + '.change()\')" onmouseover="return showMouse(\'' + this.id + '\');" onmouseout="return showMouse(\'' + this.id + '\', true);">' + sContent + '</a>'
		document.writeln(sContent + '\n');		
		if (document.layers)
			this.ftid = setTimeout(ForceStart, 10000, this.id);
		else
			this.ftid = setTimeout('ForceStart("' + this.id + '")', 10000);	
	}
}

function showMouse(id, bOut)
{
	if (document.images[id]) {
		var o;
		eval('o = document.o' + id + ';'); 
		if (o)
			window.status = (bOut) ? '' : o.getalt();
	}
	return true;
}

function ReloadImg(id)
{
	var o;
	eval('o = document.o' + id + ';');
	if (o)	o.reload();
}

function ImgLoaded(o)
{
	if (o) {
		if (document.images[o.id])
			document.images[o.id].src = o.oImg.src;
		clearTimeout(o.timerid);
		if (o.bStarted)
			o.getnext();
	}
}

function ForceStart(id)
{
	var o;
	eval('o = document.o' + id + ';');
	if (o) {
		clearTimeout(o.ftid);
		o.start();
	}
}