
AJS.toInit(function () {

	var $ = AJS.$;
	
	this.determinePixelWidth = function() {
		if (typeof popkong != 'undefined' && typeof popkong.theme != 'undefined') {
			var availableWidth = popkong.utils.determinePageWidth();
			$(".tab-extras-tab").width(availableWidth * 0.95 + "px");
		}
	}

	/**
	 * extends a jquery object with an isVisible attribute that's true when the jquery obj
	 * has elements and is not hidden (display is not 'none')
	 */
	this.extendSectionObj = function(section) {
		section.isVisible = section.length > 0;
		return section;
	};
	
	// Extend sections with isVisible attribute while grabbing them as variables...
	var comments = this.extendSectionObj($('#comments-section'));
	var labels   = this.extendSectionObj($("#labels-section"));
	var children = this.extendSectionObj($("#children-section"));
	
	// -------------------------------------------------------------
	// COMMENTS SECTION
	// -------------------------------------------------------------
	
	if (comments.isVisible) {
		$('#comments-section-title').remove();
		//$('#pop-comments-tab').append(comments.html());
		$('#pop-comments-tab').append(comments);
	} else {
		$("#pop-comments-tab, #pop-comments-tab-title").remove();
	}
	

	// -------------------------------------------------------------
	// CHILDREN SECTION
	// -------------------------------------------------------------
	if (children.isVisible) {
		$('#children-section-title').remove();
		$('#pop-children-tab').append(children);
	} else {
		$("#pop-children-tab, #pop-children-tab-title").remove();
	}

	// -------------------------------------------------------------
	// LABELS SECTION
	// -------------------------------------------------------------
	if (labels.isVisible) {
		$('#labels-section-title').remove();
		$('#pop-labels-tab').append(labels);
	} else {
		$("#pop-labels-tab, #pop-labels-tab-title").remove();
	}
	
	var popTabExtras = new popkongYAHOO.widget.TabView("pop-tab-extras");
	
	//set the active Tab to the cookie value, if present:
	var activeTabCookie = "popkong-active-tab";
	
	if (popkongYAHOO.util.Cookie.get(activeTabCookie)) {
		popTabExtras.set("activeTab", popTabExtras.getTab(popkongYAHOO.util.Cookie.get(activeTabCookie)));
	};
	
	//when a Tab changes, set the cookie:
	popTabExtras.on("activeTabChange", function(o) {
		popkongYAHOO.util.Cookie.set(activeTabCookie, this.getTabIndex(o.newValue)); 
	});
	
	// Set width on attachments tab so that overflow:auto will work on that tab.
	this.determinePixelWidth();
	$(window).bind("resize", this.determinePixelWidth);
	
	$("#pop-tab-extras").show();
	
});
	


