$(document).ready(function(){
	
	// Menu hover animation
	$('ul#menu').addClass('transformed');
	$('ul#menu li a').each(function(){
		var $ref = $(this);
		$(this).hover(
			function(event){ $ref.stop(true); $ref.fadeTo(300, 0.01); },
			function(event){ $ref.fadeTo(300, 1, function(){ $ref.css('filter',''); }); }
		);
	});
	
	// Maloca video
	$('div#footer a#maloca').click(function(){
		if(window.scrollY == 0) {
			$('div#espaco').fadeIn();
		} else {
			$('html,body').animate( { scrollTop: 0 }, Math.max(500, window.scrollY / 1.5), function(){ $('div#espaco').fadeIn(); } );
		}
		$('body, div#espaco span').one('click',function(){ $('div#espaco').fadeOut(); return false; } );
		return false;
	});
	
	// FTP Login
	$('span#login button').click(function(){
		if($.browser.msie) {
			window.open('ftp://malocaestudio.com');
		} else {
			var username = $('span#login input[name=username]').val();
			var password = $('span#login input[name=password]').val();
			if(username.length < 4) { $('span#login input[name=username]').focus(); return false; }
			if(password.length < 4) { $('span#login input[name=password]').focus(); return false; }
			var href = 'ftp://' + username + ':' + password + '@malocaestudio.com/';
			window.open(href);
		}
		return false;
	});
	$('div#footer p a:first').toggle(
		function(){
			$('span#login').animate({borderWidth: '1px', marginLeft: '10px', marginRight: '5px', width: '320px'}, 300, function(){
				$('div#footer').addClass('expanded');
			});
			return false;
		},
		function(){
			$('span#login').animate({borderWidth: '0px', marginLeft: '0px', marginRight: '5px', width: '0px'}, 200, function(){
				$('div#footer').removeClass('expanded');
			});
			return false;
		}
	);
	
	// Image switcher under Projects
	$('body#projetos div#content ul').each(function(){
		if($('li',this).length <= 1) return;
		var maxHeight = 0;
		var $switcher, $list = null;
		$list = $(this).addClass('switcher');
		$('li:not(:first)',this).hide();
		$('li img',$list).each(function(){ maxHeight = Math.max(maxHeight, $(this).attr('height')); });
		$list.height(maxHeight);
		$list.after('<p class="switcher"><span class="next bgimage">next</span></p>');
		$switcher = $list.next();
		$('span',$switcher).click(function(){
			var isLast = $('li:last:visible', $list).length == 1;
			var $out = $('li:visible', $list).fadeOut(200);
			if(isLast) {
				$('li:first', $list).fadeIn(200);
			} else {
				$out.next().fadeIn(200)
			}

		});
	});
	
	// Tabbed content under About
	$('body#quem-somos div#content').each(function(){
		$('h2:first', this).before('<ul class="tabs cleared"></ul><div class="tabs cleared"></div>');
		$('h2', this).each(function(index){
				$('ul.tabs').append('<li class="tab"><a href="#tab-' + index  + '">' + $(this).text() + '</a></li>')
				$('ul.tabs li:eq(' + index + ')').data('tab',$(this).attr('title'));
				$(this).removeAttr('title');
				$(this).next().remove().addClass('tab').attr('class','tab-content').appendTo('div.tabs').hide();
				$(this).remove();
			});
		$('li.tab a', this).click(function(){
				var index = $(this).attr('href').split('#')[1].substring('tab-'.length);
				var $newTab = $('div.tab-content:eq(' + index +')');
				$('li.tab').removeClass('selected');
				$(this).parent().addClass('selected')
				var $noFade = ($.browser.msie && $.browser.version <= 6); 
				if($('div.tab-content:visible').length == 0) {
					if($noFade) {
						$newTab.show();
					} else {
						$newTab.fadeIn(250);
					}
				} else {
					if($noFade) {
						$('div.tab-content:visible').hide();
						$newTab.show();;
					} else {
						$('div.tab-content:visible').fadeOut(250, function(){ $newTab.fadeIn(250); });
					}
				}
				window.location.hash = $(this).parent().data('tab').toLowerCase();
				return false;
			});
		function openTab(tab) {
			tab = tab.substr(1);
			$('li.tab a').each(function(){
				if($(this).parent().data('tab') == tab) {
					$(this).trigger('click');
				}
			});
		}
		if(window.location.hash.length > 0) {
			openTab(window.location.hash);
		} else {
			$('li.tab:first a').trigger('click');
		}
		
	});
	
	// Collapsble table bodies
	$('table.sonoteca').addClass('upgraded');
	$('table.sonoteca thead').each(function(){
		var $thead = $(this);
		$thead.next().hide();
		$('th',$thead).hover(
			function(){ $thead.addClass('hover'); },
			function(){ $thead.removeClass('hover'); }
		).toggle(
			function(){
				$thead.addClass('open');
				if($.browser.msie && $.browser.version <= 7) {
					$thead.next().show();
				} else {
					$thead.next().slideDown($('tr',$thead.next()).length * 20);
				}
			},
			function(){
				$thead.removeClass('open');
				if($.browser.msie && $.browser.version <= 7) {
					$thead.next().hide();
				} else {
					$thead.next().slideUp($('tr',$thead.next()).length * 10);
				}
			}
		);
	});
	
	// Background movement
	/*
	var bgPosY = $('div#bg-curly').css('background-position').split(' ')[1];
	var bgAnimate= function(id) {
		xPos = $('#' + id).css('background-position').split(' ')[0];
		xPos = parseInt(xPos.substring(0, xPos.length - 2));
		if(Math.abs(xPos) >= screen.availWidth) {
			xPos = 0;
		}
		if(id == 'bg-curly') {
			xPos -= 1;
		} else {
			xPos += 1;
		}
		$('#' + id).css('background-position',xPos + 'px ' + bgPosY);
	}
	//var bgCurlyTimer   = setInterval(function() { bgAnimate('bg-curly'); }, 100);
	//var bgStraighTimer = setInterval(function() { bgAnimate('bg-straight'); }, 100);
	//*/
	
	$('body').mousemove(function(event){
		$('div#bg-curly'   ).css('background-position', parseInt(event.clientX /  20) + 'px 50%');
		$('div#bg-straight').css('background-position', parseInt(event.clientX /  10) + 'px 50%');
	});
});