var iplsf = {
	login: function(e)
	{
		e.preventDefault();
	
		h = parseInt($(this).parent('.form-container').height());
		h2 = h / 2 - 10;
		$(this).parent('.form-container').append('<div id="form-overlay" style="height: 100%;"></div><div id="form-modal" style="margin-top: ' + h2 + 'px;"><img src="/images/loading.gif" /></div>');
		
		dest = $(this).attr('action');
		temp = dest.split('?');
		dest = temp[0];
		
		data = 'ajax=login&submit=true&username=' + encodeURIComponent($(this).find('input[name=username]:first').val()) + '&password=' + encodeURIComponent($(this).find('input[name=password]:first').val());
		var this_form = $(this);
		
		$.ajax({
			type: 'GET',
			url: dest,
			data: data,
			dataType: 'json',
			success: function(r) {
				if (typeof(r.error_message) != 'undefined')
				{
					if (this_form.parent('.form-container').children('.error-box').length > 0)
						this_form.parent('.form-container').children('.error-box:first').html(r.error_message);
					else
						this_form.before('<div class="error-box">' + r.error_message + '</div>');
				}
					
				if (typeof(r.redirect) != 'undefined')
					window.location = r.redirect;
					
				$('#form-overlay, #form-modal').remove();
			}
		});
	},
	
	month: function(e)
	{
		e.preventDefault();
		
		h = parseInt($(this).parent('span').parent('h1').parent('div').parent('div').parent('#side-box').height());
		h2 = h / 2;
		$(this).parent('span').parent('h1').parent('div').parent('div').parent('#side-box').append('<div id="form-overlay" style="height: ' + h + 'px;"></div><div id="form-modal" style="margin-top: ' + h2 + 'px;"><img src="images/loading.gif" style="width: 150px;" /></div>');
		
		mth = parseInt($(this).attr('id'));
		
		if ($(this).parent('span').attr('id') == 'previousMonth')
		{
			prev_mth = mth == 1 ? 12 : mth - 1;
			next_mth = mth == 12 ? 1 : mth + 1;
		}
		else
		{
			next_mth = mth == 12 ? 1 : mth + 1;
			prev_mth = mth == 1 ? 12 : mth - 1;
		}
			
		$('#previousMonth a').attr('id', prev_mth);
		$('#nextMonth a').attr('id', next_mth);
		
		yr = parseInt($(this).parent('span').siblings('#year').html());
		
		if ($(this).parent('span').attr('id') == 'previousMonth')
			yr = mth == 12 ? yr - 1 : yr;
		else
			yr = mth == 1 ? yr + 1 : yr;
		
		$.ajax({
			type: 'GET',
			url: '/ajax/build_month',
			data: 'mth=' + mth + '&year=' + yr,
			dataType: 'json',
			success: function(r) {
				if (typeof(r.month) != 'undefined')
				{
					$('#weeks').html(r.month);
					$('#month').html(r.month_name);
					$('#year').html(r.year);
					
					$('#form-overlay, #form-modal').remove();
				}
			}
		});
	},
	
	delete_post: function(e)
	{
		e.preventDefault();
		data = 'submit=true&';
		
		var dest = $(this).attr('action');
		temp = dest.split('/');
		dest = '/ajax/' + temp[1];
		dest += '-' + temp[2];
		
		$(this).find('input[type=checkbox]').each(function() {
			if ($(this).is(':checked'))
				data += $(this).attr('name') + '=true&';
		});
		
		$.ajax({
			type: 'GET',
			url: dest,
			data: data,
			dataType: 'json',
			success: function(r) {
				if (r.status == 'success')
				{
					decide = confirm('Are you sure you want to delete these records?');
					if (!decide)
						data = 'change=true';
					
					$.ajax({
						type: 'GET',
						url: dest,
						data: data,
						dataType: 'json',
						success: function(r) {
							if (typeof(r.redirect) != 'undefined')
								window.location = r.redirect;
						}
					});
				}
			}
		});		
	},
	
	events_post: function(e)
	{
		e.preventDefault();
		
		$('#submit_event').attr('value', 'Loading...').attr('disabled', 'disabled').css('border-color', '#aaa').css('color', '#aaa');
		
		data = 'submit=true&ajax=admin_events_post&';
		
		var dest = $(this).attr('action');
		temp = dest.split('?');
		dest = temp[0];
		
		data += 'title=' + encodeURIComponent($(this).find('input[name=title]:first').val()) + '&';
		data += 'loc=' + encodeURIComponent($(this).find('input[name=loc]:first').val()) + '&';
		data += 'street1=' + encodeURIComponent($(this).find('input[name=street1]:first').val()) + '&';
		data += 'street2=' + encodeURIComponent($(this).find('input[name=street2]:first').val()) + '&';
		data += 'city=' + encodeURIComponent($(this).find('input[name=city]:first').val()) + '&';
		data += 'state=' + encodeURIComponent($(this).find('input[name=state]:first').val()) + '&';
		data += 'zip=' + encodeURIComponent($(this).find('input[name=zip]:first').val()) + '&';
		data += 'when=' + encodeURIComponent($(this).find('input[name=when]:first').val()) + '&';
		data += 'hour=' + encodeURIComponent($(this).find('select[name=hour]:first').children('option:selected').val()) + '&';
		data += 'minute=' + encodeURIComponent($(this).find('select[name=minute]:first').children('option:selected').val()) + '&';
		data += 'ampm=' + encodeURIComponent($(this).find('select[name=ampm]:first').children('option:selected').val()) + '&';
		data += 'description=' + encodeURIComponent($(this).find('textarea[name=description]:first').val()) + '&';
		
		$('input.speaker').each(function() {
			data += $(this).attr('name') + '=' + $(this).val() + '&';
		});
		
		if ($('input[name=id]'))
			data += 'id=' + $('input[name=id]').val() + '&';
		
		var this_form = $(this);

		$.ajax({
			type: 'GET',
			url: dest,
			data: data,
			dataType: 'json',
			success: function(r) {
				if (typeof(r.error_message) != 'undefined')
				{
					if ($('.error-box').length > 0)
						$('.error-box').html(r.error_message);
					else
						this_form.before('<div class="error-box">' + r.error_message + '</div>');
						
					$('#submit_event').attr('value', 'Submit').removeAttr('disabled').css('border-color', '#58004c').css('color', '#58004c');
				}
					
				if (typeof(r.redirect) != 'undefined')
					window.location = r.redirect;
			}
		});		
	},
	
	edit_email: function(e)
	{
		e.preventDefault();
		
		email = $(this).attr('id');
		email = email.split('-')[1];
		address = $(this).parent('td').parent('tr').children('td.email-email:first').html();
		name = $(this).parent('td').parent('tr').children('td.email-name:first').html();
		company = $(this).parent('td').parent('tr').children('td.email-company').html();
		
		w = parseInt($(window).width());
		w = w / 2 - 225;
		h = 60;
		w2 = w + 10;
		h2 = h + 10;
		
		$('body').append('<div id="form-overlay" style="height: 100%; background: none;"></div><div id="form-modal" style="width: 420px; height: 185px;background: #ea7e1c;opacity: 0.65;filter: alpha(opacity=65);-moz-opacity: 0.65;padding: 10px; top: ' + h + 'px; left: ' + w + 'px;"></div><div id="form-modal2" style="background: none; top: ' + h2 + 'px; left: ' + w2 + 'px;"><div class="form-container" style="background: #fff; margin: 0; min-width: 380px; width: 380px;"><form action="/admin/email-edit" method="post" id="email_edit_form"><input type="hidden" name="record" value="' + email + '" /><p><label>Name:</label> <input type="text" name="name" value="' + name + '" /></p><p><label>Email:</label> <input type="text" name="email" value="' + address + '" /></p><p><label>Company:</label> <input type="text" name="company" value="' + company + '" /></p><p class="form-submit"><input type="checkbox" name="delete" value="true" /> Delete this entry.</p><p class="form-submit"><input type="submit" name="submit" value="Submit" /></p></form></div></div>');
		
		$('#email_edit_form').submit(iplsf.email_edit_form);
	},
	
	email_edit_form: function(e)
	{
		e.preventDefault();
		
		$(this).find('input[name=submit]:first').attr('value', 'Loading...').attr('disabled', 'disabled').css('border-color', '#aaa').css('color', '#aaa');
		
		record = $(this).find('input[name=record]:first').val();
		name = $(this).find('input[name=name]:first').val();
		email = $(this).find('input[name=email]:first').val();
		company = $(this).find('input[name=company]:first').val();
		del = $(this).find('input[name=delete]:first').is(':checked') ? 'true' : 'false';
		data = 'submit=true&record=' + record + '&name=' + encodeURIComponent(name) + '&email=' + encodeURIComponent(email) + '&company=' + encodeURIComponent(company) + '&delete=' + del;
		
		var dest = $(this).attr('action');
		temp = dest.split('/');
		dest = '/ajax/' + temp[1];
		dest += '-' + temp[2];

		var this_form = $(this);
		
		$.ajax({
			type: 'GET',
			url: dest,
			data: data,
			dataType: 'json',
			success: function(r) {
				if (typeof(r.error_message) != 'undefined')
				{
					if ($('.error-box').length > 0)
						$('.error-box').html(r.error_message);
					else
					{
						this_form.before('<div class="error-box">' + r.error_message + '</div>');
						$('#form-modal').height(parseInt($('#form-modal').height()) + parseInt(r.h));
					}
						
					this_form.find('input[name=submit]:first').attr('value', 'Submit').removeAttr('disabled').css('border-color', '#58004c').css('color', '#58004c');
				}
					
				if (typeof(r.redirect) != 'undefined')
					window.location = r.redirect;
					
				//$('#form-overlay, #form-modal, #form-modal2').remove();
			}
		});	
	},
	
	event_link: function(e)
	{
		e.preventDefault();
		
		begin = $(this).attr('start');
		end = $(this).attr('end');
		past = $(this).attr('past') ? 'true' : 'false';
		
		w = parseInt($(window).width());
		w = w / 2 - 225;
		h = 60;
		w2 = w + 10;
		h2 = h + 10;
		
		$('body').append('<div id="form-overlay" style="height: 100%; opacity: 0.00; filter: alpha(opacity=0); -moz-opacity: 0.00;"></div><div id="form-modal" style="width: 450px; height: 700px;background: #ea7e1c;opacity: 0.65;filter: alpha(opacity=65);-moz-opacity: 0.65;padding: 10px; top: ' + h + 'px; left: ' + w + 'px;"></div><div id="form-modal2" style="background: #fff; width: 430px; height: 680px; overflow: auto; top: ' + h2 + 'px; left: ' + w2 + 'px;">' + '<img src="/images/loading2.gif" style="width: 100px; height: 100px; margin: 200px 175px;" />' + '</div>');

		$.ajax({
			type: 'GET',
			url: '/ajax/get_event',
			data: 'begin=' + begin + '&end=' + end,
			dataType: 'json',
			success: function(r) {
				if (r.length > 0)
				{						
					ret = '';
					
					if (r.length > 1)
					{
						ret += '<div id="nav-container"><div id="nav"><ul>';
						for(n=0; n<r.length; n++)
						{
							 ret += '<li';
							 
							 if (n == 0)
								ret += ' id="active"';
								
							ret += '><a href="#" rel="' + r[n]['id'] + '">' + r[n]['title'] + '</a></li> ';
						}
							
						ret += '</ul></div></div>';
					}
					
					for(x=0; x<r.length; x++)
					{
						if (r[x]['street2'] != '')
						{
							r[x]['street2'] += ' ';
							street2 = '<span class="map_street2">' + r[x]['street2'] + '</span><br />';
						}
						else
							street2 = '';
							
						ret += '<form';
						
						if (x > 0)
							ret += ' style="display: none;"';
							
						ret += ' tab="' + r[x]['id'] + '" map="' + r[x]['title'].replace(' ', '_') + '">';
						ret += '<h1>' + r[x]['title'] + '</h1>';
						ret += '<p><label>When:</label> ' + r[x]['when'] + '</p>';
						ret += '<p><label>Where:</label> ';
						ret += '<span class="map_loc">' + r[x]['loc'] + '</span><br />';
						ret += '<span class="map_street1">' + r[x]['street1'] + '</span><br />';
						ret += street2;
						ret += '<span class="map_city">' + r[x]['city'] + '</span>, ';
						ret += '<span class="map_state">' + r[x]['state'] + '</span> ';
						ret += '<span class="map_zip">' + r[x]['zip'] + '</span>';
						ret += '<div id="' + r[x]['title'].replace(' ', '_') + '" style="margin-left: 115px;width: 300px; height: 300px"></div><br /><br /><br /></p>';
											
						if (r[x]['speakers'].length > 0)
						{
							ret += '<div class="speaker-container"><p><label>Speakers:</label> ';
							for(n=0; n<r[x]['speakers'].length; n++)
								ret += '<a href="' + r[x]['speakers'][n]['url'] + '" target="_blank">' + r[x]['speakers'][n]['name'] + '</a><br />';
						}
							
						ret += '<p><label>Information:</label> ' + r[x]['description'] + '</p></form>';
						
					}

					$('#form-modal2').html(ret);
					
					initialize_gmap(r[0]['title'].replace(' ', '_'), r[0]['street1'] + ' ' + r[0]['street2'] + r[0]['city'] + ', ' + r[0]['state'] + ' ' + r[0]['zip'], r[0]['street1'] + '<br />' + r[0]['street2'] + r[0]['city'] + ', ' + r[0]['state'] + ' ' + r[0]['zip'], r[0]['loc']);
				}
			}
		});
	},
	
	editor: function()
	{
		$('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : '/js/tinymce/jscripts/tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,charmap,code",
			theme_advanced_buttons2 : "cut,copy,pastetext,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,image,|,forecolor,backcolor,|,sub,sup",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,|,iespell,media,|,print,|,fullscreen",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			content_css : "/includes/css/style.css",
			height: '350px',
			relative_urls : false

		});
	},
	
	paginate: function()
	{
		$('.paginate li a').live('click', function(e) {
			e.preventDefault();
					
			total = parseInt($('ul.paginate').attr('total'));
			current = parseInt($('li.current_page a').attr('page'));
			actual_current = current - 1;
			
			if ($(this).attr('page') == 'next')
				show = current + 1;
			else if ($(this).attr('page') == 'last')
				show = total;
			else if ($(this).attr('page') == 'prev')
				show = current - 1;
			else if ($(this).attr('page') == 'first')
				show = 1;
			else
				show = parseInt($(this).attr('page'));
				
			actual_show = show - 1;
			
			$('tr.email_page[page=' + actual_current + ']').hide();
			$('tr.email_page[page=' + actual_show + ']').show();
			
			$('li.current_page').removeClass('current_page');
			$('.paginate li a[page=' + show + ']').parent('li').addClass('current_page');
			
			list = new Array();
			
			if (show <= 3)
			{
				for(n=1; n<=5; n++)
				{
					if (n > total)
						break;
					else
						list[list.length] = n;
				}
			}
			else
			{
				if (show + 2 > total)
					start = (show - 2) - (show + 2 - total);
				else
					start = show - 2;
				for(n=start; n<=show+2; n++)
				{
					if (n > total)
						break;
					else
						list[list.length] = n;
				}
			}

			for(n=1; n<=total; n++)
			{
				if (list.exists(n))
					$('.paginate li a[page=' + n + ']').parent('li').css('display', 'inline-block');
				else
					$('.paginate li a[page=' + n + ']').parent('li').css('display', 'none');
			}
				
			
			if (show > 1)
			{
				$('.paginate li a[page=prev]').parent('li').css('display', 'inline-block');
				$('.paginate li a[page=first]').parent('li').css('display', 'inline-block');
			}
			else
			{
				$('.paginate li a[page=prev]').parent('li').css('display', 'none');
				$('.paginate li a[page=first]').parent('li').css('display', 'none');
			}
			
			if (show >= total)
			{
				$('.paginate li a[page=next]').parent('li').css('display', 'none');
				$('.paginate li a[page=last]').parent('li').css('display', 'none');
			}
			else
			{
				$('.paginate li a[page=next]').parent('li').css('display', 'inline-block');
				$('.paginate li a[page=last]').parent('li').css('display', 'inline-block');
			}
		});
	},
	
	add_speaker: function()
	{
		$('#event_add_more').live('click', function(e) {
			e.preventDefault();
			count = parseInt($(this).attr('count'));
			$(this).attr('count', count+1);
			$(this).before('<input type="text" name="speaker[' + count + '][name]" style="margin-bottom: 5px;" class="speaker threepxfix" /> <input type="text" name="speaker[' + count + '][url]" style="margin-bottom: 5px;" value="http://" class="speaker" /> <br />');
		});
	},
	
	reply_to: function(e)
	{
		e.preventDefault();
		
		reply_to = $(this).attr('reply');
		post = $(this).attr('post');
		
		
		$('form#comment_form').children('input[name=reply_to]').remove();
		$('form#comment_form').parent('.form-container').children('.comment').remove();
		$('form#comment_form').prepend('<input type="hidden" name="reply_to" value="' + reply_to + '" />');
		$('form#comment_form').before('<div class="comment"><h3>In Reply to: ' + $(this).parent('.comment').children('h3:first').html() + '</h3></div>');
		window.location.hash = 'make_comment';

	}
};