// ineedaprayer.org javascripts

// global variables
var INP = {};

// get a handle to the global framework
var INPFramework = top.INPFramework;

// test the framework handle.  If "INPFramework" is undefined, the framework has not been attached.
if (INPFramework == undefined) {
	// redirect the window to the framework.
	window.location = "http://www.ineedaprayer.org";
}

INP.popupKeepOpen = true;

INP.popTimer = '';

INP.thePop = '';

INP.theElement = '';

INP.readyState = '';

// popup components
INP.prayerBody = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"left\" valign=\"middle\" nowrap=\"nowrap\" class=\"popup_itemBG\" onmouseover=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onmouseout=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/phpbb/viewforum.php?f=1';\">Read prayer requests </td></tr><tr><td nowrap=\"nowrap\" class=\"popup_itemBG\" onMouseOver=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onMouseOut=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/phpbb/posting.php?mode=newtopic&f=1';\">Post prayer request </td></tr><tr><td nowrap=\"nowrap\" class=\"popup_itemBG\" onMouseOver=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onMouseOut=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/subscribe.php';\">Subscribe </td></tr><tr><td nowrap=\"nowrap\">&nbsp;</td></tr></table>";

INP.testimonyBody = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"left\" valign=\"middle\" nowrap=\"nowrap\" class=\"popup_itemBG\" onmouseover=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onmouseout=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/phpbb/viewforum.php?f=2';\">Read testimonies   </td></tr><tr><td nowrap=\"nowrap\" class=\"popup_itemBG\" onMouseOver=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onMouseOut=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/phpbb/posting.php?mode=newtopic&f=2';\">Post a testimony  </td></tr><tr><td nowrap=\"nowrap\" class=\"popup_itemBG\" onMouseOver=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onMouseOut=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/subscribe.php';\">Subscribe </td></tr><tr><td nowrap=\"nowrap\">&nbsp;</td></tr></table>";

INP.communityBody = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"left\" valign=\"middle\" nowrap=\"nowrap\" class=\"popup_itemBG\" onmouseover=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onmouseout=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/phpbb/privmsg.php?folder=inbox';\">My Inbox </td></tr><tr><td nowrap=\"nowrap\" class=\"popup_itemBG\" onMouseOver=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onMouseOut=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/phpbb/index.php';\">Discussion Forums   </td></tr><tr><td nowrap=\"nowrap\" class=\"popup_itemBG\" onMouseOver=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onMouseOut=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/chatrooms.php';\">Chat rooms</td></tr><tr><td nowrap=\"nowrap\" class=\"popup_itemBG\" onMouseOver=\"INP.popupItemEnter(this); INP.setNoPropogate(window.event);\" onMouseOut=\"INP.popupItemExit(this); INP.setNoPropogate(window.event);\" onclick=\"window.location='http://www.ineedaprayer.org/phpbb/members.php';\">Members</td></tr><tr><td nowrap=\"nowrap\">&nbsp;</td></tr></table>";

INP.setSigninStatus = function() {
	// if a secue state exists, modify the signin pane to contain signout information instead
		var theSignInBlock = top.document.getElementById('signInBlock').value;
		
		if (INPFramework.secure == true) {
			// a secure session exists
			
			// inject the busy spinner into the targetted block
		    document.getElementById('INPSignInBlock').innerHTML = INPFramework.blockSpinnerAssembly;
				
			// we do not load if the framework object for the signin panel is already populated
			// instead we just use its content
			if (theSignInBlock == "") {
				// sign in block has not yet been retrieved
				document.getElementById('INPSignInBlock').innerHTML = INPFramework.blockSpinnerAssembly;
		
				// load the signed in block via ajax
				
				// get a random number to defeat caching
				var theRan = INPFramework.randomNumber;
				var theAccessor = "http://www.ineedaprayer.org/signInBlock.php?anticache=" + theRan;
				INPFramework.getAsynchContent(theAccessor, document.getElementById('INPSignInBlock'), top.document.getElementById('signInBlock'));
			
			} else {
				// signin block has been retrieved already
				// document.getElementById('INPSignInBlock').innerHTML = theSignInBlock;
				
				// get a random number to defeat caching
				var theRan = INPFramework.randomNumber;
				var theAccessor = "http://www.ineedaprayer.org/signInBlock.php?anticache=" + theRan;
				INPFramework.getAsynchContent(theAccessor, document.getElementById('INPSignInBlock'), top.document.getElementById('signInBlock'));
				
				// but we do need to update the message number, so we get it via ajax
				// get a random number to defeat caching
				// var theRan = INPFramework.randomNumber;
				// var theAccessor = "http://www.ineedaprayer.org/getNumMessages.php?anticache=" + theRan;
				// INPFramework.getContent(theAccessor, document.getElementById('INPNumMessages'), 'none');
			}
		
				
		}  else {
			// reset the signin block
			top.document.getElementById('signInBlock').value = "";
			document.getElementById('INPSignInBlock').innerHTML = INPFramework.signedOutBlock;
		}
		
		// size the main container also
		// fix for missing scroll bar
		INPFramework.sizeMain();
	
}

INP.noSpinner = function() {
	INPFramework.hideSpinner();
}

INP.displayRadio = function() {
	// display INP radio if appropriate

	INPFramework.displayRadio();
}

INP.setNewStuff = function() {
	// if a secue state exists, modify the signin pane to contain signout information instead
		
	if (INPFramework.secure == true) {
		// a secure session exists
		INPFramework.getAsynchContent("http://www.ineedaprayer.org/ajax/INPNewStuff.php", document.getElementById('INPNotMember'), true); 
		
	}
	
}

INP.getContent = function(theContentSource, theCallBackObject) {
	// ajax some content for a specific container
	
	// set up a request object.  We have to do this so it degrades gracefully for IE6
	if (window.XMLHttpRequest) {
		// this is IE7, safari, etc.
		var xmlhttp =  new XMLHttpRequest();

	} else {
		// this is probably IE6, so see if we can instantiate an activeX obejct
		if (window.ActiveXObject) {
     		var xmlhttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');

  		}
	}

	// wire up the callBack handler
	xmlhttp.onreadystatechange = function() {  
  		if(xmlhttp.readyState == 4)  
  		alert("Finished loading!");  
	};  

    // send the ajax request after the desired data
	xmlhttp.open('GET', theContentSource, true);
	xmlhttp.send(null);
	
}

INP.getSpotlightBody = function() {
	// get the body of the spotlight request
	try {
		var theSpotlight = document.getElementById('spotlightToGet').value;
		
		var theContentSource = "/phpbb/viewtopic_inp2.php?t=" + theSpotlight;
		
		//alert(theContentSource);
		
		var xmlhttp =  new XMLHttpRequest();
	
		//xmlhttp.overrideMimeType('text/xml');
		xmlhttp.open('GET', theContentSource, false);
		xmlhttp.send(null);
		
		document.getElementById('spotlightBody').innerHTML = xmlhttp.responseText;
	} catch(err) {
		
	}
}

INP.hitForum = function() {
	toURL = document.getElementById("postTo").value + document.getElementById("tID").value;
	//alert(toURL);
	location.href = toURL;
}

INP.popupItemEnter = function(theItem) {
	// handle mouse entry to a popup item
	
	var theItem; // the item moused
	
	// set the keepOpen variable to ensure that the popup will not close
	INP.popupKeepOpen = true;
	
	// set the className
	theItem.className = "popup_itemBGmoused";
	
	// cancel the bubble so this won't cause the popup to close
	// first figure out if this is IE and set the cancelBubble property
	if (!e) {
		try {
			var e = window.event;
			e.cancelBubble = true;
		} catch(err) {
		}
	}
	
	// if it isn't IE stopPropagation
	try {
		if (e.stopPropagation) e.stopPropagation();
	} catch(err) {
	}
	
	// try to force the popup to remain displayed
	document.getElementById('popupLayer').style.display='block';
	
}

INP.popupItemExit = function(theItem) {
	// handle mouse exit from a popup item
	
	var theItem; // the item exited
	
	// set the keepOpen variable to ensure that the popup will not close
	INP.popupKeepOpen = true;
	
	// set the className
	theItem.className = "popup_itemBG";
	
	// cancel the bubble so this won't cause the popup to close
	// first figure out if this is IE and set the cancelBubble property
	if (!e) {
		try {
			var e = window.event;
			e.cancelBubble = true;
		} catch(err) {
		}
	}
	
	// if it isn't IE stopPropagation
	try {
		if (e.stopPropagation) e.stopPropagation();
	} catch(err) {
	}
	
	// try to force the popup to remain displayed
	document.getElementById('popupLayer').style.display='block';

}

INP.GetElementLeft = function(eElement) {
    var nLeftPos = eElement.offsetLeft;          // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null)
    {                                            // move up through element hierarchy
        nLeftPos += eParElement.offsetLeft;      // appending left offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nLeftPos;                             // return the number calculated
}

INP.displayPop = function(thePop, theElement) {
	// display the popup for this item
	
	var thePop; // the popup to display
	var theElement; // the element moused
	
	INP.thePop = thePop;
	INP.theElement = theElement;
	
	// set a timer that will pop the display after 400 MS unless it gets cancelled
	// by a mouse out event.
	
	INP.popTimer = setInterval('INP.displayPopShow();', 400);
	
}

INP.cancelDisplayPop = function() {
	// display the popup for this item
	
	clearInterval(INP.popTimer);
	
}

INP.displayPopShow = function() {
	// display the popup for this item
	
	var thePop = INP.thePop; // the popup to display
	var theElement = INP.theElement; // the element moused
	
	// get the true left position of the element
	var theLeftPos = INP.GetElementLeft(theElement);
	
	// get the top position of the element
	var theTopPos = INP.GetElementTop(theElement);
	
	// set the popup to the proper location
	document.getElementById("popupLayer").style.top = theTopPos - 30 + 'px';
	document.getElementById("popupLayer").style.left = theLeftPos + 12 + 'px';
	
	// replace the popupBody
	document.getElementById('popupBody').innerHTML = eval('INP.' + thePop + 'Body');
	
	// set the outer and inner classes as appropriate
	document.getElementById('pLeft').className = "popup_midLft" + thePop;
	document.getElementById('pRight').className = "popup_midRt" + thePop;
	document.getElementById('pMid').className = "popup_midMid" + thePop;
	 
	// make it visible
	document.getElementById("popupLayer").style.display = 'block';
}

INP.GetElementTop = function(eElement) {
    var nTopPos = eElement.offsetTop;            // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element  
    while (eParElement != null)
    {                                            // move up through element hierarchy
        nTopPos += eParElement.offsetTop;        // appending top offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nTopPos;                              // return the number calculated
}

INP.exitPopup = function() {
	// close the popup window if appropriate
	
	// if the popupKeepOpen variable is true, set it false but then break 
	// in order to keep the popup from closing
	// Otherwise, close the popup
	if (INP.popupKeepOpen == true) {
		// we do nothing
	} else {
		document.getElementById('popupLayer').style.display = 'none';
	}
}

INP.setNoPropogate = function(theEvent) {
	// cancel the bubble so this won't cause the popup to close
	var theEvent; // the event fired
	
	// first figure out if this is IE and set the cancelBubble property
	try {
		theEvent.cancelBubble = true;
	
		// if it isn't IE stopPropagation
		if (theEvent.stopPropagation) {
			//alert('this is firefox');
			theEvent.stopPropagation();
		}
	
	} catch(err) {
	}
}

INP.setReadyStateInterval = function() {
	// set up a ready state interval that will keep firing until the document has completely loaded
	// check every 100MS
	INP.readyState = setInterval("INP.readyMonitor()", 100);
}

INP.readyMonitor = function() {
	// check to see if the page has fully loaded / rendered in IE
	// in mozilla, onload doesn't fire until the DOM is loaded.
	var theState = document.readyState;
	
	if (window.attachEvent) {
		if (theState == "complete") {
			// clear the readystate interval
			clearInterval(INP.readyState);
	
			// do some other IE related thing here
			INP.getSpotlightBody();
			
			INPFramework.hideSpinner();
			
			INPFramework.displayRadio();
			
		}
	} else {
		clearInterval(INP.readyState);
		INPFramework.hideSpinner();
		INPFramework.displayRadio();
	}
}
 
INP.handleRadioClick = function() {
	// handle clicks to the INP radio badge
	
	if (INPFramework.radioState == 'stopped') {
		// INP radio is stopped, so we direct the iframe to load the player
		INPFramework.loadRadioPlayer();
		
		// change the badge state to playing
		document.getElementById('inpradioImage').src = "images/inpRadioBadgeAnim.gif";
		
	} else {
		
	}
}

INP.goInsecure = function() {
	// switch to an insecure state
	INPFramework.goInsecure();
}

if (window.attachEvent) {
	//alert("ie attach event");
	window.attachEvent("onload", INP.setReadyStateInterval);
	window.attachEvent("onload", INPFramework.displayRadio);
	// window.attachEvent("onload", INP.getSpotlightBody);
	window.attachEvent("onload", INP.setSigninStatus);	
	window.attachEvent("onload", INP.setNewStuff);
	
	window.attachEvent("onunload", INPFramework.showSpinner);
	
} else {
	//alert("adding event listener");
	window.addEventListener('load', INP.setReadyStateInterval, false);
	window.addEventListener('load', INPFramework.displayRadio, false);
	window.addEventListener('load', INP.getSpotlightBody, false);
	window.addEventListener('load', INP.setSigninStatus, false);
	window.addEventListener('load', INP.setNewStuff, false);
	
	window.addEventListener('unload', INPFramework.showSpinner, false);

}
