// --------- Slider -------------

function slide() {
	$('#slider1').cycle({
		fx : 'scrollRight',
		next : '#slider1',
		width : 651,
		timeout : 1000,
		fit : 1,
		speed : 400,
		next : '#next2', // ignorar, manter
		prev : '#prev2', // ignorar, manter
		after : showSlide2
	});
}

function showSlide2(curr, next, opts) {

	var nSlide = opts.currSlide;

	$('#slider1').cycle('pause');

	setTimeout(function() {
		$('#slider2 > div').eq(nSlide).css({
			right : "-400px"
		})
		$('#slider2 > div').eq(nSlide).show();
		$('#slider2 > div').eq(nSlide).animate({
			right : "+=400"
		}, 500);
	}, 2000);
	setTimeout(function() {
		$('#slider2 > div').eq(nSlide).fadeTo(600, 0, function(){
			$(this).hide();
		});
		
	}, 8000);
	setTimeout(function() {
		$('#slider1').cycle('resume');
	}, 9000);
}

function slideToggle(el, bShow) {
	var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");

	// if the bShow isn't present, get the current visibility and reverse it
	if(arguments.length == 1)
		bShow = !visible;

	// if the current visiblilty is the same as the requested state, cancel
	if(bShow == visible)
		return false;

	// get the original height
	if(!height) {
		// get original height
		height = $el.show().height();
		// update the height
		$el.data("originalHeight", height);
		// if the element was hidden, hide it again
		if(!visible)
			$el.hide().css({
				height : 0
			});
	}

	// expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
	if(bShow) {
		$el.show().animate({
			height : height
		}, {
			duration : 250
		});
	} else {
		$el.animate({
			height : 0
		}, {
			duration : 250,
			complete : function() {
				$el.hide();
			}
		});
	}
}

$(function() {

	// Define overriding method.
	jQuery.fx.prototype.hide = function() {

		// Remember where we started, so that we can go back to it later
		this.options.orig[this.prop] = jQuery.style(this.elem, this.prop);
		this.options.hide = true;

		// Begin the animation
		this.custom(this.cur(), 1);
	}

	//Animação dos blocos de apresentação.
	$(".container").hover(function() {
		$(this).find("div.subtitle").slideDown('4000');
	}, function() {
		$(this).find("div.subtitle").slideUp('4000');
	});
	$(".container").click(function() {
		var link = $(this + ' > a').attr('href');
	});
	//Inicia as tabs
	$("#tabs1").tabs();
	$("#tabs2").tabs();
	$("#tabs3").tabs();

	//Inicia Slider
	if($("#slider1").length > 0) {
		slide();
	}
	function isset(varname) {
		if( typeof (window[varname] ) != "undefined")
			return true;
		else
			return false;
	}

	function insertAfter(parent, node, referenceNode) {
		parent.insertBefore(node, referenceNode.nextSibling);
	}

	//Inicia o Slider das Notícias
	if($("#news_slider").length > 0) {
		//Remover o SPAN
		$("#news_slider div.ca").unwrap();
		
		//Aplicar Quebras nos divs dentro do li.
		(function($) {
			$.fn.wrapChildren = function(options) {
				options = $.extend({
					childElem : undefined,
					groupSize : 1,
					wrapper : '<li>'
				}, options || {});

				if(options.childElem === undefined)
					return this;

				return this.each(function() {
					var elems = $(this).children(), len = pos = 0;

					elems.each(function(i, value) {
						len += $(value).is(options.childElem) ? 1 : 0;
						if(len > 0 && (len % options.groupSize === 0) || (i === elems.length - 1)) {
							elems.slice(pos, i + 1).wrapAll(options.wrapper);
							len = 0;
							pos = i + 1;
						}
					});
				});
			}
		})(jQuery);
		
		$('#news_slider').wrapChildren({
			childElem : 'div',
			groupSize : 3
		});

		//Alterar as Classes do News Slider
		$('#news_slider li > div:first-child').addClass('t').removeClass('ca');
		$('#news_slider li > div:last-child').addClass('b').removeClass('ca');

		//Slider das noticias
		$('#news_slider').cycle({
			fx : 'scrollHorz',
			timeout : 0,
			speed : 'slow',
			nowrap : 1,
			next : '#next1',
			prev : '#prev1',
			easing : 'easeInOutSine',
			fit : 0,
			width : 289,
			//sync:   1,
			delay : -1000
		});
	}

	if($("#lateral_banner").length > 0) {
		$('#lateral_banner').cycle({
			fx : 'fade',
			speed : 2000,
			next : '#lateral_banner',
			next : '#next3', // ignorar, manter
			prev : '#prev3', // ignorar, manter
			timeout : 8000,
			fit : 1,
			width : 300,
			pause : 1
		});
	}

	//Link para apresentar imagens em fancybox
	$("a#fancybox").fancybox();

	//Links para apresentar videos do Youtube
	$("a#fancybox_video").click(function() {
		$.fancybox({
			'padding' : 0,
			'autoScale' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'title' : this.title,
			'width' : 680,
			'height' : 495,
			'href' : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type' : 'swf',
			'swf' : {
				'wmode' : 'transparent',
				'allowfullscreen' : 'true'
			}
		});
		return false;
	});
	//Aplicar o link ao li apartir do filho a
	$(".li_get_a").click(function() {
		var url = $(this).find('a').attr('href');
		window.location = url;
		return false;
	});
	// Menu de topo
	$('.top_menu ul li.parent').bind({
		mouseover : function() {
			if($(this).children('.block04').length) {
				// get li width
				var width = $(this).children('a').css('width');
				var center = width.slice(0, -2) / 2;
				// get block width
				var bwidth = $(this).children('table').css('width');
				var bcenter = bwidth.slice(0, -2) / 2;
				// get li coordinates
				var coord = $(this).offset();
				var top = coord.top + 20;
				var left = coord.left + center - bcenter + 10;

				// Debug
				//$('.debug').show();
				//$('.debug').append('Top: '+ top + ' - Left:' + left + ' - Center:' + center + ' - Center b:' + bcenter + ' - Coord Top:' + coord.top + ' - Coord Left:' + coord.left + '<br />');
				/*
				 $(this).children('.block04').offset({
				 top : top,
				 left : left
				 });
				 $(this).children('.block04').show();*/

				$(this).children('.block04').css("top", top).css("left", left).show();
			}
		},
		mouseout : function() {
			$(this).children('.block04').offset({
				top : 0,
				left : 0
			});
			$(this).children('.block04').hide();
		}
	});

	//Corregir a limitação do IE a nivel de "":hover in parent element"
	$('.modulo').bind({
		mouseover : function() {
			$(this).children('.top').css('background-image', 'url(media/layout/modulo_top_over.png)');
			$(this).children('.bottom').css('background-image', 'url(/media/layout/modulo_bottom_over.png)');
			$(this).children('.top').css('color', '#f1edea');
		},
		mouseout : function() {
			$(this).children('.top').css('background-image', 'url(/media/layout/modulo_top.png)');
			$(this).children('.bottom').css('background-image', 'url(/media/layout/modulo_bottom.png)');
			$(this).children('.top').css('color', '#ff0000');
		}
	});

	//Corregir a limitação do IE a nivel de "":hover in li element"
	$('.noticias li .stage').bind({
		mouseover : function() {
			$(this).css('border', '1px solid #898077');
		},
		mouseout : function() {
			$(this).css('border', '1px solid #ffffff');
		}
	});

	$('input.button').bind({
		mouseover : function() {
			$(this).css('background-image', 'url(media/layout/form/btn_over.png)');
		},
		mouseout : function() {
			$(this).css('background-image', 'url(media/layout/form/btn.png)');
		}
	});

	// Formatar Select Box

});

