// ======================================================================================
// front.js
//	 Created 07/13/2008 by Greg Spry
//		 Main destalis javascript file (applicable to every page)
// 
// ======================================================================================
// Dom-Ready
// ======================================================================================
window.addEvent('domready', function() {

	// Enable debug
	//dbug.enable();

	// Login initilization
	loginShim = new IframeShim({
		element	: 'loginSlideOut',
		display	: false,
		name	: 'myShim'
	});
	
	var loginSlideOut = $('loginSlideOut');
	loginSlideOut.mySlider = new Fx.Slide($('loginSlideOutContent'),
		{open: false, duration: 500});
	loginSlideOut.mySlider.hide();
	loginSlideOut.setStyle('margin-left', '0px');

	// Sub-menu mouse-over initilization
	$$('#menu ul li').each(function(theTopLevelItem) {
		var subMenu = $E('ul', theTopLevelItem);
		if(subMenu) {
			theTopLevelItem.addEvents({
				mouseover: function() {
					subMenu.style.display = 'block';
				//	Effect.Appear(subMenu);
				},
				mouseout: function() {
					subMenu.style.display = 'none';
				}
			});
		}
	});
	
	// Hide pop-up
	$$('.popUpCloseIcon').each(function(theCloseIcon) {
		theCloseIcon.addEvent('click', hidePopUp.pass(theCloseIcon.parentNode));
	});
	
	
	// Make side picture fade in
	//$('contentConsoleSidePortrait').setStyle('opacity', 0);
	//$('contentConsoleSidePortraitLabel').setStyle('opacity', 0);
	//new Fx.Style($('contentConsoleSidePortrait'), 'opacity', {duration: 5000} ).start(1);
	//new Fx.Style($('contentConsoleSidePortraitLabel'), 'opacity', {duration: 5000} ).start(1);
	
	// Make content fade in
	$$('div.slide').setStyle('opacity', 0);
	var contentFx = new Fx.Elements($$('div.slide'), {
			duration: 2500,
			onComplete: function() {
				$('content').setStyle('overflow', 'auto');
			}
	}).start({
			'0': {
					'opacity': [0, 1],
					'height': [0, 460]
			},
			'1': {
					'opacity': [0, 1],
					'height': [0, 460]
			}
	});
	
  // Force existing member to sign agreement if they haven't already
  showMemberAgreementPopUp();
  
  // Hide loading message in IE6
  hideLoadingMessage();
	
});

// ======================================================================================
// Table Sort
// ======================================================================================
function createSortingTable(sortTableId) {
	memberAccountsSortingTable = new SortingTable(sortTableId, {
		zebra: true,
		details: false,
		alternateRowClassName: 'even',
		headerAscClass:	'sortAsc',
		headerDescClass: 'sortDesc',
		sortDirection: 'asc',
		sortColumn: 0
	});
}

// ======================================================================================
// Loading Messages
// ======================================================================================
function showLoadingMessage(text) {
	$('loadingMessage').setHTML('<h2 id="loadText">' + text + '</span>');
	truefontfamily($$('h2#loadText'));
	new Fx.Style('loadingMessage', 'opacity', {
		duration: 500,
		transition: Fx.Transitions.Quart.easeInOut,
		onComplete: function() {
			//truefontfamily($('loadingMessage'));
		}
	}).start(0,1);
}

function hideLoadingMessage() {
	new Fx.Style('loadingMessage', 'opacity', {
		duration: 500,
		transition: Fx.Transitions.Quart.easeInOut
	}).start(1,0);
}

// ======================================================================================
// Pop Up
// ======================================================================================
function showPopUp(popUpID) {
	$(popUpID).effect('opacity').start(0,1);
//	$('thePopUp').style.display = 'block';
}

function hidePopUp(popUpID) {
//	$('thePopUp').style.display = 'none';
	$(popUpID).effect('opacity').start(1,0);
}

// ======================================================================================
// Login
// ======================================================================================
function toggleLogin() {
	loginShim.show();
	$('loginSlideOut').mySlider.toggle();
}

function submitLogin() {
	new Ajax(ROOT_PATH + 'ajaxAction.php?ajaxAction=login', {
		method: 'post',
		data: $('loginForm'),
		update: $('loginMessage'),
		evalScripts: true,
		onComplete: redirectLogin
	}).request();
}
function redirectLogin() {
	var result = $('loginMessage').getText();
	if(result == "Logging in...") {
		window.location.href = ROOT_PATH + 'index.php';
	}
}

function launchLoginHelper() {
	$('loginHelperForm').username.value = $('loginForm').username.value;
	swapSections('loginFormDiv', 'loginHelperFormDiv');
	$('loginSlideOut').mySlider.show.delay(1000, $('loginSlideOut').mySlider);
}

function cancelLoginHelper() {
	swapSections('loginHelperFormDiv', 'loginFormDiv');
}

function closeLoginHelper() {
	swapSections('loginHelperCompletion', 'loginFormDiv');
}

function submitLoginHelpRequest() {
	var theForm = $('loginHelperForm');
	
	if(theForm.helpMode[0].checked  &&  theForm.username.value == '') {
		alert('Please enter your username.');
		theForm.username.focus();
		return;
	}
	
	if(theForm.helpMode[1].checked  &&  !isValidEmailAddress(theForm.email.value)) {
		alert('Please enter a valid email address.');
		theForm.email.select();
		return;
	}
	
	new Ajax(ROOT_PATH + 'ajaxAction.php?ajaxAction=loginHelper', {
		method: 'post',
		data: theForm,
		update: $('loginHelperMessage'),
		onComplete: function() {
			swapSections('loginHelperFormDiv', 'loginHelperCompletion');
		},
		evalScripts: true
	}).request();
}

/*===============================================================================*/
// Member Agreement
/*===============================================================================*/
function showMemberAgreementPopUp() {
	new Ajax(ROOT_PATH + 'ajaxAction.php', {
		method: 'post',
		data: 'ajaxAction=showMemberAgreementPopUp',
		update: $('popUpContentInjectDiv'),
		evalScripts: true,
		onComplete: function(){
      if(this.response.text != '') {
        //truefontfamily($$('h3'));
        showPopUp('thePopUp');
        $$('.popUpCloseIcon').hide();
      }
    }
	}).request();
}

/*===============================================================================*/
function memberAgreementConfirmation() {
	new Ajax(ROOT_PATH + 'ajaxAction.php', {
		method: 'post',
		data: 'ajaxAction=memberAgreementConfirmation',
		evalScripts: true,
		onComplete: function(){
      $$('.popUpCloseIcon').show();
      hidePopUp('thePopUp');
    }
	}).request();
}

/*===============================================================================*/
function memberAgreementDenial() {
	new Ajax(ROOT_PATH + 'ajaxAction.php', {
		method: 'post',
		data: 'ajaxAction=memberAgreementDenial',
		evalScripts: true,
		onComplete: function(){
      alert('By not agreeing to these terms, you are no longer allowed to access the site');
      window.location.href = ROOT_PATH + 'index.php';
    }
	}).request();
}

/*===============================================================================*/
function summonCritiqueFormsPopUp() {
	showLoadingMessage('Loading...');
	new Ajax(ROOT_PATH + 'ajaxAction.php', {
		method: 'post',
		data: 'ajaxAction=summonCritiqueFormsPopUp',
		update: $('popUpContentInjectDiv'),
		evalScripts: true,
		onComplete: function(){
			truefontfamily($$('h3'));
			//truefontfamily($$('a.readNow'));
      showPopUp('thePopUp');
      hideLoadingMessage();
    }
	}).request();
}

/*===============================================================================*/
