// JavaScript Document

/**
 * Functions used by music chart plugin
**/
var MusicChart = {
	delete_url: null,
	delete_completion: null,
	AddIndexHandlers: function() {
		if ($$('.delete_chart')) {
			$$('.delete_chart').each(function(el) {
				el.observe("click", function(event) {
		    		Event.stop(event);
					if(window.confirm("Are you sure you want to delete this item from the chart? This action cannot be undone.")) {
						var url = el.href;
						MusicChart.Delete(url,false);
					}
				});
			});
		}
		if ($$('.edit_chart')) {
			$$('.edit_chart').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 = '';
					}
					MusicChart.EditInLightview(edit_id);
				});
			});
		}
		// Make list sortable:
		if ($('sort_form')) {
			Biscuit.Crumbs.Sortable.create('music_chart','li','charts','chart_list',{
				hoverclass: 'draggable',
				array_name: 'chart_sort'
			});
		}
	},
	AddEditHandlers: function() {
		// add form validation on submit
		if ($('chart_editor')) {
			$('chart_editor').observe("submit", function(event){
				Event.stop(event);
				new Biscuit.Ajax.FormValidator('chart_editor',{
					throbber_id: 'submit_throbber',
					ajax_submit: true,
					update_div: 'page_content',
					complete_callback: function() {
						Lightview.hide();
					}
				});
			});
		}
		// add confirm to delete chart
		if ($('delete_chart')) {
			$('delete_chart').observe("click", function(event){
	    		Event.stop(event);
				if(window.confirm("Are you sure you want to delete this item from the chart? This action cannot be undone.")) {
					var url = $('delete_chart').href;
					MusicChart.Delete(url,true);
				}
			});
		}
		$('close_editor').observe("click",function(event){
			Event.stop(event);
			Lightview.hide();
		});
	},
	Renumber: function() {		// Renumber the items in the chart and update their classnames after resorting
		var element_ids = new Array();
		var i = 1;
		$$('#music_chart li').each(function(el) {
			var my_id = el.id.substr(6);		// Everything after "chart_"
			var my_rank = i;
			$('rank_'+my_id).update(my_rank)
			var correct_classname = (i%2 == 0) ? 'even' : 'odd';
			if (el.className != correct_classname) {
				el.removeClassName(el.className);
				el.addClassName(correct_classname);
			}
			i += 1;
		});
	},
	EditInLightview: function(id) {
		if (id == '') {
			var action = 'new';
			var title = 'New Chart Item';
		}
		else {
			var action = 'edit/'+id;
			var title = 'Edit Chart Item';
		}
		Lightview.show({
			href: "/charts/"+action,
			rel: 'ajax',
			title: title,
			options: {
				topclose: false,
				scrolling: true,
				autosize: true,
				ajax: {
					method: 'get',
					evalScripts: true,
					requestHeaders: Biscuit.Ajax.RequestHeaders('update')
				}
			}
		});
	},
	Delete: 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.DoDelete();
		}
		else {
			Biscuit.Crumbs.ShowThrobber();
			this.delete_completion = function() {
				$Nav.slide_in();
			}
			$Nav.slide_out(function() {
				MusicChart.DoDelete();
			})
		}
	},
	DoDelete: function() {
		new Ajax.Updater('page_content',this.delete_url,{
			method: 'get',
			evalScripts: true,
			requestHeaders: Biscuit.Ajax.RequestHeaders('update'),
			onComplete: this.delete_completion
		});
	}
}

