(function(){
 // onload listener
 function init(){
  if(typeof(window.ol)=='function'){
   window.ol.apply(this, arguments)
  }
  new tickerWidget('eventsWidget',new jeremy.molded.CalendarSearch);
  new tickerWidget('newsWidget',new jeremy.molded.NewsSearch);
  new AlertControl('alerts', {marginTop : 5});
  var magicInputs = document.getElementsByClassName('magicInput','input');
  for(var i=0; i<magicInputs.length; i++){
   new magicInput(magicInputs[i]);
  }
  window._gaq = window._gaq || [];
  window._gaq.push(['_setAccount', 'UA-2088380-1']);
  window._gaq.push(['_trackPageview']);
  var ga = document.createElement('script');
  ga.type = 'text/javascript';
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  document.getElementsByTagName('head')[0].appendChild(ga);
 }
 addEvent('load', init);
 
 // magic input class
 function magicInput(el){
  if(el.tagName!='INPUT'){
   return;
  }
  this.el = el;
  var closure = createClosure(this, magicInput.prototype.toggle);
  addEvent('focus',closure,el);
  addEvent('blur',closure,el);
 }
 magicInput.prototype.toggle = function(e){
  var el=this.el;
  if(el.value == el.defaultValue){
   el.value = '';
  }else if(el.value == ''){
   el.value = el.defaultValue;
  }
 }
 
 // ticker widget class
 function tickerWidget(el, searcher, options){
  if(!options){
   options = {}
  }
  this.el = typeof(el)=='string' ? document.getElementById(el) : el;
  this.searcher = searcher;
  this.options = {};
  searcher.setResultSetSize(options.resultSetSize || 4);
  searcher.setSearchCompleteCallback(this,tickerWidget.prototype.callback);
  this.refresh();
 }
 tickerWidget.prototype.callback=function(){
  var el=this.el,
   searcher = this.searcher;
  rmKids(el);
  if(searcher.results){
   for(var i=0;i<searcher.results.length;i++){
    el.appendChild(searcher.results[i].html.cloneNode(1));
   }
  }else{
   el.appendChild(document.createTextNode(options.errorMessage || 'We\'re sorry, but there was a probl Please try again momentarily.'));
  }
 }
 tickerWidget.prototype.refresh = function(){
  var el=this.el,
   searcher = this.searcher,
   options = this.options;
  if(this.refreshTimer){
   clearTimeout(this.refreshTimer);
  }
  rmKids(el);
  el.appendChild(document.createTextNode(options.loadingMessage || 'Loading...'));
  searcher.execute(' ');
  this.refreshTimer = setTimeout(createClosure(this,tickerWidget.prototype.refresh), (options.refreshDelay || 15) * 60000);
 }

 // Alerts Control
 function AlertControl(el, opt_options){
  this.el = typeof(el)=='string' ? document.getElementById(el) : el;
  if(!opt_options){
   opt_options = {}
  }
  this.options = opt_options;
  var searcher = this.searcher = new jeremy.molded.NewsSearch;
  searcher.setRestriction('type','alert');
  searcher.setSearchCompleteCallback(this, AlertControl.prototype.callback);
  this.refreshInterval = setInterval(createClosure(this,AlertControl.prototype.execute),(opt_options.refreshInterval || 60) * 1000);
 }
 AlertControl.prototype.callback=function(){
  var el = this.el,
   searcher = this.searcher;
  if(searcher.results){
   for(var i=0;i<searcher.results.length;i++){
    var result = searcher.results[i];
    if(!document.getElementById('alert'+result.id)){
     var resultEl = result.alertHtml = createEl('div','alert',[
      createEl('a','alert',[
       document.createTextNode(result.title)
      ],{
       'href' : result.url,
       'title' : result.title
      })
     ],{
      id : 'alert'+result.id
     });
     resultEl.style.marginTop='-100px';
     el.insertBefore(resultEl,el.firstChild);
     this.reveal(resultEl);
    }
   }
  }
 };
 AlertControl.prototype.reveal=function(el){
  if(el.revealTimer){
   clearTimeout(el.revealTimer)
  }
  var newMargin = parseInt(el.style.marginTop) + 5;
  el.style.marginTop = newMargin + 'px';
  if(newMargin < (this.options.marginTop || 0)){
   el.revealTimer=setTimeout(createClosure(this,AlertControl.prototype.reveal,[el]), 50);
  }
 };
 AlertControl.prototype.execute=function(query){
  this.searcher.execute(query || '')
 };

 // utility functions
 document.getElementsByClassName = function(className, tagName, parent){
  var allEls = (parent || document).getElementsByTagName(tagName || '*'),
   els = [],
   r = new RegExp("\\b"+className+"\\b");
  for(var i=0; i<allEls.length; i++){
   var el = allEls[i];
   if(r.exec(el.className)){
    els.push(el)
   }
  }
  return els;
 }
 function createEl(tagName, className, children, attribs, styles){
  var el = document.createElement(tagName || 'div');
  if(className){
   el.className = className
  }
  if(children){
   for(i=0;i<children.length;i++){
    el.appendChild(children[i]);
   }
  }
  if(attribs){
   for(var i in attribs){
    if(i.match(/^on/)){
     el[i] = attribs[i];
    }else{
     el.setAttribute(i,attribs[i]);
    }
   }
  }
  if(styles){
   for(var i in styles){
    el.style[i] = styles[i];
   }
  }
  return el;
 }
 function createClosure(context, method, args){
  return function(){
   return method.apply(context, args||[])
  }
 }
 function rmKids(el){
  while(el.firstChild){
   el.removeChild(el.firstChild);
  }
 }
 function addEvent(event, method, el){
  if(!el){
   el = window;
  }
  if(el.addEventListener){
   el.addEventListener(event,method,0)
  }else if(el.attachEvent){
   el.attachEvent('on'+event,method)
  }else{
   if(typeof(el['on'+event])=='function'){
    var origListener = el['on'+event]
    el['on'+event] = function(e){
     origListener.apply(el,[e]);
     method.apply(el,[e]);
    }
   }else{
    el['on'+event] = method;
   }
  }
 }
})()
