(function($) {
$.fn.slider = function(o) {
	var slides = o.slides;
	var extra_over = o.extra_over;
	
    return this.each(function() {
		var me = $(this);

        $('a', this).not('.sta').hover(function() { over(this, true); }, function() { over(this, false); });
		if ( extra_over ) extra_over.hover(function() { over(me.find('a.izq'), true); }, function() { over(me.find('a.izq'), false); });

        $('a.dch', this).not('.sta').click(function(e) { slideShow({direction: "d", speed1: 200, speed2: 200}); });
        if ( extra_over ) extra_over.click(function(e) { slideShow({direction: "i", speed1: 200, speed2: 200}); });
		else $('a.izq', this).not('.sta').click(function(e) { slideShow({direction: "i", speed1: 200, speed2: 200}); });

		init();

		function init() {
			slides.find('div.container').css({opacity: 0.0});
			slides.find('div.container div.caption').css({bottom: -32, opacity: 0.0});
			
			me.find('a').not('.sta').css({opacity: 0.0});
			me.find('a.sta div.numPost').css({
				left: (740-$(slides).find('div.container').size()*12)/2,
				width: $(slides).find('div.container').size()*12,
				opacity: 0.0
			});
			for ( i=0; i<$(slides).find('div.container').size(); i++ ) {
				$('<div class="ball ballGray png_bg"</div>').appendTo(me.find('a.sta div.numPost'));
			}

			slides.find('div.container:last').css({opacity: 1.0}).find('.caption:first').css({bottom: 0, opacity: 1.0});
			me.find('a.sta').attr("href", $(slides).find('div.container:last').attr("rel"));
			me.find('div.ball:last').removeClass('ballGray').addClass('ballRed');

			setInterval(function() {slideShow()}, 6000);
		}
		
		function over(el, inside) {
			if ( inside ) {
				me.find('a.sta div.numPost').animate({opacity: 1.0}, 400);
				$(el).animate({opacity: 1.0}, 400);
			} else {
				me.find('a.sta div.numPost').stop().animate({opacity: 0.0}, 400);
				$(el).stop().animate({opacity: 0.0}, 400);
			}
		}

		function slideShow(o) {
			var opt = $.extend({}, {direction: "d", speed1: 400, speed2: 600}, o);

			var current_ball = me.find('div.ballRed');
			var next_ball = (opt.direction == "d")?
				current_ball.prev().hasClass('ballGray')? current_ball.prev() : me.find('div.ball:last') :
				current_ball.next().hasClass('ballGray')? current_ball.next() : me.find('div.ball:first');

			var current_slide = slides.find('div.show').hasClass('container')?  slides.find('div.show') : slides.find('div.container:last');
			var next_slide = (opt.direction == "d")?
				current_slide.prev().hasClass('container')? current_slide.prev() : slides.find('div.container:last') :
				current_slide.next().hasClass('container')? current_slide.next() : slides.find('div.container:first');

			current_slide.find('.caption:first').animate({bottom: -32, opacity: 0.0}, opt.speed1, function() {
				current_slide.animate({opacity: 0.0}, opt.speed2).removeClass('show');
				current_ball.removeClass('ballRed').addClass('ballGray');
				me.find('a.sta').attr("href", next_slide.attr("rel"));
				next_ball.removeClass('ballGray').addClass('ballRed');
				next_slide.addClass('show').animate({opacity: 1.0}, opt.speed2, function() {
					next_slide.find('.caption:first').animate({bottom: 0, opacity: 1.0}, opt.speed1);
				});
			});
		}

	});
};
})(jQuery);

var $j = jQuery.noConflict();
$j(window).load(function() {
	$j('#featured-nav').slider({slides: $j('#featured'), extra_over: $j('#girlimg a.izq')});
});
