$( function()
{
	// Acao do Botao Menu
	$( "#menu.abrir li.menu" ).unbind().click( function()
	{
		var $this = $( this );
		$( "div.menu" ).slideToggle( "slow" , function()
		{
			$this.toggleClass( "ativo" );	
		});
		
		return false;
	});
	
	/****************** [Início] Incoerência com functions_deslizante.js ***************** */
		/*$( "#menu li.sobre" ).hover(
			function()
			{
				var ul = $( this ).children( "ul" );
				if( ul.is( ":hidden" ) )
				{
					ul.slideDown();
				}
			},
			function()
			{
				var ul = $( this ).children( "ul" );
				if( ul.is( ":visible" ) )
				{
					ul.slideUp();
				}
			}
		);*/
	/****************** [FIM] Incoerência com functions_deslizante.js ***************** */
	
	
	// Botao Recolher
	$( "a.bt_recolher" ).unbind().click( function()
	{
		var $this = $( "#menu li.menu" );
		$( "div.menu" ).slideUp( "slow" , function()
		{
			$this.removeClass( "ativo" );	
		});
		
		return false;
	});
	
	// Funcionalidade das abas
	$( "div.menu > #aba > li" ).unbind().click( function()
	{
		var $this = $( this );
		var pai = $this.parent()
		var classe = $this.attr( "class" );
		
		// Trocar de Class na UL para monstrar o menu ativo
		pai.attr( "class", classe );
		
		var conteudo = pai.siblings( ".conteudo" );
		conteudo.children( "div:visible" ).fadeOut( "normal" , function()
		{
			conteudo.find( "div." + classe ).fadeIn();
		});
		
		return false;
	});
	
	$( "div.menu div.topo ul > li > a" ).unbind().click( function()
	{
		var $this = $( this );
		var classe = $this.attr( "class" );
		var pai = $this.parents( "div" );
		var superPai = pai.parent().parent();
		var superClass = "." + superPai.attr( "class" ) + " ";
		
		// Troca o Conteudo
		var interno = $( "div.menu " + superClass + " div.interno" );
		interno.fade( classe );
		
		// Troca a parte do topo
		var topo = $( "div.menu " + superClass + " div.topo" );
		topo.fade( classe );
		
		return false;
	});
	

	// Pagina favorita
	var tituloCheckbox = $( "#tituloCheckbox" );
	var cookie = $.cookie( "paginainicial" );
	var url = document.location.href;

	//Alteração efetuada por Marlon Marmentini dia 25/11/2008
	//url = url.split( "/" );
	//url = url[ url.length -2 ] + "/" + url[ url.length -1 ];
	
	if( cookie && cookie == url )
	{
		tituloCheckbox.children( "a" ).show();
		tituloCheckbox.toggleClass( "ativo" );
		tituloCheckbox.children( "input" ).attr( "checked", true );
	}
	
	tituloCheckbox.children( "input" ).click( function()
	{
		// Adicionar, remover a classe ativo
		$( this ).parent().toggleClass( "ativo" );	
		
		if( this.checked )
		{
			tituloCheckbox.children( "a" ).show();
			
			$.cookie( "paginainicial", url, { expires: 30, path: '/', domain: 'caixa.gov.br' } );
		}
		else
		{
			tituloCheckbox.removeClass( "ativo" );
			tituloCheckbox.children( "a" ).hide();
			$.cookie( "paginainicial", '', { path: '/' , domain: 'caixa.gov.br' } );
		}
	});
	
	tituloCheckbox.children( "a" ).click( function()
	{
		tituloCheckbox.removeClass( "ativo" );
		tituloCheckbox.children( "input" ).attr( "checked", false );
		$( this ).hide();
		$.cookie( "paginainicial", '', { path: '/' , domain: 'caixa.gov.br'} );
		
		return false;
	});

});

$.fn.fade = function( classe )
{
	var	$this = this;
	$this.children( "div:visible" ).fadeOut( "normal" , function()
	{
		$this.find( "div." + classe ).fadeIn();
	});
}

// Responsável por fazer e trazer os cookies
jQuery.cookie = function(name, value, options)
{
	if( typeof value != 'undefined' )
	{
		// name and value given, set cookie
		options = options || {};
		var expires = '';
		if( options.expires && (typeof options.expires == 'number' || options.expires.toGMTString ))
		{
			var date;
			if ( typeof options.expires == 'number' )
			{
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			}
			else
			{
				date = options.expires;
			}
			
			expires = '; expires=' + date.toGMTString(); // use expires attribute, max-age is not supported by IE
		}
		
		var path = options.path ? '; path=' + options.path : '';
		var domain = options.domain ? '; domain=' + options.domain : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
		
		var objetoDocument = document.getElementById("cookieIf");
		var dominio = "";
		
		//COOKIES ENTRE AMBIENTES
		//alert((window.location.href.indexOf("http://" + js_institucional)<0));
		if(window.location.href.indexOf("http://" + js_institucional)>0){
			dominio = "http://" + js_transacional;		 
		}else{
			dominio = "http://" + js_institucional;		 
		}

		var objConteudo = document.getElementById('icaixa');

		if(document.getElementById('cookieIf')){
			document.getElementById('cookieIf').src = dominio + "_newincludes/cookie_adm.asp?valor=" + encodeURIComponent(value);
		}else{
			objConteudo.innerHTML = objConteudo.innerHTML + "<iframe name='cookieIf' id='cookieIf' width='550' heigth='550' style='display:none' src='" + dominio + "/_newincludes/cookie_adm.asp?valor=" + encodeURIComponent(value) + "'></iframe>";	
		}
	}
	else
	{
		var cookieValue = '';
		if ( document.cookie && document.cookie != '' )
		{
			var cookies = document.cookie.split(';');
			for ( var i = 0; i < cookies.length; i++ )
			{
				var cookie = jQuery.trim(cookies[i]);
				if ( cookie.substring( 0, name.length + 1 ) == ( name + '=' ) )
				{
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
}