var Server = {
	
	init : function()
	{
		// element server init
		
		$('img.entry.authorize').bind('click', Server.entry.toggle);
		$('img.entry.remove').bind('click', Server.entry.remove);
		$('img.entry.detail').bind('click', Server.entry.view);
		
		$('img.blast.edit').bind('click', Server.blast.edit);
		$('img.blast.mail').bind('click', Server.blast.mail);
		$('img.blast.test').bind('click', Server.blast.test);
		$('img.blast.remove').bind('click', Server.blast.remove);
		$('a.blast.add').bind('click',Server.blast.add);
		
		$('a.coupon.add').bind('click',Server.coupon.add);
		$('img.coupon.edit').bind('click',Server.coupon.edit);
		$('img.coupon.remove').bind('click',Server.coupon.remove);
		
		$('a.user.add').bind('click',Server.user.add);
		$('img.user.remove').bind('click',Server.user.remove);
		
		$('#table_id').live('change', Server.blast.update_filter_options);
		
	},
	
	
	blast:{
		
		update_filter_options : function()
		{
			var params = {};
			params.table_id = $(this).val();
			$.post(
				'/ajax/populate_columns',
				params,
				function(resp)
				{
					$('#filter_column').html(resp);
				}
			);

			
		},
		
		add : function()
		{
			$.post(
				'/ajax/blast_add/',
				{ },
				function(resp)
				{
					Server.util.make_popup(resp);
					$('#blast_form_submit').one('click', Server.blast.submit);
				}
			);
			return false;
		},
		
		edit : function()
		{
			var id = $(this).parents('li').attr('id').split('_')[1];
			$.post(
				'/ajax/blast_edit/',
				{ blast_id:id },
				function(resp)
				{
					Server.util.make_popup(resp);
					$('#blast_form_submit').one('click', Server.blast.submit);
				}
			);
			return false;
		},

		remove : function()
		{
			if(confirm('Are you sure you want to remove this blast?'))
			{
				var row = Server.util.get_row($(this));
				var blast_id = row.attr('id').split('_')[1];

				$.post(
					'/ajax/blast_remove/',
					{ blast_id:blast_id },
					function(resp)
					{
						if(resp=='1')
						{
							row.fadeOut();
						}
						else
						{
							row.effect(	'highlight',{color:'#C00'}, 1000 );
						}
					}
				);
			}
		},
		
		submit : function()
		{
			// alert('ds');
			$.post(
				'/ajax/blast_submit/',
				$('#blast_form').serializeArray(),
				function(resp)
				{
					if(resp)
					{
						Server.util.kill_popup();
						location.reload(); 
					}
					return false;
				}
			);
			return false;
		},
		
		test : function()
		{
			var id = $(this).parents('li').attr('id').split('_')[1];
			$.post(
				'/ajax/blast_test/',
				{ blast_id:id },
				function(resp)
				{
					Server.util.make_popup(resp);
					$('#blast_test_submit').one('click', Server.blast.sendtest);
				}
			);
			return false;
		},
		
		sendtest : function()
		{
			var email_address = $('#email_address').val();
			var blast_id = $('#blast_id').val();
			$.post(
				'/ajax/blast_sendtest/',
				{ email_address:email_address, blast_id:blast_id },
				function(resp)
				{
					if(resp)
					{
						Server.util.kill_popup();
					}
					return false;
				}
			);
			return false;
		},
		
		mail : function()
		{
			if(confirm('Are you sure?'))
			{
				var btn = $(this);
				var row = Server.util.get_row(btn);
				var blast_id = Server.util.get_table_id(row);
			
				btn.unbind('click', Server.blast.mail).attr('src','/images/icons/buttonloader.gif');
				
				$.post(
					'/ajax/blast_mail',
					{ id:blast_id },
					function(resp)
					{
						var o = $.evalJSON(resp);
						
						btn.attr('src','/images/icons/ok.png');
						$('#mailed_'+o.id).html(o.mailed_date);
						$('#number_'+o.id).html(o.number_mailed);
						// alert("The blast ID is "+resp);
					}
				);
			}
		}
	},
	
	coupon:{
		
		add : function()
		{
			$.post(
				'/ajax/coupon_add/',
				{ },
				function(resp)
				{
					Server.util.make_popup(resp);
					// $('#coupon_form_submit').one('click', Server.coupon.submit);
				}
			);
			return false;
		},
		
		edit : function()
		{
			var id = $(this).parents('li').attr('id').split('_')[1];
			$.post(
				'/ajax/coupon_edit/',
				{ coupon_id:id },
				function(resp)
				{
					Server.util.make_popup(resp);
					// $('#blast_form_submit').one('click', Server.blast.submit);
				}
			);
			return false;
		},
		
		remove : function()
		{
			if(confirm('Are you sure you want to remove this coupon?'))
			{
				var row = Server.util.get_row($(this));
				var coupon_id = row.attr('id').split('_')[1];
				
				$.post(
					'/ajax/coupon_remove/',
					{ coupon_id:coupon_id },
					function(resp)
					{
						if(resp=='1')
						{
							row.fadeOut();
						}
						else
						{
							row.effect(	'highlight',{color:'#C00'}, 1000 );
						}
					}
				);
			}
		}
	},
	
	user:{
		
		add : function()
		{
			$.post(
				'/ajax/user_add/',
				{ },
				function(resp)
				{
					Server.util.make_popup(resp);
					$('#user_form_submit').one('click', Server.user.submit);
				}
			);
			return false;
		},
		
		submit : function()
		{
			$.post(
				'/ajax/user_submit/',
				$('#user_form').serializeArray(),
				function(resp)
				{
					if(resp)
					{
						Server.util.kill_popup();
					}
					return false;
				}
			);
			return false;
		},
		
		remove : function()
		{
			if(confirm('Are you sure you want to remove this entry?'))
			{
				var row = Server.util.get_row($(this));
				var user_id = Server.util.get_entry_id(row);
				
				$.post(
					'/ajax/user_remove/',
					{ user_id:user_id },
					function(resp)
					{
						if(resp=='1')
						{
							row.fadeOut();
						}
						else
						{
							row.effect(	'highlight',{color:'#C00'}, 1000 );
						}
					}
				);
			}
		}
	},
	
	entry : {
		
		toggle : function()
		{
			var btn = $(this);
			var row = Server.util.get_row(btn);
			var table_id = Server.util.get_table_id(row);
			var entry_id = Server.util.get_entry_id(row);

			if(!row.hasClass('disabled'))
			{
				btn.unbind('click', Server.entry.toggle).attr('src','/images/icons/buttonloader.gif');

				$.post(
					'/ajax/toggle_verification/',
					{ entry_id:entry_id, table_id:table_id },
					function(resp)
					{
						var o = $.evalJSON(resp);
						btn.attr('src','/images/icons/'+o.icon).bind('click', Server.entry.toggle);
					}
				);
			}
		},
		
		remove : function()
		{
			var row = Server.util.get_row($(this));
			var table_id = Server.util.get_table_id(row);
			var entry_id = Server.util.get_entry_id(row);

			if(confirm('Are you sure you want to remove this entry?'))
			{
				$.post(
					'/ajax/remove_entry/',
					{ entry_id:entry_id, table_id:table_id },
					function(resp)
					{
						if(resp=='1')
						{
							row.fadeOut();
						}
						else
						{
							row.effect(	'highlight',{color:'#C00'}, 1000 );
						}
					}
				);
			}
		},

		view : function()
		{
			var btn = $(this);
			var row = Server.util.get_row(btn);
			var table_id = Server.util.get_table_id(row);
			var entry_id = Server.util.get_entry_id(row);

			if(!row.hasClass('disabled'))
			{
				btn.unbind('click', Server.entry.view).attr('src','/images/icons/buttonloader.gif');

				$.post(
					'/ajax/view_details/',
					{ entry_id:entry_id, table_id:table_id },
					function(resp)
					{
						btn.bind('click', Server.entry.view).attr('src','/images/icons/detail.png');
						Server.util.make_popup(resp);
					}
				);
			}
		}
	},
	
	util : {
		
		get_row : function (element)
		{
			return element.parents('li');
		},
		
		get_table_id : function(row)
		{
			return row.attr('id').split('_')[1];
		},
	
		get_entry_id : function(row)
		{
			return row.attr('id').split('_')[2];
		},
		
		make_popup : function(content)
		{
			var overlay = $$('div',
							'',
							{ className:'overlay', id:'overlay' });
			var box = $$('div',
						$$('div',content,{ className:'inset' }),
						$$('a',
							$$('img','',{src:'/images/icons/close.png'}),
							{ id:'close_button' }),
						{ className:'box', id:'popup_box' });

			overlay.css({
				height:$(document).height(),
				width:$(document).width(),
				opacity:0.7
			});
			var top = 100+$(document).scrollTop();
			box.css({
				width:600,
				top:top,
				left:($(window).width()*.5)-300
			});

			$('body').append(overlay).append(box);
			$('#close_button').bind('click', function(){ $('#popup_box,#overlay').remove(); });
			
		},
		
		kill_popup : function()
		{
			$('#popup_box,#overlay').remove();
		},
		
		validate_form : function(form)
		{
			if(!form) form = document.forms[0];
			var submit_it = true;
			$(form).find(".REQUIRED").each(function(req)
			{
				if(($(this).attr('type')!='checkbox' && this.value == '') || 
					($(this).attr('type')=='checkbox' && !this.checked))
				{
					$(this).parents("div.formfield").addClass('invalid');
					submit_it = false;
				}
				else
				{
					$(this).parents("div.formfield").removeClass('invalid');
				}			
			});
			if( !submit_it ) $('#error_message').html('All fields are required. Please review the highlighted entries and resubmit.').fadeIn(250);
			return submit_it;
		}
		
	}
}

$(Server.init);
