Cufon.replace('.Cufon', {hover:true});
$(document).ready(function(){
						   

	$("a[href*='http://']").click(function(){
		$(this).attr("target", "_blank");
	});
	
	$('#login input').inlineLabels();
	$('#contact-form input, #contact-form textarea').inlineLabels();
	
	var swf = $('#home > #banner').attr("file");
	$('#home > #banner').flash({							   
		src: 'assets/swf/' + swf + '.swf',
		width: 752,
		height: 250,
		wmode: 'transparent'		
	});
	
	$('#box-map').flash({
		src: 'assets/swf/localizacao.swf',
		width: 504,
		height: 354,
		wmode: 'transparent'
	});
	
	$('#box-tabs h3 a').click(function(){
		$('#box-tabs li').removeClass('Active');
		$(this).parent().parent().addClass('Active');
	});	
	
	$('#box-list h3 a').click(function(){
		var panel = $(this).parent().next();
		if(!$(panel).is(":hidden")){
			$(panel).slideUp('fast');
		}else{
			$(panel).slideDown('fast');
		}	
	});
	
	$(window).load(function(){
		$('#associations').carrossel();

		$('.qtip').mouseover(function(){
			return false;
		}).each(function(){
			$(this).qtip({
				content: $(this).attr('title'),
				style: {
					name: 'dark',
					background: '#333',
					width: 260,
					tip: 'bottomLeft',
					fontFamily: 'Arial',
					fontWeight:'normal',
					fontSize: '11px',
					border: { radius: 5 }
				},
				position: {
					corner: {
						target: 'topRight'
					},
					target: 'mouse',
					adjust: {
						mouse: true,
						y: -70
					}
				},
				show: { delay:0, effect: { length:50 } },
				hide: { delay:0, effect: { length:50 } }
			})
		}).attr('title','');
	});
});

jQuery.fn.inlineLabels = function(){
	return this.each(function(){

		var label = jQuery(this).prev('label'), self = jQuery(this);
		function fade(){
			label.fadeTo(300, 0.3);
		};
		function clear(){
			this.value
				? label.hide()
				: label.show() && label.css('opacity') < 1 && label.fadeTo(300, 1);
		};

		label
			.click(fade).click(function(){
				self.focus();
			});

		self
			.focus(fade).bind('keypress blur', clear)
			.trigger('blur');
	});
};

Array.prototype.sum = function() {
  return (!this.length) ? 0 : this.slice(1).sum() +
      ((typeof this[0] == 'number') ? this[0] : 0);
};

function limit(n,min,max){
	return Math.min(max, Math.max(min, n));
}

// carrossel
// ricardobeat@gmail.com
(function($){
$.fn.carrossel = function(){

	var
		el = this,
		container = el.find('.Clip'),
		lista = container.children('ul'),
		itens = lista.find('li'),
		clipWidth = el.innerWidth(),
		totalWidth = 0,
		limitLeft = 0,
		ws = [],
		pos = 0,
		posLeft = 0;

	container.css('overflow', 'hidden');
	var margin = parseInt($(itens[0]).css('marginLeft'), 10);
	itens.each(function(){
		ws.push( $(this).width() + margin );
	})
	totalWidth = ws.sum() + margin;
	lista.width(totalWidth);
	limitLeft = -(totalWidth - clipWidth + parseInt(itens.css('marginLeft'),10));

	function findEdge(v){
		var n = 0;
		if (v>0){
			while(ws[pos] && n-ws[pos++] > -clipWidth){
				if (ws[pos]) n -= ws[pos];
			};
		} else {
			while(ws[pos] && n+ws[pos--] < clipWidth){
				if (ws[pos]) n += ws[pos];
			};
		}
		pos = limit(pos, 0, itens.length-1);
		posLeft = limit(posLeft + n, limitLeft, 0);
		return posLeft;
	}

	var
		$prev = $('<a class="Prev"/>'),
		$next = $('<a class="Next"/>');

	$prev.add($next).click(function(){
		var oldLeft = posLeft;
		if (posLeft >= limitLeft && posLeft <= 0 && lista.is(':not(:animated)'))
			lista.animate({
				left: findEdge( $(this).is('.Next')*2-1 )
			}, Math.max(300, Math.abs(oldLeft-posLeft)*1.2), function(){
				var
					R = posLeft == limitLeft,
					L = posLeft == 0;

					$next.fadeTo(600,R?0.1:1)[(R?'add':'remove')+'Class']('Disabled');
					$prev.fadeTo(600,L?0.1:1)[(L?'add':'remove')+'Class']('Disabled');
			});
	});

	el.append($prev,$next);

	el.mousewheel(function(e){
		var d = (e.delta || e.detail || -e.wheelDelta) > 0;
		(d ? $next : $prev).click();
		return false;
	});

	$prev.addClass('Disabled').fadeTo(600,0.1);
};

})(jQuery);

// jQuery.mousewheel
/* Copyright (c) 2009 Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.2
 *
 * Requires: 1.2.2+
 */
(function(c){var a=["DOMMouseScroll","mousewheel"];c.event.special.mousewheel={setup:function(){if(this.addEventListener){for(var d=a.length;d;){this.addEventListener(a[--d],b,false)}}else{this.onmousewheel=b}},teardown:function(){if(this.removeEventListener){for(var d=a.length;d;){this.removeEventListener(a[--d],b,false)}}else{this.onmousewheel=null}}};c.fn.extend({mousewheel:function(d){return d?this.bind("mousewheel",d):this.trigger("mousewheel")},unmousewheel:function(d){return this.unbind("mousewheel",d)}});function b(f){var d=[].slice.call(arguments,1),g=0,e=true;f=c.event.fix(f||window.event);f.type="mousewheel";if(f.wheelDelta){g=f.wheelDelta/120}if(f.detail){g=-f.detail/3}d.unshift(f,g);return c.event.handle.apply(this,d)}})(jQuery);
