var hasTyped = false;
var preloaded = new Array();

function preload_images()
{
    for (var i = 0; i < arguments.length; i++){
        preloaded[i] = document.createElement('img');
        preloaded[i].setAttribute('src',arguments[i]);
    };
};

function toggleLaunch()
{
	var button = document.getElementById('urlLaunchButton');
	var imageLoc = button.src;

	if (imageLoc.indexOf('images/launch_3d_black.gif') > -1)
		button.src = 'images/launch_3d_green_ani.gif';
	else
		button.src = 'images/launch_3d_black.gif';
//	if (imageLoc.indexOf('images/launch_3d_black.png') > -1)
//		button.src = 'images/launch_3d_green.png';
//	else
//		button.src = 'images/launch_3d_black.png';
}

function setButton(onoff)
{
	var button = document.getElementById('urlLaunchButton');

	if (onoff == 'off')
		button.src = 'images/launch_3d_black.gif';
	else if (onoff == 'on')
		button.src = 'images/launch_3d_green_ani.gif';
}

function toggleInput(which)
{
	var textbox = document.getElementById('visit3dInput');

	textbox.focus();
	//resetChoices();

	if (which == 'myspace') {
		//setInputChoice('urlMyspace');
		//document.getElementById('urlMyspace').style.backgroundImage = 'url(images/sns_myspace.png)';
		textbox.value = 'http://www.myspace.com/';
	} else if (which == 'bebo') {
		//setInputChoice('urlBebo');
		//document.getElementById('urlBebo').style.backgroundImage = 'url(images/sns_bebo.png)';
		textbox.value = 'http://www.bebo.com/';
	} else if (which == 'friendster') {
		//setInputChoice('urlFriendster');
		//document.getElementById('urlFriendster').style.backgroundImage = 'url(images/sns_friendster.png)';
		textbox.value = 'http://profiles.friendster.com/';
	} else if (which == 'aim') {
		//setInputChoice('urlAim');
		//document.getElementById('urlAim').style.backgroundImage = 'url(images/sns_aim.png)';
		textbox.value = 'http://profiles.aim.com/';
	} else if (which == 'flickr') {
		//setInputChoice('urlFlickr');
		//document.getElementById('urlFlickr').style.backgroundImage = 'url(images/sns_flickr.png)';
		textbox.value = 'http://www.flickr.com/';
	} else if (which == 'youtube') {
		//setInputChoice('urlYoutube');
		//document.getElementById('urlYoutube').style.backgroundImage = 'url(images/sns_youtube.png)';
		textbox.value = 'http://www.youtube.com/';
	} else if (which == 'other') {
		//setInputChoice('urlOther');
		//document.getElementById('urlOther').style.backgroundImage = 'url(images/sns_other.png)';
		textbox.value = 'http://';
	}

	textbox.focus();
	hasTyped = false;

	//setButton('off');
}

function setInputChoice(elementId)
{
	var el = document.getElementById(elementId);
	el.style.top = '-3px';
	el.style.paddingTop = '3px';
	el.style.backgroundColor = '#FFF';
}

function resetChoices()
{
	resetChoice('urlMyspace');
	resetChoice('urlBebo');
	resetChoice('urlFriendster');
	resetChoice('urlAim');
	resetChoice('urlFlickr');
	resetChoice('urlYoutube');
	resetChoice('urlOther');

	document.getElementById('urlMyspace').style.backgroundImage = 'url(images/sns_myspace_grey.png)';
	document.getElementById('urlBebo').style.backgroundImage = 'url(images/sns_bebo_grey.png)';
	document.getElementById('urlFriendster').style.backgroundImage = 'url(images/sns_friendster_grey.png)';
	document.getElementById('urlAim').style.backgroundImage = 'url(images/sns_aim_grey.png)';
	document.getElementById('urlFlickr').style.backgroundImage = 'url(images/sns_flickr_grey.png)';
	document.getElementById('urlYoutube').style.backgroundImage = 'url(images/sns_youtube_grey.png)';
	document.getElementById('urlOther').style.backgroundImage = 'url(images/sns_other_grey.png)';
}

function resetChoice(elementId)
{
	var el = document.getElementById(elementId);
	el.style.top = '0px';
	el.style.paddingTop = '0px';
	el.style.backgroundColor = '#CCC';
}

function checkInput()
{
	if (hasTyped == false)
		setButton('on');
}

function checkEnter(e,indexNum)
{
	var key=e.keyCode || e.which;
	if (key==13)
	{
		indexNum = indexNum == null ? 'UK' : indexNum;
		urchinTracker('/UTTracking/www/'+indexNum+'/visitWebsite3dButtonClick.html');
		launch('custom',indexNum);
	}
}

function checkSearchEnter(e,indexNum)
{
	var key=e.keyCode || e.which;
	if (key==13)
	{
		launchSearch(indexNum);
	}
}

function featured(which)
{
	var textbox = document.getElementById('urlInput');
	if (which == 'myspace') {
		textbox.value = 'http://profile.myspace.com/carlsjr3d';
		setButton('on');
	} else if (which == 'bebo') {
		textbox.value = 'http://www.bebo.com/ExitR';
		setButton('on');
	} else if (which == 'friendster') {
		textbox.value = 'http://profiles.friendster.com/exitreality';
		setButton('on');
	} else if (which == 'aim') {
		textbox.value = 'http://profiles.aim.com/exitreality2008';
		setButton('on');
	} else if (which == 'flickr') {
		textbox.value = 'http://www.flickr.com/groups/blackandwhite/pool/';
		setButton('on');
	}
}

function launch3d(indexNum)
{
	var textbox = document.getElementById('visit3dInput');
	if ((textbox.value != 'http://') && (textbox.value != ''))
	{
		indexNum = indexNum == null ? 'UK' : indexNum;
		urchinTracker('/UTTracking/www/'+indexNum+'/visitWebsite3dButtonClick.html');
		window.open('http://3d.exitreality.com/?q='+textbox.value);
	}
}

function launchSearch(indexNum)
{
	var textbox = document.getElementById('searchInput');
	if (textbox.value == '')
	{
		return false;
	}
	indexNum = indexNum == null ? 'UK' : indexNum;
	urchinTracker('/UTTracking/www/'+indexNum+'/search/searchInputLaunched.html');
	//if they are just clicking search at least send them there, don't sit here and do nothing.
	if(textbox.value == 'Search for 3D Places and Objects on the Web')
	{
		location.href = 'http://search.exitreality.com/main.php';
		return;
	}
	document.getElementById('searchForm').submit();
	return true;
}

function searchInputFocus()
{
	var textbox = document.getElementById('searchInput');
	if (textbox.value == 'Search for 3D Places and Objects on the Web')
	{
		textbox.value = '';
	}
}

function searchInputBlur()
{
	var textbox = document.getElementById('searchInput');
	if (textbox.value == '')
	{
		textbox.value = 'Search for 3D Places and Objects on the Web';
	}
}

function launch(which)
{
	var textbox = document.getElementById('urlInput');
	if (which == 'myspace') {
		window.open('http://3d.exitreality.com/?q=http://profile.myspace.com/carlsjr3d');
	} else if (which == 'bebo') {
		window.open('http://3d.exitreality.com/?q=http://www.bebo.com/ExitR');
	} else if (which == 'friendster') {
		window.open('http://3d.exitreality.com/?q=http://profiles.friendster.com/exitreality');
	} else if (which == 'aim') {
		window.open('http://3d.exitreality.com/?q=http://profiles.aim.com/exitreality2008');
	} else if (which == 'flickr') {
		window.open('http://3d.exitreality.com/?q=http://www.flickr.com/groups/blackandwhite/pool/');
	} else if (which == 'advertisers') {
		window.location.href = 'http://www.exitreality.com/advertisers.html';
	} else if (which == 'websiteowners') {
		window.location.href = 'http://www.exitreality.com/owners.html';
	} else if (which == 'designers') {
		window.location.href = 'http://www.exitreality.com/designers.html';
	} else if (which == 'custom') {
		window.open('http://3d.exitreality.com/?q='+textbox.value);
	}
}

function highlight(which)
{
	if (which == 'myspace') {
		document.getElementById('featuredSpanMyspace').style.color = '#F90';
	} else if (which == 'bebo') {
		document.getElementById('featuredSpanBebo').style.color = '#F90';
	} else if (which == 'friendster') {
		document.getElementById('featuredSpanFriendster').style.color = '#F90';
	} else if (which == 'aim') {
		document.getElementById('featuredSpanAim').style.color = '#F90';
	} else if (which == 'flickr') {
		document.getElementById('featuredSpanFlickr').style.color = '#F90';
	} else if (which == 'advertisers') {
		document.getElementById('otherAdvertisers').style.color = '#F90';
		document.getElementById('otherheadingAd').style.textDecoration = 'underline';
	} else if (which == 'websiteowners') {
		document.getElementById('otherWebsiteOwners').style.color = '#F90';
		document.getElementById('otherheadingWO').style.textDecoration = 'underline';
	} else if (which == 'designers') {
		document.getElementById('otherDesigners').style.color = '#F90';
		document.getElementById('otherheadingDe').style.textDecoration = 'underline';
	}
}

function resetHighlight()
{
	document.getElementById('featuredSpanMyspace').style.color = '#000';
	document.getElementById('featuredSpanBebo').style.color = '#000';
	document.getElementById('featuredSpanFriendster').style.color = '#000';
	document.getElementById('featuredSpanAim').style.color = '#000';
	document.getElementById('featuredSpanFlickr').style.color = '#000';

	document.getElementById('otherAdvertisers').style.color = '#666';
	document.getElementById('otherWebsiteOwners').style.color = '#666';
	document.getElementById('otherDesigners').style.color = '#666';

	document.getElementById('otherheadingAd').style.textDecoration = 'none';
	document.getElementById('otherheadingWO').style.textDecoration = 'none';
	document.getElementById('otherheadingDe').style.textDecoration = 'none';
}

function showWhatIs()
{
	document.getElementById('modalWindowContent').innerHTML = document.getElementById('hiddenWhatIs').innerHTML;
	document.getElementById('modalWindow').style.width = '410px';
	showModalWindow();
}

function showVideo()
{
	window.open('http://www.exitreality.com/videoplayer.html', 'VideoPlayer', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=530,height=320,left=150,top=150');
}

function showModalWindow()
{
	document.getElementById('modalWindow').style.display = document.getElementById('modalBackground').style.display = 'block';

	// special < IE7 -only processing for windowed elements, like select
	if (window.XMLHttpRequest == null)
	{
		ReplaceSelectsWithSpans();
	}

	// call once to center everything
	OnWindowResize();

	if (window.attachEvent)
		window.attachEvent('onresize', OnWindowResize);
	else if (window.addEventListener)
		window.addEventListener('resize', OnWindowResize, false);
	else
		window.onresize = OnWindowResize;

	// we won't bother with using javascript in CSS to take care
	// of keeping the window centered
	if (document.all)
		document.documentElement.onscroll = OnWindowResize;
}

function closeModalWindow()
{
	document.getElementById('modalWindow').style.display = document.getElementById('modalBackground').style.display = 'none';
	document.getElementById('modalWindowContent').innerHTML = '';

	// special IE-only processing for windowed elements, like select
	if (document.all)
	{
		RemoveSelectSpans();
	}

	if (window.detachEvent)
		window.detachEvent('onresize', OnWindowResize);
	else if (window.removeEventListener)
		window.removeEventListener('resize', OnWindowResize, false);
	else
		window.onresize = null;
}

function OnWindowResize()
{
	// we only need to move the dialog based on scroll position if
	//   we're using a browser that doesn't support position: fixed, like < IE 7
	var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
	var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
	var div = document.getElementById('modalWindow');

	div.style.left = Math.max((left + (getWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
	div.style.top = Math.max((top + (getWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
}

function RemoveSelectSpans()
{
	var selects = document.getElementsByTagName('select');

	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];

		if (select.clientWidth == 0 || select.clientHeight == 0 ||
			select.nextSibling == null || select.nextSibling.className != 'selectReplacement')
		{
			continue;
		}

		select.parentNode.removeChild(select.nextSibling);
		select.style.display = select.cachedDisplay;
	}
}

function ReplaceSelectsWithSpans()
{
	var selects = document.getElementsByTagName('select');

	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];

		if (select.clientWidth == 0 || select.clientHeight == 0 ||
			select.nextSibling == null || select.nextSibling.className == 'selectReplacement')
		{
			continue;
		}

		var span = document.createElement('span');

		// this would be "- 3", but for that appears to shift the block that contains the span
		//   one pixel down; instead we tolerate the span being 1px shorter than the select
		span.style.height = (select.clientHeight - 4) + 'px';
		span.style.width = (select.clientWidth - 6) + 'px';
		span.style.display = 'inline-block';
		span.style.border = '1px solid rgb(200, 210, 230)';
		span.style.padding = '1px 0 0 4px';
		span.style.fontFamily = 'Arial';
		span.style.fontSize = 'smaller';
		span.style.position = 'relative';
		span.style.top = '1px';
		span.className = 'selectReplacement';

		span.innerHTML = select.options[select.selectedIndex].innerHTML +
			'<img src="custom_drop.gif" alt="drop down" style="position: absolute; right: 1px; top: 1px;" />';

		select.cachedDisplay = select.style.display;
		select.style.display = 'none';
		select.parentNode.insertBefore(span, select.nextSibling);
	}
}

function getWindowWidth()
{
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;

	return width;
}

function getWindowHeight()
{
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;

  	return height;
}

window.onload = function()
				{
					if (document.getElementById('visit3dInput'))
					{ // is the index page

						//document.getElementById('visit3dInput').focus();
						//document.getElementById('visit3dInput').value='http://www.myspace.com/';
						//document.getElementById('visit3dInput').focus();
//						document.getElementById('footerLaunchButton').style.display = 'inline';
//						document.getElementById('footerOpenStands').style.display = 'block';

/*
						preload_images(
							'images/sns_myspace_grey.png',
							'images/sns_aim.png',
							'images/sns_bebo.png',
							'images/sns_friendster.png',
							'images/sns_flickr.png',
							'images/sns_youtube.png',
							'images/sns_other.png'
							);

						setButton('off');

						document.getElementById('urlLaunchButton').src = 'images/launch_3d_black.gif';
*/
					}
				};