/**
 *	These function require the DOM to be loaded
 *
 */
window.addEvent('domready', function() {
	buttonsToUppercase();
	submenuHandler();
	showPostNavigation();
	getTweet();
	pageSlider();
});

/**
 *	Handles the read more functions on the page slider
 *
 */
function pageSlider() {

	sliders = new Hash();

	$$('.readmore').each( function(e) {
		var parent	= e.getParent('.content');
		var pos		= e.getCoordinates(parent);
		
		// add global state
		sliders.set(parent.getAttribute('id'), {'pos': pos.top, 'expanded': 0});

		parent.setStyle('height', pos.top);
		e.setStyles({
			'position' : 'absolute',
			'bottom' : 0
		});

		e.addEvent('click', function() {
			// get context
			var parent		= this.getParent('.content');
			var parentId	= parent.getAttribute('id');

			// get properties from hash
			minHeight		= sliders.get(parentId).pos;
			expandStatus	= sliders.get(parentId).expanded;
		
			// set tween
			var myFx = new Fx.Tween(parent);

			if (expandStatus == 0) {
				var expanded = 1;
				myFx.start('height', minHeight, parent.getScrollSize().y);
				e.getElement('img').setAttribute('src', e.getElement('img').getAttribute('src').replace('plus', 'min'));
			} else {
				var expanded = 0;
				myFx.start('height', parent.getScrollSize().y, minHeight);
				e.getElement('img').setAttribute('src', e.getElement('img').getAttribute('src').replace('min', 'plus'));
			}
			
			// update
			sliders.set(parentId, {'pos': minHeight, 'expanded': expanded});
		});
	});

}

/**
 *	Handles the way the submenu appears
 *
 */
function submenuHandler() {
	if ( !$chk( $$('sub_menu') ) )
		return false;

	$('navigation').getElements('li').each( function(e) {
		// only level 1 li
		if ( !e.getParent('.sub_menu')  ) {
			
			e.addEvent('mouseenter', function() {
				e.getElement('a').addClass('mouse_over');

				if ( $chk( e.getElement('ul') ) )
					e.getElement('ul').setStyle('visibility', 'visible');
			});

			e.addEvent('mouseleave', function() {
				e.getElement('a').removeClass('mouse_over');

				if ( $chk( e.getElement('ul') ) )
					e.getElement('ul').setStyle('visibility', 'hidden');
			});
		}

	});

	$$('.sub_menu').each( function(e) {
		// set height parameters
		var height		= 0;
		var amt			= 0;
		var margin		= 4;
		var maxWidth	= e.getParent('li').getSize().x + margin;
		
		// debug
		//alert(maxWidth);

		e.getElements('a').each( function(e) {
			var original_text	= e.get('text');
			var dummy_text		= original_text.replace(/ /g, '|');
			
			// make sure spaces are not wrapped
			e.set('text', dummy_text);
			var size =  e.getSize();
			e.set('text', original_text);
			
			// get height
			height = size.y;
			
			// check max width
			if ( size.x > maxWidth)
				maxWidth = size.x + margin;
			
			// fount another li
			amt++;
		});
		
		// calculate total height
		var totalHeight = height * amt;
		
		// set total height
		e.setStyles({
			'top'	: -totalHeight + 'px'
		});
		
		// set parent width
		e.getParent('li').setStyles({
			'width' : maxWidth + 'px'
		});

		// set self width
		e.getElements('li').each( function(e) {
			e.setStyle('width',  maxWidth + 'px');
		});
	});
}

/**
 *	Sets the standard form buttons to uppercase font
 *
 */
function buttonsToUppercase() {
	$$('input[type=submit]').each( function(e) {
		var uppercase = e.getProperty('value').toUpperCase();

		e.setProperty('value', uppercase);
		e.setStyle('color', '#fff');
	});
};

/**
 *	Show links to next or previous?
 *
 */
function showPostNavigation() {
	if ( !$chk( $('post_navigation') ) )
		return false;

	$('post_navigation').getElements('div').each( function(e) {
		if ( e.get('text').trim() != '')
			$('post_navigation').setStyle('display', 'block');
	})
};

/**
 *	Gets a tweet :)
 *
 */
function getTweet() {
	//Twitter JSONP request
	var r = new Request.JSONP({
		url: 'http://twitter.com/statuses/user_timeline/patrickpijl.json',
		method: 'get',
		data: {
			count: 1
		},
		noCache: true,
		onComplete: function(myTweets) {
			myTweets.each(function(tweet) {
				var date = new Date().parse(tweet.created_at).format('%Y %B %d %H:%M');
				var text = tweet.text;
				
				$('tweet').set('html', '<p>' + text + '<br /><em>' + date + '</em></p>');
			});
		}
	}).send();
}