/**
 * class	ThumbGallery
 * author	Paul Kruijt
 */
var ThumbGallery = new Class({
	
	/**
	 * initialize
	 * @return	void
	 */
	initialize: function()
	{
		// nodes
		this.root_node	= $('work_container');
		
		// classes
		this.hide_class			= 'hide';
		this.show_class			= 'show';
		this.item_wrapper		= 'work_item';
		this.handler_class		= 'handler_thumb';
		this.projecttext_class 	= 'project_text'
		this.line_wrapper_class	= 'work_line';
		
		this.handler_thumb_active 	= 'handler_thumb active';
		this.handler_thumb_inactive = 'handler_thumb inactive';		
		
		// prefixes
		this.handler_prefix		= 'handler_thumb_';
		this.listener_prefix	= 'listener_thumb_';
	},
	
	/**
	 * start
	 * @return	void
	 */
	start: function()
	{
		if (this.root_node)
		{
			// set events
			this.setEvents();
		}
	},
	
	/**
	 * set events
	 * @return	void
	 */
	setEvents: function()
	{
		this.setHandlerEvents();
		this.displayPopup();
	},

	/**
	 * Display popup when changing the page
	 * @return	void
	 */
	displayPopup: function()
	{
		var popup = this.getUrlId("popup");
		if (popup == 1)
		{
			var display_work_node 		= $('work_container');
			var display_workbutton_node = $('work_container_right');
			
			display_work_node.setStyle('visibility' , 'visible');
			display_workbutton_node.setStyle('display' , 'block');
		}
	},
	
	/**
	 * get id from url
	 * @return	integer active_id
	 */
	getUrlId: function(variable)
	{
	       var query = window.location.search.substring(1);
	       var vars = query.split("&");
	       for (var i=0;i<vars.length;i++) {
	               var pair = vars[i].split("=");
	               if(pair[0] == variable){return pair[1];}
	       }
	       return(false);
	},	
		
	/**
	 * set handler events
	 * @return	void
	 */
	setHandlerEvents: function()
	{
		var _this				= this;
		var handler_nodes		= this.root_node.getElements('.'+this.handler_class);
		var total_handler_nodes	= handler_nodes.length;
				
		if (total_handler_nodes > 0)
		{
			handler_nodes.each(function(handler_node)
			{
				handler_node.removeEvents();
				handler_node.addEvents(
				{
					'mouseover' : function()
					{
					
						var handler_id = this.get('id');
						var handler_href = this.get('href');
						if (handler_id)
						{
							handler_id = handler_id.replace(_this.handler_prefix, '');						
							
							var listener_node = $(_this.listener_prefix + handler_id);

							if (listener_node)
							{
								// get parent of listener
								var item_wrapper_node = listener_node.getParent('.'+_this.item_wrapper);

								if (item_wrapper_node)
								{
									var active_thumb_node = item_wrapper_node.getElement('.'+_this.show_class);
															
									// hide active thumbnail
									active_thumb_node.set('class', _this.hide_class);
									
									// show thumbnail
									listener_node.set('class', _this.show_class);
															
									var project_text_node = item_wrapper_node.getElement('.'+_this.projecttext_class);
									if (project_text_node)
									{	
										// Change href
										project_text_node.set('href', handler_href);
									}
									
									// hide	active line								
									var project_line_node = item_wrapper_node.getElement('.'+_this.line_wrapper_class);
									$$('.'+_this.line_wrapper_class+' a').set('class', _this.handler_thumb_inactive);
									
									// show active line
									var handler_thumb_id = _this.handler_class+'_'+handler_id;
									$(handler_thumb_id).set('class', _this.handler_thumb_active);									
								}

							}
						}
					}
				});
			});
		}
	}	
});