$(window).addEvent('domready', function(){
	// Show/hide login form
	var FxLogin = new Fx.Reveal($('loginformwrap'), {duration:500});
	$$('#loginwrap a.bttn_login').addEvent('click', function(){ FxLogin.toggle();	return false;	});
	if($('MemberUsername')){
		$('MemberUsername').addEvent('focus', function(){
			if(this.value == 'Email') this.value = '';
		}).addEvent('blur', function(){
			if(this.value == '') this.value = 'Email';
		});
	}
	/* FAQ toggle show answer
	$$('#faq dt').addEvent('click', function(){
		var el = this.getNext('dd');
		new Fx.Reveal(el, {duration:500}).toggle();
	});*/
	initDialog();
	initScreenshots();
	if($('leavingform')){
		$('leavingform').addEvent('submit', function(){
			if($('MemberLeaving1').checked){
				return confirm('Your Member Account will be deleted along with any bills/debts belonging to you.\nDo you wish to Continue?');
			}else{
					return true;
			}
		});
	}
	var currentForm = null;
	if($('form')){
		$('form').addEvent('submit', function(){
			if(currentForm) return false;
			else{
				currentForm = this;
				return true;
			}
		});
	}
});
/**
 * Init Screenshots
 */
function initScreenshots(){
	var li = $$('#introheader ul li');
	var ss = $('screenshot');
	// preload and bind events
	li.each(function(obj){
		var img = new Image();
		img.src = '/img/screenshots/'+obj.className+'.jpg';
	});
	li.addEvent('mouseover', function(){
		ss.src = '/img/screenshots/'+this.className+'.jpg';
	});
}
/**
 * Initialise popup dialog
 */
function initDialog(){
	var popup = $('popup');
	if(popup){
		$$('#popup .close span').addEvent('click', closeDialog);
		$(window).addEvent('resize', centerDialog);
		centerDialog();
		new Drag('popup',{'handle': $$('#popup .close')});
		// add auto fade to already visible popup messages
		var diag = $('popup');
		if(diag && diag.hasClass('flash')){
			setTimeout(closeDialog, 3000);
		}
	}
}
/**
 * close popup dialog effect
 */
function closeDialog(){
	new Fx.Morph('popup', {duration: 400}).start({
		'opacity':0
	});
}
/**
 * open popup dialog effect
 */
function openDialog(){
	$('popup').setStyles({'display':'block','opacity':0});
	new Fx.Morph('popup', {duration: 400}).start({
		'opacity':1
	});
	$$('#popup a.close').addEvent('click', function(e){closeDialog(); return false;});
}
/**
 * center popup dialog
 */
function centerDialog(){
	var popup = $('popup');
	if(popup){
		var dim = popup.getDimensions();
		var win = $(window).getSize();
		popup.setStyles({'left': (win.x-dim.x)/2, 'top': (win.y-dim.y)/2, 'margin': 0});
	}
}