var BASE = "/";//"http://www.itn.edu.pe/";
$(document).ready(function(){
	jQuery.fn.reset = function () {
  		$(this).each (function() { 
			this.reset();
		});
	}
	
	$("a").click(function() {
		this.blur();
	});
	
	equalHeight($(".equal"));
		
	jQuery('ul.sf-menu').superfish();

	/* external links */
	$("a.newpage").each(function(){
		$(this).attr({'target':'_blank'});
	});
	/* Captcha */
	$("#reloadCaptcha").click(function(){
		RefreshImage("imgCaptcha");
	})	
	$("#captchacode, #captchacodepage").attr({"autocomplete":"off"});
	
	$("#accordion-gestion").accordion ({ 
		header: "h4",
		event: 'mouseover',
		autoHeight: false
	});
	$("#accordion-tecnologia").accordion({ 
		header: "h4",
		event: 'mouseover',
		autoHeight: false 
	});
	$("#accordion-diseno").accordion({ 
		header: "h4",
		event: 'mouseover',
		autoHeight: false 
	});
	
	/*Faq*/	
	$("#accordion-faq").accordion({
		autoHeight: false,
		active:false, 
		collapsible:true
	});	
	
	$('li.headlink').hover(
		function() { $('ul', this).css('display', 'block'); },
		function() { $('ul', this).css('display', 'none'); 
	});


	$('.boxgrid.captionfull').hover(function(){
		$(".cover", this).stop().animate({top:'40px'},{queue:false,duration:160});
		}, function() {
		$(".cover", this).stop().animate({top:'90px'},{queue:false,duration:160});
	});
	/* gallery*/
	$("#gallery-container").scrollable({
		size: 2,
		circular: true,
		item: 'div.gallery-item',
		prev: '.Noprev', 
      	next: '.Nonext', 
		hoverClass: 'hover',
		activeClass: 'active',
		keyboard: false
	}).autoscroll({
		steps: 1,
		interval: 10000
	});
	
	/* events */
	nEvents = $(".scrollable-events dl").length;
	$("#events-counter").html("Mostrando <strong>01</strong> de <strong>"+formatNumber(nEvents)+"</strong> eventos");
	oEvents = $("#events-container").scrollable({
		api: true,
		size: 1,
		circular: true,
		item: 'div.event-item',
		prev: '.prevEvent', 
      	next: '.nextEvent', 
		disabledClass: 'disabled-event',
		keyboard: false,	
		onSeek: function(e,i){
			$("#events-counter").html("Mostrando <strong>"+formatNumber(i+1)+"</strong> de <strong>"+formatNumber(nEvents)+"</strong> eventos");		
		}
	});
	
	/* news */
	var num = $(".noticia-item").length;
	$(".news-nav-label").html("1 de "+num);
	$("#noticias-container").scrollable({		
		vertical: true,
		size: 1,
		circular: true,
		item: 'div.noticia-item',
		prev: '.prevPage', 
		next: '.nextPage', 
		hoverClass: 'hover',
		keyboard: false,
		activeClass: 'active',
		onSeek: function(e,i){
			var posN = this.getIndex().toString();
			$(".news-nav-label").html((posN)+" de "+num);			
		}
	}).autoscroll({
		steps: 1,
		interval: 8000
	});
	
	/**/
	/* gallery*/
	var numT = $(".testimonio-item").length;
	$(".testimonios-nav-label").html("1 de "+numT);	
	$("#testimonios-container").scrollable({
		size: 1,
		circular: true,
		item: 'div.testimonio-item',
      	prev: '.testimonios-nav-left', 
      	next: '.testimonios-nav-right',
		hoverClass: 'hover',
		activeClass: 'active',
		keyboard: false,
		onSeek: function(e,i){
			var posT = this.getIndex().toString();
			$(".testimonios-nav-label").html((posT)+" de "+numT);
		}
	}).autoscroll({
		steps: 1,
		interval: 10000
	});
	/**/
	
		
	$('.boxgrid.captionfull').live("mouseover", function(){
		$(".cover", this).stop().animate({top:'0'},{queue:false,duration:160});
	});
	$('.boxgrid.captionfull').live("mouseout", function(){
		$(".cover", this).stop().animate({top:'139px'},{queue:false,duration:160});
	});
		
	equalHeight($(".gallery-item-main"));
	
	/**/
	
	/*	
	var vSource;
	$(".hoverable").live("mouseover",function(){
		vSource = $(this).attr("src");
		$(this).attr("src", function(){
			return "images/"+this.id+"-over.png";
		})
	});

	$(".hoverable").live("mouseout",function(){
		$(this).attr("src", function(){
			return vSource;
		})
	});
	*/
	
	/**/
	$("#ads-container").animate({bottom : "0"},{queue : false, duration : 350});
	
	$("#ads-close").click(function(){
		$("#ads-container").animate({bottom : "-98"},{queue : false, duration : 350});
	})	
		
});

function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

function getEvents(d,m,a,s){
	$.ajax({				
		type: "POST",		
		dataType: "json",
		url: BASE + "scripts/utils/calendar-get-events-byday.asp",
		data: { "vDia": d, "vMes": m, "vAnno": a, "vSede": s},
		success: function(r){
			nEvents = r.Meta.Count;
			vItems = (nEvents > 9) ? nEvents : "0"+nEvents;
			vStrItems = (nEvents > 9) ? "eventos" : "evento";
			$("#events-block h3").html(r.Meta.Fecha);
			$("#events-counter").html("Mostrando <strong>01</strong> de <strong>"+vItems+"</strong> "+vStrItems);
			$(".events-list").empty();
			for(var i=0;i<r.Eventos.length;i++){
				var tagDL = "<dl><dt>"+r.Eventos[i].Titulo+"</dt>";
				tagDL += "<dd><strong>Organiza: </strong>"+r.Eventos[i].Responsable+"</dd>";
				tagDL += "<dd><strong>Lugar: </strong>"+r.Eventos[i].Lugar+"</dd>";
				tagDL += "<dd><strong>Hora: </strong>"+r.Eventos[i].Hora+"</dd>";
				tagDL += "<dd><strong>Duraci&oacute;n aproximada: </strong>"+r.Eventos[i].Duracion+"</dd></dl>";				
				var htmlEvent = $("<div>").addClass("event-item").append(tagDL);				
				$('.events-list').append(htmlEvent);
			}
			oEvents.seekTo(0);
		},
		error:function (xhr, ajaxOptions, thrownError){
			switch(xhr.status){
				case 500:
					strError = "<strong>Error en el Servidor</strong>";
				case 404:
					strError = "<strong>P&aacute;gina no encontrada</strong>";
		  }
			jAlert(strError, 'Instituto Tecnológico del Norte');
		} 
	});
}

function formatNumber(val){
	return (val > 9) ? val : "0"+val;
}
