﻿var TAB_LIST_ID = 'inner-tabs';
var TAB_LIST_PAGES_ID = 'inner-tab-pages';
var TEXT_SIZE_COOK_NAME = 'res_tsize';
var TEXT_SIZE_CSS_ID = 'ctl00_Small_Css';
var TEXT_COOK_VAL_LARGE = 'large';
var TEXT_COOK_VAL_SMALL = 'small';
var FIX_EMPTY_NAME = 'hide-empty';

var DisableTabInitialization = false;


/* Hide any 'empty' sub headers (actually, SharePoint renders them with an &nbsp; for some reason */
function hideEmptyTags(tag) {
	var allElements = getElements_ieFix(tag, 'name', FIX_EMPTY_NAME, false);
	for (var curElement = 0; curElement < allElements.length; curElement++) {
		if (isEmptyElement(allElements[curElement])) {
			allElements[curElement].style.display = 'none';
		}
	}
}
/*Changes the text size for the page, and stores the setting in a cookie*/
function changeTextSize(senderId, smalltxt, largetxt){
	/*Get the current value of the cookie, and switch it*/
	var cookval = getCookieValue();	
	var newcookval = (cookval == TEXT_COOK_VAL_LARGE) ? TEXT_COOK_VAL_SMALL : TEXT_COOK_VAL_LARGE;
	
	/*Now, save the cookie and set the display for the page*/
	setCookieValue(newcookval);	
	setTextSize(newcookval, senderId, smalltxt, largetxt);	
}

function getCookieValue(){
	var cookval = TEXT_COOK_VAL_SMALL;
	var cooks = document.cookie.split(';');
	for (x=0; x<cooks.length; x++) {
		var cook = trim(cooks[x]);
		if(cook.indexOf(TEXT_SIZE_COOK_NAME) == 0) {
			cookval = cook.substring(TEXT_SIZE_COOK_NAME.length + 1, cook.length);
		}
	}
	return cookval;
}

function initInnerTabs() {
	var list = document.getElementById(TAB_LIST_ID);
	var tabcontent = document.getElementById(TAB_LIST_PAGES_ID);
	if (list != null){
		var items = list.getElementsByTagName('td');		
		var tabcount = 0;
		for (x=0; x<items.length; x++) {
			var item = items[x];
			if (item.offsetParent != null){
				var noContent = isEmptyElement(item.getElementsByTagName('span')[0]);
				if (!noContent)
					tabcount += 1;
				else
					items[x].style.display = 'none';
			}
		}
		if (tabcount > 0) {
			tabcontent.style.display = 'block';
			list.style.display = 'block';
		}	
	}
}

function isEmptyElement(obj){	
	var	trimmed = trim(obj.innerHTML);	
	var empt1 = (trimmed == '');
	var empt2 = (trimmed == '&nbsp;');
	var empt3 = (trimmed == '&nbsp;&nbsp;');
	return (empt1 || empt2 || empt3);
}

function setCookieValue(value){
	var expdate = new Date();
	var expires;
	expdate.setTime(expdate.getTime() + 31536000000); //1-year expiration
	expires = ';expires=' + expdate.toGMTString();	
	document.cookie = TEXT_SIZE_COOK_NAME + "=" + value + expires + ";path=/";
}

/*Reads the cookie and sets the text size for the page accordingly.*/
function setTextSize(size, toggleObjId, smalltxt, largetxt) {
	var smcss = document.getElementById(TEXT_SIZE_CSS_ID);
	var tobj = document.getElementById(toggleObjId);
	if (smcss != null) {
		smcss.disabled = (size == TEXT_COOK_VAL_SMALL) ? false : true;
		tobj.innerHTML = (size == TEXT_COOK_VAL_SMALL) ? largetxt : smalltxt;
	}
}


function getElements_ieFix(tag, attribute, attributeval, isClassAttribute){
	var elem = document.getElementsByTagName(tag);     
	var arr = new Array();     
	for(i = 0,iarr = 0; i < elem.length; i++) {
		var att;
		if (isClassAttribute) att = elem[i].className;
		else att = elem[i].getAttribute(attribute);
		
		if(att == attributeval) {
			arr[iarr] = elem[i];
			iarr++;
		}
	}
	return arr;
}


function trim(str){ 
	return str.replace(/^\s+|\s+$/g, '') ;
}

hideEmptyTags('h1');
hideEmptyTags('h3');
hideEmptyTags('div');