/**
* David Anguita Webcard
* Author: David Anguita
* http://davidanguita.name
* Created: Sept 2009
*/

$(document).ready(function() {
	
	/**
	* Initially visible items
	*/
	
	$('#wrapper ul:first').show();
	$('.nav li a:first').addClass('current');

	/**
	* Functions
	*/
		/**
		* jQuery.tweet Library Functions
		*/
		
		function LoadTwitterLastEntries() {
			$("#twitter .last-entries").empty().tweet({
				join_text: '&raquo;',
				username: "pablomacaluso",
				count: 5,
				loading_text: "Buscando incoherencias..."
			});
		}
		
		function RefreshTwitterLastEntries() {
			$("#twitter .last-entries").fadeOut('fast', LoadTwitterLastEntries).fadeIn();
			return false;
		}
		
		/**
		* Browser-specific CSS attributes
		*/
	
		function SetBrowserSpecificCSSAttributes() {	
			if ($.browser.safari) {
				$('#twitter .last-entries, ul.module li').css({ WebkitBorderRadius: 5 });
		  	}
			else if ($.browser.mozilla) {
				$('#twitter .last-entries, ul.module li').css({ MozBorderRadius: 5 });
			}
		}
		
		/**
		* Dialog management
		*/
		
		function ShowCredits() {		
			var bgTransparent = $('<div>').attr({ className: 'bgTransparent', id: 'bgTransparent' });
			$('body').append(bgTransparent);
			bgTransparent = $('#bgTransparent');
			if ($.browser.msie) bgTransparent.css('filter', 'alpha(opacity=70)');
			bgTransparent
				.css('width', $(window).width())
				.css('height', $(window).height())
				.die('click')
				.live('click', function() { $(this).fadeOut(500, function() { $(this).remove(); }); })
				.load('./lib/credits.php').fadeIn(500);
		}
	
	/**
	* OnReady functions
	*/
	
	LoadTwitterLastEntries();
	SetBrowserSpecificCSSAttributes();
	
	/**
	* Event handling
	*/
	
		/**
		* Tabs management
		*/
		
		$('.nav li a').click(function() {
			if (!($(this).hasClass('current'))) {		
				var module = $($(this).attr('href'));
				var wrapper = $('#wrapper');
				
				wrapper.children(':visible').fadeOut(300, function() {
					module.fadeIn(400);
					wrapper.animate({ 'height': module.height() }, 500);
				});
				
				$('.nav a.current').toggleClass('current');
				$(this).toggleClass('current');
			}
			
			return false;
		});
		
		/**
		* Toggle lastest tweets module
		*/
		
		$('#twitter-switcher').toggle(
			function() {
				$(this).css({ backgroundPosition: 'bottom' });
				$('#twitter').fadeIn('fast');
			},
			function() {
				$(this).css({ backgroundPosition: 'top' });
				$('#twitter').fadeOut('fast');
			}
		);
		
		$('#twitter-switcher').hover(
			function() {
				$(this).animate({ marginLeft: '-2px' }, 100).animate({ marginLeft: '+2px' }, 100).animate({ marginLeft: '0' }, 100);
			}
		);
		
		/**
		* Refresh lastest tweets module
		*/
		
		$('#twitter .refresh a').click(RefreshTwitterLastEntries);
		
		/**
		* Show Credits (info)
		*/
		
		$('#info').click(ShowCredits);
		
		/**
		* Resize opacity layer on window resize
		*/
		
		$(window).resize(function() {
			$('#bgTransparent').css("width", $(window).width()).css("height", $(window).height());
		});
	
});