function turnTabOn( num ) {
	correctPNGBackground( 'subNavLeft_' + num, '/custom/images/tab-left.png' );
	correctPNGBackground( 'subNavRight_' + num, '/custom/images/tab-right.png' );
	correctPNGBackground( 'subNavBg_' + num, '/custom/images/tab-bg.png' );
	document.getElementById( 'subNav_' + num ).className = 'subNavItemOn';
}

function selectTab( tabContentID, tabID ) {
	document.getElementById( 'tabContent_' + currentSelectedTabContent ).style.display = 'none';
	document.getElementById( 'tabContent_' + tabContentID ).style.display = '';
	turnTabOn( tabID );
	turnSelectedTabOff();
	currentSelectedTab = tabID;
	currentSelectedTabContent = tabContentID;
	return false;
}

function turnSelectedTabOff() {
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
		document.getElementById( 'subNavLeft_' + currentSelectedTab ).style.filter = '';
		document.getElementById( 'subNavRight_' + currentSelectedTab ).style.filter = '';
		document.getElementById( 'subNavBg_' + currentSelectedTab ).style.filter = '';
	}
	document.getElementById( 'subNav_' + currentSelectedTab ).className = 'subNavItem';
}

function turnTabOff( num ) {
	if ( currentSelectedTab != num ) {
		var arVersion = navigator.appVersion.split("MSIE")
		var version = parseFloat(arVersion[1])
		if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
			document.getElementById( 'subNavLeft_' + num ).style.filter = '';
			document.getElementById( 'subNavRight_' + num ).style.filter = '';
			document.getElementById( 'subNavBg_' + num ).style.filter = '';
		}
		document.getElementById( 'subNav_' + num ).className = 'subNavItem';
	}
}

function stopLink() {
	return false;
}

function correctPNGBackground( divId, imgURL, sizingMethod ) // correctly handle PNG transparency in Win IE 5.5 , 6 & 7.
{
	if ( typeof sizingMethod == 'undefined' ) {
		sizingMethod = 'scale';
	}
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
	{
		var div = document.getElementById( divId );
		div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+imgURL+"', sizingMethod='" + sizingMethod + "')";
		div.style.background = '';
	}    
}

function correctPNG( imageId, imgURL ) // correctly handle PNG transparency in Win IE 5.5 , 6 & 7.
{
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
	{
		var img = document.getElementById( imageId );
		var imgID = (img.id) ? "id='" + img.id + "' " : "";
		var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		var imgStyle = "display:inline-block;" + img.style.cssText 
		if (img.align == "left") imgStyle = "float:left;" + imgStyle
		if (img.align == "right") imgStyle = "float:right;" + imgStyle
		if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
		var strNewHTML = "<span " + imgID + imgClass + imgTitle
		+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		+ "(src=\'" + imgURL + "\', sizingMethod='scale');\"></span>";
		img.outerHTML = strNewHTML;
		return false;
	}    
}

function getViewPortSize() {
	var viewportwidth;
	var viewportheight;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth;
		viewportheight = window.innerHeight
	}
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else if (typeof document.documentElement != 'undefined'	&& typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth;
		viewportheight = document.documentElement.clientHeight
	}
	// older versions of IE
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	
	return new Array( viewportwidth, viewportheight )
}

function isNumeric ( data ){
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;
	if( data.length > 0 ) {
		for (i = 0; i < data.length && IsNumber == true; i++){ 
			Char = data.charAt(i); 
			if (ValidChars.indexOf(Char) == -1){
				IsNumber = false;
			}
		}
	}
	return IsNumber;
}

function setSiteHeight() {
	var dimensions = getViewPortSize();
	if ( isNumeric( dimensions[1] ) ) {
		if (  document.getElementById('container').offsetHeight < dimensions[1] ) {
			document.getElementById('container').style.height = dimensions[1] + 'px';
		}
	}
}


function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		document.getElementById( passwordField ).style.display = '';
		document.getElementById( passwordField+'_text' ).style.display = 'none';
		document.getElementById( passwordField ).focus();
	} else {
		if( input.value == defaultText ) {
				input.value = '';
		}
	}
}

function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			document.getElementById( passwordField ).style.display = 'none';
			document.getElementById( passwordField+'_text' ).style.display = '';
		} else {
			input.value = defaultText;
		}
	}
}

var remoteAddress = '';
function validateCallBack() {
	var validator = new validateForm();
	validator.checkText( 'callBack_name', 'Your Name' );
	validator.checkText( 'callBack_phone', 'Your Telephone Number' );
	if ( document.getElementById( 'callBack_name' ).value == '- Your Name -' ) {
		validator.addCustomError( 'Your Name' );
	}
	if ( document.getElementById( 'callBack_phone' ).value == '- Your Phone Number -' ) {
		validator.addCustomError( 'Your Telephone Number' );
	}
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		
		document.getElementById( 'callbackForm1' ).action = '/email/cbthanks';
		
		return true;
	}
}

function validateContactForm() {
		
	document.getElementById( 'contactForm' ).action = '/email/contactthanks';
		
	return true;
}


var showGlossaryOnLoad = false;
var submitGlossaryForm = false;

function initGlossary( modalValue ) {
	if( typeof modalValue == 'undefined' )
		modalValue = true;
		
	// Initialize the temporary Panel to display while waiting for external content to load
	document.getElementById('glossaryPopup').style.display = '';
	correctPNGBackground('glossaryPopupBackground','/custom/images/glossary-bg.png');
	glossaryBox = 
			new YAHOO.widget.Panel("glossaryPopup",  
											{ width:"375px", 
											  height:"259px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:modalValue,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	glossaryBox.render(document.body);
	if( showGlossaryOnLoad )
		glossaryBox.show();
}

function hideGlossary() {
	// Show the Panel
	if( typeof glossaryBox != "undefined" )
		glossaryBox.hide();
}

function showGlossary( modal ) {
	// Show the Panel
	if( typeof glossaryBox != "undefined" )
		glossaryBox.show();
	else
		initGlossary( modal );
}




var showLoginOnLoad = false;
var submitLoginForm = false;

function initLogin() {
	// Initialize the temporary Panel to display while waiting for external content to load
	document.getElementById('loginPopup').style.display = '';
	correctPNGBackground('loginPopupBackground','/custom/images/login-bg.png');
	loginBox = 
			new YAHOO.widget.Panel("loginPopup",  
											{ width:"375px", 
											  height:"259px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	loginBox.render(document.body);
	if( showLoginOnLoad )
		loginBox.show();
}

YAHOO.util.Event.addListener(window, "load", initLogin);

function hideLogin() {
	// Show the Panel
	if( typeof loginBox != "undefined" )
		loginBox.hide();
	submitLoginForm = false;
}

function showLogin() {
	// clear the password field
	document.getElementById('popupPassword').value = '';
	// Show the Panel
	if( typeof loginBox != "undefined" )
		loginBox.show();
	else
		showLoginOnLoad = true
}

function onPopupSubmit() {
	// if we are submitting to form
	if( submitLoginForm ){
		// ensure we have username and password
		var validate = new validateForm();
		validate.checkText( 'popupUsername', 'Username' );
		validate.checkText( 'popupPassword', 'Password' );
		if( validate.numberOfErrors() > 0 ) {
			validate.displayErrors();
			// we have errors
			submitLoginForm = false;
		}
	}
	
	return submitLoginForm;
}



var currentPortfolioItem = 0;
var portfolioUrl = '';

function fadeIn() {
	anim2.animate();
}
function fadeOut() {
	anim.animate();
}
function setPortfolioImage() {
	portfolioUrl = portfolioURLs[ currentPortfolioItem ];
	document.getElementById( 'rhsPortfolio' ).style.backgroundImage = 'url(' + portfolioImages[ currentPortfolioItem ] + ')';
	fadeIn();
}
function clickPortfolio() {
	document.location = portfolioUrl;
}
function showRandomPortfolioItem( num ) {
	if ( num == '+' ) {
		currentPortfolioItem++;
	} else if ( num == '-' ) {
		currentPortfolioItem--;
	}
	
	if ( currentPortfolioItem < 0 ) {
		currentPortfolioItem = ( portfolioImages.length - 1 );
	} else if ( currentPortfolioItem > ( portfolioImages.length - 1 ) ) {
		currentPortfolioItem = 0;
	}
	
	if ( ( num != '+' ) && ( num != '-' ) ) {
		currentPortfolioItem = num;
		setPortfolioImage();
	} else {
		anim.onComplete.subscribe(setPortfolioImage);
		fadeOut();
	}
}

var scrollDelay = 4000;
var previousBanner = 0;
var bannerWidth = 310;
var numberOfItemsPerSlide = 3;
var numberOfPortfolioSlides = 1;
var intrvl = new Object();

function viewNextPortfolioItemPage( auto ) {
	previousBanner = currentBanner;
	currentBanner += numberOfItemsPerSlide;
	// if we've reached the end, go back to first
	var maxBanner = numberOfPortfolioSlides * numberOfItemsPerSlide;
	if( currentBanner > maxBanner ) {
		currentBanner = ( startingBanner == 1 ) ? 1 : 2;
	}
	scrollPortfolioItems( auto );
}

function viewPreviousPortfolioItemPage( auto ) {
	previousBanner = currentBanner;
	currentBanner -= numberOfItemsPerSlide;
	// if we've reached the beginning, go to end
	var minPage = ( startingBanner == 1 ) ? 0 : 1;
	if ( currentBanner < minPage ) {
		var maxBanner = numberOfPortfolioSlides * numberOfItemsPerSlide;
		currentBanner = maxBanner - 1;
		if ( startingBanner == 1 ) {
			currentBanner--;
		}
	}
	scrollPortfolioItems( auto );
}

function centerPortfolioItemPage() {
	previousBanner = startingBanner;
	currentBanner = startingBanner;
	scrollPortfolioItems( false );
}

function viewPortfolioItemPage( page ) {
	previousBanner = currentBanner;
	currentBanner = ( page * 2 ) + ( page - 1 );
	if ( startingBanner == 1 ) {
		currentBanner--;
	}
	scrollPortfolioItems( false );
}

function scrollPortfolioItems( auto ) {
	if ( !auto ) {
		stopPortfolioItemsScroll();
	}
	updatePortfolioButtons();
	// find the position to scroll to
	posX = ( currentBanner + 2 ) * bannerWidth;
	var attributes = {   
		scroll: { to: [posX, 0] }   
	};
	var anim = new YAHOO.util.Scroll('portfolioItemsBannerContainerOuter', attributes, 1, YAHOO.util.Easing.easeBothStrong);  
	anim.animate();
}

function stopPortfolioItemsScroll() {
	clearInterval( intrvl );
}

function startPortfolioItemsScroll() {
	intrvl = setInterval( "viewNextPortfolioItemPage( true )", scrollDelay );
	updatePortfolioButtons();
}

function updatePortfolioButtons() {
	var currentPage = Math.ceil( currentBanner / numberOfItemsPerSlide );
	if ( ( startingBanner == 1 ) && ( currentBanner == numberOfItemsPerSlide ) ) {
		currentPage++;
	}
	var previousPage = Math.ceil( previousBanner / numberOfItemsPerSlide );
	if ( ( startingBanner == 1 ) && ( previousBanner == numberOfItemsPerSlide ) ) {
		previousPage++;
	}
	if ( previousBanner > 0 ) {
		document.getElementById( 'portfolioButton' + previousPage ).src = '/custom/images/portfolio/buttonoff.gif';
		document.getElementById( 'portfolioButton' + previousPage ).className = 'portfolioItemsPaginationButtonOff';
	}
	document.getElementById( 'portfolioButton' + currentPage ).src = '/custom/images/portfolio/buttonon.gif';
	document.getElementById( 'portfolioButton' + currentPage ).className = 'portfolioItemsPaginationButton';
}

function initPortfolioBanner() {
	// add the pagination buttons
	var paginationArea = document.getElementById('portfolioItemsPaginationButtons');
	numberOfPortfolioSlides = Math.ceil( totalBanners / numberOfItemsPerSlide );
	if ( startingBanner == 1 ) {
		numberOfPortfolioSlides++;
	}
	for ( var i = 1; i <= numberOfPortfolioSlides; i++ ) {
		var btn = document.createElement('IMG');
		btn.setAttribute( 'src', '/custom/images/portfolio/buttonoff.gif' );
		btn.setAttribute( 'id', 'portfolioButton' + i );
		btn.setAttribute( 'title', i );
		btn.className = 'portfolioItemsPaginationButton' + ( ( i > 1 ) ? 'Off' : '' );
		YAHOO.util.Event.addListener( btn, 'click', function () {
			viewPortfolioItemPage( this.title );
		} );
		paginationArea.appendChild( btn );
	}
	// center the pagination
	var width = parseInt( document.getElementById( 'portfolioItemsPagination' ).offsetWidth );
	var containerWidth = 980;
	var xpos = ( containerWidth / 2 ) - ( width / 2 );
	YAHOO.util.Dom.setStyle( 'portfolioItemsPagination', 'left', xpos + 'px' );
	// start the scroll on load
	if ( startingBanner > 0 ) {
		YAHOO.util.Event.addListener( window, 'load', centerPortfolioItemPage );
	} else {
		YAHOO.util.Event.addListener( window, 'load', startPortfolioItemsScroll );
	}
	
	correctPNG( 'paginationLeft', '/custom/images/portfolio/previous.png' );
	correctPNG( 'paginationRight', '/custom/images/portfolio/next.png' );
}