if (!NS)
	var NS = {};
if (!NS.UI)
	NS.UI = {};
	
NS.UI.TabControl = function (headerId, containerId) {
	this.headerId = headerId;
	this.containerId = containerId;
	this.tabIds = [];
	this.tabs = [];
	this.sections = [];
	this.tablist = null;
	this.container = null;
}

NS.UI.TabControl.controls = { };
NS.UI.TabControl.tabHeaderClassRx = new RegExp("(^|\\s)ns_tab_container(\\s|$)");
NS.UI.TabControl.tabListClassRx = new RegExp("(^|\\s)ns_tab_nav(\\s|$)");
NS.UI.TabControl.tabSectionClassRx = new RegExp("(^|\\s)ns_tab_section(\\s|$)");

NS.UI.TabControl.prototype = {
	"type": NS.UI.TabControl,

	// toggleTab
	/// <summary>Toggle the current tab</summary>
	"toggleTab":  function (tabId) {
		if (this.tablist != null) {
			for (var i=0; i<this.tabs.length; i++) {		  
				if (this.tabs[i].getAttribute("tabid") == tabId) {
					this.tabs[i].className = "active";
					this.sections[i].style.display="block";
				} else {
					this.tabs[i].className = "";
					this.sections[i].style.display="none";
				}
				if (tabId == "tab_googlemap"){
				    //recenter map
                    window.setTimeout(function() { OneWeb.GoogleMaps.maps[0].reset(); }, 20);
				}
			}
		}
		this.currentTab = tabId;
		return false;
	},

	"clickTab": function (e) {
		if (!e) var e = window.event;
		if (e.target) elm = e.target;
		else elm = e.srcElement;
		
		//a little hack to grab the tabid from the icon or text link from the main tab
		if (elm.id == "imgGoogleMapsIcon")
		    elm = elm.parentNode;
		if (elm.id == "")
		    while (elm.tagName.toLowerCase() != "li") elm = elm.parentNode;

		var tabid;
		// pass the event on to the tab object
		if (elm != null) {
			tabid = elm.getAttribute("tabid");
			
			while (elm.tagName.toLowerCase() != "div" || typeof(NS.UI.TabControl.controls[elm.id]) == "undefined") elm = elm.parentNode;
			
			if (elm != null)
				NS.UI.TabControl.controls[elm.id].toggleTab(tabid);
				//NS.UI.TabControl.controls["nsres_DetailsTabs"].toggleTab(tabid);
				
		}
		if (e.preventDefault) { e.preventDefault(); } else { e.returnValue = false; }
		return false;
	},

	// initialize
	/// <summary>Initializes the tabs</summary>
	"initialize": function (currentTab) {
		try {
			var lists = document.getElementById(this.headerId).getElementsByTagName("ul");
			for (var i = 0; i < lists.length; i++) {
				if (this.type.tabListClassRx.test(lists[i].className)) {
					this.tablist = lists[i];
					break;
				}
			}
			if (this.tablist != null) {
				this.tabs = this.tablist.getElementsByTagName("li");
			}
		} catch (e) {
			// tab list not found
			return;
		}
		
		this.container = document.getElementById(this.containerId);
		if (this.container != null) {
			// find the tab sections and get their id's
			var divs = this.container.getElementsByTagName("div");
			var i = -1;
			for (div in divs) {
				div = divs[div];
				if (this.type.tabSectionClassRx.test(div.className)) {
					// found a tab section
					i++;
					if (div.getAttribute("id")=="") 
						div.setAttribute("id", this.containerId + "_" + i);
					this.tabIds.push(div.id);
					this.sections.push(div);
					// attach the onclick handler
					if (this.tabs.length > i && this.tabs[i] != null) {
						var li = this.tabs[i];
						var a = null;
						li.setAttribute("tabid", div.id);
						a = li.getElementsByTagName("a").item(0);
						if (a != null) {
							a.href = "javascript:void(0);";
							a.onclick = this.clickTab;
						}	
						if (li.id == "googlemapT"){
						    //also add handlers for the icon & text link on main tab
						    // add the tab click to the google map icon
                            var googlemapIcon = document.getElementById("lnkGoogleMapDirectionsIcon");
                            if (googlemapIcon !== null) {
                                googlemapIcon.setAttribute("tabid", div.id);
                                //googlemapIcon.href = "javascript:void(0);";
							    googlemapIcon.onclick = this.clickTab;
//                                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) {
                                googlemapLink.setAttribute("tabid", div.id);
                                //googlemapIcon.href = "javascript:void(0);";
							    googlemapLink.onclick = this.clickTab;
//                                OneWeb.Util.addEvent(googlemapLink, "click", function(e) {
//                                    if (!googlemapT.get_isActive())
//                                        googlemapT.tabbar.toggleTab(googlemapT.tabid);
//                                });
                            }
						}
					}
				}
			}
		} else {
			// no container
			return;
		}
		this.type.controls[this.headerId] = this;
		if (this.sections.length > 0) {
			if (currentTab != null) 
				this.toggleTab(currentTab);
			else
				this.toggleTab(this.tabIds[0]);
		}
	}
};
