(function(jQuery){
    var GalleryTimer;
    var Total = 0;
    var Atual = 0;

    jQuery.fn.Gallery = function(options){

        id = "#"+jQuery(this).attr("id");

        jQuery(id).addClass("gallery");

        target = "#"+options["target"];

        jQuery(target).html("<div class='g_img'></div><div class='g_shadow'></div><div class='g_txt'><p class='g_tit'></p></div></div>").show();

        jQuery(".thumbs a").click(function(){
            jQuery(".thumbs a").removeClass("atual");

            jQuery(this).addClass("atual");
            
            img = "<a href='"+jQuery(this).attr("name")+"' target='"+jQuery(this).attr("target")+"'><img src='"+jQuery(this).attr("href")+"' /></a>";
            tit = jQuery(this).attr("title");
            des = jQuery(this).attr("longdesc");
            jQuery(id+" "+target+" .g_img").html(img).show();

            if (tit != ""){
                jQuery(id+" "+target+" .g_txt .g_tit").html(tit).show();

                jQuery(id+" "+target+" .g_shadow").css("height", jQuery(id+" "+target+" .g_txt").height());
                jQuery(id+" "+target+" .g_shadow").show();
            }
            else {
                jQuery(id+" "+target+" .g_txt .g_tit").html("").hide();

                jQuery(id+" "+target+" .g_shadow").hide();
            }
            if (Atual < (Total-1)){
                Atual = Atual + 1;
            }
            else {
                Atual = 0;
            }

            clearInterval(GalleryTimer);
            GalleryTimer = setInterval('jQuery(this).galleryPlay()', 5000);

            return false;
        });

        jQuery(".thumbs a:first").trigger("click");


        Total = jQuery(id+" .thumbs").find("a").size();
        
        GalleryTimer = setInterval('jQuery(this).galleryPlay()', 5000);
    };
    jQuery.fn.galleryPlay = function(){
        ini = 0;
        fim = (Total - 1);

        var tmp = Atual;

        if (tmp < fim){
            tmp = tmp + 1;
        }
        else {
            tmp = 0;
        }

        jQuery(".thumbs a:eq("+tmp+")").trigger("click");
    }
})(jQuery);