(function($){
    $.fn.jqGalViewII=function(options){
        return this.each(function(i){
            var
            el=this;
            el.jqthis=$(this);
            el.jqchildren=el.jqthis.find('img');
            el.opts=$.extend({},jqGalViewII.defaults,options);
            el.index=i;el.totalChildren=el.jqchildren.size();
            el.jqjqviewii=jqGalViewII.swapOut(el);
            el.container=$('<div class="gvIIContainer">').appendTo(el.jqjqviewii);
            el.mainImgContainer=$('<div class="gvIIImgContainer">').appendTo(el.container);
						el.lightboxHolder=$('<a href="index.php" rel="lightbox"></a>').appendTo(el.mainImgContainer);
            el.image=$('<img style="display:none;"/>').appendTo(el.lightboxHolder);
            el.loader=$('<div class="gvIILoader"/>').appendTo(el.mainImgContainer);
            el.altTextBox=$('<div class="gvIIAltText"/>').appendTo(el.mainImgContainer);
            el.holder=$('<div class="gvIIHolder"/>').appendTo(el.container);
            el.jqthis.after(el.jqjqviewii).remove();
            el.imgCw=el.mainImgContainer.width();
            el.imgCh=el.mainImgContainer.height();
            el.jqchildren.each(function(j){
                var jqimage=$(this);
                var tmpimage=this;tmpimage.index=j;
                var jqdiv=$('<div id="gvIIID'+j+'" class="gvIIItem">').appendTo(el.holder).append('<div class="gvIILoaderMini">');
                if(el.opts.getUrlBy==0){
                    tmpimage.altImg=jqimage.parent().attr('href')
                }else if(el.opts.getUrlBy==1){
                    tmpimage.altImg=el.opts.fullSizePath+tmpimage.src.split('/').pop()
                }else if(el.opts.getUrlBy==2){
                    tmpimage.altImg=tmpimage.src.replace(el.opts.prefix,'')
                };this.altTxt=jqimage.attr('alt');var image=new Image();image.onload=function(){
                    image.onload=null;jqdiv.empty().append(jqimage);var margins=jqGalViewII.center({
                        "w":jqdiv.width(),
                        "h":jqdiv.height()
                    },{
                        "w":image.width,
                        "h":image.height
                    });jqimage.css({
                        marginLeft:margins.l,
                        marginTop:margins.t
                    });var largeImage=new Image();largeImage.onload=function(){
                        largeImage.onload=null;$('<div class="gvIIFlash">').appendTo(jqdiv).css({
                            opacity:".01"
                        }).mouseover(function(){
                            var $f=$(this);$f.css({
                                opacity:".75"
                            }).stop().animate({
                                opacity:".01"
                            },500)
                        }).click(function(){
                            jqimage.trigger('click')
                        }).trigger('mouseover');jqimage.click(function(){
                            jqGalViewII.view(this,el)
                        }).css({
                            marginLeft:margins.l,
                            marginTop:margins.t
                        });if(tmpimage.index==0){
                            jqimage.trigger('click');jqimage.siblings().trigger('mouseover')
                        }
                    };largeImage.src=tmpimage.altImg
                };image.src=tmpimage.src;
                if (jqimage.attr('alt').length>0) {
                    $('<div class="gvIIAltTextBottom"/>').appendTo(el.holder).text(jqimage.attr('alt'));
                }
            })
        })
    };
    jqGalViewII={
        resize:function(pDem,iDem){
            if(iDem.w>pDem.w){
                iDem.h=iDem.h*(pDem.w/iDem.w);iDem.w=pDem.w;
                if(iDem.h>pDem.w){
                    iDem.w=iDem.w*(pDem.h/iDem.h);iDem.h=pDem.h
                }
            }else if(iDem.h>pDem.h){
                iDem.w=iDem.w*(pDem.h/iDem.h);iDem.h=pDem.h;
                if(iDem.w>pDem.w){
                    iDem.h=iDem.h*(pDem.w/iDem.w);iDem.w=pDem.w
                }
            };return iDem
        },
        center:function(pDem,iDem){
            return{
                "l":(pDem.w-iDem.w)*.5,
                "t":(pDem.h-iDem.h)*.5
            }
        },
        swapOut:function(el){
            return $('<div id="jqgvii'+el.index+'">')
        },
        view:function(img,el){
            if(typeof img.altImg=='undefined')return false;var url=img.altImg;if(/picasa/.test(url)){
                url=/\?/.test(img.altImg)?'&imgmax=800':'?imgmax=800'
            };el.loader.show();image=new Image();image.onload=function(){
                image.onload=null;dem={};dem.w=$wOrg=image.width;dem.h=$hOrg=image.height;dem=jqGalViewII.resize({
                    "w":el.imgCw,
                    "h":el.imgCh
                },{
                    "w":dem.w,
                    "h":dem.h
                });var margins=jqGalViewII.center({
                    "w":el.imgCw,
                    "h":el.imgCh
                },{
                    "w":dem.w,
                    "h":dem.h
                });el.image.css({
                    width:dem.w,
                    height:dem.h,
                    marginLeft:margins.l,
                    marginTop:margins.t
                });el.loader.fadeOut('fast');el.altTextBox.fadeTo('fast',0.1);
								el.image.fadeOut('fast',function(){
                    el.image.attr('src',url).fadeIn();
										el.lightboxHolder.attr('href',url);
										el.lightboxHolder.lightBox();
										if(typeof img.altTxt!='undefined'){
                        el.altTextBox.fadeTo("fast",el.opts.titleOpacity).text(img.altTxt)
                    } else {el.altTextBox.fadeOut('fast');}
                });
								/*if ((typeof img.altTxt!='undefined') && (img.altTxt.length>0)) {
                        el.altTextBoxBottom.text(img.altTxt)
                }*/
            };image.src=url
        },
        defaults:{
            getUrlBy:0,
            fullSizePath:null,
            prefix:'thumbnail.',
            titleOpacity:.60
        }
    }
})(jQuery);
