// 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_status').each(function(n) {
			
			var tweet_layer = $('js_twitter_status');
			if (!tweet_layer.hasClassName('halt_twitter')){
				var r = new Ajax.Request
				(
				 '/xy?twitter',
				 {
					method: 'get',
					onComplete: function (transport){
						if (data = transport.responseText.evalJSON(true)){
							//tweet_layer = $('js_twitter_status');
							if (data['twitter']) {
							// status
								tweet_layer.down('.twitter_story').down('p').update(data['twitter'].text);
								
								
							// time
								tweet_layer.down('.twitter_time_one').update(data['twitter'].created.time);
								tweet_layer.down('.twitter_time_two').update(data['twitter'].created.date);
								//tweet_layer.down('.twitter_status_link').href = data['twitter'].link;
							} else {
								//alert (tweet_layer.down('.twitter_time_one'));	
							}
							Effect.Appear(tweet_layer);
								
						}
					}
				 }
				);
			} else {
				Effect.Appear(tweet_layer);	
			}
		});
	}
	
	
	function _get_fb_page()
	{
		
		 window.fbAsyncInit = function() {
			FB.init({appId: '', status: true, cookie: true,
					 xfbml: true});
		  };
		  (function() {
			var e = document.createElement('script'); e.async = true;
			e.src = document.location.protocol +
			  '//connect.facebook.net/en_US/all.js';
			document.getElementById('fb-root').appendChild(e);
		  }());
		  
	}
	
	function _get_linked_within ()
	{
		$$('.js_post_linked_within').each(function(n) {
		
		  (function() {
			var e = document.createElement('script'); e.async = true;
			e.src = 'http://www.linkwithin.com/widget.js';
			document.getElementById('post_linked_width').appendChild(e);
		  }());
		  
		});
		  
	}
	
	
	function _tweet_this ()
	{
		/*
		$$('.twitter-share-button').each(function(n) {
			var e = document.createElement('script'); e.async = true;
			
			
			e.src = document.location.protocol +
			  '//platform.twitter.com/widgets.js';
			 
			 n.up('.alt_sharing');
				document.getElementById('twitter_tweet_block').appendChild(e);
			
		});*/
		
	}
	
	function _socia_buttons_actions()
	{
		$$('._social_button_action').each(function(n){
					
			
			n.observe('click', function(e)
			{
				e.stop();
				
				window_opener = window.open(n.href, '', "status=1,toolbar=0, location=0, width=540, height=320"); 
				window_opener.moveTo((document.viewport.getDimensions().width - 480)/2, (document.viewport.getDimensions().height - 320)/2);
				//alert('hello');
				
			});
		});
	}
	
	
	function _list_item_reposition ()
	{
		//js_reposition_item
		$$('.js_reposition_item').each
		(
			function(n) 
			{
				i_h = n.getHeight();
				il_h = n.down('.item_link').getHeight();
				ia_h = n.down('.item_link').down('a').getHeight();
				
				d_h = 130 - ia_h;
				
				n.down('.item_link').setStyle(
				  {
					paddingTop: d_h + 'px'
				  });
				 
				//alert(d_h);
			
			}
		);
		
	}
	
	function _story_gallery_get_image_url (_img_src, _img_url)
	{
		if (_img_url){
			_img_url = 	_img_src.sub('thumb_', '');
			return _img_url;
			//alert(_img_url);
		}
		_url_split = _img_src.split('/');
		_thumb_url = 'thumb_' + _url_split[_url_split.length-1];
			
		_thumb_url = _img_src.sub(_url_split[_url_split.length-1], _thumb_url);
		
		return _thumb_url;
	}
	
	
	function _story_gallery_flip ()
	
	{
		$$('.story_gallery_images').each(function(n) 
		{	
			n.addClassName('clearfix');
			var img_list = new Array();
			n.select('li').each(function(r, i)						   
			{
				// gray out;
				r.down('img').hide();
				
				img_list[i] = new Array (); 
				//img_list[i].width = 5;
				//img_list[i].height = 5;
				img_list[i] = r.down('img').getDimensions();
				
				if (i==0)
				{
					
					_li_item = document.createElement('li');
					_li_item.addClassName('show_image_here');
					_li_item.update('<img src="' + r.down('img').src + '"/>');
					_li_item.setStyle(
					{ 
						display: 'block',
					
					});
					
					n.insert({'top': _li_item});
					
					_li_item.id = 'storyGalleryImageShownHere';
					
					
					$('storyGalleryImageShownHere').setStyle({ width: r.down('img').readAttribute('width') + 'px', height: r.down('img').readAttribute('height') + 'px'});
					
					//_li_item.down('img').show();
				}
				
				r.down('img').src = _story_gallery_get_image_url (r.down('img').src);		
				
				r.update('<a href="#' + i + '">' + r.innerHTML + '</a>');
				
				r.setStyle(
				{ 
					display: 'block',
					float: 'left',
					margin: '0 5px 5px 0',
					height: '70px',
					width: '70px',
					backgroundColor: '#f0f0f0'
				
				});
				
				r.down('img').setStyle(
				{ 
					width: '70px',
					height: 'auto',
				
				});
				
				
				
				r.down('img').onload = function() {
					Effect.Appear(r.down('img'));//.Appear();
				}
				
				r.down('a').observe('click', function(e)
				{
					wh = img_list[r.down('a').hash.sub(/\D+/, '')];
					
					//console.log (img_list[r.down('a').hash.sub(/\D+/, '')]);
					$('storyGalleryImageShownHere').setStyle({ width: wh.width + 'px', height: wh.height + 'px'});
					
					//Effect.Fade($('storyGalleryImageShownHere').down('img'));
					
					$('storyGalleryImageShownHere').down('img').hide();		
					
					$('storyGalleryImageShownHere').down('img').src = _story_gallery_get_image_url (r.down('img').src, true);
					
					console.log()
					
					$('storyGalleryImageShownHere').down('img').onload = function() 
					{
						Effect.Appear($('storyGalleryImageShownHere').down('img'), 'end');//.Appear();
					}
					
					e.stop();
				});
				
			});
					
			
		});
	}
	

	
	function _apply_general_actions()
	{	
		_autoHeight(); // done
		_external_internal_links(); // done
		_formSubmission(); // done
		_get_twitter_status();
		_list_item_reposition();
		
		_socia_buttons_actions();
		_story_gallery_flip();
		
		_get_linked_within();
		
		//_get_fb_page();
	}
	
	_apply_action('_apply_general_actions');
	
	
	
	




