/**
 * jQuery jqGalViewII Plugin
 * Examples and documentation at: http://benjaminsterling.com/jquery-jqgalviewii-photo-gallery/
 */
(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="gvIIOuterContainer"/>').appendTo(el.jqjqviewii);
			el.mainImgContainer = $('<div class="gvIIFullImageContainer"/>').appendTo(el.container);
			el.navigator = $('<div class="gvIIFullImageNavigator"/>').appendTo(el.mainImgContainer);
			el.imageNext = $('<div class="next"  title="" />').appendTo(el.navigator);
			el.imagePrevious = $('<div class="previous" title=""/>').appendTo(el.navigator);
			
			
			el.image = $('<img style="display:none;"/>').appendTo(el.mainImgContainer);
			el.holder = $('<div class="gvIIThumbnailsContainer"/>').appendTo(el.container);
			el.altTextBox = $('<div class="gvIIAltText"/>').appendTo(el.container);
			
		    el.imagePrevious.click(function(){
			    var i = el.image.index;
			    
		        if (i > 0)
			    {
				    jqGalViewII.view(el.jqchildren[i - 1],el);
			    }
			})
			el.imageNext.click(function(){
			    var i = el.image.index;
			    
		        if (i < (el.jqchildren.length - 1))
			    {
				    jqGalViewII.view(el.jqchildren[i + 1],el);
			    }
			})
			
			
			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);
				
				
				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});

					$('<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');
					};
				};
				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});
	
	            el.image.index = img.index;
	            if (img.index == el.jqchildren.length - 1)
		            el.imageNext.attr('className','next nextdimmed');
		        else
		            el.imageNext.attr('className','next');
		        
		        if (img.index == 0)
		            el.imagePrevious.attr('className','previous previousdimmed');
		        else
		            el.imagePrevious.attr('className','previous');
	            
				el.image.css({width:dem.w,height:dem.h, marginLeft:margins.l,marginTop:((margins.t>0)? margins.t-7 : 0 )});
				el.altTextBox.fadeTo('fast', 0.1);
				el.image.fadeOut('fast',function(){
					el.image.attr('src',url).fadeIn();
					if(typeof img.altTxt != 'undefined'){
						el.altTextBox.fadeTo("fast",el.opts.titleOpacity).text(img.altTxt);
					};
				});
			};
			image.src = url;
		},
		defaults : {
			getUrlBy : 0, // 0 == from parent A tag | 1 == the full size resides in another folder
			fullSizePath : null,
			prefix: 'thumbnail.',
			titleOpacity : 1.0
		}
	};
})(jQuery);
