//Configuration:
var leeway = 35,
	tabheight = 23,
	fixwidth = 400,
	widthlimit = 378,
	coll_objs = new Array(), //Stores objects when they're found
	coll_id = new Array('csstudy', 'csdegree', 'csintern', 'cslang', 'csvol', 'csteach', 'csjobs', 'csadv', 'cshigh', 'cstefl', 'cslearning', 'csus', 'csdiversity'),
	coll_width = new Array(80, 92, 79, 107, 101, 89, 79, 90, 79, 50, 100, 95, 95); //The number and sequence of these values must correspond to the values at the coll_id
//End of Configuration;

var activetab, 
	extrawidth = 0;
	defheight = tabheight + 'px',
	nuheight = (2*tabheight) + 'px';

function hasClass(object, className) {
	if (!object.className) {
		return false;
	}
	return (object.className.search('(^|\\s)' + className + '(\\s|$)') != -1);
}


//Javascript Class Definitions
function InitObj () {
	
}

function Tab (id, obj, width) {
	this.id = id;
	this.obj = obj;
	setStyle(obj, 'position', 'absolute');
	setStyle(obj, 'float', 'none');
	setStyle(obj, 'backgroundColor', 'transparent');
	this.width = width;
}

function TabStack (order) {
	this.order = order;
	this.tabs = new Array();
}

function _totalTabWidth () {
	if (!this.tabs.length) {
		return 0;
	}
	else {
		var total = 0,
			len = this.tabs.length;
		for (var i = 0; i < len; i++) {
			total += this.tabs[i].width;
		}
		return total;
	}
}

TabStack.prototype.totalTabWidth = _totalTabWidth;
//END: Javascript Class Definitions

function getElementsPresent () {
	var i, obj, len = coll_id.length;
	for (i = 0; i < len; i++) {
		obj = document.getElementById(coll_id[i]);
		if (obj) {
			coll_objs.push(new Tab (coll_id[i], obj, coll_width[i]));
		}
	}
}

function positionTabs () {
	if (document.getElementById) {
		if (!coll_objs.length > 0) getElementsPresent ();
		var objscount = coll_objs.length;

		var stack_index = 0,
			stackobjs = new Array(),
			width = availWidth();
		

		stackobjs[0] = new TabStack(0);
		
		
		for (var i = 0, len = objscount; i < len; i++) {
			
			if (hasClass(coll_objs[i].obj, 'active')) {
				
				//Test if adding the tab object to the stack, the stack will still fit the available width				
				if (stackobjs[0].totalTabWidth() + coll_objs[i].width <= width) {
					//Add the tab object to the stack
					stackobjs[0].tabs.push(coll_objs[i]);
				}
				else {
					//The stack exceeds the total width available.
					//Move the previous tab object to a higher stack
					
					var orphan_tab = stackobjs[0].tabs.pop();
					stackobjs[0].tabs.push(coll_objs[i]);
					
					for (var j = 1, rot = 1; rot != 0; j++) {
						if (stackobjs.length == j) {
							//Create a new stack and insert orphaned tab obj
							stackobjs[j] = new TabStack(j+1);
							stackobjs[j].tabs.push(orphan_tab);
							rot -= 1;
							break;
						}
						else if (stackobjs[j].totalTabWidth() + orphan_tab.width <= width) {
							//Okay to add the orphaned tab obj in this stack
							stackobjs[j].tabs.splice(0, 0, orphan_tab);
							rot -= 1;
							break;
						}
						else {
							//The stack exceeds the total width available.
							//Move the previous tab object to a higher stack
							tab_temp = stackobjs[j].tabs.pop();
							stackobjs[j].tabs.push(orphan_tab);
							orphan_tab = tab_temp;
							delete tab_temp;
						}
					
					}
				}
			}
			else {
				var free_tab = coll_objs[i];
				v = stackobjs.length - 1; 
				for (var j = stackobjs.length - 1; free_tab; j++) {
					if (stackobjs[j].totalTabWidth() + free_tab.width <= width) {
						//Okay to add the tab obj in this stack
						stackobjs[j].tabs.push(free_tab);
						free_tab = false;
					}
					else if (v == j) {
						//Create a new stack and insert the tab obj
						stackobjs[j+1] = new TabStack(j+1);
						stackobjs[j+1].tabs.push(free_tab);
						free_tab = false;
					}
				}
			}
		}
		
		var csearch = document.getElementById('categorysearch'),
			cslist1 = document.getElementById('cslist1'),
			cslist2 = document.getElementById('cslist2');
		
		setStyle(csearch, 'position', 'relative');
		setStyle(cslist1, 'position', 'absolute');
		setStyle(cslist1, 'top', '0px');
		setStyle(cslist1, 'zIndex', 1);
		if (cslist2) {
			setStyle(cslist2, 'position', 'absolute');
			setStyle(cslist2, 'top', '0px');
			setStyle(cslist2, 'zIndex', 0);
		}
		s = stackobjs.length;
		
		maximum_z = s + 2;
		setStyle(csearch, 'height', (s * tabheight) + 'px');
		
		for (var j = 0; j < s; j++) {
			if (!stackobjs[j]) break;
			var tabs = stackobjs[j].tabs,
				len = tabs.length,
				stackheight = (j + 1) * tabheight;
			for (var top, bot, i = 0, botleftpos = 0, topleftpos = 0; i < len; i++) {
				
				top = tabs[i].obj;
				//If it is active
				if (hasClass(top, 'active')) {
					setStyle(top, 'top',  ((s * tabheight) - stackheight - 5) + 'px');
					setStyle(top, 'height', (stackheight + 6) + 'px');
				}
				else {
					setStyle(top, 'height', stackheight + 'px');
					setStyle(top, 'top',  ((s * tabheight) - stackheight) + 'px');
				}
				setStyle(top, 'left', topleftpos + 'px');
				setStyle(top, 'zIndex', maximum_z - j);
				topleftpos += tabs[i].width;
			}
		}
	}
}

function gGetWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth;
}

function availWidth () {
	var windowwidth = gGetWidth(), 
		tabwidth = windowwidth - (leeway + fixwidth) + detectExtraWidths();
	var ar = tabwidth > widthlimit ? tabwidth : widthlimit;
	return ar;
}

function setStyle(obj, stayl, value) {	
	obj.style[stayl] = value;
}

function detectExtraWidths () {
	if (!document.getElementsByTagName) return 0;
	var container = document.getElementById('minibanners_side');
	if (!container) return 0;
	var c = container.getElementsByTagName('img');
	if (c.length > 0) {
		return 0;
	}
	else {
		return 144;
	}
}