// 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()
	{
		$$('.twitter').each(function(n) {
			var tweet_layer = $('twitter');
			if (!tweet_layer.hasClassName('halt_twitter')){
				var r = new Ajax.Request
				(
				 '/xy?url=http://twitter.com/statuses/user_timeline/burgundyfly.xml?count=1',
				 {
					method: 'get',
					onComplete: function (transport){
						if (data = transport.responseText.evalJSON(true)){
							tweet_layer = $('twitter');
							tweet_layer.down('.text').update('&hellip;' + data['twitter'].text);
							tweet_layer.down('.link').update(data['twitter'].created.time);
							tweet_layer.down('.link').href = data['twitter'].link;
							
						}
					}
				 }
				);
			}
		});
	}
	
	var fn_gallery_view = Class.create({
		initialize: function (obj){
			
			this.index = 0;
			this.container = obj;
			
			this.images = this.container.select('.gallery_image');
			this.total = this.images.length - 1;
			
			gt = obj.down('.gallery_traverse');
			
			gt.makePositioned();
			
			/*gt.down('.point').setStyle({
				height: obj.getHeight() + 'px'
						
			})*/
			this._ajust_controls();
			//alert(obj.getHeight());
			
		},
		
		_ajust_controls: function ajust_controls(){
			/*this.container.down('.gallery_viewer_previus').down('a').setStyle({
				height: this.container.getHeight() + 'px'
			})
			
			this.container.down('.gallery_viewer_next').down('a').setStyle({
				height: this.container.getHeight() + 'px'
			})*/
		},
		
		_next : function next (){
			this.images[this.index].hide();
			this._adjust_index(1);
			this.images[this.index].show();
			this._ajust_controls();
		},
		
		_prev : function prev (){
			this.images[this.index].hide();
			this._adjust_index(-1);
			this.images[this.index].show();
			this._ajust_controls();
		},
		
		_adjust_index : function ajsut_index(v){
			if (v<0){
				this.index--;
				this.index = (this.index<0) ? this.total : this.index;
			} else if (v>0) {
				this.index++;
				this.index = (this.index>this.total) ? 0 : this.index;
			}
			
			//$('gct').update(v + ' > ' + this.index + ' / ' + this.total);
		}
							
	});
	
	var _gv; // gallery viewer
	
	function _image_viewing()
	{
		
		$$('.gallery_viewer').each(function(n) {
			_gv = new fn_gallery_view (n);
			n.down('.gallery_viewer_previus').observe('click', function(e) { _gv._prev(); e.stop(); });
			n.down('.gallery_viewer_next').observe('click', function(e) { _gv._next(); e.stop(); });
		});
	}
	
	function _form_empty_fields ()
	{
		var _form_empty_field_values = new Array();
		
		$$('.js_form_empty_fields').each(function(n) {
			_form_empty_field_values[n.id] = new Array();
			n.select('input').each(function(q){
				// observe
				_form_empty_field_values[n.id][q.name] = q.value;
				q.observe('focus', function(e) { 
					// remove old values;
					q.value = '';
					//alert(q.name);
				});
				
				q.observe('blur', function(e) { 
					// remove old values;
					if (q.value == ''){
						q.value = 	_form_empty_field_values[n.id][q.name];
					};
					//alert(q.name);
				});
			});
			
		});
	}
	
	
	function _js_banners_slide ()
	{
		var _js_banners_slide_positions = new Array(0,950, 1900, 2850, 3800, 4750, 5700, 6650, 7600, 8550, 9500, 10450);
		var _js_banners_slide_counter = 0;
		
		$$('.js_banners_slider').each(function(n) {
							   
			var _js_banners_slide_handler = n.down('.js_banners_slider_handler');
					
			
			setInterval(
				function() 
				{
					_js_banners_slide_counter++
					
					if (_js_banners_slide_counter==11){
						_js_banners_slide_counter=0;
					}
					_js_movement = -1 * _js_banners_slide_positions[_js_banners_slide_counter]
					
					//$('DBG').update(_js_movement);//('hello world');
					
					new Effect.Move(_js_banners_slide_handler, { x: _js_movement, y: 0, mode: 'absolute' }, { duration: 0.2 });
				}, 
				7000
			);
			
		});
	}
	
	function _apply_general_actions()
	{	
		_autoHeight(); // done
		_external_internal_links(); // done
		_formSubmission(); // done
		_image_viewing(); //
		_get_twitter_status();
		_form_empty_fields();
		_js_banners_slide();
	}
	
	_apply_action('_apply_general_actions');
	
	
	
	




