$(document).ready(function () {
		var izquierda = Math.round($('#head li.actual').offset().left - $('#head').offset().left);
		var ancho = $('#head li.actual').width()+8;

		$('#lava').css({left: izquierda});
		$('#lava .head').css({width: ancho});
		$('#head li').hover(function () {
			izquierda = Math.round($(this).offset().left - $('#head').offset().left);
			$('#lava').stop(false, true).animate({left: izquierda},900);	
			$('#lava .head').stop(false, true).animate({width: '177px'},1200);	
		}).click(function () {
			$('#head li').removeClass('actual');	
			$(this).addClass('actual');
		});
		
		$('#head').mouseleave(function () {
			izquierda = Math.round($('#head li.actual').offset().left - $('#head').offset().left);
			ancho = $('#head li.actual').width()+8;
			$('#lava').stop(false, true).animate({left: izquierda},900);	
			$('#lava .head').stop(false, true).animate({width: ancho},1500);		
		});
	});
	
$(document).ready(function(){
	
	$("#menu_superior li").hover(function() {	
			$(this).find("ul.subnav").stop().removeAttr('style').slideDown().show();
			$(this).addClass("subhover");
	}, function(){
			$(this).find("ul.subnav").stop().slideUp('fast');
			$(this).removeClass("subhover");
	});

});
	

function cambiaSlide() {
    var $activo = $('#slideshow div.activo');
    if ( $activo.length == 0 ) $activo = $('#slideshow div:last');

    var $siguiente =  $activo.next().length ? $activo.next() : $('#slideshow div:first');
    $activo.addClass('ultimo-activo');
    $siguiente.css({opacity: 0.0}).addClass('activo').animate({opacity: 1.0}, 1500, function() { $activo.removeClass('activo ultimo-activo'); });
}

function cambiaConvenios() {
    var $activo = $('#convenios div a.activo');
    if ( $activo.length == 0 ) $activo = $('#convenios div a:last');

    var $siguiente =  $activo.next().length ? $activo.next() : $('#convenios div a:first');
    $activo.addClass('ultimo-activo');
    $siguiente.css({opacity: 0.0}).addClass('activo').animate({opacity: 1.0}, 1500, function() { $activo.removeClass('activo ultimo-activo'); });
}

$(function () {
    $('#ngg-webslice .ngg-widget').simpleSpy();
});

(function ($) {
    
$.fn.simpleSpy = function (limit, interval) {
    limit = limit || 8;
    interval = interval || 3000;
    
    return this.each(function () {
        var $list = $(this),
            items = [],
            currentItem = limit,
            total = 0,
            height = $list.find('> a:first').height();
        $list.find('> a').each(function () {
            items.push('<a>' + $(this).html() + '</a>');
        });
        total = items.length;
        $list.wrap('<div class="tweets-fz" />').parent().css({ height : height * limit/3 });
        $list.find('> a').filter(':gt(' + (limit - 1) + ')').remove();
        function spy() {
            var $insert = $(items[currentItem]).css({
                height : 0,
                opacity : 0,
                display : 'none'
            }).prependTo($list);
                        
            $list.find('> a:last').animate({ opacity : 0}, 1000, function () {
                $insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                    $(this).remove();
            });
            currentItem++;
            if (currentItem >= total) {
                currentItem = 0;
            }
            
            setTimeout(spy, interval)
        }
        
        spy();
    });
};
    
})(jQuery);

$(function() {
    setInterval( "cambiaSlide();", 4000 );
    setInterval( "cambiaConvenios();", 6000 );
});

$.fn.equiposCarrusel = function () {

    function repeat(str, num) { return new Array( num + 1 ).join( str ); }
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth),
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li');
        $wrapper.scrollLeft(singleWidth * visible);
        
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    page = 1;
                } 

                currentPage = page;
            });                
            return false;
        }
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        $('a.back', this).click(function () { return gotoPage(currentPage - 1); });
        $('a.forward', this).click(function () { return gotoPage(currentPage + 1); });
        $(this).bind('goto', function (event, page) { gotoPage(page); });
    });  
};

(function($) {
	$.fn.ultimasNoticias = function(opciones) {
		var tabs	= $(this);
		var contenido	= $(opciones.contenido);
		new $.ultimasNoticias(tabs, contenido, opciones);
		return this;	
	};

	$.ultimasNoticias = function(tabs, contenido, opciones) {
		function slide(nr) {
			if (typeof nr == "undefined") {
				nr = visible_item + 1;
				nr = nr >= total_items ? 0 : nr;
			}

			tabs.removeClass('activo').filter(":eq(" + nr + ")").addClass('activo');
			contenido.stop(true, true).filter(":visible").fadeOut();
			contenido.filter(":not(" + nr + ")").find('.ultimo_destacado').animate({top: '20px'});
			contenido.filter(":eq(" + nr + ")").find('.ultimo_destacado').animate({top: '-55px'});
			contenido.filter(":eq(" + nr + ")").fadeIn(function() {			
			visible_item = nr;
			
			});
		}

		var opciones		= opciones || {}; 
		var total_items		= tabs.length;
		var visible_item	= opciones.start_item || 0;

		opciones.pause_on_hover		= opciones.pause_on_hover		|| true;
		opciones.intervalo	= opciones.intervalo	|| 5000;

		contenido.hide().eq( visible_item ).show();
		tabs.eq(visible_item).addClass('activo');

		tabs.hover(function() {
			if ($(this).hasClass('activo')) {
				return false;	
			}
			slide( tabs.index( this) );
		});

		if (opciones.intervalo > 0) {
			var timer = setInterval(function () {
				slide();
			}, opciones.intervalo);

			if (opciones.pause_on_hover) {
				tabs.mouseenter(function() {
					clearInterval( timer );

				}).mouseleave(function() {
					clearInterval( timer );
					timer = setInterval(function () {
						slide();
					}, opciones.intervalo);
				});
			}
		}
	};
})(jQuery);
