<?xml version="1.0" encoding="UTF-8" ?>
<Module>
    <ModulePrefs
    title="Y! Stock Market"
    description="Real time quotes from Yahoo! Finance. Use symbols used by Yahoo! Finance. It is capable of showing stocks not only from US but also from other major exchanges worldwide."
    author="Nuscpe"
    author_email="nuscpe+gg@gmail.com"
    screenshot="http://nuscpe.googlepages.com/yystock_screenshot.PNG"
    thumbnail="http://nuscpe.googlepages.com/yystock_thumbnail.PNG"
    author_location="Singapore"
    author_affiliation="Nuscpe">
    <Require feature="dynamic-height"/> 
    <Require feature="analytics" />
    <Require feature="setprefs" />
  </ModulePrefs>
  <UserPref name="mystocks" display_name="Add a stock" datatype="list" required="true" default_value="^GSPC|^DJI|^STI|BS6.SI"/>
  <UserPref name="show_chart" display_name="Show Chart?" datatype="bool"/>
  <UserPref name="link_url" display_name="Yahoo Finance Url:" default_value="http://finance.yahoo.com"/>
  <UserPref name="default_symbol" display_name="Default Symbol:" default_value="^STI" />
  <UserPref name="font_size" display_name="Font Size (px):" default_value="12" />
  <UserPref name="show_symbol" display_name="Display:" datatype="enum" default_value="0">
    <EnumValue value="0" display_value="Full Name"/>
    <EnumValue value="1" display_value="Symbol"/>
  </UserPref>
  <UserPref name="graph_scale" display_name="Graph Scale:" datatype="enum" default_value="0">
    <EnumValue value="0" display_value="1d"/>
    <EnumValue value="1" display_value="5d"/>
    <EnumValue value="2" display_value="3m"/>
    <EnumValue value="3" display_value="6m"/>
    <EnumValue value="4" display_value="1y"/>
    <EnumValue value="5" display_value="2y"/>
    <EnumValue value="6" display_value="5y"/>
    <EnumValue value="7" display_value="max"/>
  </UserPref>  
  <UserPref name="group_by_market" display_name="Indexes at top?" datatype="bool" default_value="0" />  
    <Content type="html">
        <![CDATA[

<style type="text/css">   
td.right {
  text-align:right;
  white-space:nowrap;    
}
td.last {
  width: 1%;
}
td.up{
  color: #00aa00;
}  
td.down{
  color: #aa0000; 
}
img {
  width: 192px;
  height: 96px;
}
table.center {
  margin-left:auto; 
  margin-right:auto;
}  
#disclaimer {
  font-size:10px;
  color:#aaaaaa;
  text-align:center;
}

@import url(http://www.google.com/ig/tablib.css);
</style>

      <script>
     _IG_Analytics("UA-3422670-1", "/stock");
     </script>

<script language="JavaScript" type="text/javascript">
// Updated on 2009/07/20
// 1. Center the graph
// 2. Add an option to display graph in various scales ( 1 day, 3 Month, etc )
  
// Updated on 2009/03/10
// 1. Add Yahoo Finance URL customization

// Updated on 2008/11/01
// 1. Fix the script error due to igoogle library update recently

// Updated on 2008/01/29
// 1. Add new option to display indexes at the top
// 2. When symbol is displayed instread of full name, the stock is ordered by symbol
// 3. Rename the title to Y! Stock Market

// Updated on 2008/01/17
// 1. Add new option to display symbol instead of full name
// 2. Fix the parsing error if there is "," in stock/index names

// Updated on 2008/01/12
// Changes:
// 1. Reduce default font size and add a setting entry
// 2. Order stock alphabetically
  
var prefs = new _IG_Prefs(__MODULE_ID__);
var symbols = prefs.getArray("mystocks");
var bShowChart = prefs.getBool("show_chart");
var strLink = prefs.getString("link_url");  
var strSymbol = prefs.getString("default_symbol");
var container = "container";
var fontSize = prefs.getInt("font_size");
if (fontSize == 0) fontSize = 12;
var bShowSymbol = (prefs.getInt("show_symbol")==1);
var bGroupByMarket = (prefs.getInt("group_by_market")==1);
var nDefaultScale = prefs.getInt("graph_scale");
var nScale = nDefaultScale;
var GraphScales = new Array( 
  new GraphScale( "1d",  "/q/bc?t=1d&s=", "/z?t=1d&z=b&s=" ),
  new GraphScale( "5d",  "/q/bc?t=5d&s=", "/z?t=5d&z=b&s=" ),
  new GraphScale( "3m",  "/q/bc?t=3m&s=", "/z?t=3m&z=b&s=" ),
  new GraphScale( "6m",  "/q/bc?t=6m&s=", "/z?t=6m&z=b&s=" ),
  new GraphScale( "1y",  "/q/bc?t=1y&s=", "/z?t=1y&z=b&s=" ),
  new GraphScale( "2y",  "/q/bc?t=2y&s=", "/z?t=2y&z=b&s=" ),
  new GraphScale( "5y",  "/q/bc?t=5y&s=", "/z?t=5y&z=b&s=" ),
  new GraphScale( "max", "/q/bc?t=my&s=", "/z?t=my&z=b&s=" )
);

function formatURL(){
    var url = "http://quote.yahoo.com/d/quotes.csv?s=";
    url += encodeURI(symbols[0]);

    for (var j = 1; j < symbols.length; j++) {
        url += "+" + encodeURI(symbols[j]);
    }
    url += "&f=snl1d1t1c1&e=.csv" + "&random=" + Math.floor((Math.random()*10000)+1);

//    alert(url);
    return url.replace(/%5E/g,"^");
}

function loadDummy() {
  _gel("link").style.display = bShowChart?"block":"none";
  _gel("scales").style.display = bShowChart?"block":"none";
  if (bShowChart) {  
    showStock(strSymbol,nScale,false);
  }
  
  var html = "<table style='font-size:"+fontSize+"px;'><tbody>";

  for (var j=0; j< symbols.length; j++) {
    html+="<tr><td>"+symbols[j]+"</td><td class='right'>-</td><td class='right'>&nbsp;</td><td class='right last'>-</td></tr>";
  }
  html += "</tbody></table>";
  _gel(container).innerHTML = html;
}

function loadContent(data) {
    try{
        //alert(data);
        
        var symbols = new Array();
        var lines = data.replace(/\r+\n/g, "\n").split("\n");
        var html = "<table style='font-size:"+fontSize+"px;'><tbody>";
        for (var j = 0; j < lines.length; j++) {
            line = lines[j];
            if(line.length == 0) continue;
            //alert(line);
                
            var ws;
            try { 
              ws = eval("(["+line+"])");
            }
            catch ( ex ) {
              ws = _trim(line.replace(/\"/g, "")).split(',');
            }
            
            if(ws.length == 6){
                _gel('d1').innerHTML = ws[4] + " " + ws[3];
                symbols.push(new StockSymbol(ws[0], ws[1], parseFloat(ws[2]), parseFloat(ws[5])));
            }
        }
        symbols.sort(stockCompare);
        for (var i=0; i<symbols.length; i++) {
          s = symbols[i];
          //alert(s._strSymbol);
          var style = (s._fChange>0)?" up":((s._fChange<0)?" down":""); 
          html += "<tr><td><a onclick='showStock(\""+s._strSymbol+"\","+nScale+",true)' href='javascript:;' ";
          if (bShowSymbol) html += "title='"+s._strName+"'>"+s._strSymbol;
          else html += "title='"+s._strSymbol+"'>"+s._strName;
          html += "</a></td><td class='right'>"+s._fPrice+"</td><td class='right"+style+"'>";
          html += s._fChange+"</td><td class='right last"+style+"'>(";
          html += (s._fChange*100/(s._fPrice-s._fChange)).toFixed(2)+"%)</td></tr>";
        }
        html += "</tbody></table>";
        _gel(container).innerHTML = html;

    }catch(err){
       alert(err);
    }
}
  
function stockCompare(s1, s2) {
  if (bGroupByMarket) {
    if (s1._strMarket > s2._strMarket) return 1;
    if (s1._strMarket < s2._strMarket) return -1;
  }
  
  if (bShowSymbol ) {
    if (s1._strSymbol > s2._strSymbol) return 1;
    if (s1._strSymbol < s2._strSymbol) return -1;
  }
  else {
    if (s1._strName > s2._strName) return 1;
    if (s1._strName < s2._strName) return -1;
  }
  return 0;
}

function fetch_price(){
    _IG_FetchContent(formatURL(), loadContent);

    setTimeout("fetch_price()",120000);
}
  
function showStock(symbol, sca, bOpen) {
  if (bShowChart) {
    _gel("link").href = strLink + GraphScales[sca]._strPageUrl + symbol;
    _gel("graph").src = "http://chart.finance.yahoo.com" + GraphScales[sca]._strGraphUrl + symbol + "&random=" + Math.floor((Math.random()*10000)+1);; 
    
    var html = "<table style='font-size:10px;' class='center'><tbody><tr>";
    for (var i=0; i<GraphScales.length; i++) {
      if ( i == sca) html += "<td><b>"+GraphScales[i]._strLabel+"</b></td>";
      else html += "<td><a onclick='showStock(\""+symbol+"\","+i+",false)' href='javascript:;'>"+GraphScales[i]._strLabel+"</a></td>";
    }
    html += "</tr></tbody></table>";
    _gel("scales").innerHTML = html;
  }
  else if (bOpen) {  
    window.open(strLink + GraphScales[sca]._strPageUrl + symbol);
  }
}

function init() {
  loadDummy();
  // Tells gadget to resize itself
  _IG_AdjustIFrameHeight();
  fetch_price();
}

function StockSymbol(strSymbol, strName, fPrice, fChange) {
  this._strSymbol = strSymbol;
  this._strName = strName;
  this._fPrice = fPrice;
  this._fChange = fChange;
  //var index = strSymbol.lastIndexOf('.');
  //if (index > 0) this._strMarket = strSymbol.substring(index+1);
  if (strSymbol.charAt(0)=="^") this._strMarket = "#";
  else this._strMarket = "$";
}
  
function GraphScale(strLabel, strPageUrl, strGraphUrl) {
  this._strLabel = strLabel;
  this._strPageUrl = strPageUrl;
  this._strGraphUrl = strGraphUrl;
}

// Call init function to initialize and display tabs.
_IG_RegisterOnloadHandler(init);


</script>

<div style="overflow:auto">
<div id="container"></div>
<div style="text-align:center">
<a target="_blank" id="link" href="http://sg.finance.yahoo.com/q/bc?t=1d&s=^sti">
<img style="border-style: none; cursor: pointer;" id="graph" src="http://ichart.yahoo.com/t?s=^sti" />
</a>  
</div>
<div style="text-align:center" id="scales"></div>  
<div id="disclaimer">Quotes delayed, except where indicated otherwise. </div>
<span id="d1" style="display:none"></span>
<span id="url" style="display:none"></span>
</div>
        ]]>
    </Content>
</Module>
































































