// Constants
var TOOLBAR_URL = '/toolbar.php';
var SLIDE_RATE = 30;

var clearTagBox = true;
var timer = null;

if ( self.addLoadEvent && isDefined( 'window', 'preferences' ) )
	addLoadEvent( restoreToolbarPrefs );

function restoreToolbarPrefs()
{
	var controlbox = document.getElementById( 'controlbox' );
	var hider = document.getElementById( 'hideButton' );

	// Do ugly and stupid things to make the toolbar work in IE < 7
	if ( BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7 )
	{
		window.onscroll = updatePosition;
		window.onresize = updatePosition;
		controlbox.style.position = 'absolute';
		controlbox.style.left = '0px';
		updatePosition();
	}
	else
	{
		controlbox.style.position = 'fixed';
	}

	// If no user logged in, load settings from cookies
	if ( uid == 0 )
	{
		preferences['toolbar'] = getCookie( 'toolbar', '1' );
		preferences['popup'] = getCookie( 'popup', 'ASV' );

		var cboPopup = document.getElementById( 'selPopupVersion' );
		cboPopup.value = preferences['popup'];
	}

	// Hide the toolbar if that is the user's preference
	if ( preferences['toolbar'] == 0 )
	{
		controlbox.style.left = -controlbox.offsetWidth + hider.offsetWidth + 8 + 'px';
		hider.value = '>';
		hider.onclick = function() { slideControlBox('in') };
	}
	controlbox.style.visibility = 'visible';
}

function getCookie( name, defaultVal )
{
	var start = document.cookie.indexOf( name + '=' );
	var len = start + name.length + 1;
	if ( !start && name != document.cookie.substring( 0, name.length ) )
		return defaultVal;
	if ( start == -1 )
		return defaultVal;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 )
		end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( cookieName, value )
{
	var expires = new Date();
	expires.setTime( expires.getTime() + 1000 * 60 * 60 * 24 * 365 * 100 );	// 100 years
	document.cookie = cookieName + '=' + escape( value ) + 
		'; expires=' + expires.toGMTString() +
		'; path=/';
}

function slideControlBox( direction )
{
	var hider = document.getElementById( 'hideButton' );
	var controlbox = document.getElementById( 'controlbox' );
	var curLeft = controlbox.offsetLeft;

	if ( direction == 'out' )
	{
		curLeft -= SLIDE_RATE;
		if ( curLeft < -controlbox.offsetWidth + hider.offsetWidth + 8 )
		{
			curLeft = -controlbox.offsetWidth + hider.offsetWidth + 8;
			hider.value = '>';
			hider.onclick = function() { slideControlBox('in') };
			setToolbarPref( 'toolbar', '0' );
		}
		else
		{
			resetTimer();
			timer = setTimeout( "slideControlBox('out')", 10 );
		}
	}
	else if ( direction == 'in' )
	{
		curLeft += SLIDE_RATE;
		if ( curLeft > 0 )
		{
			curLeft = 0;
			hider.value = '<';
			hider.onclick = function() { slideControlBox('out') };
			setToolbarPref( 'toolbar', '1' );
		}
		else
		{
			resetTimer();
			timer = setTimeout( "slideControlBox('in')", 10 );
		}
	}
	controlbox.style.left = curLeft + 'px';
}

/* For IE, keep the toolbar at the bottom of the screen when scrolling */
function updatePosition()
{
	var s = 'document.documentElement.scrollTop = ' + document.documentElement.scrollTop
		+ '\ndocument.documentElement.clientHeight = ' + document.documentElement.clientHeight
		+ '\ndocument.body.clientHeight = ' + document.body.clientHeight
		+ '\nwindow.innerHeight = ' + window.innerHeight;
//	alert( s );

    if (document.documentElement.clientHeight != 0) {
        scrollLeft = document.documentElement.scrollLeft;
        scrollTop = document.documentElement.scrollTop;
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body.clientHeight != 0) {
        scrollLeft = document.body.scrollLeft;
        scrollTop = document.body.scrollTop;
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    } else if (window.innerHeight != 0) {
        scrollLeft = window.pageXOffset;
        scrollTop = window.pageYOffset;
        windowWidth = window.innerWidth;
        windowHeight = window.innerHeight;
    } else {
        return;
    }
	
	var controlbox = document.getElementById( 'controlbox' );
	var newTop = windowHeight + scrollTop - controlbox.offsetHeight;
	controlbox.style.top = newTop + 'px';
}

function enterTagBox()
{
	var tagBox = document.getElementById( 'txtTag' );
	if ( clearTagBox )
	{
		tagBox.value = '';
//		tagBox.style.fontStyle = '';
		tagBox.style.color = '';
		clearTagBox = false;
	}
}

function leaveTagBox()
{
	var tagBox = document.getElementById( 'txtTag' );
	if ( tagBox.value == '' )
	{
		tagBox.value = 'Tag this page';
//		tagBox.style.fontStyle = 'italic';
		tagBox.style.color = 'gray';
		clearTagBox = true;
	}
}

function chooseVersion( newVersion, allowNIV )
{
	if ( !allowNIV && ( newVersion == 'NIV' || newVersion == 'TNIVOTNT' ) )
	{
		txt = 'The NIV and TNIV translations are only available for members with a 10-book or 1-year subscription to the site.  ';
		txt += 'For more information, please click the Support tab.';
		alert( txt );
		var popupCombo = document.getElementById( 'selPopupVersion' );
		popupCombo.value = preferences['popup'];
	}
	else
	{
		preferences['popup'] = newVersion;
		setToolbarPref( 'popup', newVersion );
	}
}

function setToolbarPref( prefName, prefValue )
{
	if ( uid == 0 )
	{
		setCookie( prefName, prefValue );
	}
	else
	{
		var url = TOOLBAR_URL + '?save&' + prefName + '=' + prefValue;
		var req = newXHR();
		req.onreadystatechange = function() {};
		req.open( 'GET', url, true );
		req.send( null );
	}
}

function resetTimer()
{
	if ( timer != null )
		clearTimeout( timer );
	timer = null;
}

function isDefined( obj, variable )
{
	return ( typeof( eval( obj )[variable] ) != 'undefined' );
}

// Browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


