// JavaScript Document

	function _append_zero(n){
		if (n>0 && n<10){
			return '0' + n;	
		}
		return n;
	}
	
	function _external_internal_links()
	{
		// links;
		
		$$("a").each(function(n) {
			if ((n.protocol=='http:' || n.protocol=='https:') && window.location.host!=n.hostname){
				n.target = '_blank';
				/*n.observe('click', function (e){
					//window.open(n.href);
					alert(n.hostname + ' ? ' + window.location.host);
					e.stop();
				});
				//e.stop();*/
				
			}	
		});
	}
	
	// auto height
	function _autoHeight(){
		$$('textarea.autoHeight').each(function (e){
			var txt = $(e);
			var ht = txt.rows-1;
	
			e.onkeydown = function (el){
				/*alert(txt.rows);	
				txt.height != txt.dummy.getHeight() + txt.line_height*/
				var c = 0;
				var r = 2;				
				var t = txt.value;
				for(var i=0; i<t.length; i++) {
					if (t.substr(i,1) == '\n') {
						c = 0;
						r++;
					}
					else {
						c++;
						if(c >= txt.cols) { c=0; r++;}
					}
				}
				if (r>ht){
					txt.rows = r;				
				}
			}
		});
	}
	
	function _formSubmission()
	{
		$$("form").each(function(n) {
			
			// check for no magic;
			
			if (n.hasClassName('noMagic')==false){
				n.observe('submit', function(e){
					// process form;
					var valid = true;
					
					if (_required_fields = n.select('.required')){
						_required_fields.each(function(r)						   
						{
							if (r_object = $(r.readAttribute('for'))){
								
								if (r_object.getValue()=='' && r_object.disabled==false){
									valid = false;
									r.up('li').addClassName('error');
								}
							}
						});
						
					}
					
					if (valid){
					
						_process_indicator_html = 	'<dl class="clearfix spin processing">';
						_process_indicator_html += 		'<dt class="title">Un, momento!</dt>';
						_process_indicator_html += 		'<dd class="story">';
						_process_indicator_html +=			'<blockquote><p>We are processing what you submitted &mdash; <em>sometimes</em> it takes longer than expected.</p></blockquote>';
						_process_indicator_html +=			'<p>We hope it doesn&rsquo;t.</p>';
						_process_indicator_html +		'</dd>';
						_process_indicator_html += 	'</dl>';
								
						_process_object = document.createElement('div');
						Element.extend(_process_object);
						_process_object.update(_process_indicator_html);
						_process_object.id = 'form_processing';
				
						n.select('fieldset').invoke('hide'); // hide forms;
						_process_object.addClassName('form_processing');
						n.insert(_process_object);
						
						//e.stop();
					} else {
						e.stop();
					}
				});
			}
		});
	}
	
	function _get_twitter_status()
	{
		$$('.js_twitter_status').each(function(n) {
			var tweet_layer = $('js_twitter_status');
			tweet_layer.hide();
			
			var r = new Ajax.Request
			(
			 '/xy?url=http://twitter.com/statuses/user_timeline/nemisa_.xml?count=1',
			 {
				method: 'get',
				onComplete: function (transport){
					if (data = transport.responseText.evalJSON(true)){
						tweet_layer = $('js_twitter_status');
						tweet_layer.down('.story').update(data['twitter'].text);
						tweet_layer.down('.time').down('a').update(data['twitter'].created.time);
						tweet_layer.down('.time').down('a').href = data['twitter'].link;
						Effect.Appear(tweet_layer);
					}
				}
			 }
			);
		});
	}
	
	function _show_and_hide()
	{
		$$('.js_show_hide_point').each(function(n) {
			//alert(n.up('.js_show_hide_block') + n.up('.js_show_hide_block').down('.js_show_hide_object'));
			if (n.up('.js_show_hide_block') && n.up('.js_show_hide_block').down('.js_show_hide_object')){
				n.observe('click', function(e){
					n.up('.js_show_hide_block').down('.js_show_hide_object').toggle();
				});
			}
		});
	}
	
	/* slide shows (try to use objects);
		borrowed from here: http://codesnippets.joyent.com/posts/show/1825
	
	
	function start_slideshow(start_frame, end_frame, delay, lis) {
		setTimeout(fadeInOut(start_frame,start_frame,end_frame, delay, lis), delay);
	}
	
	function fadeInOut(frame, start_frame, end_frame, delay, lis) {
		return (function() {
			lis = $('js_slide_show_block').getElementsByTagName('li');
			Effect.Fade(lis[frame]);
			if (frame == end_frame) { frame = start_frame; } else { frame++; }
			lisAppear = lis[frame];
			setTimeout("Effect.Appear(lisAppear);", 0);
			setTimeout(fadeInOut(frame, start_frame, end_frame, delay), delay + 1850);
		})
		
	}
	


	
	function _slide_show()
	{
		var delay = 10;
		var start_frame = 0;

		$$('.js_slide_show').each(function(n) {
			n.down('.js_slide_show_element').select();
			
			if (_slide_show_elements = n.select('.js_slide_show_element')){
				_slide_show_elements.each(function(r, i)						   
					{
						if (i!=0){
							_slide_show_elements[i].style.display = 'none';	
						}
					}
				);
				
				end_frame = _slide_show_elements.length -1;
				start_slideshow(start_frame, end_frame, delay, _slide_show_elements);


			}
		});
	}*/
	
	function _slide_show()
	{
		

		$$('.js_slide_show').each(function(n) {
			
			
			if (_slide_show_elements = n.select('.js_slide_show_element')){
				_slide_show_elements.each(function(r, i)						   
					{
						if (i!=0){
							/*_slide_show_elements[i].style.display = 'none';*/
						}
					}
				);
			}
			var start_frame = 0;
			var last_frame = _slide_show_elements.length;
			var distance = 950;
			var pos = new Array(0, 950, 1900, 2850, 3800);
			var move = 0;
			
			if (_slide_show_controllers = $('controller').select('.js_slide_show_controller')){
				var _controller_blocks = $('controller').select('.flowctrl');
				// controller blocks no clicking, don't ask me why they want this
					// var _response_id = n.id.sub(/\D+/, '');
					
				$('controller').select('.js_slide_show_jump').each(function(a, i)						   
				{
						
						a.down('a').href = '/#neck';
						
						a.observe('click',function(e)
						{
							_block_number = a.id.sub(/\D+/, '');
							start_frame = _block_number;
							move = -pos[start_frame];
							
							new Effect.Move($('js_slide_show_block'), { x: move, y: 0, mode: 'absolute' }, { duration: 0.2 });
							
							_controller_blocks.each(function(cb)						   
							{
								cb.removeClassName('jFlowSelected');
							});
							
							_controller_blocks[start_frame].addClassName('jFlowSelected');
							
							e.stop();					   
						});
				});
				
				_slide_show_controllers.each(function(r, i)						   
					{
						r.observe('click', function(e){
							
							if (r.hasClassName('js_slide_show_next')){
								if (start_frame == last_frame -1){
									start_frame = 0;
								} else {
									start_frame++;
								}
								
							} else {
								if (start_frame == 0){
									start_frame = 4;
								} else {
									start_frame--;
								}
							}
							
							move = -pos[start_frame];
							
							
							new Effect.Move($('js_slide_show_block'), { x: move, y: 0, mode: 'absolute' }, { duration: 0.2 });
							
							_controller_blocks.each(function(cb)						   
							{
								cb.removeClassName('jFlowSelected');
							});
							
							_controller_blocks[start_frame].addClassName('jFlowSelected');
							
							e.stop();
						});
					}
				);
			}
			
		});
	}
	
	function _apply_general_actions()
	{	
		_autoHeight(); // done
		_external_internal_links(); // done
		_formSubmission(); // done
		_get_twitter_status();
		_show_and_hide(); 
		_slide_show();
	}
	
	_apply_action('_apply_general_actions');
	
	
	
	




