/*
	$Id: common.js 6445 2008-05-14 04:56:05Z mjhorne $
*/

function showStandardTab(parentID, index)
{
	var parentNode = document.getElementById(parentID);

	if(parentNode)
	{
		for(var i = 0; i < parentNode.childNodes.length; i++)
		{
			if((parentNode.childNodes[i].nodeName == "DIV") && 
			(parentNode.childNodes[i].className.indexOf("tab") == 0))
			{
				var headerLink = document.getElementById(parentNode.childNodes[i].id.replace("Tab", "Header"));
				if(headerLink)
				{
					var header = headerLink.parentNode;
				}
				else
				{
					var header = null;
				}
				if(parentNode.childNodes[i].id == index + "Tab")
				{
					if(parentNode.childNodes[i].className.indexOf("activeTab") == -1)
					{
						parentNode.childNodes[i].className += " activeTab";
					}
					if(header && (header.className.indexOf("activeTabHeader") == -1))
					{
						header.className += " activeTabHeader";
					}
				}
				else
				{
					hideStandardTab(parentNode.childNodes[i].id.replace("Tab", ""));
				}
			}
		}
	}

	resizeBody("standardTabs", getComputedHeight("planCategoryDetails") + 40);
	return false;
}


function hideStandardTab(index)
{
	var headerLink = document.getElementById(index + "Header");
	var tab = document.getElementById(index + "Tab");

	if(headerLink)
	{
		var header = headerLink.parentNode;
		if(header)
		{
			header.className = "";
		}
	}

	if(tab)
	{
		tab.className = tab.className.replace(" activeTab", "");
		tab.className = tab.className.replace("activeTab", "");
	}
}


function submitOnce(input)
{
	var name = input.name;
	input.name = name + "_disabled";

	var parent = input.parentNode;
	while(parent && parent.nodeName != "FIELDSET")
	{
		parent = parent.parentNode;
	}

	if(parent)
	{
		var element = document.createElement("INPUT");
		element.name = name;
		element.type = "hidden";
		element.style.display = "none";
		element.value = input.value;
		parent.appendChild(element);
	}

	return true;
}


function getComputedHeight(source)
{
	var height = 0;
	var browserName=navigator.appName;
	if (browserName=="Microsoft Internet Explorer"){
		height = document.getElementById(source).offsetHeight;
	}
	return height;
}


function resizeBody(source, additional)
{
	var browserName=navigator.appName;
	if (browserName=="Microsoft Internet Explorer") 
	{
		var version = 999;
		if (navigator.appVersion.indexOf("MSIE") != -1)
		// bah, IE again, lets downgrade version number
		version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		if(version < 7)
		{
			var height = document.getElementById(source).offsetHeight;
			var body = document.getElementById("body");
			body.style.height = (height + additional + 50) + "px";
		}
	}
}


function initialiseMascot()
{
	if(!displayMascot)
	{
		return;
	}
	
	$(window).bind('resize', function() 
	{
		toggleMascot();
	});

	toggleMascot();
}


function toggleMascot()
{
	if($(window).width() >= 1150)
	{
		if($('#mascot').height() <= $('#mascot').parent().height())
		{
			if(($('#mascot').height() + parseInt($('#mascot').css('margin-top'), 10)) > 
				($('#mascot').parent().height() - 150))
			{
				$('#mascot').css('margin-top', '60px');
			}
			$("#mascot").show();
		}
		else
		{
			$('#mascot').hide();
		}
	}
	else
	{
		$('#mascot').hide();
	}
}


function randRange( min, max ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Leslie Hoare
    // +   bugfixed by: Onno Marsman
    // *     example 1: rand(1, 1);
    // *     returns 1: 1
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


function initialiseSmallBrowser()
{
	$(window).bind('resize', function() {
		resetBrowserElements();
	});
	resetBrowserElements(true);
}

var currentPageWidth = 'wide';

function resetBrowserElements(initialise)
{
	var element = document.getElementById('crazyJohnsBanner');
	var elementBody = document.getElementById('body');
	var width = window.outerWidth;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) { //test for MSIE x.x;
		var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ieversion>=6) 
		{
			width = document.body.clientWidth;
		}
	}

	// if on home page get custom content element
	if(!elementBody)
	{
		elementBody = document.getElementById('homeBody');
	}

	//  if the banner is smaller than window width, set alignment to window edge instead
	//  otherwise center the banner

	if(width < 1000)
	{
		if(currentPageWidth == 'wide')
		{
			element.style.left = 'auto';
			element.style.marginLeft = '0px';
			elementBody.style.left = 'auto';
			elementBody.style.marginLeft = '0px';

			currentPageWidth = 'narrow';
		}
	}
	else if(!initialise && currentPageWidth == 'narrow')
	{
		element.style.left = '50%';
		element.style.marginLeft = '-494px';
		elementBody.style.left = '50%';
		elementBody.style.marginLeft = '-494px';

		// this code allows us to check screen width
		/*
		elementSearch = document.getElementById('crazyJohnsQuickSearchText');
		elementSearch.value = width + ' : ' + (width % 2);
		*/
		currentPageWidth = 'wide';
	}
}
