/**
 * class	JH_Slider
 * author	Paul Kruijt
 */
var JH_Slider = new Class({
	
	/**
	 * initialize
	 * @return void
	 */
	initialize: function()
	{
		// nodes
		this.body_node					= document.getElement('body');
		this.filter_page_node				= $('filter_page');
		this.filter_capture_this_node			= $('filter_capture_this');
		this.loader_node					= $('loader');
		this.page_capture_this_node		= $('page_capture_this');
		this.slider_node					= $('slider');
		this.menu_node					= $('menu');
		this.logo_node					= $('logo');
		this.menu_handler_node			= $('menu_handler');
		this.menu_indicator_node			= $('menu_indicator');
		this.content_text_node				= $('content_text');
		this.book_node					= $('book');
		this.book_form_node				= $('book_form');
		this.book_preview_button_node		= $('book_preview_button');
		this.capture_this_node			= $('capture_this');
		this.capture_this_on_node			= $('capture_this_on');
		this.logo_node					= $('logo');
		this.info_logo_node				= $('info_logo');
		this.logo_img_node				= this.logo_node.getElement('img');
		
		// id's
		this.menu_id			= 'menu';
		this.menu_handler_id		= 'menu_handler';
		this.menu_indicator_id		= 'menu_indicator';
		
		// classes
		this.animate_class			= 'animate';
		this.menu_indicator_off_class	= 'menu_indicator_off';
		this.menu_indicator_on_class	= 'menu_indicator_on';
		this.hide_class				= 'hide';
		
		// strings
		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;
		this.focus_duration		= 500;
	},
	
	/**
	 * start
	 * @return void
	 */
	start: function()
	{
		if (this.slider_node && this.menu_handler_node && this.menu_indicator_node && this.content_text_node)
		{
			// settings
			this.slider_pos_left_start			= parseInt(this.slider_node.getStyle('left'));
			this.content_text_pos_left_start	= parseInt(this.content_text_node.getStyle('left'));
			
			// set events
			this.setEvents();
		}
	},
	
	/**
	 * set events
	 * @return void
	 */
	setEvents: function()
	{
		// set object as var
		var _this	= this;
		
		if (this.logo_node)
		{
			this.logo_node.onclick = function()
			{
				// check if search is open
				if (search && search.isSlideOpen())
				{
					search.slide();
				}
				
				// check if portfolio is open
				if (portfolio && portfolio.isSlideOpen())
				{
					portfolio.slide();
				}
				
				// slide menu
				_this.slideMenu();
			}
		}
		
		if (this.info_logo_node)
		{
			this.info_logo_node.onclick = function()
			{
				// check if search is open
				if (search && search.isSlideOpen())
				{
					search.slide();
				}
				
				// check if portfolio is open
				if (portfolio && portfolio.isSlideOpen())
				{
					portfolio.slide();
				}
				
				// slide menu
				_this.slideMenu();
			}
		}
		
		this.menu_handler_node.onclick = function()
		{
			// check if search is open
			if (search && search.isSlideOpen())
			{
				search.slide();
			}
			
			// check if portfolio is open
			if (portfolio && portfolio.isSlideOpen())
			{
				portfolio.slide();
			}
			
			// slide menu
			_this.slideMenu();
		}
		
		this.menu_indicator_node.onclick = function()
		{
			// check if search is open
			if (search && search.isSlideOpen())
			{
				search.slide();
			}
			
			// check if portfolio is open
			if (portfolio && portfolio.isSlideOpen())
			{
				portfolio.slide();
			}
			
			// slide menu
			_this.slideMenu();
		}
		
		// menu items
		var menu_item_nodes			= $$('#'+this.menu_id+' a');
		var total_menu_item_nodes	= menu_item_nodes.length;
		
		if (total_menu_item_nodes > 0)
		{
			for (var a=0; a<total_menu_item_nodes; a++)
			{
				var menu_item_node	= menu_item_nodes[a];
				
				menu_item_node.onclick = function()
				{
					if (this.className == 'use_js')
					{
						_this.focusMenuItem(this);
						
						return false;
					}
				}
			}
		}
		
		// book preview
		if (this.book_preview_button_node && this.book_node)
		{
			this.book_preview_button_node.addEvent('click', function(event)
			{
				event.stopPropagation();
				
				if (_this.book_form_node && !_this.book_form_node.hasClass('hide'))
				{
					_this.book_form_node.addClass('hide');
				}
				else
				{
					_this.book_node.toggleClass('hide');
				}
			});
		}
		
		// capture this
		if (this.capture_this_node && this.filter_capture_this_node && this.page_capture_this_node)
		{
			this.capture_this_node.onclick = function()
			{
				_this.filter_capture_this_node.className	= '';
				_this.page_capture_this_node.className		= '';
			}
		}
		
		// capture this on
		if (this.capture_this_on_node && this.filter_capture_this_node)
		{
			this.capture_this_on_node.onclick = function()
			{
				_this.filter_capture_this_node.className	= _this.hide_class;
				_this.page_capture_this_node.className		= _this.hide_class;
			}
		}
	},
	
	/**
	 * slide menu in or out
	 * @return void
	 */
	slideMenu: function()
	{
		// set object as var
		var _this	= this;
		
		// get coordinates of menu (parts)
		var menu_width			= parseInt(this.menu_node.getStyle('width'));
		var menu_left			= parseInt(this.menu_node.getStyle('left'));
		var menu_handler_width	= parseInt(this.menu_handler_node.getStyle('width'));
		var slider_left_start	= parseInt(this.slider_node.getStyle('left'));
		var slider_left_end		= slider_left_start < this.slider_pos_left_start ? this.slider_pos_left_start : parseInt((slider_left_start - (menu_width + menu_left) + menu_handler_width));
		
		// set indicator
		var menu_indicator_class		= slider_left_start < this.slider_pos_left_start ? this.menu_indicator_off_class : this.menu_indicator_on_class;
		var book_preview_button_opacity	= slider_left_start < this.slider_pos_left_start ? 1 : 0;
		var capture_this_opacity		= slider_left_start < this.slider_pos_left_start ? 1 : 0;
		
		// set logo
		var logo_file_old	= slider_left_start < this.slider_pos_left_start ? this.str_logo_fade_back : this.str_logo_fade_flat;
		var logo_file_new	= slider_left_start < this.slider_pos_left_start ? this.str_logo_fade_flat : this.str_logo_fade_back;
		
		// show page filter
		this.filter_page_node.className = '';
		
		// start effect
		var slider_effect = new Fx.Morph(this.slider_node, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeOut});
		
		slider_effect.start(
		{
			'left': [slider_left_start, slider_left_end]
		})
		.chain(function()
		{
			// empty content text
			_this.content_text_node.empty();
			
			// switch indicator
			_this.menu_indicator_node.className = menu_indicator_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;
			}
			
			if (_this.book_preview_button_node && _this.capture_this_node)
			{

				var alpha_effect_1	= new Fx.Morph(_this.book_preview_button_node, {duration: _this.alpha_duration, transition: Fx.Transitions.Quad.easeOut});
				var alpha_effect_2	= new Fx.Morph(_this.capture_this_node, {duration: _this.alpha_duration, transition: Fx.Transitions.Quad.easeOut});

				alpha_effect_1.start(
				{
					'opacity': book_preview_button_opacity
				});

				alpha_effect_2.start(
				{
					'opacity': capture_this_opacity
				})
				.chain(function()
				{
					if (slider_left_start < _this.slider_pos_left_start)
					{
						_this.focusMenuItemDefault();
					}

					// hide page filter
					_this.filter_page_node.className = _this.hide_class;
				});
			}
			else
			{
				if (slider_left_start < _this.slider_pos_left_start)
				{
					_this.focusMenuItemDefault();
				}
				
				// hide page filter
				_this.filter_page_node.className = _this.hide_class;
			}
		});
	},
	
	/**
	 * focus menu item
	 * @param	object anchor_node
	 * @return	void
	 */
	focusMenuItem: function(anchor_node)
	{
		var _this		= this;
		var http_url	= anchor_node.href;
		var img_node	= anchor_node.getElement('img');
		var page_name	= img_node.alt;
		
		// get coordinates of menu indicator
		var menu_indicator_coordinates	= this.menu_indicator_node.getCoordinates();
		var menu_indicator_pos_top		= menu_indicator_coordinates.top;
		
		// get coordinates of menu
		var slider_top			= parseInt(this.slider_node.getStyle('top'));
		var menu_coordinates	= this.menu_node.getCoordinates();
		var menu_pos_top		= menu_coordinates.top - slider_top;
		
		// get coordinates of menu indicator
		var menu_item_coordinates	= anchor_node.getCoordinates();
		var menu_item_pos_top		= menu_item_coordinates.top;
		
		// set adjustment (macintosh check)
		var menu_top_adjustment	= navigator.userAgent.toLowerCase().indexOf("mac") != -1 ? 1 : 2;
		
		// set end position
		var menu_top_diff		= parseInt(menu_item_pos_top - menu_pos_top);
		var menu_top_end		= menu_indicator_pos_top - menu_top_diff - menu_top_adjustment;
		
		// show page filter
		this.filter_page_node.className = '';
		
		// start slide effect
		var slide_effect = new Fx.Morph(this.menu_node, {duration: this.focus_duration, transition: Fx.Transitions.Quad.easeOut});
 		
		slide_effect.start({
		    'top': [menu_pos_top, menu_top_end]
		}).chain(function(){
			
			// slide content text
			_this.slideContentText(http_url, page_name);
			
		});
	},
	
	/**
	 * focus menu item default
	 * @return	void
	 */
	focusMenuItemDefault: function()
	{
		// menu items
		var menu_item_nodes			= $$('#'+this.menu_id+' a');
		var total_menu_item_nodes	= menu_item_nodes.length;
		
		if (total_menu_item_nodes > 0)
		{
			var anchor_node	= menu_item_nodes[0];
			
			var _this		= this;
			var http_url	= anchor_node.href;
			
			// get coordinates of menu indicator
			var menu_indicator_coordinates	= this.menu_indicator_node.getCoordinates();
			var menu_indicator_pos_top		= menu_indicator_coordinates.top;
			
			// get coordinates of menu
			var slider_top			= parseInt(this.slider_node.getStyle('top'));
			var menu_coordinates	= this.menu_node.getCoordinates();
			var menu_pos_top		= menu_coordinates.top - slider_top;
			
			// get coordinates of menu indicator
			var menu_item_coordinates	= anchor_node.getCoordinates();
			var menu_item_pos_top		= menu_item_coordinates.top;
			
			// set adjustment (macintosh check)
			var menu_top_adjustment	= navigator.userAgent.toLowerCase().indexOf("mac") != -1 ? 1 : 2;
			
			// set end position
			var menu_top_diff		= parseInt(menu_item_pos_top - menu_pos_top);
			var menu_top_end		= menu_indicator_pos_top - menu_top_diff - menu_top_adjustment;
			
			// start slide effect
			var slide_effect = new Fx.Morph(this.menu_node, {duration: this.focus_duration, transition: Fx.Transitions.Quad.easeOut});
	 		
			slide_effect.start({'top': [menu_pos_top, menu_top_end]});
		}
	},
	
	/**
	 * slide content text
	 * @param	string	http_url
	 * @param	string	page_name
	 * @return	void
	 */
	slideContentText: function(http_url, page_name)
	{
		// set object as var
		var _this	= this;
		
		// get coordinates of menu
		var menu_width			= parseInt(this.menu_node.getStyle('width'));
		var menu_left			= parseInt(this.menu_node.getStyle('left'));
		var menu_handler_width	= parseInt(this.menu_handler_node.getStyle('width'));
		
		switch (page_name)
		{
			// login
			case 'Login':
				this.slider_node.setStyle('width', 1010+'px');
				this.content_text_node.setStyle('width', 840+'px');
			break;
			
			// default
			default:
				//this.slider_node.setStyle('width', 550+'px');
				this.content_text_node.setStyle('width', 380+'px');
			break;
		}
		
		// get coordinates of slider
		var content_text_width	= parseInt(this.content_text_node.getStyle('width'));
		var slider_left_start	= parseInt(this.slider_node.getStyle('left'));
		var slider_left_current	= this.slider_pos_left_start - (menu_width + menu_left) + menu_handler_width;
		var slider_left_end		= parseInt((slider_left_current - content_text_width));
		
		// start effect
		if (slider_left_start != slider_left_end)
		{
			var slider_effect = new Fx.Morph(this.slider_node, {duration: this.slider_duration, transition: Fx.Transitions.Quad.easeOut});
 			
			slider_effect.start({'left': [slider_left_start, slider_left_end]});
		}
		
		// start alpha effect
		var alpha_effect = new Fx.Morph(this.content_text_node, {duration: this.alpha_duration, transition: Fx.Transitions.Quad.easeOut});
		
		alpha_effect.start({
		   'opacity': 0
		}).chain(function(){
			
			// empty content text
			_this.content_text_node.empty();
			
			// get content text
			_this.getContentText(http_url);
			
		});
	},
	
	/**
	 * get content text
	 * @param	string	http_url
	 * @return	void
	 */
	getContentText: function(http_url)
	{
		// set object as var
		var _this	= this;
		var timer_loader;
		
		var http_request = new Request.HTML({
			url			: http_url,
			update		: this.content_text_node,
			onRequest	: function()
			{
				// show loader with timer
				timer_loader = setTimeout(function() {_this.loader_node.className = '';}, 1000);
			},
			onSuccess	: function(data)
			{
				// start alpha effect
				var alpha_effect = new Fx.Morph(_this.content_text_node, {duration: _this.alpha_duration, transition: Fx.Transitions.Quad.easeOut});
				
				alpha_effect.start({
				   'opacity': 1
				});
				
				// hide page filter and loader
				_this.filter_page_node.className	= _this.hide_class;
				
				// clear timer
				clearTimeout(timer_loader);
				 
				 // hide loader
				_this.loader_node.className			= _this.hide_class;
				
				// set events for inpage editing (framework)
				if (window.page_object_handler)
				{
					page_object_handler.setEvents();
				}
				
				// check for form on content page, and set events
				var jh_form	= new JH_Form();
				jh_form.setEvents();
			}
		});
		
		http_request.get();
	},
	
	/**
	 * check if slide is open
	 * @return boolean
	 */
	isSlideOpen: function()
	{
		var slider_node = $('slider');
		
		// get coordinates of slider
		if (slider_node)
		{
			var slider_left_start	= parseInt(this.slider_node.getStyle('left'));
			
			if (slider_left_start < this.slider_pos_left_start) return true;
			else return false;
		}
	}
	
});
