/**
* 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: "danguita",
				count: 5,
				loading_text: "Leyendo tweets..."
			});
		}
		
		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, #about-webcard').css({ WebkitBorderRadius: 5 });
		  	}
			else if ($.browser.mozilla) {
				$('#twitter .last-entries, ul.module li, #about-webcard').css({ MozBorderRadius: 5 });
			}
		}
		
		/**
		* Dialog management
		*/
		
		function ShowCredits() {		
			var bgTransparent = $('<div>')
									.attr({ className: 'bgTransparent', id: 'bgTransparent' })
									.css({ 'width': $(window).width(), 'height': $(window).height() })
									.click(function() {
										$(this).fadeOut(500, function() { $(this).remove(); });
									});
									
			if ($.browser.msie) bgTransparent.css('filter', 'alpha(opacity=70)');
			
			$('body').append(bgTransparent);
			
			bgTransparent.load('./pages/credits.php', function() { $(this).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').removeClass('current');
				$(this).addClass('current');
			}
			
			return false;
		});
		
		/**
		* Toggle lastest tweets module
		*/
		
		$('#twitter-switcher')
			.toggle(
				function() {
					$(this).css({ backgroundPosition: 'bottom' });
					$('#about-webcard').fadeOut('fast', function() {
						$('#twitter').fadeIn('fast');
					});
				},
				function() {
					$(this).css({ backgroundPosition: 'top' });
					$('#twitter').fadeOut('fast', function() {
						$('#about-webcard').fadeIn('fast');
					});
				}
			)		
			.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(), 'height': $(window).height() });
		});
	
});
