var TabView = function() {};

TabView.prototype = {
  tabList: [],
  currentTab: null,

  addEvent: function(oElement,event,func) {
    if (oElement.addEventListener) {
      oElement.addEventListener(event,func,false);
    } else if (oElement.attachEvent) {
      oElement.attachEvent('on'+event,func);
    }
  },

  hideTitles: function(id,tag) {
    var titles = document.getElementById(id).getElementsByTagName(tag);
    for (i=0; i<titles.length; i++) {
      titles[i].style.display = 'none';
    }
  },

  switchTab: function(tabId) {

    var currentTabId = document.getElementById ( this.tabList[this.currentTab] );
    var newTabId = document.getElementById ( this.tabList[tabId] );

    currentTabId.className = 'off';
    newTabId.className = 'on';

    document.getElementById(this.currentTab).className = 'off';
    document.getElementById(tabId).className = 'on';

    this.currentTab = tabId;

  },

    addTab: function(tabId,tabContentId,current) {

        this.tabList[tabId] = tabContentId;

        var id = document.getElementById(tabId);

        this.addEvent(id,'click', 
		(function(obj) {
			return function(e) {
				if (e.stopPropagation) { e.stopPropagation(); }
				obj.switchTab(tabId);
				return false;
				};
		})(this)
       	);

        if (current) {

            this.currentTab = tabId;
	    document.getElementById(tabId).className = 'on';
            document.getElementById(tabContentId).className = 'on';

        } else {

	    document.getElementById(tabId).className = 'off';
            document.getElementById(tabContentId).className = 'off';

	}
    },

    outputLinkToTab: function(tabId,newId,text) {
		document.write('<p><a id="'+newId+'" class="more" href="#">'+text+'</a></p>');
		var id = document.getElementById(newId);
		this.addEvent(id,'click',
           (function(obj) {
              return function(e) {
                 if (e.stopPropagation) {
                    e.stopPropagation();
                    e.preventDefault();
                 } else {
                    e.cancelBubble = true;
                 }
                 obj.switchTab(tabId);
                 return false;
              };
		})(this)
		);
    }
};

function createTabs(elem) {
  document.write('<ul class="tabs">');
  for (i=0; i<elem.length; i++) {
    document.write('<li id="tab'+(i+1)+'"><a href="#nogo"><span>' + elem[i] + '</span></a></li>');
  }
  document.write('</ul>');
}

