// JScript 文件
// Create by aaron 2008-12-03
// Modify by aaron 2008-12-24
// 个股首页 个股行情处理,支持获取指数行情

//获取数据请求class
GGQuoteRequest = new function(){
	this.scriptid ="GGQuoteRequest";
	this.url = "http://quote.stock.hexun.com/stockdata/stock_quote.aspx";
	//this.url = "http://192.168.21.157:9016/stockdata/stock_quote.aspx";
	this.stockcode = "";
	this.indexcode = "000001_1|399001_2";
	this.showFormat = 2;//默认显示的格式 保留2位小数
	
	//None=0,AShare=1,BShare=2,CloseFund=3,Index=10,Warrant=15

	this.CreateLink = function()
	{
		var request = this.url + "?";
		request += "stocklist=" + this.stockcode + "&indexlist=" + this.indexcode + "&time=" +Common.Time();
		return request;
	}	
	this.Request = function()
	{
		Common.AppendDataArray(this.scriptid,this.CreateLink());
	}
	this.interval = function()
	{
		var time = new Date();
		GGQuoteRequest.Request();
		//行情刷新先不做限制
		//if(time.getHours() > 9 && time.getHours() < 16)
		setInterval("GGQuoteRequest.Request()",30000);
	}
}

//NewQuoteListPage = new function(){
NewQuoteListPage = new function(){
	this.dataArray = null;//股票数据数组
	this.indexArray = null;//指数数据数组
	//this.currentColumn;//当前列索引值
	this.divName = "NewQuoteListPage";//输出容器ID
	//this.columnArray = new Array();//列升降序状态
	//this.reloadTime = 30000;//自动刷新时间30秒
	//this.reload = true;//是否自动刷新
	//this.setTimeObj;//计时器
	this.Constockprofit = 1;
	this.Exc = 1;
	this.Isw = 0;
	
	//flash效果处理
	this.timeObj = null;
	this.curColor = "";
	this.flashNum = 3;
	this.flashswitch = 0;
	
	this.GetData = function(stockarraydata,indexarraydata)
	{
		this.dataArray = stockarraydata;
		this.indexArray = indexarraydata;
		//this.LoadDataFinish();
		this.LoadMainStockData();//加载主市场
		this.LoadOtherStockData();//加载其他市场
		this.LoadIndexStockData();//加载指数数据
	}
	
	//个股数据
  this.LoadMainStockData = function() {
		if (this.dataArray.length >0)
		{
		  var i=0;
		  var formattype = GGQuoteRequest.showFormat;
		  var curPric = Number(this.dataArray[i][2]);
		  var stockPric = 0;
		  
		  //大于1时才能取到这个值 小于等于1没必要取
		  if (this.dataArray.length>1)
				stockPric = Number(this.dataArray[1][2]);//正股价格
			
			if (curPric==0)
			{
				Common.$("q_current").innerHTML = "停牌";
				Common.$("q_current").style.color="#000000";
				Common.$("q_updownprice").innerHTML = "--&nbsp;";
				Common.$("q_upDownRate").innerHTML = "--&nbsp;";
				Common.$("q_change").innerHTML = "--&nbsp;";
				Common.$("q_preclose").innerHTML = this.dataArray[i][4].toFixed(formattype);//昨收
				Common.$("q_high").innerHTML = "--&nbsp;";
				Common.$("q_low").innerHTML = "--&nbsp;";
				Common.$("q_open").innerHTML = "--&nbsp;";
				Common.$("q_tv").innerHTML = "--&nbsp;";
				Common.$("q_amp").innerHTML = "--&nbsp;";
				Common.$("q_profitrate").innerHTML = "--&nbsp;";
				Common.$("q_img").innerHTML = "";
			}
			else
			{
			
				Common.$("q_current").innerHTML = this.getColor(curPric.toFixed(formattype),this.dataArray[i][4],"q_current");//最新价
			
				var udPrice = Number(this.dataArray[i][2]) - Number(this.dataArray[i][4]);
				Common.$("q_updownprice").innerHTML = this.getColor(udPrice.toFixed(formattype),0,"q_updownprice");//涨跌值(无)
			
				Common.$("q_upDownRate").innerHTML = this.getColor(this.dataArray[i][3].toFixed(formattype),0,"q_upDownRate") + "%";//涨跌幅
				Common.$("q_change").innerHTML = this.dataArray[i][10].toFixed(formattype);//换手
				Common.$("q_preclose").innerHTML = this.dataArray[i][4].toFixed(formattype);//昨收
				Common.$("q_high").innerHTML = this.getColor(this.dataArray[i][6].toFixed(formattype),this.dataArray[i][4],"q_high");//最高
				Common.$("q_low").innerHTML = this.getColor(this.dataArray[i][7].toFixed(formattype),this.dataArray[i][4],"q_low");//最低
				Common.$("q_open").innerHTML = this.getColor(this.dataArray[i][5].toFixed(formattype),this.dataArray[i][4],"q_open");//今开
				var tmp_tv = Number(this.dataArray[i][8])/10000;
				Common.$("q_tv").innerHTML = tmp_tv.toFixed(formattype);//成交量
				Common.$("q_amp").innerHTML = this.dataArray[i][11].toFixed(formattype);//振幅
				var profit = (curPric*NewQuoteListPage.Exc)/NewQuoteListPage.Constockprofit;
				
				//权证计算 溢价 特殊处理
				if (NewQuoteListPage.Isw==1)
				{
					profit = (stockPric + curPric - NewQuoteListPage.Constockprofit)/NewQuoteListPage.Exc;
					//alert(stockPric);
				}
				
				if(profit < 0) Common.$("q_profitrate").innerHTML = "--&nbsp;";
				else Common.$("q_profitrate").innerHTML = profit.toFixed(formattype);//市盈率
			 
				NewQuoteListPage.curColor = Common.$("q_current").style.color;
				NewQuoteListPage.timeObj=setInterval(this.textFlash,200);

				this.showPic(this.dataArray[i][2],this.dataArray[i][4]);
			}
		}
	}
	
	
	//其他市场
	this.LoadOtherStockData = function() {
		if (this.dataArray.length >0)
		{
			for(var i=1;i < this.dataArray.length;i++)
			{
				//获取股票代码
				var stockcode = this.dataArray[i][0];
				//根据股票代码 生成 页面span元素的id（因为代码唯一 所以生成的id唯一）
				var priceId = "q_o_p" + stockcode;
				var updownId = "q_o_u" + stockcode;
				//根据id获取数据
				Common.$(priceId).innerHTML = this.getColor(this.dataArray[i][2].toFixed(2),this.dataArray[i][4],priceId);//最新价
				Common.$(updownId).innerHTML = "("+this.getColor(this.dataArray[i][3].toFixed(2),0,updownId) + "%)";//涨跌幅	
				var mId = "M_" + stockcode;
				Common.$(mId).style.display = "block";
			}
		}
	}
	
	//指数
	this.LoadIndexStockData = function() {
		if(this.indexArray.length>0)
		{
			for(var i=0;i < this.indexArray.length;i++)
			{
				//获取股票代码
				var stockcode = this.indexArray[i][0];
				//根据股票代码 生成 页面span元素的id（因为代码唯一 所以生成的id唯一）
				var priceId = "q_i_p" + stockcode;
				var updownId = "q_i_u" + stockcode;
				//根据id获取数据
				Common.$(priceId).innerHTML = this.getColor(this.indexArray[i][2].toFixed(2),this.indexArray[i][4],priceId);//最新价
				Common.$(updownId).innerHTML = this.getColor(this.indexArray[i][3].toFixed(2),0,updownId) + "%";//涨跌幅
			}
		}
	}
	
	
	//根据价格显示图片
	this.showPic = function(currentPrice,preclosePrice)
	{
		if (parseFloat(currentPrice) > parseFloat(preclosePrice))
			Common.$("q_img").innerHTML = "<img src='/2008/img/ico3.gif'/>";
		else if (parseFloat(currentPrice) < parseFloat(preclosePrice))
			Common.$("q_img").innerHTML = "<img src='/2008/img/ico4.gif'/>";
	}
	
	this.textFlash = function()
	{
		if(NewQuoteListPage.flashNum < 0){
			Common.$("q_current").style.color=NewQuoteListPage.curColor;
			clearInterval(NewQuoteListPage.timeObj);
			NewQuoteListPage.flashNum=3;
			return;
		}
		if(NewQuoteListPage.flashswitch == 0){
			Common.$("q_current").style.color="#000000";
			NewQuoteListPage.flashswitch = 1;
		}else{
			Common.$("q_current").style.color=NewQuoteListPage.curColor;
			NewQuoteListPage.flashswitch = 0;
		}
		NewQuoteListPage.flashNum=NewQuoteListPage.flashNum-1;
	}
	
	this.getColor = function(value,fiducial,id)
	{
		if(parseFloat(value) == 0.00){
			Common.$(id).style.color="#000000";
			if(id == "q_current"){
				value = "停牌";
			}
		}
		else if(parseFloat(value) == parseFloat(fiducial)){
			Common.$(id).style.color="#000000";
		}
		else if(parseFloat(value) > parseFloat(fiducial)){
		 Common.$(id).style.color="#FE0000";
		}
		else if(parseFloat(value) < parseFloat(fiducial)){
		 Common.$(id).style.color="#009A00";
		}
		return value;
	}
}



//港股获取数据请求class
HKQuoteRequest = new function(){
	this.scriptid ="HKQuoteRequest";
	this.url = "http://hkquote.stock.hexun.com/stockdata/stock_quote.aspx";
	this.stockcode = "";

	this.CreateLink = function()
	{
		var request = this.url + "?";
		request += "stocklist=" + this.stockcode + "&time=" +Common.Time();
		return request;
	}	
	this.Request = function()
	{
		Common.AppendDataArray(this.scriptid,this.CreateLink());
	}
	this.interval = function()
	{
		if(HKQuoteRequest.stockcode.length==5)
		{
			var time = new Date();
			HKQuoteRequest.Request();
			if(time.getHours() > 9 && time.getHours() < 16)
				setInterval("HKQuoteRequest.Request()",120000);
		}
	}
}

//NewQuoteListPage = new function(){
HKQuoteListPage = new function(){
	this.dataArray = null;//股票数据数组
	this.divName = "HKQuoteListPage";//输出容器ID
	
	this.GetData = function(stockarraydata)
	{
		this.dataArray = stockarraydata;
		this.LoadMainStockData();//加载主市场
	}

	this.LoadMainStockData = function() {
		if (this.dataArray.length >0)
		{
			for(var i=0;i < this.dataArray.length;i++)
			{
				//获取股票代码
				var stockcode = this.dataArray[i][0];
				//根据股票代码 生成 页面span元素的id（因为代码唯一 所以生成的id唯一）
				var priceId = "q_o_p" + stockcode;
				var updownId = "q_o_u" + stockcode;
				//根据id获取数据
				Common.$(priceId).innerHTML = this.getColor(this.dataArray[i][2].toFixed(2),this.dataArray[i][4],priceId);//最新价
				Common.$(updownId).innerHTML = "(" + this.getColor(this.dataArray[i][3].toFixed(2),0,updownId) + "%)";//涨跌幅	
				var mId = "M_" + stockcode;
				Common.$(mId).style.display = "block";
			}
		}
	}
	
	this.getColor = function(value,fiducial,id)
	{
		if(parseFloat(value) == 0.00){
			Common.$(id).style.color="#000000";
			if(id == "q_current"){
				value = "停牌";
			}
		}
		else if(parseFloat(value) == parseFloat(fiducial)){
			Common.$(id).style.color="#000000";
		}
		else if(parseFloat(value) > parseFloat(fiducial)){
		 Common.$(id).style.color="#FE0000";
		}
		else if(parseFloat(value) < parseFloat(fiducial)){
		 Common.$(id).style.color="#009A00";
		}
		return value;
	}
}
