$( document ).ready( function() {
	
	// BROWSER DETECTION
	// Figure out what browser is being used
	var userOSAgent = navigator.userAgent.toLowerCase();
	jQuery.platform = {
	        mac: /mac/.test(userOSAgent),
	        osx: /mac os x/.test(userOSAgent),
	        win: /win/.test(userOSAgent),
	        linux: /linux/.test(userOSAgent)
	};
	// change default foint
	if( jQuery.platform.mac == true )
	{
		$( 'body' ).css( 'fontFamily' , "Courier, Arial, sans-serif");
		$( 'input[type="text"]' ).css( 'fontFamily' , "Courier, Arial, sans-serif");
		$( 'textarea' ).css( 'fontFamily' , "Courier, Arial, sans-serif");
	}
	else if( jQuery.platform.win == true )
	{
		// display old-browsers (IE6 & 7) alert
		jQuery.each(jQuery.browser, function(i) {
			jQuery.each(jQuery.browser, function(i, val) {
				if(val == true)
					$.browser.versionName = i;
			});
			if($.browser.msie){
				if( $.browser.version == '6.0' ||  $.browser.version == '7.0')
					$( ".browser_alert" ).slideDown( 500 );
			}
		});	
	}
	
	// general swap B&W image to COLOR on mouseover
	$( 'a.image_swap' ).hover( function(){
		$( this ).find( 'img.original' ).css( 'display' , 'none' );
		$( this ).find( 'img.hidden_image' ).css( 'display' , 'block' );
	}, function(){
		$( this ).find( 'img.original' ).css( 'display' , 'block' );
		$( this ).find( 'img.hidden_image' ).css( 'display' , 'none' );
	});
	$( '.ministerio a.image_swap' ).hover( function(){
		$( this ).parent().find( 'p.caption:not(.hover)' ).css( 'display' , 'none' );
		$( this ).parent().find( 'p.caption.hover' ).css( 'display' , 'block' );
	}, function(){
		$( this ).parent().find( 'p.caption:not(.hover)' ).css( 'display' , 'block' );
		$( this ).parent().find( 'p.caption.hover' ).css( 'display' , 'none' );
	});
	
	// clean up search fields on focus
	(function($){
		$.fn.clearDefault = function(){
			return this.each(function(){
				var default_value = $(this).val();
				var original_value = $( this ).attr('original_value');
				if( original_value == default_value )
				{
					$(this).focus(function(){
						if ($(this).val() == default_value) $(this).val("");
					});
					$(this).blur(function(){
						if ($(this).val() == "") $(this).val(default_value);
					});
				}
			});
		};
	})(jQuery);
	$('.search input[type="text"]').clearDefault();
	// general dropdown
	$( '.options_container , .options_trigger' ).click( function(){
		$( this ).parents( '.dropdown' ).find( '.options' ).slideToggle(150);
	});
	$( '.dropdown .options li' ).click( function(){
		$res_id = $( this ).attr( 'id' );
		$res_id = $res_id.substr( $res_id.lastIndexOf( '_' ) + 1 );
		$( this ).parents( '.dropdown' ).find( '.hidden_value' ).attr('value',$res_id);
		$( this ).parents( 'form' ).submit();
	});
	// header dropdown
	$( '.header_options_container , .header_options_trigger' ).click( function(){
		$( this ).parents( '.header_dropdown' ).find( '.header_options' ).slideToggle(150);
	});
	$( '.header_dropdown .header_options li' ).click( function(){
		$scope_label = $( this ).parents('.header_dropdown').find( '.scope_label' );
		$scope_id = $( this ).parents( '.header_dropdown' ).find( '.hidden_value' );
		// get NEW scope label & id
		$new_id = $( this ).attr( 'id' );
		$new_label = $( this ).html();
		// get OLD scope label & id
		$old_label = $scope_label.html();
		$old_id = $scope_id.attr('value');
		// set new scope label
		$scope_label.html( $new_label );
		$scope_id.attr('value',$new_id);
		// replace old
		$( this ).attr( 'id' , $old_id );
		$( this ).html( $old_label );
	});
	// form submit button
	$( '.form_submit' ).click( function(){
		$( this ).parents( 'form' ).submit();
	});
	
/*
	if( $('.datepicker').hasClass( 'datepicker' ) )
	{
		$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['']));
		// $('.datepicker').datepicker( $.datepicker.regional['pt-BR'] );
		// $( '.datepicker' ).datepicker( $.extend( {showMonthAfterYear: false} , $.datepicker.regional['pt-BR'] ) );
		$('.datepicker').datepicker( { dateFormat: 'dd/mm/yy' } );
		$('.datepicker_select').datepicker( { dateFormat: 'dd/mm/yy' , changeMonth: true, changeYear: true } );
		$( '#ui-datepicker-div' ).hide();
	}
	if( $('.datepicker_select').hasClass( 'datepicker_select' ) )
	{
		$.datepicker.setDefaults($.extend({showMonthAfterYear: false}, $.datepicker.regional['']));
		// $('.datepicker').datepicker( $.datepicker.regional['pt-BR'] );
		// $( '.datepicker' ).datepicker( $.extend( {showMonthAfterYear: false} , $.datepicker.regional['pt-BR'] ) );
		var data = new Date();
		var year = data.getFullYear();
		$('.datepicker_select').datepicker( { dateFormat: 'dd/mm/yy' , changeMonth: true, changeYear: true } );
		$('.datepicker_select').datepicker('option', 'yearRange', '1900:'+year);
		$( '#ui-datepicker-div' ).hide();
	}
*/
	
	// GENERAL HOVER COLOR
	$(".cover-entry").mouseover(function(){
		$(this).find('h1.rule-two').addClass('ativo');
	});
	$(".cover-entry").mouseout(function(){
		$(this).find('h1.rule-two').removeClass('ativo');
	});
	
	// JOURNAL HOVER 
	$( '.line-block .entry' ).hover( function(){
		$( this ).find( 'img.original' ).css( {'display': 'none'} );
		$( this ).find( 'img.hidden_image' ).css( {'display': 'block'} );
	}, function(){
		$( this ).find( 'img.original' ).css( 'display' , 'block' );
		$( this ).find( 'img.hidden_image' ).css( 'display' , 'none' );
	});
	// $( '.line-block .entry , .line-pagination .entry' ).hover( function(){
	$( '.entry' ).hover( function(){
		$( this ).find( 'h1, h2, h2 > a, h3:not(.index,.author), p.lead' ).addClass( 'ativo' ).css( {'text-decoration': 'underline'} );
		$( this ).find('h3').addClass( 'ativo' );
	}, function(){
		$( this ).find( 'h1, h2, h2 > a, h3:not(.index,.author), p.lead' ).removeClass( 'ativo' ).css( {'text-decoration': 'none'} );
		$( this ).find('h3').removeClass( 'ativo' );
	});
	
	// FORMAT USER NAME DISPLAY ON PROFILE
	$('.contribution-form .firstname, .contribution-form .lastname').keyup(function() {
		var first = $('.contribution-form .firstname').val();
		var last = $('.contribution-form .lastname').val();
		$('#showname').html( first + ' ' + last );
		$('#showname_cit').html( last.toUpperCase() + ', ' + first );
	});
});