/**
 * class	ShowPopup
 * author	Marco Troost
 */
var ShowPopup = new Class({
	
	/**
	 * initialize
	 * @return	void
	 */
	initialize: function(root_node_id)
	{
		// nodes
		this.root_node			= $(root_node_id);
		this.loader_node		= $('loader_popup');
		this.popup_content_node	= $('popup_content');
		this.overlay_node		= $('overlay');
		
		this.content_url = '/modules/multishop/http/energiewaaier.php';
	
		// classes
		this.hide_class = 'hide';
	},
	
	/**
	 * start
	 * @return void
	 */
	start: function()
	{
		if (this.root_node)
		{
			// set events
			this.setEvents();
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		// set vars
		var _this = this;
		
		// read cookie
		cookie_energiewaaier = Cookie.read('energiewaaier');
		
		// check if you are redirected
		var check_link = _this.getUrl('#');
		
		// always show popup when redirect from energiecomfortwaaier.nl
		if (check_link == 'energiewaaier')
		{
			Cookie.write('energiewaaier', '1', {duration: 31});
			_this.showPopup();
		}
		else if (!cookie_energiewaaier) // on orcon.nl: only show popup when cookie is not set
		{
			Cookie.write('energiewaaier', '1', {duration: 31});
			_this.showPopup();
		}
	},
	
	/**
	 * Show popup
	 * @return	void
	 */
	showPopup: function() 
	{
		var _this		= this;
		var content_url	= this.content_url;
		
		this.overlay_node.setStyle('display', 'block');
		this.popup_content_node.setStyle('display', 'block');
		
		_this.loadPopupContent(content_url);
		
		return false;
	},
	
	/**
	 * Load popup content
	 * @return	void
	 */
	loadPopupContent: function(content_url)
	{
		var _this = this;
		
		// make request
		if (content_url)
		{
			var http_request = new Request.HTML(
			{
				url			: content_url,
				update		: this.popup_content_node,
				onRequest	: function()
				{
					// show page filter and loader
					_this.loader_node.className = '';
				},
				onComplete	: function()
				{
					// hide page filter and loader
					_this.loader_node.className = _this.hide_class;
				}
			});
			
			http_request.get();
		}
	},
	
	/**
	 * get url
	 * @return	void
	 */
	getUrl: function(key, url)
	{
		var _this = this;
		
		if (arguments.length < 2)
		{
			url =location.href;
		}
		
		if (arguments.length > 0 && key != '')
		{
			if (key == '#')
			{
				var regex = new RegExp('[#]([^$]*)');
			}
			else if (key == '?')
			{
				var regex = new RegExp('[?]([^#$]*)');
			}
			else
			{
				var regex = new RegExp('[?&]'+key+'=([^&#]*)');
			}
			
			var results = regex.exec(url);
			
			return (results == null )? '' : results[1];
		}
		else
		{
			url			= url.split('?');
			var results	= {};
			
			if (url.length > 1)
			{
				url = url[1].split('#');
				
				if (url.length > 1)
				{
					results['hash'] = url[1];
				}
				
				url[0].split('&').each(function(item,index)
				{
					item				= item.split('=');
					results[item[0]]	= item[1];
				});
			}
			
			return results;
		}
	}
});
