(function($){
		  

		

	$.fn.jqGalViewII = function(options){
		var ii = 0;
	
		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.link2 = $('<a style="border:0;" class="lightbox" id="single_image"  href=""/>').appendTo(el.mainImgContainer);
							
			el.image = $('<img style="display:none;"  />').appendTo(el.link2);
			
			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;
				

				
				if (j != 0)
				el.link3 = $('<a style="border:0;" class="lightbox" id="single_image"  href="'+jqimage.parent().attr('href')+'"/>').appendTo(el.mainImgContainer);
				
			
				

		
				
				tmpimage.index = j;

				var jqdiv = $('<div id="gvIIID'+j+'" class="gvIIItem">')
				.appendTo(el.holder)
				.append('<div class="gvIILoaderMini">');// end : $div
				
				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});
					//alert(margins.l);
					margins.l = 2.5;
					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');
						margins.t = 2.5;
						
						jqimage.click(function(){
							jqGalViewII.view(this,el);	
							//alert(image.scr);
						})
						

						.css({marginLeft:margins.l,marginTop:margins.t});

						

						if( tmpimage.index  == 0 ){
							jqimage.trigger('click');
							jqimage.siblings().trigger('mouseover');
						};
					};  // end : largeImage.onload 
					largeImage.src = tmpimage.altImg;
				};// end : image.onload 
				image.src = tmpimage.src;
			});
		});
	};

	jqGalViewII = {
		//pDem parent deminsions
		//iDem img deminsions
		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});
		
				//margins.t = 6;
				el.image.css({width:dem.w,height:dem.h, marginLeft:margins.l,marginTop:margins.t,border:'0px'});
				

				
				el.loader.fadeOut('fast');
				el.altTextBox.fadeTo('fast', 0.1);
				el.image.fadeOut('fast',function(){
												 
					el.image.attr('src',url).fadeIn();
					el.image.attr('id',"img_agrand").fadeIn();
					
					el.link2.attr('href',url).fadeIn();
				
					if(typeof img.altTxt != 'undefined'){
						el.altTextBox.fadeTo("fast",el.opts.titleOpacity).text(img.altTxt);
					};
				});
				
				el.image.click(function(){
										
			
				});
				
			};
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			

			
			
			
			
			
			
			
			
			

	
			image.src = url;
			el.link2.href = url;
			
			
		},
		defaults : {
			getUrlBy : 0, // 0 == from parent A tag | 1 == the full size resides in another folder
			fullSizePath : null,
			prefix: 'thumbnail.',
			titleOpacity : .60
		}
	};
})(jQuery);
