/*
//*******
//
//	filename: module_helper.js
//	author: Zack Brown
//	date: 5th July 2009
//
//*******
*/

//observe window onload
Event.observe(window, 'load', function()
{
	//check hot bar modules element is valid
	if($('hot_bar_modules') != undefined)
	{
		//determine hot bar module div height
		var vertical_rule_height = parseInt($('hot_bar_modules').getHeight()) - 20;
		
		//gather all vertical rules
		var elements = $$('#hot_bar_modules .vertical_rule');
		
		//check elements
		if(elements.length > 0)
		{
			//loop through array of elements
			elements.each(function(element)
			{
				//set element height
				element.setStyle({height: vertical_rule_height + "px"});
			});
		}
	}
	
	//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;
			}
		});
	}
	
	//array of toggle fields
	var toggle_fields = new Hash();
	
	//append toggle field key and value pairs
	toggle_fields.set("toggle_shop_items", "input_shop_item");
	toggle_fields.set("toggle_gallery_images", "input_gallery_image");
	
	//loop though array of toggle fields
	toggle_fields.each(function(field)
	{
		//check toggle element is valid
		if($(field.key) != undefined)
		{
			//observe toggle selection
			Event.observe($(field.key), "click", function(event)
			{
				//toggle value
				var toggle = 'none';
				
				//gather all checkboxes in form
				var checkboxes = $$('.' + field.value);
				
				//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 Notification 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 || {});
	},
	
	//send an ajax request
	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});
		}
	}
};

//declare ShopItemOptions class
var ShopItemOptions = Class.create();

//delcare the ShopItemOptions prototype
ShopItemOptions.prototype = {

	//wrapper function for init
	initialize: function()
	{
		//gather create element
		var smcms_shop_item_options_create = $("smcms_shop_item_options_create");
		
		//check element is valid
		if(smcms_shop_item_options_create != undefined)
		{
			//observe click events
			Event.observe(smcms_shop_item_options_create, "click", this.create_option.bind(this));
		}
		
		//gather current options
		var current_options = $$(".smcms_shop_item_options_wrapper");
		
		//check current options
		if(current_options.length > 0)
		{
			//loop through current options
			current_options.each(function(option)
			{
				//check option id
				if(option.id != "smcms_shop_item_options_wrapper_1")
				{
					//determine unique option id
					var option_id = option.id.replace("smcms_shop_item_options_wrapper_", "");
					
					//gather option toggle element
					var toggle_element = $("smcms_shop_item_options_toggle_" + option_id);
					
					//check element is valid
					if(toggle_element != undefined)
					{
						//observe click events
						Event.observe(toggle_element, "click", this.remove_option);
					}
				}
			}.bind(this));
		}
	},
	
	//create a new shop item option
	create_option: function(event)
	{
		//gather template element
		var template_element = $("smcms_shop_item_options_wrapper_1");
		
		//gather current options
		var current_options = $$(".smcms_shop_item_options_wrapper");
		
		//determine new option id
		var option_id = current_options.length + 1;
		
		//check element is valid
		if(template_element != undefined)
		{
			//clone template element
			var template_clone = new Element("div", {className: "smcms_shop_item_options_wrapper", id: "smcms_shop_item_options_wrapper_" + option_id});
			
			//create toggle element
			var toggle_element = new Element("img", {src: "images/smcms/cross.png", id: "smcms_shop_item_options_toggle_" + option_id, className: "smcms_shop_item_options_toggle", width: 16, height: 16, alt: "Remove this item"});
			
			//append toggle element to template
			template_clone.appendChild(toggle_element);
			
			//gather template child elements
			var template_children = template_element.childElements();
			
			//check child elements
			if(template_children.length > 0)
			{
				//loop through template children
				template_children.each(function(template_child_label)
				{
					//create a new label
					var new_label = new Element("label", {});
					
					//append label title
					new_label.innerHTML = template_child_label.innerHTML.substring(0, template_child_label.innerHTML.indexOf(":") + 1);
					
					//gather label child elements
					var label_child = template_child_label.childElements().first();
					
					//clone input element
					var input_clone = label_child.cloneNode(true);
					
					//check input field name is valid
					if(input_clone.name != undefined)
					{
						//compile new element name
						input_clone.name = input_clone.name.substring(0, input_clone.name.indexOf("[")) + "[" + current_options.length + "]";
						
						//check field type
						if(input_clone.type == "hidden")
						{
							//reset value
							input_clone.value = "0";
						}
					}
					else
					{
						//gather element inside wrapper
						input_element = input_clone.childElements().first();
						
						//compile new element name
						input_element.name = input_element.name.substring(0, input_element.name.indexOf("[")) + "[" + current_options.length + "]";
					}
					
					//append input element to label
					new_label.appendChild(input_clone);
					
					//append label to template
					template_clone.appendChild(new_label);
				});
			}
			
			//gather delete wrapper element
			var smcms_module_delete_all_link_wrapper = $$(".smcms_module_delete_all_link_wrapper").first();
			
			//check element is valid
			if(smcms_module_delete_all_link_wrapper != undefined)
			{
				//append template clone
				Insertion.Before(smcms_module_delete_all_link_wrapper, template_clone);
				
				//observe click events
				Event.observe(toggle_element, "click", this.remove_option);
			}
		}
	},
	
	//remove a shop item option
	remove_option: function(event)
	{
		//determine event element
		var element = Event.element(event);
		
		//determine unique option id
		var option_id = element.id.replace("smcms_shop_item_options_toggle_", "");
		
		//gather option element
		element = $("smcms_shop_item_options_wrapper_" + option_id);
		
		//check element is valid
		if(element != undefined)
		{
			//remove element
			element.remove();
		}
		
		//gather current options
		var current_options = $$(".smcms_shop_item_options_wrapper");
		
		//check current options
		if(current_options.length > 0)
		{
			//set new option id
			var new_option_id = 1;
			
			//loop through current options
			current_options.each(function(option)
			{
				//determine unique option id
				var option_id = option.id.replace("smcms_shop_item_options_wrapper_", "");
				
				//gather option toggle element
				var toggle_element = $("smcms_shop_item_options_toggle_" + option_id);
				
				//check element is valid
				if(toggle_element != undefined)
				{
					//update element ids
					option.id = "smcms_shop_item_options_wrapper_" + new_option_id;
					toggle_element.id = "smcms_shop_item_options_toggle_" + new_option_id;
				}
				
				//increment new option id
				++new_option_id;
			});
		}
	}
};
