_pageOnloadQueue = [];
_isDebug=false;
_ieLastTextSize="100%";

function subscribeFeedburner()
{
  var l=location.href;
  var uri="StockChartscom-";
  if (/\/chipanderson(\/|$)/i.test(l)) uri+="ChipAnderson";
  else if (/\/dont_ignore_this_chart(\/|$)/i.test(l)) uri+="DontIgnoreTheChart";
  else if (/\/chartwatchers(\/|$)/i.test(l)) uri+="ChartWatchers";
  else if (/\/mailbag(\/|$)/i.test(l)) uri+="Mailbag";
  else if (/\/scanning(\/|$)/i.test(l)) uri+="ScanningStocks"; 
  else if (/\/status(\/|$)/i.test(l)) uri+="Status";
  else if (/\/step_by_step(\/|$)/i.test(l)) uri+="Step-by-step";
  else if (/\/whats_new(\/|$)/i.test(l)) uri+="WhatsNew";
  else return false;
  location.href="http://feedburner.google.com/fb/a/mailverify?uri="+uri+"&amp;loc=en_US";
}
//------------------------------
function activateChangeTextSize() 
{
  try{  
    $("#scc-font-toggler").css("display","block"); 
    $("#scc-font-toggler").append("<span>Font&nbsp;Size:</span>&nbsp;"+
        "<a href=\"#\" onclick=\"changeTextSize('decrease');return false;\">&nbsp;-&nbsp;</a>&nbsp;"+
        "<a href=\"#\" onclick=\"changeTextSize('increase');return false;\">&nbsp;+&nbsp;</a>&nbsp;"+
        "<a href=\"#\" onclick=\"changeTextSize('reset');return false;\">reset</a>");
  }catch(e){}
}
//------------------------------
function changeTextSize(option)
{
  var parent=$("#alpha");
  var base=$("#alpha-inner");
  if ($("#alpha .entry-content")) base=$("#alpha .entry-content");
  if (!base) return;
  if (option=="reset")  {
    base.css("font-size","");
    return;
  }
  var currentSize=base.css("font-size");
  if (parent.css("font-size")==base.css("font-size")) currentSize="100%";
  if ( currentSize=="") currentSize="100%";
  if (!/^\d+\%$/.test(currentSize)) currentSize=_ieLastTextSize;
  if ((currentSize=="300%" && option=="increase") || (currentSize=="30%" && option=="decrease")) return;
  var nextTextSize=parseInt(currentSize)+((option=="increase")?10:-10)+"%";
  base.css("font-size", nextTextSize);
  _ieLastTextSize=nextTextSize;
}
//------------------------------
function breakFrames()
{
  if (typeof(self) == "undefined" || typeof(top) == "undefined") return;
  if (self==top) return; //no frames found
  try{  $("a").attr("target","_top");  }catch(e){}
/*
  var as=document.getElementsByTagName("a");
  for (var i=0;i<as.length;i++) {
    if (typeof(as.item)!="undefined" && as.item(i) && as.item(i).setAttribute){
      as.item(i).setAttribute("target","_top");
    }
  }
*/
}
//------------------------------
function personalizeMembersPage() {
  //still thinking...
  if (!document.getElementById) return;
  var b = document.getElementsByTagName("body")[0];
  if (!b || !/\bscc\-members\b/.test(b.className)) return;
  var d = document.getElementById("banner-description");
  if (!d) return;
  d.innerHTML="";
}

//------------------------------
function searchValidate(fieldName) {
  if (!document.getElementById || !fieldName) return true; //don't stop search
  var field = document.getElementById(fieldName);
  if (!field) return true; //don't stop search
  if (!field.value || /^\s*$/.test(field.value)) return false; //stop search on blank values
  return true;
}

//------------------------------
function chartSearch(prefix) {
//never returns true, just switches page
  var field=document.getElementById(prefix+"text");
  var opts=document.getElementById(prefix+"options");
  if (!field || !opts) return false;
  if (!field.value || /^\s*$/.test(field.value)) return false;
  var urls=["/h-sc/ui?s=",//0
	"/def/servlet/SC.pnf?c=",//1
	"/candleglance?",//2
	"/webcgi/perf.html?",//3
	"/gallery?",//4
	"/symsearch?"];//5
  if (opts.selectedIndex<urls.length) {
    var url=urls[opts.selectedIndex]+encodeURIComponent(field.value);
    if (opts.selectedIndex==1) url+=",P";
    if (typeof(self) != "undefined" && typeof(top) != "undefined" && self!=top) 
      parent.location="http://stockcharts.com"+url;
    else
      window.location="http://stockcharts.com"+url;
  }
  return false;
}

//------------------------------
function addAnalytics() {
//run last, external sites may fail or be slow
  if (!document.getElementById) return; 
  if (typeof(urchinTracker)=="function" && _uacct) return;//already added to page
  var g1 = document.createElement("script");
  g1.setAttribute("type","text/javascript");
  g1.setAttribute("src","http://www.google-analytics.com/urchin.js");
  g1.onload=function(){_uacct = "UA-518575-1";urchinTracker();};
  document.getElementsByTagName("body")[0].appendChild(g1);
}

//------------------------------
function checkDate() {
  if (!document.getElementById) return; 
  var copyright=document.getElementById("footer-copyright");
  if (!copyright) return;
  var year = new Date().getFullYear();
  if (copyright.innerHTML.indexOf(year)>0) return;
  copyright.innerHTML=copyright.innerHTML.replace(/1999\-\d{4}/,"1999-"+year);
}

//------------------------------
function limitImageSize() {
  if (!document.getElementById) return; 
  var maxWidth=511;
  var container = document.getElementById("alpha-inner");
  if (!container) return;
  if (container.offsetWidth) maxWidth=container.offsetWidth-10;
  if (/<div class=[\'\"]entries/i.test(container.innerHTML)) maxWidth-=30;;
  var imgs = container.getElementsByTagName("img");
  for (var i=0;i<imgs.length;i++) {
    if (imgs[i].offsetWidth && parseInt(imgs[i].offsetWidth)>maxWidth) {
      imgs[i].style.width = maxWidth +"px";
      if (imgs[i].getAttribute("width") && parseInt(imgs[i].getAttribute("width"))!= maxWidth) {
         //Safari/FF need this, but breaks IE. Above conditional diverts IE for now...
         imgs[i].setAttribute("width", maxWidth);
         imgs[i].setAttribute("height",null);
      }
    }
    else if (imgs[i].style.pixelWidth && imgs[i].style.pixelWidth>maxWidth) {
      imgs[i].style.pixelWidth = maxWidth;
    }
  }
}

//------------------------------
function pageOnload() {
  _isDebug=/debug=1/.test(location.search);
  for (var i in _pageOnloadQueue) {
try{
    if (typeof(_pageOnloadQueue[i])=="function") _pageOnloadQueue[i]();
}catch(e){ //sail past errors on live pages unless in debug
  //if (_isDebug) alert(e); 
}
  }
}

//------------------------------
_pageOnloadQueue.push(limitImageSize);
_pageOnloadQueue.push(addAnalytics);
_pageOnloadQueue.push(breakFrames);
_pageOnloadQueue.push(checkDate);
_pageOnloadQueue.push(activateChangeTextSize);
if (window.addEventListener) window.addEventListener("load", pageOnload, false);
else if (document.addEventListener) document.addEventListener("load", pageOnload, false);
else if (window.attachEvent) window.attachEvent("onload", pageOnload);