window.addEvent('domready', function()
{
	/* hide using opacity on page load */
	if($('fb-modal'))
	{
		$('fb-modal').setStyles({
			opacity:0,
			display:'block'
		});
		
		/* hiders */
		window.addEvent('keypress',function(e) { if(e.key == 'esc') { $('fb-modal').fade('out'); } });
		$(document.body).addEvent('click',function(e) { 
			if($('fb-modal').get('opacity') == 1 && !e.target.getParent('.generic_dialog')) { 
				$('fb-modal').fade('out');
				$('pop_content').set('html','<img style="margin:20px" src="/images/ajax-loader.gif" border="0"/>');
				$('fb-modal').getChildren('table').removeClass('expanded'); 
			} 
		});
	}
});

function movePlane(where){
	amount = '-' + (where * 930) +'px';
	if($('flowpanes')){
		var items = $$('.items')[0];
		items.morph({left:amount});
	}
}

function sendMessage(){
	new Request({
		url: '/mod_exc/contact_form',
		method: 'post',
		data: { 
			name: $('contactName').get('value'),
			email:$('contactEmail').get('value'),
			message:$('message').get('value'),
			newsletter:$('newsletter').checked
		},
		onSuccess: function(){
			alert('Message Sent!');
		},
		onFailure: function(){
			alert('An error occurred, please try again at another time');
		}
	}).send();
}

function login()
{
	new Request({
		url: '/mod_exc/login',
		method: 'post',
		data: { 
			username: $('_username').get('value'),
			password:$('_password').get('value'),
		},
		onSuccess: function(response){
			switch(response){
				case '1': window.location = "cp";
				break;
				default: $('alert').show();$('alert').set('html',response);break;
			}
		},
		onFailure: function(){
			$('alert').show();
			$('alert').set('html','An error occurred, please try again at another time');
		}
	}).send();
}

function delete_file(file_id,name)
{
	if(confirm("Are you sure you want to delete this file?"))
	{
		new Request({
			url: '/mod_exc/delete_file',
			method: 'post',
			data: { filename : name },
			onRequest: function()
			{
				$('file_'+file_id).setStyle('opacity', '.2');
			},
			onSuccess: function()
			{
				$('file_'+file_id).dispose();
			}
		}).send();
	}
	else { return false; }
}

function email_send()
{	
	new Request({
		url: '/mod_exc/email',
		method: 'post',
		data: { 
			subject: $('email_subject').get('value'),
			message:$('email_message').get('value'),
		},
		onSuccess: function(){
			alert('Message Sent!');
			window.location.reload();
		},
		onFailure: function(){
			alert('An error occurred, please try again at another time');
		}
	}).send();
}

function open_message(id)
{
	$('fb-modal').fade('in');
	new Request.HTML({
			url: '/cp/messages_detail/'+id,
			method: 'post',
			update: $('pop_content')
		}).send();
}

function fb_close()
{
	$('fb-modal').fade('out');
	$('pop_content').set('html','<img style="margin:20px" src="/images/ajax-loader.gif" border="0"/>');
	$('fb-modal').getChildren('table').removeClass('expanded');
}

function create_weblink()
{
	new Request({
			url: '/mod_exc/add_link',
			method: 'post',
			data: { link_url : $('addLink').get('value') },
			onSuccess : function(data)
			{
				window.location.reload();
			}
	}).send();
}

function del_link(id)
{
	new Request({
			url: '/mod_exc/delete_link/'+id,
			method: 'post',
			onSuccess : function(data)
			{
				$('link_'+id).dispose();
			}
	}).send();
}

function del_ebook(id)
{
	new Request({
			url: '/mod_exc/delete_order/'+id,
			method: 'post',
			data: { order_id : id},
			onSuccess : function(data)
			{
				$('order_'+id).dispose();
			}
	}).send();
}

function create_order()
{
	new Form.Request('order_btn',{
		onSuccess : function(data)
		{
			window.location.reload();
		}
	}).send();
}
