waitInit_tipsAndQuotations = function(){
  try{
    var jsonURL="http://stockcharts.com/j-ts/ts?cmd=get_tip_and_quote";
    if (typeof(pageInitializing)!="undefined" && pageInitializing==true){
      setTimeout(function(){waitInit_tipsAndQuotations();},100);
    }else {
      if ($(".scc-sc2").get().length>=1){
          if (!(/\&location/.test(SCC.Tip.jsonURL))) SCC.Tip.jsonURL+="&location=CWB";
          if (!(/\&location/.test(jsonURL))) jsonURL+="&location=CWB";
      }
      $.getJSON(jsonURL+"&callback=?", function(data){
      try {
        if (data.tip && data.tip.id) SCC.Tip.insert(data.tip);
        if (data.quote && data.quote.id) SCC.Quotation.insert(data.quote);
      }catch(e){}
      });
    }
  }catch(e){}
};
SCC.Quotation = {
  text:"",
  root:null,
  div:null,
  divContent:null,
  divHTML:"<div id=\"sccquotation\"><div id=\"sccquotationcontent\">{{QUOTE}}</div></div>",
  init :function(){ 
    this.root=$("#alpha-inner");
    var quoteHTML=this.divHTML.replace("{{QUOTE}}",this.text);
    this.root.append(quoteHTML);
    this.divContent=$("#sccquotationcontent");
  },
  set: function(data){
    if (!data) return;
    if (data.quote) this.text=data.quote;
  },
  insert :function(data){
    if (typeof(data)!="undefined" && data.quote){
      this.set(data);
    }
    if (!this.text) return;
    if (!this.divContent || this.divContent.get().length!=1) this.init();
    if (!this.divContent || this.divContent.get().length!=1) return; //error!
    this.divContent.html(this.text);
  }
}
SCC.Tip = {
  jsonURL:"http://stockcharts.com/j-ts/ts?cmd=get_tip",
  id:"",
  text:"",
  defaultText:"Loading tip...",
  root:null,
  div:null,
  divContent:null,
  divHTML:"<div id=\"scctip\"><strong>Tip:</strong> <span id=\"scctipcontent\">{{TIP}}</span><div id=\"scctipoptions\"><a href=\"#\" onclick=\"SCC.Tip.loadNext();return false;\">Next tip &raquo;</a></div></div>",
  init :function(){ 
    this.root=$("#alpha-inner");
    var tipHTML=this.divHTML.replace("{{TIP}}",this.text);
    this.root.append(tipHTML);
    this.div=$("#scctip");
    this.divContent=$("#scctipcontent");
  },
  set: function(data){
    if (!data) return;
    if (data.id) this.id=data.id;
    if (data.tip) this.text=data.tip;
  },
  setDefault: function(data){
    this.id=0;
    this.text=this.defaultText;
  },
  insert :function(data){
    if (typeof(data)!="undefined" && data.tip){
      this.set(data);
    }
    if (!this.divContent || this.divContent.get().length!=1) {
      this.init();
      if (!this.text) return; //dont show if empty
    }
    if (!this.divContent || this.divContent.get().length!=1) return; //error!
    if (!this.text) {
      this.setDefault();
    }
    this.divContent.html(this.text); 
  },
  loadNext :function(){
    this.setDefault();
    $.getJSON(this.jsonURL+"&callback=?", function(data){SCC.Tip.insert(data);});
  }
}

if (Math.random()<0.5){
if (window.addEventListener) window.addEventListener("load", waitInit_tipsAndQuotations, false);
else if (document.addEventListener) document.addEventListener("load", waitInit_tipsAndQuotations, false);
else if (window.attachEvent) window.attachEvent("onload", waitInit_tipsAndQuotations);
}


