/* $Id: core.js 131 2010-03-26 15:58:41Z jure.merhar $ */

/* onDOMReady */

Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
  },
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});


/* StartUp */

var StartUp = Class.create(); StartUp.prototype =
{
  initialize: function(runnable)
  {
    Event.onDOMReady(runnable.run.bindAsEventListener(runnable));
  }
}

/* PostBack */

var PostBack = Class.create(); PostBack.prototype =
{
  initialize: function(select, submit)
  {
    this.select = select;
    this.submit = submit;
  },

  run: function()
  {
    var select = $(this.select);
    Event.observe(select, 'change', this.submitForm.bindAsEventListener(select));
    if (typeof this.submit != 'undefined') {
      $(this.submit).remove();
    }
  },

  submitForm: function()
  {
    this.form.submit();
  }
}

/* RadioToggle */

var RadioToggle = Class.create(); RadioToggle.prototype =
{
  initialize: function(radioHide, radioShow, container)
  {
    this.radioHide = radioHide;
    this.radioShow = radioShow;
    this.container = container;
  },
  
  run: function()
  {
    this.radioHide = $(this.radioHide);
    this.radioShow = $(this.radioShow);
    this.container = $(this.container);
    var listener = this.toggleForm.bindAsEventListener(this);
    if (this.radioHide && this.radioShow && this.container) {
      if (this.radioHide.checked) {
        this.container.hide();
      }
      Event.observe(this.radioHide, 'click', listener);
      Event.observe(this.radioShow, 'click', listener);
    }
  },

  toggleForm: function(e)
  {
    if (Event.element(e) == this.radioShow) {
      this.container.show();
    } else {
      this.container.hide();
    }
  }
}

/* ExpandingBoxes */

var ExpandingBoxes = Class.create(); ExpandingBoxes.prototype =
{
  initialize: function(selector)
  {
    this.selector = selector;
  },

  run: function()
  {
    $$(this.selector).each(this.assignListeners.bindAsEventListener(this));
  },

  assignListeners: function(item)
  {
    var target = $(item.rel);
    target.addClassName('collapsed');
    Event.observe(item, 'click', this.clickListener.bindAsEventListener(target));
  },

  clickListener: function (e)
  {
    this.toggleClassName('collapsed');
    Event.stop(e);
  }
}

/* AjaxLinks */

var AjaxLinks = Class.create(); AjaxLinks.prototype =
{
  lastHash: '',

  initialize: function(container, links)
  {
    this.container = container;
    this.links = links;
  },

  run: function()
  {
    this.containerObject = $$(this.container).first();
    if (typeof this.containerObject == 'undefined') return;
    this.alterLinks();
    var obj = this;
    setInterval(function() { obj.pollHash.call(obj) }, 100);
  },

  alterLinks: function()
  {
    var base = document.location.href.replace(/#.+$/, '');
    for (var i = 0; i < this.links.length; i++) {
      var links = $$(this.links[i]);
      for (var j = 0; j < links.length; j++) {
        var link = links[j];
        if (link) link.href = base + link.href.substr(base.length).replace(/^\//, '#:/');
      }
    }
  },

  pollHash: function()
  {
    if ((this.lastHash !== document.location.hash) && (document.location.hash.substring(0, 3) == '#:/')) {
      this.lastHash = document.location.hash;
      this.update(document.location.href);
    }
  },

  completeListener: function(transport)
  {
    if (typeof pageTracker != 'undefined') {
      var url = transport.request.url.replace(location.protocol + '//' + location.host + '/', '');
      pageTracker._trackEvent(document.body.id, 'ajax_link', url);
    }
    this.alterLinks.call(this);
  },
  
  update: function(url)
  {
    new Ajax.Updater(
      this.containerObject,
      url.replace('#:/', '/'),
      { method: 'get', onComplete: this.completeListener.bindAsEventListener(this) }
    );
  }
}

/* ExternalLinks */

var ExternalLinks =
{
  run: function()
  {
    $$('a.external').each(ExternalLinks.assignListener);
  },

  assignListener: function(anchor) {
    if (anchor.getAttribute("href")) {
      Event.observe(anchor, 'click', ExternalLinks.clickListener.bindAsEventListener(anchor));
    }
  },

  clickListener: function(e)
  {
    open(this.href);
    Event.stop(e);
  }
}

/* InlineText */

var InlineText =
{
  run: function()
  {
    $$('.inline_text').each(InlineText.assignListeners);
  },

  assignListeners: function(field) {
    Event.observe(field, 'focus', InlineText.focusListener.bindAsEventListener(field));
    Event.observe(field, 'blur', InlineText.blurListener.bindAsEventListener(field));
    InlineText.blurListener.call(field);
  },

  focusListener: function()
  {
    if (this.value == this.title) {
      this.value = '';
    }
  },

  blurListener: function()
  {
    if (this.value == '') {
      this.value = this.title;
    }
  }
}

/* PopupLinks */

var PopupLinks =
{
  defaultWidth:  320,
  defaultHeight: 240,

  run: function()
  {
    $$('a.popup').each(PopupLinks.assignListener);
  },

  assignListener: function(anchor)
  {
    var width  = PopupLinks.defaultWidth;
    var height = PopupLinks.defaultHeight;
    if (anchor.rel) {
      var dims = anchor.rel.split('_');
      if (dims.length == 2) {
        width = dims[0];
        height = dims[1];
      }
    }
    anchor.setAttribute('popup_width',  width);
    anchor.setAttribute('popup_height', height);
    anchor.setAttribute('popup_left', (screen.width  / 2) - (width  / 2));
    anchor.setAttribute('popup_top',  (screen.height / 2) - (height / 2));
    Event.observe(anchor, 'click', PopupLinks.clickListener.bindAsEventListener(anchor));
  },

  clickListener: function(e)
  {
    var name = 'popup_' + Math.floor(Math.random() * 8999) + 1000;
    var width  = this.getAttribute('popup_width');
    var height = this.getAttribute('popup_height');
    var left   = this.getAttribute('popup_left');
    var top    = this.getAttribute('popup_top');
    open(this.href, name, 'resizable=1,scrollbars=no,status=no,toolbar=no,menubar=no,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top);
    Event.stop(e);
  }
}

/* Tooltips */

var Tooltips = {
  selects: null,

  run: function()
  {
    Tooltips.selects = $$('select');

    $$('a').each(function(link) {
      var title = link.title;
      var rel = link.rel;

      if (title && title.length > 0 && rel=="tooltip") {
        Event.observe(link, "mouseover", Tooltips.showTipListener.bindAsEventListener(link));
        Event.observe(link, "focus",     Tooltips.showTipListener.bindAsEventListener(link));
        Event.observe(link, "mouseout",  Tooltips.hideTipListener.bindAsEventListener(link));
        Event.observe(link, "blur",      Tooltips.hideTipListener.bindAsEventListener(link));
      }
    });
  },

  showTip: function(link)
  {
    Tooltips.hideTip(link);

    var tip = document.createElement("span");
    tip.className = "tooltip";
    var tipText = document.createTextNode(link.title);
    tip.appendChild(tipText);
    link.appendChild(tip);
    link._tooltip = tip;
    link.title = "";

    // Fix for Safari2/Opera9 repaint issue
    //document.documentElement.style.position = "relative";
  },

  hideTip: function(link)
  {
    if (link._tooltip) {
      link.title = link._tooltip.childNodes[0].nodeValue;
      link.removeChild(link._tooltip);
      link._tooltip = null;

      // Fix for Safari2/Opera9 repaint issue
      //document.documentElement.style.position = "static";
    }
  },

  showTipListener: function(event)
  {
    var link = this;
    this._timer = setTimeout(function() { Tooltips.showTip(link); }, 500);
    Event.stop(event);

    Tooltips.hideSelects();
  },

  hideTipListener: function(event)
  {
    var link = this;
    clearTimeout(this._timer);
    Tooltips.hideTip(link);

    Tooltips.showSelects();
  },
  
  showSelects: function() {
    if (Tooltips.selects != null) {
      Tooltips.selects.each(function(item) { item.style.visibility = 'visible'; });
    }
  },

  hideSelects: function() {
    if (Tooltips.selects != null) {
      Tooltips.selects.each(function(item) { item.style.visibility = 'hidden'; });
    }
  }
}

/* FlashHeader */

var FlashHeader =
{
  run: function()
  {
    var header = $('header');
    var div = document.createElement('div');
    div.id = 'flash_header';
    header.appendChild(div);
    var so = new SWFObject('/images/header.swf', "flash_header", '600', '139', '9', '#710d15');
    so.addParam("wmode", "transparent");
    so.write("flash_header");
  }
}

new StartUp(ExternalLinks);
//new StartUp(InlineText);
//new StartUp(PopupLinks);
//new StartUp(Tooltips);
new StartUp(FlashHeader);
new StartUp(new AjaxLinks('#main', ['#main #pager a']));

