$(document).ready(function(){
	$("ol#categoria").hide();
	$("#coluna h2#cat").click(function(){
		$("ol#categoria").animate({
			height: 'toggle',
			opacity: 'toggle'
		});
		$(this).find('.collapser').toggleClass('open');
	});
	$("#form-comment").hide();
	$("#i-comment").click(function(e){
		e.preventDefault();
		$("#form-comment").show('slow');
	});
	
	$("#overlay-j").live('click', function(){
		$(this).remove();
		$('#banner_flutuante').remove();
		$('body').css({
			overflow: 'auto'
		});
	});
	
	$('.section-header').append('<span class="h-left"></span><span class="h-right"></span>')
	
	activateMenu();
	activateForm();	
	activateHoverLabels();
	fixIE();
});

function activateMenu(){
	$('#menu a').removeClass('selected');
	
	var url = window.location.href;
	var test = /\?(?:pg\=)?(\w+)/.exec(url);
	var pg = 'home';
	if (test && test.length) 
		pg = test[1];
	
	$('#menu-' + pg).addClass('selected');
}

function activateForm(){
	$('#search-form button').hover(function(){
		$(this).prev().addClass('hover');
	}, function(){
		$(this).prev().removeClass('hover');
	}).mousedown(function(){
		$(this).prev().addClass('active');
	}).bind('mouseup, mouseout',function(){
		$(this).prev().removeClass('active');
	});
}

function activateHoverLabels(){
	$("label.hover-label+input").focus(function(){
		$(this).prev().stop().fadeOut(100);
	}).blur(function(){
		if (!this.value) 
			$(this).prev().stop().fadeIn(100)
	}).each(function(){
		$(this).prev().addClass("hover-active");
		if (this.value) 
			$(this).prev().stop().fadeOut(100);
	});
}

function fixIE(){
	$('#baladas li:last-child').css('margin-right', 0);
}

function hora(){
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds();
	
	if (hours <= 9) hours = "0" + hours;
	if (minutes <= 9) minutes = "0" + minutes;
	
	document.getElementById('c-hours').innerHTML = hours;
	document.getElementById('c-minutes').innerHTML = minutes;
	setTimeout('hora()', 20000);
}

function overlay_j(opac, body_height, color, zindex){
	var lay = document.createElement('div');
	$(lay).attr({
		id: 'overlay-j'
	});
	$(lay).css({
		background: color
	});
	$(lay).css({
		'z-index': zindex
	});
	$('body').after(lay);
	
	$(lay).css({
		height: body_height + 'px',
		opacity: opac
	});
}


/* floater */
function id(id){
	return document.getElementById(id);
}

function mover(ele, qnto){
	var top = parseInt(id(ele).style.top);
	var left = parseInt(id(ele).style.left);
	
	var body_width = document.body.clientWidth - id(ele).width;
	var body_height = id('cobaia').offsetTop * 2 - id(ele).height;
	
	var new_top = dimensao(h, body_height, qnto, top);
	var new_left = dimensao(w, body_width, qnto, left);
	
	if (top < 0) h = 0;
	if (left < 0) w = 0;
	
	id(ele).style.top = new_top;
	id(ele).style.left = new_left;
	
	h++;
	w++;
	window.setTimeout("mover( '" + ele + "', 2, '" + q + "' )", 20);
}

function dimensao(count, dimensao, qnto, direcao){
	if (count < dimensao / qnto) return parseInt(direcao) + qnto + 'px';
	else 
		return parseInt(direcao) - qnto + 'px';
}

function createFloater(el, id, top, src){
	var floater = document.createElement(el);
	
	if (el == 'img') floater.src = src;
	floater.id = id;
	
	if (document.all) floater.style.position = 'absolute';
	else 
		floater.style.position = 'fixed';
	floater.style.top = top;
	floater.style.left = '0';
	floater.style.cursor = 'pointer';
	floater.style.zIndex = '100';
	
	document.body.appendChild(floater);
}

