if((typeof Prototype=='undefined') || 
    (typeof Element == 'undefined') || 
    (typeof Element.Methods=='undefined') ||
    parseFloat(Prototype.Version.split(".")[0] + "." +
               Prototype.Version.split(".")[1]) < 1.5){
    throw("Tabber requires the Prototype JavaScript framework >= 1.5.0")
}

var TabMaster = Class.create();
TabMaster.DefaultCSS = {
    ul : "tabber-tabs",
    contentContainer : "tabber-content-container",
    tabOff : "tabber-tab-off",
    tabOn : "tabber-tab-on",
    activatedNotifer : "arrow-down"
}
TabMaster.prototype = {
    tabs :[],
    ul : null,
    tabContentContainer : null,
    css : null,
  
    initialize:function(ulId, tabContentContainer, cssOveride){
      this.css = Object.extend(Object.extend({},TabMaster.DefaultCSS), cssOveride || {});
      this.tabs = $A();
      this.ul = $(ulId);
      this.tabContentContainer = $(tabContentContainer);
      this.style();
      
      return this;
    },
    
    style:function(){
        this.ul.addClassName(this.css.ul);
        this.tabContentContainer.addClassName(this.css.contentContainer);
    },
    
    findTab:function(tabName){
        return this.tabs.find(function(tab){return tabName == tab.tabName});
    },

    addTab:function(tab){
      if(!tab instanceof Tab) throw "TabMaster.addTab(tab) must be passed an instance of Tab.class";
          this.tabs.push(tab);
          return this;
    },

    activateTab:function(tab){
      this.tabs.invoke("deactivate");
      tab.activate(true);
      if(!$$(".prototip")) return
      $$(".prototip").invoke("hide");
    }
}

var Tab = Class.create();
Tab.DefaultOptions = {
    focusCall:null,
    isNew:false,
    asisURL:null,
    asisFunc:null,
    newImage:""
}
Tab.prototype = {
    li : null,
    div : null,
    tabName : "",
    tabMaster : null,
    options : null,


    initialize:function(tabMaster, name, divId){
        this.tabMaster = tabMaster;
        this.tabName = this.textNodeSafe(name);
        this.li = divId + "_li";
        this.div = $(divId);
        this.active = false;
        this.options = Object.extend(Object.extend({},Tab.DefaultOptions), arguments[3] || {});
        return this;
    },

    render:function(){
        var tizab = $(document.createElement("li"));
        tizab.addClassName(this.tabMaster.css.tabOff);
        // Changed to a span to resolve issue where IE6 stops page load after user clicks the href with javascript:void(0);
        // var href = document.createElement("a");        
        // href.setAttribute("href", "javascript:void(0);");
        var href = document.createElement("span");
        var title = document.createTextNode(this.tabName);
        href.appendChild(title);
        tizab.appendChild(href);
        var divDwnArrw = $(document.createElement("div"));
        divDwnArrw.addClassName(this.tabMaster.css.activatedNotifer);
        tizab.appendChild(divDwnArrw);
        this.tabMaster.ul.appendChild(tizab);
        this.li = tizab;

        if(this.options.isNew){
            var tmp = $(document.createElement("IMG"));
            tmp.src = this.options.newImage;
            var _li = $(document.createElement("li"));
            _li.appendChild(tmp);
            this.tabMaster.ul.appendChild(_li);
            Event.observe(tmp, "load", function(){
                var dim = tmp.getDimensions();
                _li.setStyle({position:"relative",width:dim.width+"px", height:dim.height+"px",top:"2px",left:"-28px"});
            }.bind(this));
        }

        Event.observe(href, "click", function(){this.tabMaster.activateTab(this); this.triggerAsIs(true)}.bind(this));

        return this;
    },

    deactivate:function(){
        this.li.removeClassName(this.tabMaster.css.tabOn);
        this.li.addClassName(this.tabMaster.css.tabOff);
        this.div.hide();
        return this;
    },

    activate:function(){
        this.li.removeClassName(this.tabMaster.css.tabOff);
        this.li.addClassName(this.tabMaster.css.tabOn);
        this.div.show();
        if(this.options.focusCall) this.options.focusCall();

        return this;
    },
    triggerAsIs:function(callAsIs){
        var shouldCallAsIs = (typeof callAsIs == 'undefined') || (callAsIs);
        if(shouldCallAsIs && this.options.asisURL != null && this.options.asisURL != ""){
           if(this.options.asisFunc){
               this.options.asisFunc(this.options.asisURL);
           } else {
               this.sendAsIs();
           }
        }
    },
    sendAsIs:function(){
        var asisURLs = this.options.asisURL.split("|");
        var asisImg="", asisURLcacheKiller="";

        for (var as=0; as<asisURLs.length; as++) {
            asisImg = new Image();
            asisURLcacheKiller = asisURLs[as];

            //Defect 21010 - tabber didn't use cache_kill.  Check the string for a ?, so we know if we need to append & or ? before adding cache_kill
            asisURLcacheKiller += (asisURLs[as].indexOf("?")!=-1) ? "&" : "?";

            asisURLcacheKiller += "cache_kill=" + new Date().getTime();
            asisImg.src = asisURLcacheKiller;
        }
    },
    textNodeSafe:function(str){
        return str.gsub(/&#(.*?)\;/, function(match){
            return String.fromCharCode(match[1]);
        });
    }
}
