/*
 * jQuery script for ThemeMX public pages
 * http://theme.mx/
 *
 * Copyright (c) 2010 WorryFreeLabs
 *
 * This file is part of ThemeMX WordPress theme
 */
$j = jQuery.noConflict();

$j(document).ready
(
	function()
	{
		/**
		 * Main navigation
		 */
		jQuery('#navigation li:first').addClass('first');
		
		jQuery('#navigation li a').click
		(
			function()
			{
				jQuery('#navigation li').removeClass('current_page_item');
				jQuery('#navigation a').removeClass('selected');
				jQuery(this).addClass('selected');
			}
		);
		
		jQuery('#navigation li')
			.mouseover(function() {	jQuery(this).addClass('sfhover'); })
			.mouseout(function() { jQuery(this).removeClass('sfhover'); });	
	
		
		/**
		 * Older Posts / Newer Posts links
		 */
		jQuery('div.posts_nav a:contains("Older")').addClass('older');
		jQuery('div.posts_nav a:contains("Newer")').addClass('newer');
		
		
		/**
		 * Forms
		 */
		jQuery('form label').each
		(
			function()
			{
				var labelText = jQuery(this).text();
				
				jQuery('input#' + jQuery(this).attr('for') + ', textarea#' + jQuery(this).attr('for'))
					.addClass('waiting')
					.val(labelText)
					.focus
					(
						function()
						{
							if (jQuery(this).hasClass('waiting'))
							{
								jQuery(this).removeClass('waiting').val('');
							}
						}
					)
					.blur
					(
						function()
						{
							if (jQuery.trim(jQuery(this).val()) == '')
							{
								jQuery(this).addClass('waiting').val(labelText);
							}
						}
					);
					
				jQuery(this).hide();
			}
		);
		
		
		/**
		 * Comments
		 */
		jQuery('div.comment div.comment_content p.reply a').click
		(
			function()
			{
				var $this = jQuery(this);
				var $commentContent = $this.parent();
				var $commentContainer = $commentContent.parent();
				
				jQuery('#add_comment').appendTo($commentContent);
				jQuery('#comment_parent').val($this.attr('rel'));
				jQuery('#comment :input').css('max-width', $commentContainer.width() - 60);
				
				return false;
			}
		);
		
		jQuery('#add_primary_comment').click
		(
			function()
			{
				jQuery('#add_comment').prependTo(jQuery('#comments div.comment:first'));
				jQuery('#comment_parent').val(0);
				
				return false;
			}
		);
		
		
		/**
		 * Twitter
		 */
		var $tweet = jQuery('#tweet');
		
		if ($tweet.is('div'))
		{
			var twitterCookieName = 'thememx-twitter-cache';
			
			var twitterSettings = $tweet.attr('class').split('|');
			var twitterCaching = parseInt(twitterSettings[0]);
			var twitterCacheLifetime = parseInt(twitterSettings[1]);
			
			var twitterHtml = jQuery.cookie(twitterCookieName);
			
			if (!twitterCaching || !twitterHtml)
			{
				jQuery($tweet).getTwitter
				({
					userName: $tweet.attr('title'),
					numTweets: 1,
					loaderText: 'Loading tweets...',
					slideIn: false,
					showHeading: false,
					headingText: 'Latest Tweets',
					showProfileLink: false,
					showTimestamp: false,
					callbackOnComplete: function()
					{
						if (twitterCaching)
						{
							var twitterExpires = new Date();
							twitterExpires.setTime(twitterExpires.getTime() + twitterCacheLifetime * 60 * 1000);
							
							jQuery.cookie(twitterCookieName, $tweet.html(), {expires: twitterExpires, path: '/'});
						}
					}
				});
			}
			else
			{
				$tweet.html(twitterHtml);
			}
		}
		
		
		/**
		 * Calendar
		 */
		$wpCalendar = jQuery('#wp-calendar');
		
		if ($wpCalendar.is('table'))
		{
			var $calCaption = jQuery('caption', $wpCalendar);
			var $calHead = jQuery('thead tr', $wpCalendar);
			var $calFoot = jQuery('tfoot tr', $wpCalendar);
			
			// New calendar head
			$calHead.empty()
				.append
				(
					jQuery('<th colspan="1"></th>').append
					(
						jQuery('#prev a', $calFoot)
							.addClass('prev')
							.text('Prev')
					)
				)
				.append
				(
					jQuery('<th colspan="5"><h4 class="month"></h4></th>')
						.text($calCaption.text())
				)
				.append
				(
					jQuery('<th colspan="1"></th>').append
					(
						jQuery('#next a', $calFoot)
							.addClass('next')
							.text('Next')
					)
				);
			
			$calCaption.hide();
			$calFoot.hide();
			
			// Empty cells
			function rebuildCalEmptyCells($calRow)
			{
				var $tempRow = jQuery('<tr></tr>');
				
				$calRow.children('td').each
				(
					function()
					{
						var $this = jQuery(this).clone();
						
						if ($this.hasClass('pad'))
						{
							var index;
							var colspan = parseInt($this.attr('colspan'));
							
							for (index = 0; index < colspan; index++)
							{
								$tempRow.append(jQuery('<td class="empty"></td>'));
							}
						}
						else
						{
							$tempRow.append($this);
						}
					}
				);
				
				return $tempRow;
			}
			
			$calBody = jQuery('tbody', $wpCalendar);
			
			var $calFirstWeekRow = jQuery('tr:first', $calBody);
			$calBody.prepend(rebuildCalEmptyCells($calFirstWeekRow));
			$calFirstWeekRow.remove();
			
			var $calLastWeekRow = jQuery('tr:last', $calBody);
			$calBody.append(rebuildCalEmptyCells($calLastWeekRow));
			$calLastWeekRow.remove();
		}
	}
);