var PodCast = {
	AddIndexHandlers: function() {
		var comment_links = $$('.comment_link');
		if (comment_links) {
			comment_links.each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					var href = el.href;
					Lightview.show({
						href: href,
						rel: 'ajax',
						title: 'Podcast Comments',
						options: {
							topclose: false,
							scrolling: false,
							width: 850,
							height: 450,
							ajax: {
								method: 'get',
								evalScripts: true,
								requestHeaders: Biscuit.Ajax.RequestHeaders('update')
							}
						}
					});
				});
			});
		}
		var tracklist_expanders = $$('.tracklist_expander');
		if (tracklist_expanders) {
			tracklist_expanders.each(function(el) {
				var my_id = el.id;
				$('content_'+my_id).hide();
				el.observe("click",function(event) {
					Event.stop(event);
					var content_id = 'content_'+el.id;
					var my_content_el = $(content_id);
					if (my_content_el.getStyle('display') == "block") {
						Effect.Fade(my_content_el,{duration: 0.2});
					}
					else {
						Effect.Appear(my_content_el,{duration: 0.4});
					}
				});
			});
		}
		var del_buttons = $$('.delete_track');
		if (del_buttons) {
			del_buttons.each(function(el) {
				el.observe("click", function(event) {
		    		Event.stop(event);
					if(window.confirm("Are you sure you want to delete this track? This action cannot be undone.")) {
						var url = el.href;
						PodCast.TrackDelete(url,false);
					}
				});
			});
		}
		var edit_buttons = $$('.edit_track');
		if (edit_buttons) {
			edit_buttons.each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					var my_id = el.id;
					if (my_id.match(/edit\_/)) {
						var edit_id = my_id.substring(5);		// value after 'edit_'
					}
					else {
						var edit_id = '';
					}
					PodCast.EditTrackInLightview(edit_id);
				});
			});
		}
	},
	AddCommentHandlers: function() {
		var remove_btns = $$('.comment_delete');
		if (remove_btns) {
			remove_btns.each(function(el) {
				el.observe("click",function(event) {
					Event.stop(event);
					if (confirm("Are you sure you want to remove this comment?")) {
						var my_url = el.href;
						Lightview.show({
							href: el.href,
							rel: "ajax",
							title: "Podcast Comments",
							options: {
								topclose: false,
								scrolling: false,
								width: 850,
								height: 450,
								ajax: {
									method: 'get',
									evalScripts: true,
									requestHeaders: Biscuit.Ajax.RequestHeaders('update')
								}
							}
						})
					}
				});
			});
		}
		$('comment_editor').observe("submit",function(event) {
			Event.stop(event);
			new Biscuit.Ajax.FormValidator('comment_editor',{
				throbber_id: 'comment_throbber',
				ajax_submit: true,
				custom_ajax_submitter: PodCast.SubmitComment
			});
		});
		$('comment-form-container').hide();
		$('post-comment').observe("click",function(event) {
			Event.stop(event);
			if ($('comment-form-container').getStyle('display') == "block") {
				Effect.Fade('comment-form-container',{duration: 0.2});
			}
			else {
				Effect.Appear('comment-form-container',{duration: 0.4});
			}
		});
	},
	SubmitComment: function() {
		var comment_form = $('comment_editor');
		Lightview.show({
			href: comment_form.action,
			rel: 'ajax',
			title: 'Podcast Comments',
			options: {
				topclose: false,
				scrolling: false,
				width: 850,
				height: 450,
				ajax: {
					method: 'post',
					evalScripts: true,
					requestHeaders: Biscuit.Ajax.RequestHeaders('update'),
					parameters: Form.serializeElements(comment_form.getElements(),true) // the parameters from the form
				}
			}
		});
	},
	EditTrackInLightview: function(id) {
		if (id == '') {
			var action = 'new_track';
			var title = 'Add Track';
		}
		else {
			var action = 'edit_track/'+id;
			var title = 'Edit Track';
		}
		Lightview.show({
			href: "/podcast/"+action,
			rel: 'ajax',
			title: title,
			options: {
				topclose: false,
				scrolling: true,
				autosize: true,
				ajax: {
					method: 'get',
					evalScripts: true,
					requestHeaders: Biscuit.Ajax.RequestHeaders('update')
				}
			}
		});
	},
	AddTrackEditHandlers: function() {
		// add form validation on submit
		if ($('track_editor')) {
			$('track_editor').observe("submit", function(event){
				Event.stop(event);
				new Biscuit.Ajax.FormValidator('track_editor',{
					throbber_id: 'submit_throbber',
					ajax_submit: true,
					update_div: 'page_content',
					complete_callback: function() {
						Lightview.hide();
					}
				});
			});
		}
		// add confirm to delete chart
		if ($('delete_track')) {
			$('delete_track').observe("click", function(event){
	    		Event.stop(event);
				if(window.confirm("Are you sure you want to delete this track? This action cannot be undone.")) {
					var url = $('delete_track').href;
					PodCast.TrackDelete(url,true);
				}
			});
		}
		$('close_editor').observe("click",function(event){
			Event.stop(event);
			Lightview.hide();
		});
	},
	TrackDelete: function(url,from_lightview) {
		this.delete_url = url;
		if (from_lightview === true) {
			$('SubmitButton').value = 'Hang on...';
			$('SubmitButton').disabled = true;
			this.delete_completion = function() {
				Lightview.hide();
			}
			this.DoTrackDelete();
		}
		else {
			Biscuit.Crumbs.ShowThrobber();
			this.delete_completion = function() {
				$Nav.slide_in();
			}
			$Nav.slide_out(function() {
				PodCast.DoTrackDelete();
			})
		}
	},
	DoTrackDelete: function() {
		new Ajax.Updater('page_content',this.delete_url,{
			method: 'get',
			evalScripts: true,
			requestHeaders: Biscuit.Ajax.RequestHeaders('update'),
			onComplete: this.delete_completion
		});
	}
}
