var rutaStatics = "/StaticFiles/UniversidadAutonomaMadrid/" //para desarrollo
//var rutaStatics = "./" //para local
function onytplayerStateChange(newState) {
	if (newState == 1){
			if( myCarousel = $("#carruselfranja").data('jcarousel') ) {
					myCarousel.stopAuto()
			}

			$("#pause").css ( 'display' , 'none' );
			$("#play").css ( 'display' , 'block' );
				
		}
	else if (newState == 2){
		if( myCarousel = $("#carruselfranja").data('jcarousel') ) {
					myCarousel.startAuto()
			}
		$("#pause").css ( 'display' , 'block' );
		$("#play").css ( 'display' , 'none' );
	}
}
	
function mycarousel_initCallback(carousel){

	var numSlider = $('#carrusul').children('li').length;
	for ( var i = 0; i < numSlider; i++ ){
	$('.controles').append('<a href="#"><img alt="" /></a>')
	var clases = "boton" + (i + 1);
	$('.controles a:last').attr({ 'id' : clases , 'class' : 'boton' })
	$('.controles a img:last').attr( 'class' , clases );
	}
	$('.controles a img').attr( 'src' , rutaStatics +'/img/punto_blanco.png');
	$('.controles a img:first').attr( 'src' , rutaStatics +'/img/punto_verde.png');

     $('.controles a').bind('click', function() {
		var selector = $(this).attr('id');
		var indice = selector.substr(5,1);
		var selector = "."+ selector;
		$('.controles a img').attr('src' , rutaStatics +'/img/punto_blanco.png');
		$(selector).attr('src' , rutaStatics +'/img/punto_verde.png');
		carousel.scroll($.jcarousel.intval(indice));
		
        return false;
    });

	$("#pause").bind('click', function(){
		carousel.stopAuto();
		$("#pause").css ( 'display' , 'none' );
		$("#play").css ( 'display' , 'block' );
		
		return false;
	});
	
	$("#play").bind('click', function(){
		carousel.startAuto();
		$("#play").css ( 'display' , 'none' );
		$("#pause").css ( 'display' , 'block' );
		
		return false;
	});

};

var eventos = {
	q_evento: function(xmlEvt){
		var eventos_dia = $("#dCalendar table a");
		eventos_dia.each(function(){
			var _this = $(this);
			_this.bind("click", function(){
			var esteDia = _this.text();
			var esta_Abbr = _this.attr("name").substring(0,3);
			var numevt = _this.attr("id").split("n_evt_")[1];
			eventos.creo_capa(xmlEvt, esteDia, esta_Abbr,numevt );
			return false;
			});
		});
	},
	
	//para crear y cerrar capa.
	creo_capa: function(xmlEvt, esteDia, esta_Abbr, numevt){
		var div_dCalendar = $("#dCalendar");
		div_dCalendar.css("display","none")
		var div_detalles = $('<div />').attr({"id":"detalles_agenda"});
		var div_para_alto = $('<div />').attr({"id":"para_alto"});
		var dl_evt =  $('<dl />').attr({"id":"dl_evt","class":"clear"});
		$.get(xmlEvt,{}, function (data) {
			$(data).find('daycollection day[day='+esteDia+'] eventscollection dia_mes evento_dia').each( function(e){
				var src_icono = $(this).find('icono').text();
				var hora_evt  = $(this).find('hora').text();
				var tit_evt   = $(this).find('evento').text();
				var lnk_evt   = $(this).find('evento').attr("enlace");
				var dt_dd_evt = '<dt><img src="'+src_icono+'" alt="" />'+hora_evt+'</dt><dd><a href="'+lnk_evt+'" class="dda">'+tit_evt+'</a></dd>';
				dl_evt.append(dt_dd_evt);
			});
		}, 'xml');
		var div_tit_evt = '<div class="fecha_evt"><strong>'+esteDia+' </strong><abbr title="dia">'+esta_Abbr+'</abbr><a href="#"><img id="cierra_evts" src="'+rutaStatics+'/img/btn_close_agenda.gif" alt="cerrar" /></a></div>'
		div_detalles.append(div_tit_evt);
		div_detalles.append(div_para_alto);
		div_para_alto.append(dl_evt);
		div_dCalendar.after(div_detalles);
		//ver si el dl es mayor que la capa que lo contiene
		var alto_det_agenda = $("#detalles_agenda").height();
		var alto_dl_agenda = numevt * 50 //50 es el alto del dt
		if(alto_det_agenda < alto_dl_agenda){
			eventos.scroll_eventos(numevt);
		}
		
		//genero evento para cerrar capa eventos
		$("#cierra_evts").live("click",function(e){
			$("#detalles_agenda").remove();
			$("#ulScroll").remove();
			div_dCalendar.css("display","block");
			return false;
		})
		setTimeout("eventos.cortoCaract()",100);
	},
	
	scroll_eventos: function(numevt){
		//crea scroll
		var ulScroll='<ul id="ulScroll">'
				ulScroll+='<li id="btn_baja_evt"><a href="#"><img src="'+rutaStatics+'img/btn_baja_evts.gif" alt="" /></a></li>'
				ulScroll+='<li id="btn_sube_evt"><img src="'+rutaStatics+'img/btn_sube_evts_des.gif" alt="" /></li>'
			ulScroll+='</ul>'
		$("#leyenda_agenda").append(ulScroll);
		
		$("#dl_evt").css("top", "0")
		
		//baja
		$("#btn_baja_evt a").live("click", function(){
			var topDl =  parseInt($("#dl_evt").css("top"))
			var numDt = ((numevt * -50)+150);
			if ((topDl)>numDt){
				$('#btn_sube_evt').html('<a href="#"><img src="'+rutaStatics+'img/btn_sube_evts.gif" alt="" /></a>')
				$("#dl_evt").css("top", "-=50")
			}else if(topDl==(numDt)){
				$('#btn_baja_evt').html('<img src="'+rutaStatics+'img/btn_baja_evts_des.gif" alt="" />')
			}
			return false;
		})
		//sube
		$("#btn_sube_evt a").live("click", function(){
			var topDl = parseInt($("#dl_evt").css("top"))
			if (topDl<0){
				$('#btn_baja_evt').html('<a href="#"><img src="'+rutaStatics+'img/btn_baja_evts.gif" alt="" /></a>')
				$("#dl_evt").css("top", "+=50")
			}
			if(topDl=="-50"){
				$('#btn_sube_evt').html('<img src="'+rutaStatics+'img/btn_sube_evts_des.gif" alt="" />')
			}
			return false;
		})
	},
	
	//para cortar los caracteres del los eventos y entre en 3 lineas
	cortoCaract : function(){
		var maxCaract_a = $(".dda")
		maxCaract_a.each(function(){
			var _this = $(this);
			var aux_txtrcorte = _this.text();
			var txtrcorte = aux_txtrcorte.length;
			if(txtrcorte> 90){
				var recorte = aux_txtrcorte.substring(0,89)+"...";
				_this.text(recorte);
			}
		})
		return false;		
	}
};

jQuery(document).ready(function() {	
	if($("#dCalendar")){
		$("#dCalendar").append('<div id="cargaCalendar"><img src="'+rutaStatics+'/img/cargando_agenda.gif" alt="" /></div>')
	}
	if ($("#carruselfranja").length !=0){
		var tiempoTransicion = 5;
		$("#carruselfranja").jcarousel({
			scroll: 1,
			initCallback: mycarousel_initCallback,
			// This tells jCarousel NOT to autobuild prev/next buttons
			buttonNextHTML: null,
			buttonPrevHTML: null,
			wrap: 'both',
			auto: tiempoTransicion,
			vertical: false,
			itemVisibleInCallback: {
				onAfterAnimation: function(c, o, i, s) {
				
				
				
				var numSlider = $('.controles').children('a').length;
				var indice = (i % numSlider);
				
				if (indice == 0){
					indice = numSlider;
					}
				var boton = '.boton' + indice;
					
				$(boton).attr('src' , rutaStatics +'/img/punto_verde.png');

				indice = indice - 1;	
				
				if (indice == 0){
					indice = numSlider;
					}
				boton = '.boton' + indice;

				$(boton).attr('src' , rutaStatics +'/img/punto_blanco.png');
				}
			}

		});
	}

	if ($("#mycarousel").length !=0){
		$("#mycarousel").jcarousel({
			vertical: true,
			scroll: 3
		});
	}
	
	if ($("#carruselmanal").length !=0){
		$("#carruselmanal").jcarousel({
			scroll: 7,
			itemVisibleInCallback: {
				onAfterAnimation: function(c, o, i, s) {

					var indice = i - 1;
					var lilis = ".jcarousel-item-" + indice +" .numes";
					var lili = ".jcarousel-item-" + indice;
					
					var numes = $(lilis).html();
					
					if (i == 9){
						var numes = $(lilis).html();
						$('.mesanio').empty();
						$('.mesanio').append(numes);
					}
					if (i == 2){
						var numes = $('#capasemana #carruselmanal .activo .numes').html();
						$('.mesanio').empty();
						$('.mesanio').append(numes);
					}
	
				}
			}
		});
	}
	
	if ($("#tab_list li").lenght !=0){
		$(".div_tab h2").css('display' , 'none');
		$("#tab_list").css('display' , 'block');
		var numPestanas = $('#tab_list').children('li').length;
		if (numPestanas > 1){
			for (var i = 2; i <= numPestanas; i++){
				var divis = "#tab_" + i;
				$(divis).css('display' , 'none');
			}
		}
	}
	
	if ($("#tab_ul li").lenght !=0){
		$(".div_tab_2 h2").css('display' , 'none');
		$("#tab_ul").css('display' , 'block');
		var numPestanas = $('#tab_ul').children('li').length;
		if (numPestanas > 1){
			for (var i = 2; i <= numPestanas; i++){
				var divis = "#tablack_" + i;
				$(divis).css('display' , 'none');
			}
		}
	}
	
	$("#tab_list li").bind('click', function( ){
			var numPestanas = $('#tab_list').children('li').length;
			var atributo = $(this).attr('id');
			var num = atributo.substr(8,1);
			atributo = "#" + atributo;
			
			for (var i = 1; i < (numPestanas + 1); i++){
				var divis = '#tab_' + i;
				var aes = '#tabalist' + i;
				var lilis = '#tab_list' + i;
				
				$(divis).css('display' , 'none');
				$(aes).css({'background' : 'none #EBEBEC' , 'color' : '#009966', 'cursor' : 'pointer'});
				$(lilis).css('background' , 'url('+rutaStatics+'/img/img_tab_sombra.png) no-repeat 100% 0 #EBEBEC');
				
			}
			divis = '#tab_' + num;
			aes = '#tabalist' + num;
			
			$('#tab_list li.last').css('backgroundColor' , 'white'); 
			$(divis).css( 'display' , 'block' );
			$(aes).css({'backgroundColor' : '#CBC9C6' , 'color' : '#333', 'cursor' : 'default'});
			
			if (num != 1){
				lilis = '#tab_list' + (num -1);
				aes = '#tabalist' + (num -1);
				$(lilis).css('background' , 'none #CBC9C6');
				$(aes).css('background' , 'url('+rutaStatics+'/img/img_tab_sombra2.png) no-repeat 100% 0 #EBEBEC');
			
			}
			
			return false;
	});
	
	$("#tab_ul li").bind('click', function( ){
			var numPestanas2 = $('#tab_ul').children('li').length;
			var atributo = $(this).attr('id');
			var num = atributo.substr(3,1);
			atributo = "#" + atributo;
			
			for (var i = 1; i < (numPestanas2 + 1); i++){
				var divis = '#tablack_' + i;
				var aes = '#taba' + i;
				var lilis = '#tab' + i;
				
				$(divis).css('display' , 'none');
				$(aes).css({'background' : 'none #48494E' , 'color' : '#009966' , 'cursor' : 'pointer'});
				$(lilis).css('background' , 'url('+rutaStatics+'/img/img_tab_sombra.png) no-repeat 100% 0 #48494E');
				
				
			}
			divis = '#tablack_' + num;
			aes = '#taba' + num;
			$('#tab_ul li.last').css('backgroundColor' , 'white'); 
			$(divis).css({'display' : 'block' , 'backgroundColor' : 'black'});
			$(aes).css({'backgroundColor' : '#121315' , 'color' : '#fff' , 'cursor' : 'default' });
			
			if (num != 1){
				lilis = '#tab' + (num -1);
				aes = '#taba' + (num -1);
				$(lilis).css('background' , 'none #121315' );
				$(aes).css('background' , 'url('+rutaStatics+'/img/img_tab_sombra2.png) no-repeat 100% 0 #48494E');
			}
			
			return false;
	});
	
	if ($("#carruselmanal").length !=0){
		var numes = $('#capasemana #carruselmanal .activo .numes').html();
		$('.mesanio').empty();
		$('.mesanio').append(numes);
	}
	/*
	if ($("#amplphoto").length !=0){
		var imgsDestacadas =  $("#amplphoto").find("img");
		var altoFoto = $("#amplphoto").css('height')
		if($.browser.msie && parseInt($.browser.version, 10) == 7) {
			altoFoto= parseInt(altoFoto,10)-20
		}
		$.each(imgsDestacadas,function(){
			$(this).css("cursor","pointer");
			$(this).bind('click',function(){
				 $("#amplphoto").append("<div id='imgAmpliada'><img class='dest' style='height:"+altoFoto+";' src="+$(this).attr('src')+" alt=''/><a href='#' id='cls_img_ppal'><img src='"+rutaStatics+"/img/btn_close_photo.gif' alt=''/></a><p>"+$(this).attr('alt')+"</p></div>")
				return false;			
			})
		})	
		$("#cls_img_ppal").live('click',function(){
			$("#imgAmpliada").remove();
			return false;			
		})
	}*/
});


