Indica.messages.appMessage.hideAll = true;

Indica.messages.appMessage = function( titulo, msg, tipo, hideAll ){
	var t = tipo || 1;

	if( t == 1 ){
		alert( msg );

		var cb = Indica.messages.hideAppMessage.callback;

		if( cb ){
			cb.apply();
		}

		return true;
	}

	Indica.messages.appMessage.hideAll = typeof hideAll == "undefined" ? true : hideAll;
	
	NOIX.get( 'titulo_mensagens' ).innerHTML = titulo;
	NOIX.get( 'texto_mensagens' ).innerHTML = msg;

	jQuery( "#alerta" ).removeClass( 'alerte_box' );
	jQuery( "#alerta" ).addClass( 'alerte_box_' + t );

	NOIX.lightbox.show( '#box_mensagens' );
	
	centralizar();
};
 
Indica.messages.hideAppMessage = function(){
	if( Indica.messages.appMessage.hideAll ){
		NOIX.lightbox.hideAll();
	}
	else{
		NOIX.lightbox.hide( '#box_mensagens' );
	}
	
	var cb = Indica.messages.hideAppMessage.callback;

	if( cb ){
		cb.apply();
	}
}

Indica.messages.confirmMessage = function( msg ){
	return confirm( msg );
};

Indica.messages.serverMessage = function( response ){
	if( response.getMessage() ){
		alert( response.getMessage() );
	}
};

Indica.messages.ajaxShowMessage = function( msg ){
	NOIX.get( 'texto_aguarde' ).innerHTML = msg;
	NOIX.lightbox.show( '#box_aguarde' );
};

Indica.messages.ajaxHideMessage = function(){
	NOIX.lightbox.hide( '#box_aguarde' );
};

Indica.showForm = function( tipo, cor ){
	jQuery( '.menu_principal ul li a.selecionado' ).removeClass( 'selecionado' );
	jQuery( '.menu_principal ul li.' + tipo + ' a' ).addClass( 'selecionado' );
	
	NOIX.get( 'menu_formulario_fundo' ).className = 'menu_formulario_fundo_' + cor
	NOIX.get( 'bt_ok' ).className = 'txtIndent bt_ok_' + cor;
	NOIX.get( 'bt_cadastre_se' ).className = 'txtIndent bt_cadastre_se_' + cor;
	
	switch( cor ){
		case Indica.COR_EMPRESA:
			NOIX.get( 'bt_cadastre_se' ).href = Indica.siteUrl + '/cadastroempresa';
			var tipoHidden = Indica.TIPO_EMPRESA;
		break;
		
		case Indica.COR_INDICADOR:
			NOIX.get( 'bt_cadastre_se' ).href = Indica.siteUrl + '/cadastroindicador';
			var tipoHidden = Indica.TIPO_INDICADOR;
		break;
		
		case Indica.COR_CANDIDATO:
			NOIX.get( 'bt_cadastre_se' ).href = Indica.siteUrl + '/cadastrocandidato';
			var tipoHidden = Indica.TIPO_CANDIDATO;
		break
	}
	
	NOIX.get( 'tipo_login' ).value = tipoHidden;
};

Indica.grid = {

	_grids: {},

	get: function( id ){
		return this._grids[ id ];
	},

	create: function( id, url ){
		this._grids[ id ] = new Indica.gridInternal( id, url );
	}
};

Indica.gridInternal = function(){
	this.init.apply( this, arguments );
};

Indica.gridInternal.prototype = {

	id: null,

	url: null,

	_keyword: '',

	_campoOrdenacao: '',

	_tipoOrdenacao: 'ASC',

	ordens: {},

	pars: {},

	init: function( id, url ){
		this.id = id;
		this.url = url;
	},

	buscar: function( keyword ){
		this._keyword = keyword || '';
		this.execAjax();
	},

	ordenar: function( campo, t ){
		var tipo = t || 'ASC';

		if( campo != this._campoOrdenacao ){
			tipo = 'ASC';
		}
		else{
			tipo = ( this.ordens[ campo ] == 'ASC' ) ? 'DESC' : 'ASC';
		}

		this._campoOrdenacao = campo;
		this._tipoOrdenacao = tipo;

		this.ordens[ campo ] = tipo;

		this.execAjax();
	},

	setParam: function( key, value ){
		this.pars[ key ] = value;
	},

	getParam: function( key ){
		return this.pars[ key ];
	},

	resetParams: function(){
		this.pars = {};
	},

	execAjax: function(){
		var that = this;

		var pars = this.pars;
		pars['text'] = this._keyword;
		pars['campoOrdenacao'] = this._campoOrdenacao;
		pars['tipoOrdenacao'] = this._tipoOrdenacao;
		pars['_ajax'] = 1;

		jQuery.ajax(
		{
			url: this.url,

			data: pars,

			beforeSend: function(){
				Indica.messages.ajaxShowMessage( 'Aguarde...' );
			},

			complete: function(e){
				Indica.messages.ajaxHideMessage();
				NOIX.get( 'grid_' + that.id ).innerHTML = e.responseText;
			},

			type: 'post'
		}
		);
	}
};

jQuery( document ).ready(
	function(){
		jQuery( '#menu h3' ).click(
			function(){
				this.className = ( this.className == 'menu_mais' ) ? 'menu_menos' : 'menu_mais';
				
				if( this.className == 'menu_mais' ){
					jQuery( '~ ul', this ).slideUp();
				}
				else{
					jQuery( '~ ul', this ).slideDown();
				}
			}
		).each(
			function(){
				jQuery( '~ ul', this ).hide();
			}
		);
	}
);

Indica.selectCollection = function(){
	this.init.apply( this, arguments );
};

Indica.selectCollection.prototype = {

	init: function( strOptions ){
		eval( 'var strOptions = ' + strOptions );

		this.original = strOptions;
		this.selects = {};
		this.selectedOptions = {};
	},

	add: function( idSelect ){
		if( typeof this.selects[ idSelect ] == 'undefined' ){
			this.selects[ idSelect ] = new Indica.select( idSelect );
		}
	},

	get: function( idSelect ){
		return this.selects[ idSelect ];
	},

	selectOption: function( valueOption, idSelect ){
		this.selectedOptions[ idSelect ] = valueOption;
		this.refresh();
	},

	refresh: function(){
		for( i in this.selects ){
			var s = this.selects[i];
			s.refresh( this.original, this.selectedOptions );
		}
	}
};

Indica.select = function(){
	this.init.apply( this, arguments );
};

Indica.select.prototype = {

	init: function( idSelect ){
		this.id = idSelect;
		this.el = NOIX.get( idSelect );
	},

	foiSelecionado: function( option, selecteds ){
		for( s in selecteds ){
			if( selecteds[s] == option ){
				return true;
			}
		}

		return false;
	},

	refresh: function( original, selecteds ){
		var sel = this.el.value;

		this.el.clear();

		for( i in original ){
			var optOriginal = original[i];

			if( original[i] == '' || !this.foiSelecionado( optOriginal, selecteds ) || ( this.foiSelecionado( optOriginal, selecteds ) && selecteds[ this.id ] == optOriginal ) ){
				this.el.append( i, optOriginal );

				if( sel == optOriginal ){
					this.el.options[ this.el.length-1 ].selected = true;
				}
			}
		}
	}

};

Indica.esqueciminhasenha = function(){
	var flasHome = jQuery( '.flash_home' );
	
	if( flasHome.get(0) ){
		flasHome.get(0).style.visibility = 'hidden';
	}
	
	NOIX.lightbox.show( '#esqueciminhasenha' );
};

Indica.esqueciminhasenha.enviar = function(){
	var email = NOIX.get( 'esqueciEmail' ).value;
	
	if( email == '' ){
		Indica.messages.appMessage( '', 'Informe o seu email' );
		return false;
	}

	jQuery.ajax({
		url: Indica.siteUrl + '/index/esqueciminhasenha',
		
		data: {
			email: email
		},
		
		beforeSend: function(){
			Indica.messages.ajaxShowMessage( 'Aguarde...' );
		},
		
		complete: function(e){
			Indica.messages.ajaxHideMessage();
			
			eval( 'var response = ' + e.responseText );
			
			Indica.messages.appMessage( '', response.message );
			
			Indica.esqueciminhasenha.fechar();
		},
		
		type: 'POST'
	});
};

Indica.esqueciminhasenha.fechar = function(){
	NOIX.lightbox.hide( '#esqueciminhasenha' );
	
	var flasHome = jQuery( '.flash_home' );
	
	if( flasHome.get(0) ){
		flasHome.get(0).style.visibility = 'visible';
	}
};

Indica.executaAlertaUnload = true;

Indica.alertaUnload = function(){	
	window.onbeforeunload = function(ev){
		if( Indica.executaAlertaUnload ){
			return "Se você sair perderá as informações não salvas. Deseja continuar?";
		}
	}
};

Indica.data = {
	JANEIRO: 1,
	FEVEREIRO: 2,
	MARCO: 3,
	ABRIL: 4,
	MAIO: 5,
	JUNHO: 6,
	JULHO: 7,
	AGOSTO: 8,
	SETEMBRO: 9,
	OUTUBRO: 10,
	NOVEMBRO: 11,
	DEZEMBRO: 12,
	FEVEREIRO_BISSEXTO: 29,
	FEVEREIRO_COMUM: 28,

	verificaDataNascimento: function( dd, mm, yyyy ){
		var day = new Number( dd );
		var mounth = new Number( mm );
		var year = new Number( yyyy );
		var ultimoAno = new Date();
		var firstYear = ultimoAno.getFullYear() - 90;

		if( day > 31 || day < 1 ){
			return false;
		}

		if( mounth > 12 || mounth < 1 ){
			return false;
		}

		if( year < firstYear || year > ultimoAno.getFullYear() ){
			return false;
		}

		var mesesCom31Dias = [
			Indica.data.JANEIRO,
			Indica.data.MARCO,
			Indica.data.MAIO,
			Indica.data.JULHO,
			Indica.data.AGOSTO,
			Indica.data.OUTUBRO,
			Indica.data.DEZEMBRO
		];
		if( jQuery.inArray( mounth, mesesCom31Dias ) ){
			if( mounth > 31 ){
				return false;
			}
		}
		else{
			if( mounth == Indica.data.FEVEREIRO ){
				if( year % 4 == 0 ){
					if( day > Indica.data.FEVEREIRO_BISSEXTO ){
						return false;
					}
				}
				else
					if( day > Indica.data.FEVEREIRO_COMUM ){
						return false;
					}
			}
		}

		return true;
	}
};