
/* -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

	jQuery Gallery

*/

(function($){


	$.fn.Gallery = function(options){	//-------------------------------------------------------------------------------	Start - Gallery
		
		
		var o = $.extend({
			
			overlayRef: '#overlay',
			overlayEffect: 'swing',
			overlayEffectDuration: 200,
			overlayTop: 50,
			
			loaderClass: 'loader',
			loaderSrc: 'ajax-loader.gif',
			
			frameClass: 'frame',
			frameEffect: 'swing',
			frameDuration: '300',
			
			largeImgClass: 'img',
			/*largeImgDimensions: {width: 800,height: null},*/
			largeImgWidth: 960,
			largeImgHeight: null,
			
			captionClass: 'caption',
			captionEffect: 'linear',
			captionDuration: 450,
			
			hasAnchors: true,
			prevClass: 'previous',
			nextClass: 'next',
			nextStyle: {},
			prevStyle: {},
			prevText: 'previous',
			nextText: 'next',
			curClass: 'selected',
			anchorsEffect: 'swing',
			anchorsDuration: 300,
			
			onLargeImgLoad: function(){}
		
				
		},options);
		
		if(this.length < 2)o.hasAnchors = false;
		
		//--------------------------------------------------------------------------------------	Start	-
		//--------------------------------------------------------------------------------------	End		-
		
		
		
		//--------------------------------------------------------------------------------------	Start	- Properties
		
		var anchors = this;
		var simpleOverlay = null;
		var frame = null;	
		var largeImg = null;
		var caption = null;
		var prevAnchor = null;
		var nextAnchor = null;
		
		//--------------------------------------------------------------------------------------	End		- Properties
		
		//--------------------------------------------------------------------------------------	Start	- Methods
		
		function createFrame()
		{
			frame = $('<div></div>')
					.attr({
						'class': o.frameClass
					})
					.css({
						opacity: 0
					});
		}
		
		function refreshFrame()
		{
			
			frame.css({
				height: (caption) ? (caption.innerHeight() + largeImg.height()) : largeImg.height(),
				width: largeImg.width()
			});
			
		}
		
		function setLargeImg(sSrc,sCaption)
		{
			var overlayTrimming = simpleOverlay.getOverlayTrimmings();
			
			simpleOverlay.setContent(frame);
			
			var tempImg = $('<img />').attr({src: sSrc});
				
			tempImg.WaitForImgLoad({
				width: (o.largeImgWidth) ? o.largeImgWidth : $(document.body).width() - overlayTrimming.horizontal,
				height: (o.largeImgHeight) ? o.largeImgHeight : null,
				onload: function(){
					
					
					
					if(sCaption)createCaption(sCaption,tempImg[0].width);
					
					largeImg =  $('<img />')
						.attr({
							'class': o.largeImgClass,
							src: tempImg[0].src,
							width: tempImg[0].width,
							height: tempImg[0].height
						})
						.css({
							display: 'block',
							width: tempImg[0].width,
							height: tempImg[0].height
						});
					
					frame
						.css({
							opacity: 0,
							display: 'block',
							overflow: 'hidden'
						})
						.html(largeImg);
					
					
					
					refreshFrame();
					
					if(sCaption)
					{
						createCaption(sCaption);
						var capCount = 0;
						var capInterval = setInterval(function(){
							
							capCount ++;
							if(capCount > 150)
							{
								clearInterval(capInterval);
								capInterval = null;
							}
							if(caption.height() > 0)
							{
								clearInterval(capInterval);
								capInterval = null;
								frame.css('height',frame.height() + caption.innerHeight());
								
								if(o.hasAnchors)createAnchors.call(simpleOverlay);
								
								simpleOverlay.resize({
									width: frame.innerWidth(),
									height: frame.innerHeight(),
									after: $.proxy(function(){
										frame.animate({opacity:1},o.frameDuration,o.frameEffet);
									},simpleOverlay)
								});
							}
						},20);
						
					} else {
					
						if(o.hasAnchors)createAnchors.call(simpleOverlay);
						simpleOverlay.resize({
							width: frame.innerWidth(),
							height: frame.innerHeight(),
							after: $.proxy(function(){
								frame.animate({opacity:1},o.frameDuration,o.frameEffet);
							},simpleOverlay)
						});
					}
					
					
					
				},
				onerror: function(){
					
				}
			});
		}	
		
		function createCaption(sCaption)
		{
			caption = $('<div></div>')
						.attr({
							'class': o.captionClass
						}).css({
							width: frame.width()
						})
						.html(sCaption)
						.appendTo(frame);
						
		}
		
		function createAnchors()
		{
			prevAnchor = $('<a></a>')
				.attr({
					href: '#',
					'class': o.prevClass,
					title: o.prevText	
				})
				.text(o.prevText)
				.css((typeof o.prevStyle == 'function') ? o.prevStyle.call(simpleOverlay) : o.prevStyle)
				.appendTo(frame)
				.click(function($e){
					$e.preventDefault();
					
					var obj = getPreviousAnchor();
					setCurrentAnchor(obj);
					loadImage.call(simpleOverlay,obj);
				});	
				
				
			nextAnchor = $('<a></a>')
				.attr({
					href: '#',
					'class': o.nextClass,
					title: o.nextText 	
				})
				.text(o.nextText)
				.css((typeof o.nextStyle == 'function') ? o.nextStyle.call(simpleOverlay) : o.nextStyle)
				.appendTo(frame)
				.click(function($e){
					$e.preventDefault();
					
					var obj = getNextAnchor();
					setCurrentAnchor(obj);
					loadImage.call(simpleOverlay,obj);
				});	
		}
		
		function loadImage(obj)
		{
			frame
			  .animate({opacity:0},o.frameDuration,o.frameEffect,$.proxy(function(){
				  
				  var overlayDims = simpleOverlay.getOverlayDimensions();
				  
				  frame
					  .html('')
					  .css({
						  opacity: 1,
						  display: 'block',
						  width: overlayDims.width,
						  height: overlayDims.height
					  });
					  
					  $('<img />')
						  .attr({'class': o.loaderClass,src: o.loaderSrc})
						  .css({
							  top: Math.round(frame.innerHeight() / 2),
							  left: Math.round(frame.innerWidth() / 2)
						  })
						  .appendTo(frame);
	  
					  simpleOverlay.setContent(frame);
					  
					  var imgSrc = obj.attr('href');
					  var imgCaption = ($.trim(obj.attr('title')).length > 0) ? $.trim(obj.attr('title')) : null; 
					  setLargeImg.call(simpleOverlay,imgSrc,imgCaption);
					  
							  
			  },simpleOverlay));	
		}
		
		function setCurrentAnchor(oAnchor)
		{
			anchors.removeClass(o.curClass);
			anchors.eq(anchors.index(oAnchor)).addClass(o.curClass);	
		}
		
		function getCurPosAnchor()
		{
			var pos = 0;
			anchors.each(function($key,$value){
				if($(this).hasClass(o.curClass))pos = $key	;
			});
			return pos;	
		}
		
		function getNextAnchor()
		{
			var pos = getCurPosAnchor();
			if((pos + 1) == anchors.length)return anchors.eq(0);
			return anchors.eq(pos + 1);
		}
		
		function getPreviousAnchor()
		{
			var pos = getCurPosAnchor();
			if((pos - 1) < 0)return anchors.eq(anchors.length - 1);
			return anchors.eq(pos - 1);
				
		}
		
		
		//--------------------------------------------------------------------------------------	End		- Methods
		
		createFrame();
		
		simpleOverlay = this.SimpleOverlay({
				overlayRef: o.overlayRef,
				overlayTop: o.overlayTop,
				overlayEffect: o.overlayEffect,
				overlayEffectDuration: o.overlayEffectDuration,
				showByDefault: false,
				closeBtnText: 'x',
				overlayStyle: {
					overflow: 'hidden'
				},
				overlayContent: null
			}).click(function($e){
				
				var obj = $(this);
				setCurrentAnchor(obj);
				
				var overlayDims = simpleOverlay.getOverlayDimensions();
				
				frame
					.html('')
					.css({
						opacity: 1,
						display: 'block',
						width: overlayDims.width,
						height: overlayDims.height
					});
					
					
				
				$('<img />')
					.attr({'class': o.loaderClass,src: o.loaderSrc})
					.css({
						top: Math.round(frame.innerHeight() / 2),
						left: Math.round(frame.innerWidth() / 2)
					})
					.appendTo(frame);
				
						
				simpleOverlay.setContent(frame);
				simpleOverlay.show();
				
				var imgSrc = obj.attr('href');
				var imgCaption = ($.trim(obj.attr('title')).length > 0) ? $.trim(obj.attr('title')) : null; 
				setLargeImg.call(simpleOverlay,imgSrc,imgCaption);
				
			});
		
		return simpleOverlay;	
		
	}; //---------------------------------------------------------------------------------------------------------------------	End - Gallery

	
	
	
})(jQuery);
