jQuery(document).ready(function(){

    jQuery(function() {

        var lock_carousel = false;


		function carousel_loadslide() {

            if (lock_carousel) {
                return false;
            }

            lock_carousel = true;
            
            var $t = jQuery(this);
            var $act = $t.parent().find('.carousel-frame-act');
            var $img = $act.find('img');

            if ($act.data('current') == undefined) {
            
                $act.data('current', 1);
                var $copy = $act.clone();
                $copy.find('img').data('src', $act.find('img').data('src'));
                $copy.find('a.image').attr('href', $act.find('a.image').attr('href'));
                //$copy.click(function(){alert($act.find('a.text').attr('href'))});
                $copy.removeClass('carousel-frame-act');
                $copy.insertAfter($act);
            }

            var current_index = $act.data('current');

            var new_index = $t.is('.btn-right') ? current_index + 1 : current_index - 1;
			if(current_index == 4) {
				new_index = 1;	
			}
			
            var $act_new = $act.parent().children(':eq(' + (new_index) + ')');
            var $new_img = $act_new.find('img');

            $act.data('current', new_index);

            if ($t.is('.btn-right')) {
                $t.parent().find('.btn-left').show();
                if ($act_new.next().length == 0) {
                    $t.fadeOut(400);
                }
            } else {
                $t.parent().find('.btn-right').show();
                if ($act_new.prev().prev().length == 0) {
                    $t.fadeOut(400);
                }
            }


            var $subtitle = $act.find('div');

            $subtitle.animate({

                bottom: -230
                
            }, 600, function () {

                $subtitle.html($act_new.find('div').html());

                var new_src = ($new_img.data('src') != undefined) ? $new_img.data('src') : $new_img.attr('src');
				
				$act.find('a.image').attr('href', $act_new.find('a.image').attr('href'));
				
                $img.fadeOver(800, function () {

                    $subtitle.animate({
                        bottom: 0
                    }, 600, function () {
                        lock_carousel = false;
                    });

                }, { }, { src: new_src } );


            });

		};

        jQuery('.carousel-container .btn-left, .carousel-container .btn-right').click(carousel_loadslide);
        
        var carrousel_interval;
		
		function set_carrousel_interval() {
			clearInterval(carrousel_interval);
			carrousel_interval = setInterval(function(){
				if (jQuery('.carousel-container .btn-right').is(':visible')) {
					jQuery('.carousel-container .btn-right').trigger("click");
					//console.log(current_index);
				} else {
					jQuery('.carousel-container .btn-left').trigger("click");
					//console.log(current_index);
				}
				
			},6000);
		}
		
		set_carrousel_interval();
		
		jQuery('.carousel-container').hover(function(){
			clearInterval(carrousel_interval);
		},function(){
			set_carrousel_interval();
		});


		jQuery('.carousel-container .btn-left, .carousel-container .btn-right').hover(function(){
			var tab = this;
			clearInterval(carrousel_interval);

		},function(){
			set_carrousel_interval();
		});
	});

});

jQuery.fn.extend({
	fadeOver: function (speed, callback, css, attr) {
            var $t = this;
            //setTimeout(function () {
		c = $t.clone();
		c.css( { position: 'absolute',
                         width: $t.width(),
                         height: $t.height(),
                         display: 'none',
			 zIndex: 2 } );
                if (css != undefined) {
                    c.css(css);
                }
                if (attr != undefined) {
                    c.attr(attr);
                }
                $tp = $t.parent(); // container
		$tp.css( { width: $tp.width(),
                           height: $tp.height()} );

		c.insertBefore($t).fadeIn(speed, function () {
			c.next().remove();
			c.css( {position: 'relative', zIndex: 1} );
			if (typeof(callback) == 'function') callback();
		});
            //}, 10);
            return $t;
	}
});
