;(function ($, window, undefined) {
	'use strict';

	var $doc = $(document),
			Modernizr = window.Modernizr;

	$(document).ready(function() {
		$.fn.foundationAlerts           ? $doc.foundationAlerts() : null;
		$.fn.foundationButtons          ? $doc.foundationButtons() : null;
		$.fn.foundationAccordion        ? $doc.foundationAccordion() : null;
		$.fn.foundationNavigation       ? $doc.foundationNavigation() : null;
		$.fn.foundationTopBar           ? $doc.foundationTopBar() : null;
		$.fn.foundationCustomForms      ? $doc.foundationCustomForms() : null;
		$.fn.foundationMediaQueryViewer ? $doc.foundationMediaQueryViewer() : null;
		$.fn.foundationTabs             ? $doc.foundationTabs({callback : $.foundation.customForms.appendCustomMarkup}) : null;
		$.fn.foundationTooltips         ? $doc.foundationTooltips() : null;
		$.fn.foundationMagellan         ? $doc.foundationMagellan() : null;
		$.fn.foundationClearing         ? $doc.foundationClearing() : null;

		$.fn.placeholder                ? $('input, textarea').placeholder() : null;
	});

	// UNCOMMENT THE LINE YOU WANT BELOW IF YOU WANT IE8 SUPPORT AND ARE USING .block-grids
	// $('.block-grid.two-up>li:nth-child(2n+1)').css({clear: 'both'});
	// $('.block-grid.three-up>li:nth-child(3n+1)').css({clear: 'both'});
	// $('.block-grid.four-up>li:nth-child(4n+1)').css({clear: 'both'});
	// $('.block-grid.five-up>li:nth-child(5n+1)').css({clear: 'both'});

	// Hide address bar on mobile devices (except if #hash present, so we don't mess up deep linking).
	if (Modernizr.touch && !window.location.hash) {
		$(window).load(function () {
			setTimeout(function () {
				window.scrollTo(0, 1);
			}, 0);
		});
	}

})(jQuery, this);
jQuery(document).ready(function($){
	if($('tbody tr').length>0){
		$('tbody tr').hover(function(){
			$(this).css({'background-color':'#E8E8E8'});
		},function(){
			$(this).css({'background-color':'#FFF'});
		});
	}
	if($('[name="registrado"]').length>0){
		var valor = null;
		$('[name="registrado"]').change(function(){
			if(valor === null || valor !== $(this).val()){
				if(valor !== null)
					$('#oculto'+valor).toggle();
				valor = $(this).val();
				$('#oculto'+valor).toggle();
			}
		});
	}
	if($('[name="patente"]').length>0){
		$('[name="patente"]').keyup(function(){
			var texto = $(this).attr('value').toLowerCase(),
				expresion = /^[a-z]{2}[\-][0-9]{2}[\-][0-9]{2}$|^[b-d,f-h,j-l,p,r-t,v-z]{2}[\-][b-d,f-h,j-l,p,r-t,v-z]{2}[\-][0-9]{2}$/;
			if(texto.match(expresion) && texto!=='' && texto!==null){
				var elemento = $(this);
				$(this).css({'background-color':'#B7EEAE'}).each(function(){
					if(typeof fondo == 'function')
						fondo(elemento);
				});
			}else{
				$(this).css({'background-color':'#FFD2CD'});
			}
		});
	}
	if($('[name="correo"]').length>0){
		$('[name="correo"]').keyup(function(){
			var texto = $(this).attr('value'),
				expresion = /^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/;
			if(texto.match(expresion) && texto!=='' && texto!==null){
				$(this).css({'background-color':'#B7EEAE'});
			}else{
				$(this).css({'background-color':'#FFD2CD'});
			}
		});
	}
	if($('[name="rContrasena"]').length>0){
		$('[name="rContrasena"]').keyup(function(){
			var cNueva = $('[name="cNueva"]').attr('value'),
				rContrasena = $(this).attr('value');
			if(cNueva===rContrasena && cNueva!=='' && cNueva!==null){
				$(this).css({'background-color':'#B7EEAE'});
			}else{
				$(this).css({'background-color':'#FFD2CD'});
			}
		});
	}
	if($('[name="pago"]').length>0){
		$('[name="pago"]').on('change keyup',function() {
			var monto = $('[name="monto"]').attr('value'),
				pago = $(this).attr('value'),
				vuelto = pago - monto;
			if(vuelto<0){
				vuelto = 0;
			}
			$('[name="vuelto"]').attr('value',vuelto);		});
	}
	if($('#project').length>0){
		xajax_autocomplete();
	}
	if($('#generar').length>0){
		$('[type="submit"]').click(function(e){
			var expFecha = /^[0-9]{2}-[0-9]{2}-[0-9]{4}$/, f_inicio = $('[name="f_inicio"]').attr('value'), f_termino = $('[name="f_termino"]').attr('value'), error = '';
			if(!f_inicio.match(expFecha) || !fechaMenor(f_inicio,getToday())){
				error += '<div>Error en la primera fecha ingresada.</div>';
			}
			if(!f_termino.match(expFecha) || !fechaMenor(f_termino,getToday())){
				error += '<div>Error en la segunda fecha ingresada.</div>';
			}
			if(!fechaMenor(f_inicio,f_termino)){
				error += '<div>La primera fecha es mayor a la segunda.</div>';
			}
			if($('[name="informe"]:checked').length === 0){
				error += '<div>No se selecciono ning&uacute;n informe.</div>';
			}
			if(error!==''){
				$('html, body').animate({scrollTop:0},'slow');
				$('#mError').slideUp('slow',function(){
					$(this).html(error).slideDown();
				});
				e.preventDefault();
			}else{
				$('#mError').slideUp();
			}
		});
	}
});
function autocomplete(recursos){
	$("#project").autocomplete({
		minLength: 0,
		source: recursos,
		focus: function( event, ui ) {
			$( "#project" ).val( ui.item.label );
			return false;
		},
		select: function( event, ui ) {
			$( "#project" ).val( ui.item.label );
			$( "#project-id" ).val( ui.item.value );

			return false;
		}
	})
	.data("ui-autocomplete")._renderItem = function( ul, item ) {
		return $("<li>")
			.append("<a>ID: " + item.value + " - Nombre: " + item.label + "</a>")
			.appendTo(ul);
	};
}
function fechaMenor(fMenor,fMayor){
	fMenor = new Date(fMenor.substring(6,10),fMenor.substring(3,5)-1,fMenor.substring(0,2));
	fMayor= new Date(fMayor.substring(6,10),fMayor.substring(3,5)-1,fMayor.substring(0,2));
	if(fMayor >= fMenor){
		return true;
	}else{
		return false;
	}
}
function getToday(){
	var temp = new Date();
	// var dateStr = padStr(temp.getHours()) + ":" +
	// 			padStr(temp.getMinutes()) + ":" +
	// 			padStr(temp.getSeconds()) + " " +
	// 			padStr(temp.getDate()) + "-" +
	// 			padStr(1 + temp.getMonth()) + "-" +
	// 			padStr(temp.getFullYear());
	var dateStr = padStr(temp.getDate()) + "-" +
				padStr(1 + temp.getMonth()) + "-" +
				padStr(temp.getFullYear());
	return dateStr;
}
function padStr(i) {
	return (i < 10) ? "0" + i : "" + i;
}
// function cargando(){
// 	$('#mAcierto,#mError').hide();
// 	$('[type="submit"],[type="button').attr('disabled','disabled');
// }
// function completo(){
// 	$('[type="submit"],[type="button').removeAttr('disabled');
// }