/*  Functions and variables for loading styles */

var mac=false;win=false;linux=false;
var bn='';
var ie=false;var ie4=false;
var op=false;
var ns=false;
var mz=false;
var kde=false;
var currentSize ;
var currentFamily ;

var agent=navigator.userAgent.toLowerCase();
if (agent.indexOf('mac') != -1) { mac=true; dir='mac'; }
if (agent.indexOf('windows') != -1) { win=true; dir='win'; }
if (agent.indexOf('linux') != -1) { linux=true; dir='lin'; }

if (agent.indexOf('msie 4') != -1) { ie4=true; bn='ie';}
   else if (agent.indexOf('opera') != -1) { op=true; bn='op'; }
   else if (agent.indexOf('gecko') != -1||agent.indexOf("safari")!=-1||agent.indexOf("netscape/6")!=-1||agent.indexOf("netscape6")!=-1||agent.indexOf("netscape/7")!=-1||agent.indexOf("netscape7")!=-1) { ns=true; bn='mz'; }
   else if (agent.indexOf('msie 7') != -1||agent.indexOf('msie 6') != -1||agent.indexOf('msie 5') != -1) { ie=true; bn='ie'; }

document.write('<link rel="stylesheet" type="text/css" href="http://new.studylight.org/style/' + dir + '/' + bn + '_style.css">') ;

var trans = '';
var url = '' ;
var verseRef = new Array() ;
var current_qb = '';

function setPreferences(t) {
	if (t == 'bible') {
		
	   } else if (t == 'interlinear') {

	   } else if (t == 'drp') {

	   }
   }

function getPreferences(t) {
	

   }

function findNote(obj) {
	var info = verseRef[obj.id];
	return info ;
   }

function init(id, checkfont) {

	// document.getElementById('navlist').style.offsetTop = 200 + 'px' ;

	if (checkfont == 'yes') {
		currentSize = (getCookie('fontsize')) ? parseInt(getCookie('fontsize')) : 0 ;
        	currentFamily = (ie) ? document.styleSheets[0].rules[5].style.fontFamily : document.styleSheets[0].cssRules[5].style.fontFamily ;
        	if (getCookie('fontfamily')) {
        	        currentFamily = getCookie('fontfamily') ;
        	   }
		setSizeByClass((currentSize * 2), 'up') ;
		fontSwitcher(currentFamily) ;
	   }
	url = 'http://new.studylight.org/cgi-bin/default/verse_popups.cgi?trans=nas&list=';
	var lnkCount = 1 ;
	var list = '';
	var verseList = document.getElementById(id).getElementsByTagName("A");
	for (var i = 0; i < verseList.length; i++) {
		lnk = verseList[i];
		if (lnk.href.indexOf('desk/') != -1) {
			var pair = lnk.search.split('&');
			pair[0] = pair[0].replace('?p=','');
			pair[0] = pair[0].replace('?passage=','');
			pair[0] = pair[0].replace('?q=','');
			pair[0] = pair[0].replace('?query=','');
			list = list + pair[0] ;
			if (pair[1]) {
				pair[1] = pair[1].replace('amp;','');
				list = list + '&' + pair[1] ;
			   }
			list = list + '/' ;
			lnk.id = lnkCount++ ;
			lnk.onmouseover=function(event){
				return makeTrue(domTT_activate(this, event, 'caption', 'Verse Reference', 'content', findNote(this), 'trail', true));
			   };
		   }
	   }

	parseData = function() {
		var xmlDocument = this.req.responseXML ;
		var total = xmlDocument.getElementsByTagName('listtotal').item(0).firstChild.data ;
		for (var i = 1; i <= total; i++) {
			verseRef[i] = xmlDocument.getElementsByTagName('v'+i).item(0).firstChild.data ;
		   }
	   }
	var http = new net.ContentLoader( url + escape(list), parseData);
   }

function setCookie(cookieName, cookieValue, cookiePath, cookieDomain, expireDays) {
	var expires = "";
	var re = /^\d+$/;
	if (expireDays != "" && re.test(expireDays)) {
		expires = new Date();
		expires.setDate(expires.getDate() + expireDays);
		expires = expires.toGMTString();
	   }
	document.cookie = cookieName + "=" + cookieValue +
	((cookiePath) ? "; path=" + cookiePath : "") +
        ((cookieDomain) ? "; domain=" + cookieDomain : "") +
	"; expires="+expires;
   } 

function removeCookie(cookieName, cookiePath, cookieDomain) {
	if (getCookie(cookieName)) {
		document.cookie = cookieName + "=" +
		((cookiePath) ? "; path=" + cookiePath : "") +
		((cookieDomain) ? "; domain=" + cookieDomain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
   }

function getCookie(cookieName) {
	cookieValue = ""
	if (document.cookie.indexOf(cookieName) == -1) {
		return cookieValue;
	   } else {
		cookieStart = document.cookie.indexOf(cookieName);
		cookieValStart = (document.cookie.indexOf("=", cookieStart) + 1);
		cookieValEnd = document.cookie.indexOf(";", cookieStart);
		if (cookieValEnd == -1) {
			cookieValEnd = document.cookie.length
		   }
		cookieValue = document.cookie.substring(cookieValStart, cookieValEnd);
		return cookieValue;
	   }
   }

function detectCookies() {
	setCookie("test", "test", "/", "");
	tmp = getCookie("test")
	if (tmp != "test") {
		return false;
	   }
	return true;
   }

function toggledisplay(obj) {
        if (obj == 'all') {
		var oAllSpans = document.getElementsByTagName('span') ;
		var fnotes = document.getElementById('shownotes') ;
		for (var i = 0; i < oAllSpans.length; i++) {
			if (oAllSpans[i].id.indexOf("F") != -1) {
				oAllSpans[i].style.display = (fnotes.title == "Show Footnotes") ? "inline" : "none" ;
			   }
		   }
		fnotes.title = (fnotes.title == "Show Footnotes") ? "Hide Footnotes" : "Show Footnotes" ;
	   } else {
	   	fnote  = document.getElementById(obj) ;
		fnote.style.display = (fnote.style.display == "none" || fnote.style.display == "") ? "inline" : "none" ;
	   }
  }

function getNote(obj) {
	var info = document.getElementById(obj).innerHTML ;
	info = info.replace(/[\{\}]/g, "") ;
	return info ;
   }

function setSizeByClass(c, d) {
	for (i = 5 ; i <= 16 ; i++ ) {
		cSize = (ie) ? parseFloat(document.styleSheets[0].rules[i].style.fontSize) : parseFloat(document.styleSheets[0].cssRules[i].style.fontSize) ;
		if (isNaN(cSize) != 1) {
			cSize = (d == 'up') ? (cSize + 1) + 'px' : (cSize - 1) + 'px' ;
			if (ie) {
				eval('document.styleSheets[0].rules[i].style.fontSize' + " = '" + cSize + "'") ;
			   } else {
			   	eval('document.styleSheets[0].cssRules[i].style.fontSize' + " = '" + cSize + "'") ;
			   }
		   }
	   }
   }

function fontSwitcher(d) {
	for (i = 5 ; i <= 12 ; i++ ) {
		if (!d) {
			currentFamily = (ie) ? document.styleSheets[0].rules[i].style.fontFamily : document.styleSheets[0].cssRules[i].style.fontFamily ;
			currentFamily = (eval(currentFamily == 'arial,helvetica,sans-serif')) ? 'times,serif' : (eval(currentFamily == 'times,serif')) ? 'verdana,geneva,sans-serif' : 'arial,helvetica,sans-serif' ;
		   }
		if (ie) {
			eval('document.styleSheets[0].rules[i].style.fontFamily' + " = '" + currentFamily + "'") ;
		   } else {
			eval('document.styleSheets[0].cssRules[i].style.fontFamily' + " = '" + currentFamily + "'") ;
		   }
	   }
	setCookie('fontfamily',currentFamily,'/',365) ; 
   }

function fontSizer(dir) {
	if (dir == 'up') {
		if (currentSize < 8) {
			currentSize += 1 ;
		   } else {
			return false ;
		   }
	   } else {
		if (currentSize > 0) {
			currentSize -= 1 ;
		   } else {
			return false ;
		   }
	   }

	setSizeByClass(1, dir) ;
	
	setCookie('fontsize',currentSize,'/',365) ;
	return true ;
   }

function ps(load, scroll, width, height) {
	wide = (ie) ? ((screen.AvailWidth - width)/2) : ((screen.width - width)/2) ;
	high = (ie) ? ((screen.AvailHeight - height)/2) : ((screen.height - height)/2) ;
	msg = window.open("","results","scrollbars=" + scroll + " ,toolbar=no,width=" + width + ",height=" + height + ",left=" + wide + ",top=" + high) ;
	msg.location.href = load ;
	setTimeout('msg.focus()', 100) ;
   }

function sectionJump(URL) {
	self.location.href = URL.options[URL.selectedIndex].value ;
	return true;
   }
   
/*  Functions for switching styles */
   
function setActiveStyleSheet(title) {
        var i, a, main;
        for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
                if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
                        a.disabled = true;
                        if(a.getAttribute("title") == title) a.disabled = false;
                   }
           }
   }

function getActiveStyleSheet() {
        var i, a;
        for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
                if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
           }
        return null;
}

function getPreferredStyleSheet() {
        var i, a;
        for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
                if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) return a.getAttribute("title");
           }
        return null;
   }


window.onresize = function(){
	if (document.getElementById('navList') != null) {
		var obj = getObjectPosition('navbar');
		var mDiv = document.getElementById('navList');
		mDiv.style.top = (obj.y + obj.z) + 'px';
		mDiv.style.left = obj.x + "px";
		mDiv.style.width = obj.w + 'px';
	}

        if (document.getElementById('referenceTable') != null) {
		var obj = getObjectPosition(current_qb);
		var mDiv = document.getElementById('referenceTable');
		mDiv.style.top = (obj.y + (obj.z + 2)) + 'px';
		mDiv.style.left = obj.x + "px";
	}

	if (document.getElementById('verse_listOrganizer') != null) {

	}

	if (document.getElementById('logregDiv') != null) {
		var obj = getObjectPosition('logReg');
		var tmpDiv = document.getElementById('logregDiv');
		tmpDiv.style.top = (obj.y + obj.z) + 'px';
                tmpDiv.style.left = (obj.x + 1) + "px";
	}

	if (document.getElementById('transDiv') != null) {
		var obj = getObjectPosition('ptc');
		var tmpDiv = document.getElementById('transDiv');
		tmpDiv.style.top = (obj.y + (obj.z + 2)) + 'px';
		tmpDiv.style.left = ((obj.x - ((parseInt(tmpDiv.style.width) / 2) - 18)) + obj.w) + 'px';
	}

}
