



/*MAIN*/



String.prototype.empty = function(){
	return this.match(/^(\s*|0+)$/);
};

function banner_fn(){
	var id = $('body').attr('id');
	swfobject.embedSWF('swf/' + id + '.swf', 'flash_content', 950, id == 'home' ? 300 : 250, '9.0.0', 'swf/expressInstall.swf', {}, {wmode:'transparent'});
}

function banner(){
	var banner = $('#banner');
	if(!banner.length) return false;
		
	var banner_img = $('#banner_img').children();
	var banner_text = $('#banner_text').children();
		
	var current = false;
	var dir = +1;
	
	banner.find('.cursor').click(function(){
		dir = $(this).attr('rel');
		var el = current[dir == 1 ? 'next' : 'prev']();
		if(!el.length)
			el = banner_img[dir == 1 ? 'first' : 'last']();
		desactive(banner_img.index(el));
	});
	
	var desactive = function(index){
		current.animate({left:dir * -350}, 200, function(){
			activate(index);
		});
		banner_text.eq(banner_img.index(current)).animate({top:dir * -100}, 100, function(){
			$(this).hide();																																								 
		});
	};
	
	var activate = function(index){
		current = banner_img.eq(index);
		current.css({'left': dir * 350, 'top':20}).animate({top:0, left:0}, 300);
		banner_text.eq(index).css('top', dir * 10).show().animate({top:0}, 200);
	};
	activate(Math.floor(Math.random()*5));
}

function representative(){
	if(!$('#representative').length) return false;
	
	var bck = function(num){
		return {'background-position': (num%10)*-540 + 'px ' + parseInt(num/10)*-480 + 'px'};
	}
	
	var map = $('#brasil_map');
	$('#brasil_area area').each(function(i){
		var div, tooltip,
				t = $(this),
				pos = $(this).attr('alt').split(',');
		t.data('title', t.attr('title')).attr('title', '')
			.hover(function(){
				if(!tooltip)
					tooltip = $('<div class="tooltip"><span class="left"></span><em>' + t.data('title').replace('|', '<br />') + '</em><span class="right"></span></div>').insertAfter(map);
				tooltip.stop(true).css({'top':parseInt(pos[0])-60, 'left':parseInt(pos[1]) - tooltip.width()/2}).animate({top:'-=20'}, 100);
				if(!div)
					div = $('<div />').css(bck(i+1)).appendTo(map);
				div.stop(true).fadeTo(100, .6);
			}, function(){
				tooltip.stop(true).remove();
				tooltip = false;
				div.stop(true).fadeOut(300, function(){
					div.remove();
					div = false;
				});
			})														 
	});
	
	var current = document.location.href;
	var select = function(){
		$('#brasil_content').css('opacity', 0).fadeTo(200, 1);
		
		$('#brasil_area area').each(function(i){
			if(current.match(new RegExp($(this).attr('href') + '?'))){
				$('<div class="selected" />').css(bck(i+1)).appendTo($('#brasil_map').children().remove().end());
				return false;
			}
		});
	};
	
	$('#brasil area').click(function(){
		current = $(this).attr('href');
		$('#brasil_content').load(current + '?ajax', select);			
		return false;
	});	
	
	select();
}


function content_fn(){
	// BACK
	$('.back').click(function(){
		window.history.back();
		return false;
	});
	
	// BANNER
	banner();
	
	// REPRESENTATIVE
	representative();
	
	// TABS
	var content = $('#cont_details');
	if(content.hasClass('tabs')){
		var loc = document.location.href.replace(href_base, '');
		
		var current;
		var current_link = $();
		$.address.change(function(event){
			event.value = event.value.replace(/\//g, '');
			if(!event.value)
				event.value = loc.replace(/^.*?\/([^\/]+)\/?$/, '$1');
			
			var change = function(){
				current = content.children('.' + event.value).css({left:-530, display:'block'});
				content.height(current.height() * 1.05);
				current.animate({left:0}, 300);
			};
			
			if(!current){
				$('#sidebar a').click(function(){
					if(!loc.match(/t\/?(#.*)?$/)){
						$(this).attr('href', $(this).attr('href').replace(/^(.*?)\/([^\/]*)\/?$/, '$1/t#$2'));
					}else{
						current_link.removeClass('select');			
						current_link = $(this).addClass('select');
						$.address.value($(this).attr('href').replace(/^([^\/]+\/){2}/, ''));	
						return false;
					}
				}).each(function(){
					if($(this).attr('href').replace(/^.*?\/([^\/]*)\/?$/, '$1') == event.value)
						current_link = $(this).addClass('select');
				});	
				change();
			}else{
				current.stop(true).animate({left:550}, 200, function(){
					change();																											
				});
			}
		});
	}
}

$(function(){
	// BCK
	$('<img />').load(function(){
		$('#bck_img').css({'left': 100, 'background-image':'url(' + $(this).attr('src') + ')'})
			.animate({left:0}, 300);
	}).attr('src', 'images/bck/img0' + (Math.floor(Math.random()*4) + 1) + '.png');
	
	var people = $('#home').length || $('#rh').length;
	$('<img />').load(function(){
		$('#bck-footer_img').css({'bottom': 100, 'background-image':'url(' + $(this).attr('src') + ')'})
			.animate({bottom:0}, 300);
	}).attr('src', 'images/bck/footer' + (people ? '_people' : '') + '.png');
					 
	// LINKS
	$('a').hover(function(){
		$(this).stop().fadeTo(300, .8);
	}, function(){
		$(this).stop().fadeTo(200, 1);
	});
	
	// INPUT
	$('#news input').focus(function(){
		$(this).parents('p').children('label').stop(true).fadeOut(100);
	}).blur(function(){
		$(this).parents('p').children('label').stop(true)[$(this).val().empty() ? 'fadeIn' : 'fadeOut'](100);
	}).blur();
	
	// fANCYBOX
	$('a[rel=lightbox]').fancybox({
		'titlePosition':'over',
		'padding':8
	});
	// CURE
	$('#footer .cure').css('opacity', 0);
	$('#footer .cure').mouseover(function(){ 
      $(this).stop().animate({opacity: '1'}, 500); 
   });

	$('#footer .cure').mouseout(function(){ 
      $(this).stop().animate({opacity: '0'}, 1000); 
   });
});
