;(function($){


//	if(!$.fn.fixPNG){
		
		$.fn.fixPNG = function() {
			return this.each(function () {
				var image = $(this).css('backgroundImage');

				if (image && image.match(/^url\(["']?(.*\.png)["']?\)$/i)) 
				{
					image = RegExp.$1;
					$(this).css({
						'backgroundImage': 'none',
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=" + ($(this).css('backgroundRepeat') == 'no-repeat' ? 'crop' : 'scale') + ", src='" + image + "')"
					}).each(function () {
						var position = $(this).css('position');
						if (position != 'absolute' && position != 'relative')
							$(this).css('position', 'relative');
					});
				}
			});
		};
//	};
	
	$.fn.gallery = function(thumbs){
		var isIE = ($.browser.msie && parseInt($.browser.version.substr(0,1)) <= 8);
		var opts = arguments[1];
		
		var Settings = jQuery.extend({
			filterSourceHref: function(a){ return a.href; },
			imageClass : 'gallery-image'
		}, opts);

		return this.each(function(){  
			
			this.getImageSource = function(){
				return current().source;
			};

			var me = this;
			var $me = $(me);
			var width = $me.width();
			var height = $me.height();
			if(height==0) return;
			
			var aspect = width/height;
			var store = [];
			var sources = [];
			var index = 0;
			var started = false;
			
			var isLoaded = function(i){
				if (!i.complete) 	return false;
				if (typeof i.naturalWidth != 'undefined' && i.naturalWidth== 0) return false;
				return true;
			}
			
			
			
			
			var current = function(){ 
				return store[index]; 
			};
			
			var Move = { 
				next : function(){
					index++;
					if(index==store.length) index = 0;
					return store[index];
				},
				prev : function(){
					index--;
					if(index<0) index = store.length-1;
					return store[index];
				}
			};
			
			var onload = function(e){ 
				var i = this;

				if(i.height==0) return;
				var a = i.width/i.height;
				if(a>aspect){
					var w = Math.min(width, i.width);
					var h = w / a;
				}
				else{
					var h = Math.min(height, i.height);
					var w = h * a;
				}
				i.width = w;
				i.height = h;

				var sty = i.style;
				i.className = Settings.imageClass;
				var p = Math.floor((width - i.width)/2);
				sty.paddingLeft = p+'px';
				sty.paddingRight = (width - i.width - p)+'px';
				var p = Math.floor((height - i.height)/2);
				sty.paddingTop = p+'px';
				sty.paddingBottom = (height - i.height -p)+'px'; 
			};
			
			$me.empty();
			var $imgbox = $(document.createElement('div'));
			$me.html($imgbox);
			
			var start = function(){
				$active = current();
				$active.addClass('active');
			    $imgbox.append( $active );
			    $active.fadeIn( 500 );
			    started = true;
			}
			
			$(thumbs).find('a').each(function(){ 
				var i = new Image(); 
				var $i = $(i)
				sources.push( this );
				store.push( $i );
				i.src = Settings.filterSourceHref(this);
				$i.load( onload );
				if(store.length==1) $i.load( start );
				if( isLoaded(i) ){ 
					$i.trigger('load'); 
				}

			});
			
			var swap = function(dir) {
				if(!started || store.length<=1) return;
			    var $active = current();
			    var $coming = Move[ dir ? 'next' : 'prev' ]();

			    $imgbox.append($coming);

			    $active.addClass('last-active');
	    
			    $coming.hide().addClass('active').fadeIn( 500, function(){ 
			    	$active.removeClass('active last-active'); 
		        });
			}
						
			if(store.length>1){
				var mouseover = function(){ $(this).fadeTo(10,0.8); }
				var mouseout = function(){ $(this).fadeTo(10,0.2); }

				
				var icoR = $(document.createElement('div')).addClass('ico');
				var icoL = icoR.clone(true);

				
				var $right = $(document.createElement('div'))
					.addClass('moveRight')
//					.mouseout( mouseout ).mouseover( mouseover )
					.fadeTo(0,0.5)
					.mousedown( function(e){e.preventDefault();})
					.append( icoR );
			
				var $left = $(document.createElement('div'))
					.addClass('moveLeft')
//					.mouseout( mouseout ).mouseover( mouseover )
					.fadeTo(0,0.5)
					.mousedown( function(e){e.preventDefault();})
					.append( icoL ); 
				
				
				// To stop the blue flash
				$me.append($left);
				$me.append($right);

				if(isIE){
					icoL.fixPNG();				
					icoR.fixPNG();	
				}
				
			}
			

			$me.click( function(e){ 
//				window.getSelection().removeAllRanges();
				var $e = $(e.target), i;
				if($e.closest('.moveRight').length) swap( true );
				else if($e.closest('.moveLeft').length) swap( false );
				else if(i = sources[index]) $(i).trigger('click');	
			});
			

		});
	};


})(jQuery);

