(function($){
    jQuery.fn.mbGallery=function(F)
    {
        return this.each(function()
                {
                    $(this).hide();
                    var c=!this.id?Math.floor(Math.random()*100):this.id;
                    var d=this;
                    
                    if(d.initialized)
                        return;                    
                    d.initialized=true;
                    
                    var e=$(this).clone();e.attr("id","gall_"+c);c=e.attr("id");
                    var f,actualImg,actualThumb,thumbUnsel,thumbOver,thumbs,full,imgDesc;                    
                    var g=$.extend({},$.fn.mbGallery.defaults,F);g.thumbsBorder=g.thumbsBorder+"px solid";
                    var h={thumbSel:{border:g.thumbsBorder,borderColor:g.thumbSelectColor},thumbUnsel:{border:g.thumbsBorder,borderColor:g.thumbStripColor},thumbOver:{border:g.thumbsBorder,borderColor:g.thumbOverColor}};$.extend(g,h);
                    if(g.slideTimer<2000)g.slideTimer=2000;thumbUnsel=g.thumbUnsel;thumbOver=g.thumbOver;thumbs=e.find(g.thumbnailSelector);full=e.find(g.imageSelector);
                    imgDesc=e.find(g.descSelector);e.empty();
                    if(g.startFrom=="random")g.startFrom=Math.floor(Math.random()*full.length);
                    function preloadImg(i)
                    {
                        i = parseInt(i);
                        $(s).find("img").attr("src",""+g.iconFolder+"/loader.gif");
                        var a=$(full[i]).attr("src");
                        var b=new Image();
                        b.onload=function()
                            {
                                $(s).find("img").attr("src",""+g.iconFolder+"/loaded.gif");
                                changePhoto(i);
                            };
                        b.onerror=function()
                            {
                                alert('Imagem não carregada');
                            };
                        b.src=a;
                    }
                    var j="";
                    function setThumbPos(w)
                    {
                        var a=0;
                        switch(g.thumbStripPos)
                        {
                            case"left":
                                a=0;
                                break;
                            case"center":
                                a=((w/2)-(g.thumbStripWidth/2));
                                break;
                            case"right":
                                a=(w-g.thumbStripWidth);
                                break;
                            default:
                                a=0;
                                break
                        }
                        return a;
                    }
                    j=setThumbPos(g.galleryWidth);
                    $("body").append("<div id='mbg_"+c+"' ><div class='mbMask' id='mbMask_"+c+"'></div><table id='tbl_"+c+"'  cellpadding='0' cellspacing='0' height='"+g.galleryHeight+"'><tr><td id ='gallery_"+c+"' class='mbGallery'></td></tr></table></div>");
                    var k=$("#tbl_"+c).find('#gallery_'+c);
                    $(k).css({border:g.galleryFrameBorder+"px solid "+g.galleryFrameColor,background:g.galleryColor});
                    $(k).append(e);
                    $(e).append("<div class='FullImg'></div>");
                    var m=$(e).find(".FullImg");
                    var n=g.labelHeight>0?g.labelHeight:g.galleryFrameBorder;
                    $(k).prepend("<div class='thumbBox'></div>");
                    var o=$(k).find(" .thumbBox");
                    $(o).prepend("<div class='header'>"+"<table cellpadding='0' cellspacing='0'><tr>"+"<td class='thumbWinBtn pointer'> </td>"+"<td class='spacer' > </td>"+"<td class='slideShow pointer' > </td>"+"<td class='spacer' > </td>"+"<td class='prev pointer' > </td>"+"<td class='next pointer' > </td>"+"<td class='spacer' > </td>"+"<td class='loader'> </td>"+"<td class='indexLabel' nowrap> </td>"+"<td class='spacer' > </td>"+"<td class='close' nowrap> </td>"+"</tr></table>"+"</div>");
                    var p=$(o).find(".header");
                    var q=$(p).find(".thumbWinBtn");
                    $(q).append("<img src='"+g.iconFolder+"/thumb.gif' class='thumbIco'>");
                    var r=$(p).find(".slideShow");
                    $(r).append("<img src='"+g.iconFolder+"/play.gif' class='slideIco'>");
                    var s=$(p).find(".loader");$(s).append("<img src='"+g.iconFolder+"/loaded.gif'>");
                    var t=$(p).find(".spacer");$(t).append("<img src='"+g.iconFolder+"/separator.gif'>");
                    var u=$(p).find(".next");$(u).append("<img src='"+g.iconFolder+"/next.gif'>");
                    var v=$(p).find(".prev");$(v).append("<img src='"+g.iconFolder+"/prev.gif'>");
                    var y=$(p).find(".close");
                    $(y).append("<img src='"+g.iconFolder+"/close.gif' alt='close'>");
                    var z=$(o).find(" .indexLabel").html((g.startFrom)+"-<b>"+full.length+"</b>");$(o).append("<div class='ThumbImg' style='display:none;padding-left:14px;'></div>");
                    var A=$(o).find(" .ThumbImg");$(A).prepend($(thumbs));$(o).append("<div class='descriptionBox'></div>");
                    var B=$(k).find(".descriptionBox");$("#mbg_"+c).css({position:"fixed",bottom:0,left:0,width:"100%",height:"100%",display:"none"});$("#mbMask_"+c).css({opacity:g.maskOpacity,background:g.maskBgnd,display:"none"});
                    $("#tbl_"+c).css({margin:"auto",paddingTop:"10%",position:"relative"});
                    $(B).css({position:"absolute",padding:10,zIndex:0,width:g.thumbStripWidth-20});
                    e.css({width:g.galleryWidth,height:g.galleryHeight,overflow:"hidden"});$(thumbs).css({width:g.thumbHeight,padding:"0px",border:"1px solid "+g.labelColor,cursor:"pointer"});$(o).css({textAlign:"left",zindex:1000,marginTop:"-"+n+"px",position:"absolute",width:g.thumbStripWidth+"px",marginLeft:j+"px",zIndex:10000});$(A).css({opacity:g.headerOpacity,backgroundColor:g.thumbStripColor,border:"5px solid "+g.labelColor});
                    $(p).css({opacity:g.headerOpacity,textAlign:"left",color:g.labelTextColor,padding:"0px",border:"0px",height:n});$(p).find("td").css({background:g.labelColorDisactive,padding:"2px",paddingRight:"10px",height:n,color:g.labelTextColor,fontFamily:"Arial, Helvetica, sans-serif",fontSize:g.labelTextSize});$(".pointer").css({cursor:"pointer"});
                    jQuery.fn.extend({getW:function(){var a=$(this).width();if(g.galleryMaxWidth>0&&a>g.galleryMaxWidth){$(this).attr("width",g.galleryMaxWidth);a=g.galleryMaxWidth}return a}});
                    function changePhoto(i)
                    {
                        i=parseInt(i);
                        $(B).fadeTo(g.fadeTime,0);
                        $(m).fadeTo(g.fadeTime,0,function()
                            {
                                $(m).html(full[i]);
                                $(B).html(imgDesc[i]);
                                setTimeout(function()
                                    {
                                        $(m).fadeTo(g.fadeTime,1);
                                        $(B).fadeTo(g.fadeTime,.8)
                                    },g.fadeTime);
                                if(g.autoSize)
                                {
                                    var w=$(full[i]).getW();
                                    e.animate({height:full[i].offsetHeight,width:w},g.fadeTime);
                                    if(g.thumbStripWidth==g.galleryWidth)
                                    {
                                        $(o).animate({width:full[i].offsetWidth},g.fadeTime)
                                    }
                                    else
                                    {
                                        var l=setThumbPos($(full[i]).width());
                                        $(o).animate({marginLeft:l},g.fadeTime)
                                    }
                                }
                                        
                            });
                        $(actualThumb).css(thumbUnsel);
                        actualImg=full[i];
                        actualThumb=thumbs[i];
                        $(actualThumb).css(g.thumbSel);
                        $(z).html((parseInt(i)+1)+"-<b>"+full.length+"</b>");                        
                    }
                    thumbs.each(function(i)
                        {
                            i=parseInt(i);
                            $(this).click(function()
                                {
                                    x=i;
                                    stopShow();
                                    preloadImg(i);
                                    x++
                                })
                        });
                    e.show();
                    var C,thumbOpen,headerMO;
                    $(q).click(function()
                        {
                            if(!thumbOpen)
                            {
                                $(A).slideDown(500);
                                thumbOpen=true;
                            }
                            else
                            {
                                $(A).slideUp(500);
                                thumbOpen=false;
                            }
                            stopShow()});
                    $(m).click(function()
                        {
                            stopShow()
                        });
                    $(m).bind("dblclick",function(){startShow()});
                    $(o).mouseover(function(){clearTimeout(C);clearTimeout(headerMO);$(p).find("td").css({opacity:g.headerOpacity,background:g.labelColor});clearTimeout(f)});
                    $(o).mouseout(function(){headerMO=setTimeout(function(){$(p).find("td").css({opacity:g.headerOpacity,background:g.labelColor})},100);
                    C=setTimeout(function(){$(A).slideUp(500);thumbOpen=false},1000)});
                    $(thumbs).mouseover(function(){if(this!=actualThumb){$(this).css(thumbOver)}});
                    $(thumbs).mouseout(function(){if(this!=actualThumb){$(this).css(thumbUnsel)}});
                    $(r).click(function(){startSlide=!startSlide;if(startSlide){startShow()}else stopShow()});
                    var D;
                    $(u).click(function(){stopShow();clearTimeout(D);x+=1;D=setTimeout(function(){if(x>=full.length)x=0;preloadImg(x)},200)});
                    $(v).click(function(){stopShow();clearTimeout(D);x-=1;D=setTimeout(function(){if(x<0)x=full.length-1;preloadImg(x)},200)});
                    $(y).click(function(){stopShow();d.initialized=false;$("#mbMask_"+c).fadeOut("slow",function(){$("#mbg_"+c).slideUp("slow",function(){$("#mbg_"+c).remove()})});if(typeof(g.close)=='function')g.close();});
                    $("#mbMask_"+c).click(function(){stopShow();d.initialized=false;$(this).fadeOut("slow",function(){$("#mbg_"+c).slideUp("slow",function(){$("#mbg_"+c).remove()})})});
                    actualImg=full[parseInt(g.startFrom)];
                    $(thumbs).css(thumbUnsel);
                    actualThumb=thumbs[parseInt(g.startFrom)];
                    $(actualThumb).css(h);
                    f=setTimeout(function(){$(A).hide(500)},2000);
                    var E,x=parseInt(g.startFrom),startSlide=g.autoSlide;
                    function startShow(){$(r).find("img").attr("src",g.iconFolder+"/stop.gif");if(x==full.length)x=0;preloadImg(x);E=setTimeout(startShow,g.slideTimer);x++};
                    function stopShow(){clearTimeout(E);$(r).find("img").attr("src",g.iconFolder+"/play.gif");startSlide=false}
                    if(startSlide){setTimeout(startShow,g.startTimer)}else{setTimeout(function(){preloadImg(g.startFrom)},g.startTimer)}
                    $("#mbg_"+c).mb_BringToFront().slideDown("slow",function(){$("#mbMask_"+c).fadeIn("slow")})
                })};
                $.fn.mbGallery.defaults={galleryWidth:300,galleryHeight:300,galleryMaxWidth:0,galleryColor:"#333",galleryFrameBorder:12,galleryFrameColor:"#fff",maskOpacity:.5,maskBgnd:"transparent",startFrom:"random",headerOpacity:0.8,thumbsBorder:4,thumbHeight:50,thumbStripWidth:250,thumbStripColor:"#333333",thumbStripPos:"right",thumbSelectColor:"black",thumbOverColor:"#cccccc",imageSelector:".imgFull",thumbnailSelector:".imgThumb",descSelector:".imgDesc",descriptionWidth:300,labelColor:"#333",labelColorDisactive:"#333",labelTextColor:"#fff",labelTextSize:"9px",labelHeight:20,iconFolder:"elements/white",fadeTime:500,autoSlide:true,slideTimer:6000,autoSize:true,startTimer:0};
                jQuery.fn.mb_BringToFront=function(){var b=10;$('*').each(function(){if($(this).css("position")=="absolute"){var a=parseInt($(this).css('zIndex'));b=a>b?parseInt($(this).css('zIndex')):b}});
                $(this).css('zIndex',b+=1);return $(this)}
    })(jQuery);

