var tabs = new Array();
var tabBlurbs = new Array();

function inittabs() {
	temptabCounter = 0;
	// of all the images, find those that are tabs.
	arr_img = document.getElementsByTagName('img');
		for(var i = 0; i < arr_img.length; i++) {
			if(null !== arr_img[i].getAttribute("type") && arr_img[i].getAttribute("type") == 'tab') {
				// copy the posting element to the global 'tabs' array.
				tabs[temptabCounter] = arr_img[i];
				// assign that tab a unique number.
				tabs[temptabCounter].setAttribute("id", temptabCounter);
				// give the tabs their "bring to front" behavior.
				tabs[temptabCounter].onclick = function() { bringToFront(this);};
				// give the tabs their hover state
				tabs[temptabCounter].onmouseover = function() {mouseOverState(this);};
				// give the tabs their hover off state
				tabs[temptabCounter].onmouseout = function() { mouseOutState(this);};
				// lastly, give each tab the hand cursor on hover.
				tabs[temptabCounter].style.cursor = "pointer";
				tabs[temptabCounter].style.cursor = "hand";

				temptabCounter++;
			}
		}
	arr_img = [];
	totaltabs = temptabCounter;
	currtab = 0;

	preloadTabImages();

}

function inittabblurbs() {

	temptabblurbCounter = 0;
	// of all the divs, find those that are of "tabBlurb" type.
	arr_divs = document.getElementsByTagName('div');
		for(var i = 0; i < arr_divs.length; i++) {
			if(null !== arr_divs[i].getAttribute("type") && arr_divs[i].getAttribute("type") == 'tabBlurb') {
				// copy the posting element to the global 'tab blurbs' array.
				tabBlurbs[temptabblurbCounter] = arr_divs[i];
				// assign that tab blurb a unique number.
				tabBlurbs[temptabblurbCounter].setAttribute("id", temptabblurbCounter);

				temptabblurbCounter++;
			}
		}
	arr_divs = [];
	totalTabBlurbs = temptabblurbCounter;
}

function preloadTabImages(){

	// PRELOAD IMAGES

	var preloaderArray = new Array();

	for(var i=0; i < tabImages.length;i++) {

		var img = new Image;
		img.src = tabImages[i];
		preloaderArray.push(img);

	}

}

function resetTabs(){

	for(var i=0; i < tabs.length; i++) {
		tabs[i].style.zIndex = 1;
		tabBlurbs[i].style.zIndex = 1;
		tabBlurbs[i].style.visibility = 'hidden';
		tabs[i].src=tabImages[i*3];

		if (i == currtab ) {
			tabs[i].style.zIndex = 999;
			tabBlurbs[i].style.zIndex = 998;
			tabBlurbs[i].style.visibility = 'visible';
			tabs[i].src=tabImages[(i*3)+1];
		}
	}

	if(typeof(PositionTabs)=='function') PositionTabs();

}

////////////////////////////////////////////////////////////////
function show(id) {
	document.getElementById(id).style.visibility = "visible";
}
////////////////////////////////////////////////////////////////
function hide(id) {
	document.getElementById(id).style.visibility = "hidden";
}

function bringToFront(obj,pageLoad) {
	currtab = obj.id;
	resetTabs();
	if(!pageLoad){
		var cm_pageId = obj.getAttribute('cm_pageId');
		if(cm_pageId&&typeof toughbook_model=='string'){
			cmCreatePageviewTag(cm_sanitize('Toughbook Awards '+toughbook_model+' ' + cm_pageId),'Toughbook Products Sub');
		}
	}
}

function sendToBack(obj) {
	tabBlurbs[currtab].style.visibility = 'hidden';
	obj.src="./img/tab_off.gif";
	obj.zIndex=1;
}

function mouseOverState(obj) {
	if (obj.id != currtab){
		obj.src=tabImages[obj.id*3+2];
	}
}

function mouseOutState(obj) {
	if (obj.id != currtab){
		obj.src=tabImages[obj.id*3];
	}
}

// SHOW TAB ACCORDING TO QUERYSTRING, IF SPECIFIED
Event.observe(window,'load',function(){
	var requested_tab = window.details.queryString()['tab'];
	if(requested_tab){
		var tab = $$('img[tab_id='+requested_tab+']')[0];
		bringToFront(tab,true);
		window.scrollTo(0,tab.cumulativeOffset()[1]);
	}
});
