Array.prototype.each = function(func,context) {
  if (!context) { context = this; }
  for (var i = 0; i < this.length; i++) {
    func.call(context,this[i],i);
  }
};

Array.prototype.map = function(func,context) {
  if (!context) { context = this; }
  for (var i = 0; i < this.length; i++) {
    this[i] = func.call(context,this[i],i);
  }
  return this;
};

function el(el) {
  return document.getElementById(el);
}

var ajax = function(opts) {
  var xmlhttp;
  try { xmlhttp = new XMLHttpRequest(); } catch(e) { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); }
  xmlhttp.onreadystatechange = function() {
    switch (xmlhttp.readyState) {
    case 1:
      if (typeof opts.loading == 'function') { opts.loading(); }
      break;
    case 2:
      if (typeof opts.loaded == 'function') { opts.loaded(); }
      break;
    case 4:
      var res = xmlhttp.responseText;
      switch (xmlhttp.status) {
        case 200: if (typeof opts.success == 'function') { opts.success(res); } break;
        case 404: if (typeof opts.error == 'function') { opts.error(); } break;
        case 500: if (typeof opts.error == 'function') { opts.error(); } break;
      };
      break;
    };
    if (typeof opts.complete == 'function') { opts.complete(); }
  };
  xmlhttp.open((opts.type || 'get').toUpperCase(),opts.url,true);
  xmlhttp.send(null);
};


function setCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function ready(func) {
  if (!window.readyFuncs) {
    window.readyFuncs = {
      run: function() {
        for (var i = 0; i < window.readyFuncs.funcs.length; i++) {
          window.readyFuncs.funcs[i]();
        }
      }
    };
    window.readyFuncs.funcs = [func];
  } else {
    window.readyFuncs.funcs.push(func);
    return;
  }
  /*@cc_on
   @if (@_win32 || @_win64)
     document.write('<script id="ieScriptLoad" defer src="//:"><\/script>');
     document.getElementById('ieScriptLoad').onreadystatechange = function() {
     if (this.readyState == 'complete') {
       window.readyFuncs.run();
     }
   };
  @end @*/
  if (window.addEventListener) {
    window.addEventListener('DOMContentLoaded',function() {
                              window.readyFuncs.run();
                            },true);
  } else if (navigator.userAgent.match(/KHTML|WebKit/i)) {
    var DOMLoadTimer = setInterval(function() {
                                     if (document.readyState.match(/loaded|complete/i)) {
                                       clearInterval(DOMLoadTimer);
                                       window.readyFuncs.run();
                                     }
                                   }, 10);
  }
}

function listen(evnt, elem, func) {
  if (elem.addEventListener) // W3C DOM
    elem.addEventListener(evnt,func,true);
  else if (elem.attachEvent) { // IE DOM
    var r = elem.attachEvent("on"+evnt, func);
    return r;
  }
  return false;
}

function getElementsByClassName(el,classname) {
  try {
    return el.getElementsByClassName(classname);
  } catch(err) {
    return _getElementsByClassName(classname,el);
  }
}

function _getElementsByClassName(searchClass,node,tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function targ(e) {
  var targ;
  if (!e) var e = window.event;
  if (e.target) targ = e.target;
  else if (e.srcElement) targ = e.srcElement;
  if (targ.nodeType == 3) // defeat Safari bug
    targ = targ.parentNode;
  return targ;
}

var tabs = function() {
  var activeTab;

  var getTabs = function() {
    return getElementsByClassName(el('tabbed_content'), 'tab');
  };

  var showTab = function(elId) {
    el(activeTab).style.display = 'none';
    el('link_' + activeTab).parentNode.className = '';
    el('link_' + elId).parentNode.className = 'active';
    el(elId).style.display = 'block';
    activeTab = elId;
  };

  return {
    init: function() {
      var tabs = getTabs();
      for (var i = 0; i < tabs.length; i++) {
        listen('click', el('link_' + tabs[i].id), function(e) {
                 var event = (e || window.event);
                 event.cancelBubble = true;
                 if (event.stopPropagation) event.stopPropagation();
                 if (event.preventDefault) event.preventDefault();
	         var target = (event.target || window.event.srcElement);
                 showTab(target.getAttribute('rel'));
                 return false;
               });
        if (i == 0) {
          el(tabs[i].id).style.display = 'block';
          activeTab = tabs[i].id;
        } else {
          el(tabs[i].id).style.display = 'none';
        }
      }
    },
    showTab: showTab
  };
}();
