//var ns_currentTab = 1;

ns_TabBar = function(barid, containerid) {
    this.barid = barid;
    this.containerid = containerid;
    this.tabs = [];
}

ns_TabBar.prototype.toggleTab = function(tabId) {
    if (this.bar != null) {
        var newactive = null, oldactive = null;
        for (var i = 0; i < this.tabs.length; i++) {
            if (this.tabs[i].tabid === tabId && !this.tabs[i].get_isActive()) {
                newactive = this.tabs[i];
            } else if (this.tabs[i].tabid !== tabId && this.tabs[i].get_isActive()) {
                oldactive = this.tabs[i];
            }
        }
        // deactivate old tabs first
        if (oldactive !== null) {
            if (oldactive.deactivate() === false)
                return;
        }
        // activate new tab
        if (newactive !== null) {
            if (newactive.activate() === false) {
                if (oldactive !== null)
                    oldactive.activate();
                return;
            }
        }
        this.container.className = "tabsec_" + tabId;
    }
    this.currentTab = tabId;
}
ns_TabBar.prototype.addTab = function(tab) {
    this.tabs.push(tab);
    tab.tabbar = this;
    tab.tabid = this.tabs.length;
    OneWeb.Util.addEvent(tab, "click", function(e) { this.tabbar.toggleTab(this.tabid); });
}
ns_TabBar.prototype.initialize = function() {
    this.bar = document.getElementById(this.barid);
    this.container = document.getElementById(this.containerid);
}
	
// node.TEXT_NODE = 3

ns_Tab = function(id) {
    this.id = id;
    var active = false;
    this.get_isActive = function() { return active; }
    this.set_isActive = function(value) { active = value; }
}
ns_Tab.prototype.deactivate = function() {
if (typeof (this.onpredeactivate) !== "undefined") {
        var e = { bubbles: false, cancelable: true, target: this, type: "predeactivate", returnValue: true };
        if (this.onpredeactivate(e) === false || e.returnValue === false)
            return false;
    }
    this.elem.className = "";
    this.set_isActive(false);
    if (typeof (this.ondeactivate) !== "undefined") {
        var e = { bubbles: false, cancelable: false, target: this, type: "deactivate", returnValue: true };
        this.ondeactivate(e);
    }
    return true;
}
ns_Tab.prototype.activate = function() {
    if (typeof (this.onpreactivate) !== "undefined") {
        var e = { bubbles: false, cancelable: true, target: this, type: "preactivate", returnValue: true };
        if (this.onpreactivate(e) === false || e.returnValue === false)
            return false;
    }
    this.elem.className = "active";
    this.set_isActive(true);
    if (typeof (this.onactivate) !== "undefined") {
        var e = { bubbles: false, cancelable: false, target: this, type: "activate", returnValue: true };
        this.onactivate(e);
    }
    return true;
}
ns_Tab.prototype.click = function() {
    // pass the click through to the anchor element.
    this.anchor.focus();
    // fire the click event on the anchor
    if (this.anchor.dispatchEvent) {
        var e = document.createEvent("MouseEvents");
        e.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
        this.anchor.dispatchEvent(e);
    } else if (this.anchor.fireEvent)
        this.anchor.fireEvent("onclick");
    else {
        try { this.anchor.click(); } catch (e) { }
    }
}
ns_Tab.prototype.onclick = function(e) { };
ns_Tab.prototype.initialize = function() {
    ns_Tab[this.id] = this;
    this.elem = document.getElementById(this.id);
    if (this.elem !== null) {
        this.anchor = this.elem.getElementsByTagName("a").item(0);
        if (this.anchor != null) {
            this.anchor.href = "javascript:void(0);";
            OneWeb.Util.addEvent(this.anchor, "click", function(e) { ns_Tab[this.parentNode.id].onclick(e); e.preventDefault(); });
        }
    }
}

function ns_initTabs() {
    // create tab bar
    var tabbar = new ns_TabBar("ns_tab_nav", "ns_tab_sections");
    tabbar.initialize();
    // create tabs and add them to the tab bar
	var ids = ["profileT","googlemapT","packagesT","holdingsT"];
	for (var i = 0; i < ids.length; i++) {
//	    elem = document.getElementById(ids[i]);
	    //	    if (elem != null) {
            // add the tab whether or not the tab exists so the sections still line up correctly with their display style classes
	        var tab = new ns_Tab(ids[i]);
	        tab.initialize();
	        tabbar.addTab(tab);

	        // add a handler to the activate event to reset the Google Map
	        if (ids[i] === "googlemapT") {
	            OneWeb.Util.addEvent(tab, "activate", function(e) {
	                window.setTimeout(function() { OneWeb.GoogleMaps.maps[0].reset(); }, 20);
	                //OneWeb.GoogleMaps.maps[0].reset();
	                return true;
	            });
	        }
//	    }
	}
	// set the first tab active
	tabbar.toggleTab(1);
}

OneWeb.Util.appendLoadEvent(function() {
    // initialize the tabs
    ns_initTabs();

    // attach some spcial handlers for google map links.
    var googlemapT = ns_Tab["googlemapT"];
    if (googlemapT !== null) {

        // add the tab click to the google map icon
        var googlemapIcon = document.getElementById("lnkGoogleMapDirectionsIcon");
        if (googlemapIcon !== null) {
            OneWeb.Util.addEvent(googlemapIcon, "click", function(e) {
                if (!googlemapT.get_isActive())
                    googlemapT.tabbar.toggleTab(googlemapT.tabid);
            });
        }
            
        // add the tab click to the google map link
        var googlemapLink = document.getElementById("lnkGoogleMapDirections");
        if (googlemapLink !== null) {
            OneWeb.Util.addEvent(googlemapLink, "click", function(e) {
                if (!googlemapT.get_isActive())
                    googlemapT.tabbar.toggleTab(googlemapT.tabid);
            });
        }
    }
});