jQuery(document).ready(function(){
	$(".titulo_subportada:contains('Información pública'), .breadcrumb_item_final a:contains('Información pública')").text('Universidad Transparente');
	$(".navegacion_inferior .campus a").html("<a href='/transparencia' title='Acceso a la información institucional pública – Ley de transparencia'>Universidad transparente</a>");
	$('#becaDBE_7_8736 .lista_inicio:first').addClass('caja-destacada');
	$('.portlet_login').login();
	//portlet_enviar_amigo
	$('.portlet_enviar_amigo .titulo h2').text('Enviar');
	$('.portlet_enviar_amigo').each(function(){
		if(window.location.hash.indexOf('#ok') != -1){
			$('#FormularioCorreo').hide();
			$(this).height('100px');
			var oldTop = $('.ventana-enviar-amigo').offset().top;
			$('.ventana-enviar-amigo').css({top:(oldTop - 50) + 'px' });
			setTimeout(function(){
				$('#FormularioCorreo').show();
				$('.portlet_enviar_amigo').removeAttr('style');
				$('.mensaje, .ventana-enviar-amigo').hide();
			}, 4000);
		}
	});
	//correccion acceso facultades fau
	$('.menu_acceso_rapido_facultades ul li:eq(0) a').attr({href:'http://fau.uchile.cl'});
	
	$('.plantilla_74609 form').submit(function(){
		return validarFormSeminarioEducacion(this);
	});

	if(window.location.hash != ""){
		if(window.location.hash == '#ok'){
			alert('Su mensaje fue enviado exitosamente.');
		/*
		}else{
			alert('Ocurrió un error. Intentar nuevamente.');
		*/
		}
	}
	//relacDBE_7_8738
	$('#relacDBE_7_8738 ul li:eq(1)').addClass('btn');
	
	//copiar url corta
	$('.herramientas li.url_corta .copiar').remove();
	//ver mas conv invest
	$('#invest .portlet_convocatorias_vigentes .ver_mas a').attr('href', 'http://www.uchile.cl/portal/presentacion/asuntos-academicos/financiamiento-y-becas');
	
	//multimedia
	//$('#multimedia .portlet-muetra')
	//menu activo general

	//$('.nav_departamentos, .nav_canales, .nav_principal').menuactivo({typeSelector:'class', classNameHome:'home'});
	
	//$('.navegacion_inferior_aux li:eq(6) ').html('<li class="creditos">&copy; <a target="_blank" href="http://bibliotecas.uchile.cl">SISIB</a> - <a class="uchile" href="http://www.uchile.cl">Universidad de Chile</a>, 1994-2012</li>');
	
});

function validarFormSeminarioEducacion(form){
	var inputs = form.getElementsByTagName('input');
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].id != 'button'){
			switch(inputs[i].name){
				case 'mail':
					if(!emailValue(inputs[i].value)){
						inputs[i].focus();
						return false;
					}
				break;
				default:
					if(!minValue(inputs[i].value)){
						inputs[i].focus();
						return false;
					}
				break;
			}
		}
	}
	return true;
}
function emailValue(val){
	var reg = /^[\w\.-]+@[\w\.-]+\.\w+$/i;
	console.log(reg.test(val), val);
	return reg.test(val);
}
function minValue(val){
	if(val == ''){
		return false;
	}
	return true;
	
	
}

/* palnes de estudio */
$(document).ready(function(){
	$('#campos_plan').tabsPlan({
		selector:'parent-tabs'
	});
	//subtabs
	$('#subtab-presentacion').tabsPlan({selector:'child-tabs'});
	$('#subtab-postulacion').tabsPlan({selector:'child-tabs'});
	$('#subtab-plan').tabsPlan({selector:'child-tabs'});
});
(function($){
	$.fn.tabsPlan = function(opt){
		var defaults = {
			instId:'',
			instIdTab:'',
			selector:'',
			init: $.fn.tabsPlan.init,
			find: $.fn.tabsPlan.find,
			tools: $.fn.tabsPlan.tool,
			activesTab:{},
			url: ''
		};
		var options = $.extend(defaults, opt);
		//
		this.each(function(){
			options.instId = this;
			options.instIdTab = $(this).find('ul');
			options.find.findAndMarkTabContent(options);
			$(this).find('> ul').addClass('ul-tab ' + options.selector).find('li').each(function(){
				$(this).bind('click', function(e){
					options.init.setUrl(options, $(this).find('a').attr('href'));
					options.init.setFuntionality(options, $(this).find('a').attr('href'));
				});
			});
			options.init.setHash(options);
			options.init.setFuntionality(options, options.url);
			$(window).bind("history", function(e){
				options.init.setHash(options);
				options.init.setFuntionality(options, options.url);
			});
			$(window).trigger("history");
			setTimeout(options.init.update, 100);
		});
	}
	$.fn.tabsPlan.init = {
		update: function(opt){
			$(window).trigger("history");
			setTimeout($.fn.tabsPlan.init.update, 1000);
		},
		getHash: function(opt){
			return opt.url;
		},
		setHash: function(opt){
			if(location.hash.length > 0){
				opt.url = location.hash;
			}else{
				opt.url = $('ul.parent-tabs li:first a').attr('href');
			}
		},
		setUrl: function(opt, href){
			opt.url = href;
		},
		createObjectFromSelectedTabs: function(opt, url){
			opt.activesTab = {};
			return  {
				href: $('ul.parent-tabs').find('li a[href^=' + opt.tools.getFullHash(url) + ']').attr('href'),
				parentTab: $('ul.parent-tabs').find('li a[href^=' + opt.tools.getParentHash(url) + ']').closest('li'), 
				childTab: $('ul.child-tabs').find('li a[href$=' + opt.tools.getChildHash(url) + ']').closest('li'),
				parentContent:$('div' + opt.tools.getParentHash(url) + ''),
				childContent:$('div[id$=' + opt.tools.getChildHash(url) + ']')
			};
		},
		setFuntionality: function(opt, url){
			opt.init.setUrl(opt, url);
			opt.activesTab = opt.init.createObjectFromSelectedTabs(opt, opt.url);
			opt.find.findSelectedContent(opt);
			opt.find.findSelectedTabs(opt);
		}
	};
	$.fn.tabsPlan.find = {
		findAndMarkTabContent: function(opt){
			$('#campos_plan span.planoculto').parent().addClass('tab-content');
		},
		findSelectedTabs: function(opt){
			$('#campos_plan ul.ul-tab li').removeClass('tab-selected');
			$(opt.activesTab.parentTab).addClass('tab-selected');
			$(opt.activesTab.childTab).addClass('tab-selected');
		},
		findSelectedContent: function(opt){
			$(opt.instId).find('span.planoculto').parent().addClass('tab-content-inactive');
			$(opt.activesTab.parentContent).removeClass('tab-content-inactive').addClass('tab-content-active');
			$(opt.activesTab.childContent).removeClass('tab-content-inactive').addClass('tab-content-active');
		}
	};
	$.fn.tabsPlan.tool = {
		getHashesByComma: function(opt, hash){
			return hash.replace(':', ', #');
		},
		getParentHash: function(hash){
			return hash.split(':')[0];
		},
		getChildHash: function(hash){
			return hash.split(':')[1];
		},
		getFullHash: function(hash){
			return hash;
		}
	};
})(jQuery);

