/**
 * CS_PopupOrderProductOption Class
 * @author Paul Kruijt
 */
var CS_PopupOrderProductOption = new Class({
	
	/**
	 * initialize
	 * @param	integer	product_id
	 * @param	integer	option_group_id
	 * @param	array	arr_display_listener
	 * @param	integer	minimal_amount
	 * @return	void
	 */
	initialize: function(product_id, option_group_id, arr_display_listener, minimal_amount)
	{
		// nodes
		this.document_node = document.getElement('body');
		
		// id's
		this.filter_id							= 'cs_filter_page';
		this.popup_wrapper_id					= 'cs_popup_order_product_option_wrapper';
		this.popup_id							= 'cs_popup_order_product_option';
		this.popup_content_wrapper_id			= 'cs_popup_order_product_option_content_wrapper';
		this.popup_content_id					= 'cs_popup_order_product_option_content';
		this.popup_content_header_wrapper_id	= 'cs_popup_order_product_option_header_wrapper';
		this.popup_content_main_wrapper_id		= 'cs_popup_order_product_option_main_wrapper';
		this.popup_content_footer_wrapper_id	= 'cs_popup_order_product_option_footer_wrapper';
		this.popup_content_close_window_id		= 'cs_popup_order_product_option_close_window';
		this.add_to_cart_button_id				= 'cs_popup_order_product_option_add_to_cart';
		this.option_total_price_id				= 'cs_popup_order_product_option_total_price';
		this.form_order_product_option_id		= 'cs_form_order_product_option';
		this.loader_id							= 'cs_popup_loader';
		
		// classes
		this.show_class					= 'cs_show';
		this.hide_class					= 'cs_hide';
		this.checkbox_class				= 'cs_checkbox';
		this.option_checker_class		= 'cs_product_option_checker';
		this.option_radio_checker_class	= 'cs_product_option_radio_checker';
		this.option_amount_class		= 'cs_product_option_amount';
		
		// integers
		this.minimal_amount		= !minimal_amount ? 1 : minimal_amount;
		this.product_id			= product_id;
		this.option_group_id	= option_group_id;
		
		// arrays
		this.arr_display_listener	= arr_display_listener;
		
		// strings
		this.loader_url				= 'components/multishop/html/default/img/icon_loading.gif';
		this.http_content_url		= 'components/multishop/http/getProductOptions.php';
		
		// prefixes
		this.option_checker_prefix	= 'cs_product_option_checker_';
		this.option_amount_prefix	= 'cs_product_option_amount_';
		
		// settings
		this.max_popup_height			= 500;
		this.popup_height_adjustment	= 4;
		this.slider_duration			= 500;
		this.alpha_duration				= 500;
		this.slider_timer				= 2000;
	},
	
	/**
	 * create
	 * @return void
	 */
	create: function()
	{
		// create nodes
		this.filter	= new Element('div', {
			'id' : this.filter_id
		});
		
		this.popup_wrapper	= new Element('div', {
			'id' : this.popup_wrapper_id
		});
		
		this.popup	= new Element('div', {
			'id' : this.popup_id
		});
		
		this.popup_content_wrapper	= new Element('div', {
			'id' : this.popup_content_wrapper_id
		});
		
		this.popup_content	= new Element('div', {
			'id' : this.popup_content_id
		});
		
		this.loader	= new Element('img', {
			'id'	: this.loader_id,
			'src'	: this.loader_url
		});
		
		// inject nodes
		this.filter.inject(this.document_node);
		this.popup_wrapper.inject(this.document_node);
		this.popup.inject(this.popup_wrapper);
		this.popup_content_wrapper.inject(this.popup);
		this.popup_content.inject(this.popup_content_wrapper);
		this.loader.inject(this.popup_content_wrapper);
		
		// get content
		this.getContent();
	},
	
	/**
	 * get content
	 * @return void
	 */
	getContent: function()
	{
		// set vars
		var _this	= this;
		
		// make request
		if (this.popup_content)
		{
			var obj_params	= {
				'product_id' 		: this.product_id,
				'option_group_id'	: this.option_group_id
			}
			
			var http_request = new Request.HTML({
				url			: this.http_content_url,
				update		: this.popup_content,
				data		: obj_params,
				onComplete	: function()
				{
					// remove loader
					if (_this.loader) _this.loader.dispose();
					
					// toggler events
					var node_toggler = new CS_NodeToggler('cs_form_order_product_option', _this.arr_display_listener);
					node_toggler.openAll();
					
					// position popup
					_this.position();
					
					node_toggler.start();
					
					// set events
					_this.setEvents();
				}
			});
			
			http_request.get();
		}
	},
	
	/**
	 * set events
	 * @return void
	 */
	setEvents: function()
	{
		// set vars
		var _this						= this;
		var content_main_wrapper		= $(this.popup_content_main_wrapper_id);
		var add_to_cart_button			= $(this.add_to_cart_button_id);
		var form_order_product_option	= $(this.form_order_product_option_id);
		
		if (content_main_wrapper)
		{
			var option_checkers			= content_main_wrapper.getElements('.'+this.option_checker_class);
			var total_option_checkers	= option_checkers.length;
			
			if (total_option_checkers > 0)
			{
				option_checkers.each(function(option_checker, index)
				{
					option_checker.addEvents(
					{
						'click' : function()
						{
							_this.toggleInputAmount(this);
						}
					});
				});
			}
			
			var option_radio_checkers		= content_main_wrapper.getElements('.'+this.option_radio_checker_class);
			var total_option_radio_checkers	= option_radio_checkers.length;
			var add_to_cart_button			= $(this.add_to_cart_button_id);
			
			if (total_option_radio_checkers > 0)
			{
				option_radio_checkers.each(function(option_radio_checker, index)
				{
					option_radio_checker.addEvents(
					{
						'click' : function()
						{
							if (add_to_cart_button) add_to_cart_button.set('class', _this.show_class);
						}
					});
				});
			}
		}
		
		// close window event
		if ($(this.popup_content_close_window_id))
		{
			$(this.popup_content_close_window_id).addEvents(
			{
				'click' : function()
				{
					_this.remove();
					
					return false;
				},
				
				'focus' : function()
				{
					this.blur();
				}
			});
		}
		
		// submit button
		if (add_to_cart_button)
		{
			var add_to_cart_anchor	= add_to_cart_button.getElement('a');
			
			add_to_cart_anchor.addEvents(
			{
				'click' : function()
				{
					// submit form
					if (form_order_product_option) form_order_product_option.submit();
					
					// close popup
					_this.remove();
					
					return false;
				},
				
				'focus' : function()
				{
					this.blur();
				}
			});
		}
	},
	
	/**
	 * position
	 * @return void
	 */
	position: function()
	{
		// set vars
		var _this = this;
		
		if (this.popup_wrapper && this.popup_content)
		{
			// get coordinates
			var popup_wrapper_margin_top_start	= this.popup_wrapper.getStyle('margin-top').toInt();
			var popup_content_wrapper_height	= this.popup_content_wrapper.getStyle('height').toInt();
			var popup_content_coordinates		= this.popup_content.getCoordinates();
			var popup_content_height_original	= popup_content_coordinates.height.toInt();
			var popup_content_height			= popup_content_height_original > this.max_popup_height ? this.max_popup_height : popup_content_height_original;
			var popup_wrapper_margin_top_end	= 0 - (popup_content_height / 2).round().toInt();
			
			// start effects
			var tween_effect = new Fx.Morph(this.popup_wrapper, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeOut});
			tween_effect.start({'margin-top': [popup_wrapper_margin_top_start, popup_wrapper_margin_top_end]});
			
			var tween_effect = new Fx.Morph(this.popup_content_wrapper, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeOut});
			tween_effect.start({'height': [popup_content_wrapper_height, popup_content_height]});
			
			var tween_effect = new Fx.Morph(this.popup_content, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeOut});
			tween_effect.start({'height': [popup_content_wrapper_height, popup_content_height]}).chain(function()
			{
				_this.popup_content.setStyles(
				{
					'opacity'		: 0,
					'visibility'	: 'visible',
					'overflow'		: 'hidden'
				});
				
				// set height main content node
				//if (popup_content_height_original > _this.max_popup_height)
				//{
					// get coordinates
					var popup_content_header_wrapper_coordinates	= $(_this.popup_content_header_wrapper_id).getCoordinates();
					var popup_content_header_wrapper_height			= popup_content_header_wrapper_coordinates.height.toInt();
					var popup_content_footer_wrapper_coordinates	= $(_this.popup_content_footer_wrapper_id).getCoordinates();
					var popup_content_footer_wrapper_height			= popup_content_footer_wrapper_coordinates.height.toInt();
					
					var popup_content_header_wrapper	= $(_this.popup_content_header_wrapper_id);
					var popup_content_main_wrapper		= $(_this.popup_content_main_wrapper_id);
					var popup_content_footer_wrapper	= $(_this.popup_content_footer_wrapper_id);
					
					if (popup_content_header_wrapper && popup_content_main_wrapper && popup_content_footer_wrapper)
					{
						popup_content_main_wrapper.setStyles(
						{
							'height'	: (popup_content_height - (popup_content_header_wrapper_height + popup_content_footer_wrapper_height + 2)) + 'px',
							'overflow'	: 'auto'
						});
					}
				//}
				
				var alpha_effect = new Fx.Morph(_this.popup_content, {duration: _this.alpha_duration, transition: Fx.Transitions.Quad.easeOut});
				alpha_effect.start({'opacity': [0, 1]});
			});
		}
	},
	
	/**
	 * remove
	 * @return void
	 */
	remove: function()
	{
		if (this.filter) this.filter.dispose();
		if (this.popup_wrapper) this.popup_wrapper.dispose();
	},
	
	/**
	 * toggle input amount
	 * @param	object	obj
	 * @return	void
	 */
	toggleInputAmount: function(obj)
	{
		var _this			= this;
		var arr_handler		= obj.id.split('_');
		var handler_id		= arr_handler[arr_handler.length - 1];
		var input_amount	= $(this.option_amount_prefix + handler_id);
		
		// enable input
		if (input_amount.disabled)
		{
			input_amount.disabled = false;
			
			input_amount.value	= _this.minimal_amount;
			
			input_amount.focus();
			input_amount.select();
			
			input_amount.addEvents(
			{
				'blur' : function()
				{
					if (!_this.checkValidNumber(this.value) || this.value < _this.minimal_amount)
					{
						this.value = _this.minimal_amount;
					}
				}
			});
		}
		
		// disable input
		else
		{
			input_amount.blur();
			input_amount.value		= 0;
			input_amount.disabled	= true;
		}
		
		// toggle add to cart button
		this.toggleAddToCartButton();
	},
	
	/**
	 * check valid number
	 * @param	object	obj
	 * @return	void
	 */
	checkValidNumber: function(val)
	{
		var result = null;
		
		// For only positive whole numbers, including zero, uncomment the following:  
		result = val.match(/^\d+$/);
		
		// For only positive decimal numbers, including zero, uncomment the following:  
		// result = val.match(/^\d+(\.\d+)?$/);
		
		// For positive and negative decimal numbers, including zero, uncomment the following:  
		// result = val.match(/^-?\d+(\.\d+)?$/);
		
		// For positive and negative numbers, including zero, uncomment the following:  
		// result = val.match(/^-?\d+$/);
		
		return (result != null);
	},
	
	/**
	 * toggle add to cart button
	 * @return void
	 */
	toggleAddToCartButton: function()
	{
		// set vars
		var add_to_cart_button		= $(this.add_to_cart_button_id);
		var option_total_price		= $(this.option_total_price_id);
		var content_main_wrapper	= $(this.popup_content_main_wrapper_id);
		
		if (content_main_wrapper)
		{
			// hide button
			if (add_to_cart_button) add_to_cart_button.set('class', this.hide_class);
			//if (option_total_price) option_total_price.set('class', this.hide_class);
			
			var option_checkers			= content_main_wrapper.getElements('.'+this.option_checker_class);
			var total_option_checkers	= option_checkers.length;
			
			for (var a=0; a<total_option_checkers; a++)
			{
				var option_checker = option_checkers[a];
				
				if (option_checker.checked)
				{
					if (add_to_cart_button) add_to_cart_button.set('class', this.show_class);
					//if (option_total_price) option_total_price.set('class', this.show_class);
					break;
				}
			}
		}
	}
});
