/**
 * @author tara
 */

/**
 * XFiles2 namespace -- All XFiles2-specific classes will extend
 * this namespace to avoid namspace conflicts with other libraries
 */
XFiles2 = function() {};

document.observe("dom:loaded", function() {
	if ((typeof(window['Tip']) != 'undefined') && (typeof(window['xfilesHelp']) != 'undefined') && (xfilesHelp != null)) {
		if ($('helplink').hasClassName('footip')) {
			new Tip($('helplink'), xfilesHelp, {
			  style: 'darkgrey',
			  viewport: 'true',
			  stem: 'bottomRight',
			  title: (typeof(window['xfilesHelpTitle']) != 'undefined') ? xfilesHelpTitle : 'Help: '+(document.title.replace(/The X Files: I Want to Believe -/,"")),
			  hook: { target: 'topLeft', tip: 'bottomRight' },
			  width: 400
			});
		} else {
			new Tip($('helplink'), xfilesHelp, {
			  style: 'darkgrey',
			  viewport: 'true',
			  stem: 'topLeft',
			  title: (typeof(window['xfilesHelpTitle']) != 'undefined') ? xfilesHelpTitle : 'Help: '+(document.title.replace(/The X Files: I Want to Believe -/,"")),
			  hook: { target: 'bottomRight', tip: 'topLeft' },
			  width: 400
			});
		}
	} else if ((typeof(window['xfilesHelp']) == 'undefined') && $('helplink')) {
		$('helplink').style.display = 'none';
	}

	// Grab error messages from Struts output, hide Struts output, 
	// display error messages in JS popup
	var fieldErrorMsgs = ($('main')) ? $('main').select('.wwerr') : 0;
	var actionErrorMsgs = ($('main')) ? $('main').select('ul li span.errorMessage', 'ul li span.actionMessage') : 0;
	var popupErrorText = '';
	if (fieldErrorMsgs.length > 0) {
		fieldErrorMsgs.each(function(msg) {
			var msgText = msg.select('.errorMessage')[0].innerHTML.strip();
			popupErrorText += msgText + '\n';
			// Exception code for certain fields
			switch(msg.id)  {
				case 'wwerr_RegisterUser_submit2_birthMonth':
					$('RegisterUser_submit2_birthMonth').addClassName('errorHighlight');
					$('RegisterUser_submit2_birthDay').addClassName('errorHighlight');
					$('RegisterUser_submit2_birthYear').addClassName('errorHighlight');
				break;
				default:
					var msgCtrl = msg.id.gsub('wwerr_','');
					if ($(msgCtrl)) {
						$(msgCtrl).addClassName('errorHighlight');
					}
			}
			msg.remove();
		});
	}
	if (actionErrorMsgs.length > 0) {
		actionErrorMsgs.each(function(msg) {
			var msgText = msg.innerHTML.strip();
			popupErrorText += msgText + '\n';
		});
		Element.extend($('main').select('ul li span.errorMessage','ul li span.actionMessage')[0].parentNode.parentNode).remove();
	}
	if (fieldErrorMsgs.length > 0 || actionErrorMsgs.length > 0) {
		alert(popupErrorText);
	}
	
	// Show botton nav item "Edit Agent ID" on certain pages
	var pagesShowingEditAgentID = ['AgentHome_display.action', 
		'UserProfile_display.action', 
		'UserProfile_submit.action', 
		'ChangePassword_display.action',
		'ChangePassword_submit.action', 
		'AgentPhoto_display.action', 
		'CaseFilesHome.action', 
		'CreateAnnotation_display.action',
		'Contact_display.action',
		'Contact_submit.action', 
		'privacy.jsp',
		'terms.jsp',
		'SolveDrummyCase_display.action'];
	if ($('navItemAgentID')) {
		pagesShowingEditAgentID.each(function(page){
			if ((document.location.pathname).include(page)) {
				$('navItemAgentID').style.display = 'block';
			}
		});
	}

	// Don't show botton nav item "Edit User Profile" on certain pages
	var pagesNotShowingEditUserProfile = [];
	if ($('navItemUserProfile')) {
		pagesNotShowingEditUserProfile.each(function(page){
			if ((document.location.pathname).include(page)) {
				$('navItemUserProfile').style.display = 'none';
			}
		});
	}
});

