/**
 * W_ShowStatsDetailPopup Class
 * @author Marco Troost
 */
var W_ShowStatsDetailPopup = new Class({
	
	/**
	 * initialize
	 * @param	string	overlay_node_id
	 * @param	string	popup_node_id
	 * @return	void
	 */
	initialize: function(root_node_id, popup_node_id)
	{
		// id's
		this.popup_node_id		= popup_node_id;
		this.root_node_id		= root_node_id;		

		this.root_node			= $(this.root_node_id);	
		this.popup_node			= $(this.popup_node_id);	
		this.close_button_node	= $('close_popup_button');
		
		// strings
		this.object_prefix		= 'object_';
		this.company_prefix		= 'company_';
		this.search_for_prefix	= 'search_for_';
		this.cid_prefix			= '#cid_';
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		if (this.root_node_id)
		{
			this.setEvents();			
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set var as object
		var _this	= this;

		// get anchors
		var anchor_nodes		= this.root_node.getElements('.show_details a');
		var total_anchor_nodes	= anchor_nodes.length;
		if (total_anchor_nodes)
		{
			anchor_nodes.each(function(anchor_node, index)
			{
				anchor_node.removeEvents();
				anchor_node.addEvents(
				{
					'click' : function()
					{
						var company_id 	= this.get('class');
						var object_id 	= this.get('id');
						var search_for 	= this.get('name');
						object_id		= object_id.replace(_this.object_prefix, '');
						company_id		= company_id.replace(_this.company_prefix, '');
						search_for 		= search_for.replace(_this.search_for_prefix, '');
						_this.loadPopup(object_id, search_for, company_id);
						return false;
					}
				});
			});
		}
	},
		
	/**
	 * load popup
	 * @return	void
	 */
	loadPopup: function(object_id, search_for, company_id)
	{
		// set vars
		var _this = this;
		var content_url = '/http/popup_stats_detail.php?search_for='+search_for+'&object_id='+object_id+'&use_company_id='+company_id;
		
		// make request
		var http_request = new Request.HTML({
			url			: content_url,
			update		: this.popup_node,
			onComplete	: function() {
				_this.orderBy();
				_this.closePopup();
			}
		});
		http_request.get();

		this.popup_node.setStyle('display', 'block');

		return false;
	},
	
	/**
	 * close popup
	 * @return	void
	 */
	closePopup: function()
	{
		// set vars
		var _this = this;

		$('close_detail_popup_button').addEvent('click',function()
		{
			$('popup_stats_detail').innerHTML = '';
			$('popup_stats_detail').setStyle('display', 'none');			
			return false;
		});
	},
	
	/**
	 * order by
	 * @return	void
	 */
	orderBy: function()
	{
		// set vars
		var _this = this;

		/* order by date */
		this.sort_branche_node	= $('stats_detail_sort_date');
		if(this.sort_branche_node)
		{
			this.sort_branche_node.removeEvents();
			this.sort_branche_node.addEvents(
			{
				'click' : function()
				{
					order_by		= 'da';
					_this.reorderPopup('stats_detail_sort_date', order_by)
				}
			});
		}
		
		/* order by branche */
		this.sort_branche_node	= $('stats_detail_sort_branch');
		if (this.sort_branche_node)
		{
			this.sort_branche_node.removeEvents();
			this.sort_branche_node.addEvents(
			{
				'click' : function()
				{
					order_by		= 'br';
					_this.reorderPopup('stats_detail_sort_branch', order_by)
				}
			});	
		}
		
		/* order by company */
		this.sort_branche_node	= $('stats_detail_sort_company');
		if (this.sort_branche_node)
		{
			this.sort_branche_node.removeEvents();
			this.sort_branche_node.addEvents(
			{
				'click' : function()
				{
					order_by		= 'co';
					_this.reorderPopup('stats_detail_sort_company', order_by)
				}
			});	
		}
		
		/* order by city */
		this.sort_branche_node	= $('stats_detail_sort_city');
		if (this.sort_branche_node)
		{
			this.sort_branche_node.removeEvents();
			this.sort_branche_node.addEvents(
			{
				'click' : function()
				{
					order_by		= 'ci';
					_this.reorderPopup('stats_detail_sort_city', order_by)
				}
			});	
		}
		
		return false;
	},
	
	/**
	 * reorder popup
	 * @return	void
	 */
	reorderPopup: function(sort_id, order_by)
	{
		// set vars
		var _this = this;
		
		this.sort_node	= $(sort_id);
		var object_id 	= this.sort_node.get('class');
		var search_for 	= this.sort_node.get('name');
		var cid 		= this.sort_node.get('href'); // use_company_id
		object_id		= object_id.replace(_this.object_prefix, '');
		search_for 		= search_for.replace(_this.search_for_prefix, '');
		cid		 		= cid.replace(_this.cid_prefix, '');
		
		var content_url = '/http/popup_stats_detail.php?search_for='+search_for+'&object_id='+object_id+'&order='+order_by+'&use_company_id='+cid;
		
		// make request
		var http_request = new Request.HTML({
			url			: content_url,
			update		: this.popup_node,
			onComplete	: function() {
				_this.orderBy();
				_this.closePopup();
			}
		});
		http_request.get();		
	}

});
