/*
//*******
//
//	filename: module_helper.js
//	author: Zack Brown
//	date: 5th July 2009
//
//*******
*/

//observe window onload
Event.observe(window, 'load', function()
{
	//check module delete all link element is valid
	if($('module_delete_all_link') != undefined)
	{
		//observe form submission for inertia model delete form
		Event.observe($('module_delete_all_link'), 'click', function(event)
		{
			//submit form
			$('smcms_model_delete_form').submit();
		});
	}
	
	//check module backup all link element is valid
	if($('module_backup_all_link') != undefined)
	{
		//observe form submission for inertia model delete form
		Event.observe($('module_backup_all_link'), 'click', function(event)
		{
			//submit form
			$('smcms_model_delete_form').submit();
		});
	}
	
	//check module toggle all link element is valid
	if($('module_toggle_all_link') != undefined)
	{
		//observe toggle checkbox selections for inertia model delete form
		Event.observe($('module_toggle_all_link'), 'click', function(event)
		{
			//toggle value
			var toggle = 'none';
			
			//gather all checkboxes in form
			var checkboxes = $$('.table_checkbox input');
			
			//loop through checkboxes
			for(var index = 0; index < checkboxes.length; ++index)
			{
				//check toggle value
				if(toggle == 'none')
				{
					//save first checkbox toggle value
					toggle = !checkboxes[index].checked;
				}
				
				//set toggle value for checkbox
				checkboxes[index].checked = toggle;
			}
		});
	}
	
	//gather all module method toggle links
	var module_toggle_all_links = $$('.module_toggle_all_link');
	
	//loop though all toggle links
	for(var index = 0; index < module_toggle_all_links.length; ++index)
	{
		//save element
		var element = module_toggle_all_links[index];
		
		//observe toggle checkbox selection event
		Event.observe($(element.id), 'click', function(event)
		{
			//toggle value
			var toggle = 'none';
			
			//gather all checkboxes in form
			var checkboxes = $$("." + event.element().id.replace("_toggle", "_toggle_checkbox"));
			
			//loop through checkboxes
			for(var index = 0; index < checkboxes.length; ++index)
			{
				//check toggle value
				if(toggle == 'none')
				{
					//save first checkbox toggle value
					toggle = !checkboxes[index].checked;
				}
				
				//set toggle value for checkbox
				checkboxes[index].checked = toggle;
			}
		});
	}
});

//declare Notification class
var Notification = Class.create();

//declate the HotBar prototype
Notification.prototype = {

	//wrapper function for init
	initialize: function(options)
	{
		//set options
		this.set_options(options);
		
		//gather all module method toggle links
		var smcms_notifications = $$('.smcms_notification');
		
		//loop though all toggle links
		for(var index = 0; index < smcms_notifications.length; ++index)
		{
			//save element
			var element = smcms_notifications[index];
			
			//check element id
			if(element.id)
			{
				//gather toggle element
				var toggle = $$('#' + element.id + " .smcms_notification_toggle").first();
				
				//observe toggle checkbox selection event
				Event.observe($(toggle), 'click', function(event)
				{
					//gather event element
					event = Event.element(event);
					
					//parse toggle notification id
					var notification_id = event.id.replace("toggle_notification_", "");
					
					//send ajax request
					this.ajax_request(notification_id);
					
					//remove element
					$(event.id.replace("toggle_", "")).remove();
				}.bind(this));
			}
		}
	},
	
	//wrapper function for setting options
	set_options: function(options)
	{
		//set options
		this.options = {
			save_url: ""
		};
		
		//extend options
		Object.extend(this.options, options || {});
	},
	
	ajax_request: function(request)
	{
		//check save url and request are valid
		if(this.options.save_url.length > 0 && request.length > 0)
		{
			//escape request
			request = "notification=" + escape(request);
					
			//create new ajax request
			new Ajax.Request(this.options.save_url, {method: 'post', postBody: request});
		}
	}
};
