/**
 * class	JH_Portfolio
 * author	Paul Kruijt
 */
var JH_Portfolio = new Class({
	
	/**
	 * initialize
	 * @return void
	 */
	initialize: function()
	{
		// nodes
		this.filter_page_node			= $('filter_page');
		this.loader_node				= $('loader');
		this.main_node					= $('main');
		this.details_node				= $('details');
		this.main_img_node				= $('main_img');
		this.logo_node					= $('logo');
		this.logo_img_node				= this.logo_node.getElement('img');
		
		this.search_navigation_node			= $('search_navigation');
		this.homepage_navigation_node		= $('homepage_navigation');
		this.page_navigation_node			= $('page_navigation');
		
		this.search_results_node			= $('search_results');
		this.homepage_results_node			= $('homepage_results');
		this.homepage_results_found_node	= $('homepage_results_found');
		this.homepage_results_total_node	= $('homepage_results_total');
		this.details_results_node			= $('details_results');
		this.details_results_found_node		= $('details_results_found');
		this.details_results_total_node		= $('details_results_total');
		
		this.details_results_previous_node	= $('details_results_previous');
		this.details_results_next_node		= $('details_results_next');
		
		this.thumbnails_node			= $('thumbnails');
		this.thumbnails_handler_node	= $('thumbnails_handler');
		
		// classes
		this.animate_class					= 'animate';
		this.thumbnails_handler_off_class	= 'thumbnails_handler_off';
		this.thumbnails_handler_on_class	= 'thumbnails_handler_on';
		this.active_class					= 'active';
		this.hide_class						= 'hide';
		
		// strings
		this.thumbnail_item_prefix	= 'thumbnail_item_';
		this.str_logo_fade_flat		= 'logo_fade_flat';
		this.str_logo_fade_back		= 'logo_fade_back';
		this.str_logo				= 'logo';
		
		// settings
		this.slider_duration		= 300;
		this.alpha_duration			= 400;
	},
	
	/**
	 * start
	 * @return void
	 */
	start: function()
	{
		if (this.details_node && this.thumbnails_node)
		{
			// settings
			this.thumbnails_pos_top_start	= parseInt(this.thumbnails_node.getStyle('top'));
			
			// set total thumbnails
			this.setTotalThumbnails();
			
			// set active thumbnail node
			var thumbnails_node			= $('thumbnails');
			this.active_thumbnail_node	= thumbnails_node.getElement('.item');
			
			// set events
			this.setEvents();
			
			// display navigation
			this.page_navigation_node.className = '';
			this.page_navigation_node.setStyle('display', 'block');
			
			this.details_results_node.className = '';
			this.details_results_node.setStyle('display', 'block');
			
			// get total items
			var total_item_nodes = this.getTotalThumbnails();
			
			// no right arrow
			if (total_item_nodes == 1) this.details_results_next_node.setStyle('visibility', 'hidden');
		}
	},
	
	/**
	 * get active photo
	 * @param	integer	photo_id
	 * @return	void
	 */
	getActivePhoto: function(photo_id)
	{
		// set object as var
		var _this					= this;
		var current_thumbnail_node	= $('thumbnail_item_'+photo_id);
		
		if (current_thumbnail_node && current_thumbnail_node != this.active_thumbnail_node)
		{
			// set active border for thumbnail
			var thumbnail_item_node				= $('thumbnail_item_'+photo_id);
			var current_thumbnail_anchor_node	= thumbnail_item_node.getElement('a');
			var active_thumbnail_anchor_node	= this.active_thumbnail_node.getElementsByTagName('a')[0];
			
			current_thumbnail_anchor_node.className	= this.active_class;
			active_thumbnail_anchor_node.className	= '';
			
			// get key of active photo
			var active_photo_key	= this.getActivePhotoKey(current_thumbnail_node);
			var active_photo_nr		= active_photo_key + 1;
			
			// set key of active photo
			this.details_results_found_node.innerHTML = active_photo_nr;
			
			// display previous / next button
			var total_item_nodes		= this.getTotalThumbnails();
			
			if (active_photo_nr > 1) this.details_results_previous_node.setStyle('visibility', 'visible');
			else this.details_results_previous_node.setStyle('visibility', 'hidden');
			
			if (active_photo_nr < total_item_nodes) this.details_results_next_node.setStyle('visibility', 'visible');
			else this.details_results_next_node.setStyle('visibility', 'hidden');
			
			// show page filter
			this.filter_page_node.className = '';
			this.loader_node.className		= '';
			
			// start alpha effect
			var alpha_effect = new Fx.Morph(this.main_img_node, {duration: this.alpha_duration, transition: Fx.Transitions.Quad.easeOut});
			
			alpha_effect.start({
			   'opacity': 0
			}).chain(function(){
				
				// slide closed
				if (_this.isSlideOpen())
				{
					_this.slide();
				}
				
				// empty content text
				_this.main_img_node.empty();
				
				var http_url = 'index.php?page_id='+photo_page_id+'&photo_id='+photo_id;
				
				var http_request = new Request({
					url:		http_url,
					method:		'get',
					onSuccess:	function(data)
					{
						alpha_effect.start({
						   'opacity': 1
						});
						
						// hide page filter and loader
						_this.filter_page_node.className	= _this.hide_class;
						_this.loader_node.className			= _this.hide_class;
						
						// set active thumbnail node
						_this.active_thumbnail_node	 = current_thumbnail_node;
						
						// set content html
						_this.main_img_node.innerHTML = data;
						
						// reset events
						_this.setEvents();
					}
				});
				
				http_request.send();
			});
		}
	},
	
	/**
	 * get previous photo
	 * @return	void
	 */
	getPreviousPhoto: function()
	{
		// get item nodes
		var item_nodes			= $$('#thumbnails .item');
		var total_item_nodes	= item_nodes.length;
		
		if (total_item_nodes > 0)
		{
			// set vars
			var previous_photo_id	= 0;
			
			for (var a=0; a<total_item_nodes; a++)
			{
				var item_node			= item_nodes[a];
				var previous_item_node	= item_nodes[a-1];
				
				if (previous_item_node)
				{
					var item_node_id		= item_node.id;
					var arr_item_node_id	= item_node_id.split(this.thumbnail_item_prefix);
					var photo_id			= arr_item_node_id[1];
					
					var previous_item_node_id		= previous_item_node.id;
					var arr_previous_item_node_id	= previous_item_node_id.split(this.thumbnail_item_prefix);
					var previous_photo_id			= arr_previous_item_node_id[1];
					
					var thumbnail_item_node	= $('thumbnail_item_'+photo_id);
					var item_anchor_node	= thumbnail_item_node.getElement('a');
					
					if (item_anchor_node)
					{
						if (item_anchor_node.className == this.active_class)
						{
							this.getActivePhoto(previous_photo_id);
							
							break;
						}
					}
				}
			}
		}
	},
	
	/**
	 * get next photo
	 * @return	void
	 */
	getNextPhoto: function()
	{
		// get item nodes
		var item_nodes			= $$('#thumbnails .item');
		var total_item_nodes	= item_nodes.length;
		
		if (total_item_nodes > 0)
		{
			// set vars
			var next_node		= 0;
			var first_photo_id	= 0;
			
			var i = 1;
			for (var a=0; a<total_item_nodes; a++)
			{
				var item_node			= item_nodes[a];
				var item_node_id		= item_node.id;
				var arr_item_node_id	= item_node_id.split(this.thumbnail_item_prefix);
				var photo_id			= arr_item_node_id[1];
				
				// first thumbnail
				if (i==1) first_photo_id = photo_id;
				
				var thumbnail_item_node	= $('thumbnail_item_'+photo_id);
				var item_anchor_node	= thumbnail_item_node.getElement('a');
				
				if (item_anchor_node)
				{
					if (next_node == 1)
					{
						this.getActivePhoto(photo_id);
						
						break;
					}
					
					if (item_anchor_node.className == this.active_class) next_node = 1;
				}
				
				i++;
			}
			
			// goto first item
			if (i == (total_item_nodes + 1))
			{
				this.getActivePhoto(first_photo_id);
			}
		}
	},
	
	/**
	 * get active photo key
	 * @param	object	thumbnail_node
	 * @return	integer key
	 */
	getActivePhotoKey: function(thumbnail_node)
	{
		var key = 0;
		
		// get item nodes
		var item_nodes			= $$('#thumbnails .item');
		var total_item_nodes	= item_nodes.length;
		
		if (total_item_nodes > 0)
		{
			for (var a=0; a<total_item_nodes; a++)
			{
				var item_node	= item_nodes[a];
				
				if (item_node == thumbnail_node)
				{
					key = a;
					
					break;
				}
			}
		}
		
		return key;
	},
	
	/**
	 * get total thumbnails
	 * @return integer	total_item_nodes
	 */
	getTotalThumbnails: function()
	{
		// get item nodes
		var item_nodes			= $$('#thumbnails .item');
		var total_item_nodes	= item_nodes.length;
		
		return total_item_nodes;
	},
	
	/**
	 * set total thumbnails
	 * @return void
	 */
	setTotalThumbnails: function()
	{
		// get total item nodes
		var total_item_nodes	= this.getTotalThumbnails();
		
		// set total
		this.details_results_total_node.innerHTML = total_item_nodes;
		
		// display total
		this.details_results_node.className = '';
	},
	
	/**
	 * set events
	 * @return void
	 */
	setEvents: function()
	{
		// set object as var
		var _this	= this;
		
		// handler
		if (this.thumbnails_handler_node)
		{
			this.thumbnails_node.onclick = function()
			{
				// check if search is open
				if (search && search.isSlideOpen())
				{
					search.slide();
				}
				
				// check if slider is open
				if (slider && slider.isSlideOpen())
				{
					slider.slideMenu();
				}
				
				// slide thumbnails
				_this.slide();
				
			}			
		}
		
		// get item nodes
		var item_nodes			= $$('#thumbnails .item');
		var total_item_nodes	= item_nodes.length;
		
		if (total_item_nodes > 0)
		{
			for (var a=0; a<total_item_nodes; a++)
			{
				var item_node	= item_nodes[a];
				
				item_node.onclick = function()
				{
					// check if search is open
					if (search && search.isSlideOpen())
					{
						search.slide();
					}
					
					// check if slider is open
					if (slider && slider.isSlideOpen())
					{
						slider.slideMenu();
					}
					
					var item_node_id		= this.id;
					var arr_item_node_id	= item_node_id.split(_this.thumbnail_item_prefix);
					var photo_id			= arr_item_node_id[1];
					
					// get active photo
					_this.getActivePhoto(photo_id);
					
					return false;
				}
			}
		}
		
		// main image
		var main_image_root_node	= $('main_img');
		var main_image_node			= main_image_root_node.getElement('img');
		
		if (main_image_node)
		{
			main_image_node.onclick = function()
			{
				// check if search is open
				if (search && search.isSlideOpen())
				{
					search.slide();
				}
				
				// check if slider is open
				if (slider && slider.isSlideOpen())
				{
					slider.slideMenu();
				}
				
				// get next photo
				_this.getNextPhoto();
			}
		}
		
		if (this.details_results_previous_node)
		{
			this.details_results_previous_node.onclick = function()
			{
				// check if search is open
				if (search && search.isSlideOpen())
				{
					search.slide();
				}
				
				// check if slider is open
				if (slider && slider.isSlideOpen())
				{
					slider.slideMenu();
				}
				
				// get previous photo
				_this.getPreviousPhoto();
			}
		}
		
		if (this.details_results_next_node)
		{
			this.details_results_next_node.onclick = function()
			{
				// check if search is open
				if (search && search.isSlideOpen())
				{
					search.slide();
				}
				
				// check if slider is open
				if (slider && slider.isSlideOpen())
				{
					slider.slideMenu();
				}
				
				// get next photo
				_this.getNextPhoto();
			}
		}
		
		// back button
		var page_navigation_node	= $('page_navigation');
		var page_back_anchor_node	= page_navigation_node.getElement('a');
		
		if (page_back_anchor_node)
		{
			page_back_anchor_node.onclick = function()
			{
				/*
				// check if slider is open
				if (slider && slider.isSlideOpen())
				{
					slider.slideMenu();
				}
				
				// display navigation
				_this.page_navigation_node.className = _this.hide_class;
				_this.page_navigation_node.setStyle('display', 'none');
				
				_this.details_results_node.className = _this.hide_class;
				_this.details_results_node.setStyle('display', 'none');
				
				// send form (go to global search page number)
				//search.sendForm(search_page_nr);
				*/
				
				// go back
				document.location = 'index.php?page_id='+home_page_id;
				
				return false;
			}
		}
		
	},
	
	/**
	 * slide thumbnail wrapper in or out
	 * @return void
	 */
	slide: function()
	{
		// set object as var
		var _this	= this;
		
		// get coordinates of search
		var thumbnails_coordinates	= this.thumbnails_node.getCoordinates();
		var thumbnails_height		= thumbnails_coordinates.height;
		var thumbnails_top_start	= thumbnails_coordinates.top;
		var thumbnails_top_end		= thumbnails_top_start < this.thumbnails_pos_top_start ? this.thumbnails_pos_top_start : parseInt((this.thumbnails_pos_top_start - thumbnails_height));
		
		// set indicator
		var thumbnails_handler_class	= thumbnails_top_start < this.thumbnails_pos_top_start ? this.thumbnails_handler_off_class : this.thumbnails_handler_on_class;
		
		// set logo
		var logo_file_old	= thumbnails_top_start < this.thumbnails_pos_top_start ? this.str_logo_fade_back : this.str_logo_fade_flat;
		var logo_file_new	= thumbnails_top_start < this.thumbnails_pos_top_start ? this.str_logo_fade_flat : this.str_logo_fade_back;
		
		// start effect
		var thumbnails_effect = new Fx.Morph(this.thumbnails_node, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeOut});
 
		thumbnails_effect.start({
		    'top': [thumbnails_top_start, thumbnails_top_end]
		}).chain(function(){
			
			_this.thumbnails_handler_node.className = thumbnails_handler_class;
			
			// set new logo
			if (_this.logo_img_node.className == _this.animate_class && logo_file_old != logo_file_new)
			{
				var logo_src_new = _this.logo_img_node.src.replace(logo_file_old, logo_file_new);
				_this.logo_img_node.src = logo_src_new;
			}
			
		});
	},
	
	/**
	 * check if slide thumbnails is open
	 * @return boolean
	 */
	isSlideOpen: function()
	{
		var thumbnails_node = $('thumbnails');
		
		// get coordinates of thumbnails
		if (thumbnails_node)
		{
			var thumbnails_coordinates	= thumbnails_node.getCoordinates();
			var thumbnails_top_start	= thumbnails_coordinates.top;
			
			if (thumbnails_top_start < this.thumbnails_pos_top_start) return true;
			else return false;
		}
	}
	
});