(function(jq){
    jq.fn.gallery = function() {
        
        function resizeImgs(img) {
			
			var winwidth  = jq(window).width();
			var winheight = jq(window).height();
			
			if(winwidth>0 && winheight>0)
			{
				var imgwidth    = img.width();
				var imgheight   = img.height();
				var widthratio  = winwidth / imgwidth;
				var heightratio = winheight / imgheight;
				var widthdiff   = heightratio * imgwidth;
				var heightdiff  = widthratio * imgheight;
				if(heightdiff>winheight) { img.css({ width: winwidth+'px', height: heightdiff+'px' }); }
				else                     { img.css({ width: widthdiff+'px', height: winheight+'px' }); }
			}
		}
		
        return this.each(function(){
            var self = jq(this);
            
            var _outer_wrap = self.parent().find('#gallery_outer_wrap') ;
            var wrap = self.find('#items');
            var _count_items = wrap.children().size() ; 
            wrap.width(_count_items * 54) ; 
            var thumbs = wrap.find('a.item');
            var holder = jq('#mood').css({'background-image':'url(/public/media/page/images/loader.gif)'});
            var active = false;
            var remove = function() { jq(this).remove(); };
            var lock = false;
            var unlock = function() { lock = false; };
            
            thumbs.each(function(i) {
                if (i == 0) {
                    this.previous = thumbs[_count_items-1] ; 
                    this.next = thumbs[i+1] ; 
                }
                else if (i == _count_items-1) {
                    this.next = thumbs[0] ; 
                    this.previous = thumbs[i-1] ; 
                }
                else {
                    this.next = thumbs[i+1] ; 
                    this.previous = thumbs[i-1] ; 
                }
            }) ; 
            
            var verticalMoveThumbs = function(source) {
                var width = self.width();
                var center = Math.floor(width / 52 / 2) * 52;
                var middle = center -jq(source).position().left;
                var max   = -(wrap.width() - width); 
                if(middle>0) { middle = 0; } 
                else if(middle<max) { middle = max; }
                wrap.animate({'left':middle+'px'}, 300);
            };
            
            var open = function() {
                me = this;
                if(this != active && lock === false) {               
                    lock = true;
                    jq('#mood img').fadeOut(200, remove);
                    jq(active).find('img').css({'opacity':1});
                    verticalMoveThumbs(me);
                    active = me;            				
        			jq(active).find("img").css({'opacity':0.2});
                    var img = new Image();
                    jq(img).load(function(){
                        jq(this).css({'opacity':0}).addClass('first');
    					jq(this).appendTo(holder);
    					resizeImgs(jq(this));
    					jq(this).animate({'opacity':1}, 500) ;
        				lock = false;
    				}).attr({
    				    'src':jq(this).attr('href')
    				});
                }
                return false;
            };

            thumbs.click(open);
            jq(thumbs[0]).click();   
            
            jq('#gallery_next').click(function() {
                jq(active.next).click() ; 
            }) ;   
            
            jq('#gallery_previous').click(function() { 
                jq(active.previous).click() ; 
            }) ;    
        });
    }
})(jQuery);
