// JavaScript Document
	
// objects of type posts;

		
// social
	var Body =  Class.create (
	{
		initialize: function(_doc, _extended_body)
		{
			this._doc = _doc;
			this._init();
		},
		
		_init :  function (_specific_)
		{
			_all = (_specific_) ? false : true;
			
			// links
			if ( _specific_ == 'links' || _all ) this._doc.select('a').each ( function (_link) { this._link_actions (_link) }, this );
					
			// forms
			if ( _specific_ == 'forms' || _all ) this._doc.select('form').each ( function (_form) { this._form_actions (_form) }, this );
			
			
			// extra extension;
				
			if ( _all )	Object.extend(this, _extended_body);
		},
		
		_form_actions : function (_form)
		{
			// auto_height
				_form.select ('textarea.auto_resize_height').each
				(
					function (_textarea, _index)
					{	
						var _textarea_txt = _textarea;
						var _textarea_height = _textarea_txt.rows-1;
				
						_textarea.onkeydown = function (_e)
						{	
							c = 0; r = 2; t = _textarea_txt.value;
							
							for(var i=0; i<t.length; i++) 
							{
								if (t.substr(i,1) == '\n') {
									c = 0;
									r++;
								} else {
									c++;
									if(c >= _textarea_txt.cols) { c=0; r++; }
								}
							}
							
							if (r>_textarea_height){
								_textarea_txt.rows = r;				
							}
						}
					}
				)
				
			// submit	
				if (_form.hasClassName('no_form_submit_magic') == false)
				{
					_form.onsubmit = function (_e)
					{
						_valid = true;
						
						_process_html  = '<dl class="clearfix submit_spin_icon warn inform"><dt class="title"><b>un</b> <span>momento</span></dt>'
						_process_html += '<dd class="story"><p>processing.</p></dd><dd class="tag">please, be patient &ndash; it does <em>tend</em> to take awhile.</dd></dl>';
										
						_form.select('.required').each 
						(
							function (	_field )
							{
								if ( _field.tagName.toLowerCase()  == 'label') 
								{	
									_value = ( $( _field.readAttribute('for') ) ) ? $( _field.readAttribute('for') ).getValue() : null;
								} else {
									_value = _field.getValue();
								}
								
								if ( _value.empty() == true ) {
									_field.addClassName('error');
									_valid = false;
								} else {
									_field.removeClassName('error');
								}
							}
						)
						
						if ( _valid == true ) 
						{ 
							if (! $('form_processing'))
							{
								_process = document.createElement( 'div');
									Element.extend (_process);
								_process.update ( _process_html);
								_process.id = 'form_processing';
								_process.addClassName ('form_processing br_all');
									
								_form.insert( {'top':  _process } );
							} else {
								$('form_processing').show();	
							}
							_form.select('fieldset').invoke('hide');
						} else {
							_e.stop();
						}
					}
				}
				
			// switch panels;
				
				this._doc.select ('.js_form_panels .point').each
				(
					function (_p, i)
					{
						if ( _b = $( _p.down('a').hash.sub(/#/, '') ) )
						{
							_p.observe ('click', function (_e) {
								
								_form.select('.js_form_panel').invoke('hide');
								
								_p.up('.js_form_panels').select('.point').each ( function ( _h ) { _h.removeClassName('selected'); } );
								
								_p.addClassName('selected');
								
								$( _p.down('a').hash.sub(/#/, '') ).show();
								
								_e.stop();
							});
						}
					}.bind(_form)
				);
			
			// default field;
					
			this._doc.select ('.js_default_value').each ( function (_n, _i)
			{
				var _df = new Array();	
					_df[_n.name] = 	_n.getValue();	
					
				_n.observe ( 'focus', function (_e)
				{
					if (_e.element().getValue() == _df[_e.element().name]) {
						_e.element().setValue('');	
					}
				});
				
				_n.observe ( 'blur', function (_e)
				{
					if (_e.element().getValue() == '' ) {
						_e.element().setValue(_df[_e.element().name]);	
					}
				});
				
			});			
		},
		
		_link_actions : function (_link)
		{
			
			if ((_link.protocol == 'http:' || _link.protocol == 'https:') && window.location.host != _link.hostname)
			{
				_link.target = '_blank';
			}
		}  
	});
	
	
	var _extended_body = 
	{
		_extend : function ()
		{
			this._doc.select('.js_slider').each ( function (_obj) { this._slider (_obj) }, this );
			
			this._doc.select('.js_adjust_items_height').each ( function (_obj) { this._height_adujst (_obj) }, this );
		},
		
		_height_adujst: function (_list)
		{
			var _h = 0;
			_list.select('.item').each ( 
				function (_e) 
				{ 
					
					if (_e.getHeight() > _h) 
					{
						_h = _e.getHeight(); 
					}
					
					console.log (_h);
				} 
			);
			
			_list.select('.item').each ( function (_e) 
			{ 
				_e.setStyle ( { height: _h + 'px' } )
			});
			
		},
		_slider: function (_obj) {
			var _s = 0;
			var _el = _obj.select('.js_slider_block .js_slider_item');
			var _pos = new Array (0,-618,-1236,-1854);
			var _pause = false;
			
			var slide_timer = window.setInterval (function () {
			  //do something with a and b
			 	if (_obj.hasClassName('pause') == false) {
				 	_s = (_s == 2) ? 0 : _s+1;
					new Effect.Move(_obj.down('.js_slider_block'), { x: _pos[_s], y: 0, mode: 'absolute' });
					
					_obj.down('.js_slider_side_effects').select('.js_slider_side_effect_item').each (function (_b, _i) { 
						_b.removeClassName('brown_bg');
						if (_i == _s) _b.addClassName('brown_bg')
					});
				}

			},3500, _s);
			
			_obj.observe ('mouseover', function (_e) { _obj.addClassName('pause') });
			
			_obj.observe ('mouseout', function (_e) { _obj.removeClassName('pause')	});
			
			
		}
		
	};
	
	
	function _apply_general_actions()
	{	
		$$('body').each
		(
			function(_n)
			{
				_doc = new Body (_n, _extended_body);	
				// extentions
					if (typeof _doc._extend) _doc._extend ();
			}
		);
	}
	
	_apply_action('_apply_general_actions');
	
	
	
	




