(function($) {

    $.girlsMustHavePress = function(element, options) {

        var defaults = {
            foo: 'bar',
            onFoo: function() {}
        }

        var plugin = this;

        plugin.settings = {}

        var $element = $(element),
             element = element;
        
		
	    var $collection_wrapper	= $element;     
		var $collection	= $('.ngg-galleryoverview', $collection_wrapper);
        
        
        //PROPERTIES
        var scroll_acceleration	= 20;
        var collection_x		= 0;
        var collection_animated = false;
        var scroll_api;

        plugin.init = function() {
            plugin.settings = $.extend({}, defaults, options);
            initListeners();
            initCollection();
           	onDocumentResize();
        }

        plugin.foo_public_method = function() {
            // code goes here
        }

        var initListeners = function() {
            $element.bind('mousewheel', onDocumentScroll);
            $(window).bind('resize', onDocumentResize);
			//$(document).bind('ready', onLocationHash);
			
			//$collection_wrapper.bind('jsp-scroll-x', showNextPrev);
			
			/*
            $back_item.bind('click', onBackClick);
			$next_item.bind('click', onNextClick);
			$prev_item.bind('click', onPrevClick);
			*/
			/*
            $scroll_button.bind('click', onScrollButtonClick);
            */
			
			$('.ngg-gallery-thumbnail-box', $collection).bind('mouseenter', onCollectionItemMouseEnter);
            $('.ngg-gallery-thumbnail-box', $collection).bind('mouseleave', onCollectionItemMouseLeave);
            //$('.overlay-info', $collection).bind('click', onCollectionItemClick);
			
			
			
			
        }
        
        var initCollection = function(){
        	scroll_api = $collection_wrapper.jScrollPane({ animateDuration: 1000});
        }        
        
        /********************************/
		/* EVENT HANDLING				*/
		/********************************/
		
		var onDocumentScroll = function(event, delta, deltaX, deltaY){
			if( deltaY ){
				scroll_api.data('jsp').scrollByX(-deltaY * scroll_acceleration);
			}
		}
		
		
		
		var onDocumentResize = function( event ){
			
			var arr_size = new Array();
			
			$('.ngg-galleryoverview').each(function(){
			
				var collection_width = 0;
				
				$('.ngg-gallery-thumbnail-box', $(this)).each(function(){
					collection_width += $(this).outerWidth(true);
				})
			
				arr_size.push(collection_width);
			
			})
			
			arr_size.sort(sortNumber);

			var w = Math.ceil(arr_size[0]);
			$collection.css({ width: w });
			
			//console.info('w: '+w);
			
			scroll_api.data('jsp').reinitialise();
		 }
		
		
		function sortNumber(a,b){
			return b - a;
		}


		 
		 
		 /*
		 var onNextClick = function( event ){
			 
			if(position_x < max_items)
				position_x++;
			
			var x = $('.ngg-gallery-thumbnail-box:eq('+position_x+')').position().left+$('#header').outerWidth();
			scroll_api.data('jsp').scrollToX(x, true);
			document.location.hash = '#'+position_x;			
			return false;
		}

		 var onPrevClick = function( event ){
			 
			var x = 0
			if(position_x >= 0){
				position_x--;
				if(position_x == -1)
					x = 0;
				else
					x = $('.ngg-gallery-thumbnail-box:eq('+position_x+')').position().left+$('#header').outerWidth();
			}
			
			if(x == 0)
				document.location.hash = '';
			else
				document.location.hash = '#'+position_x;
			
			scroll_api.data('jsp').scrollToX(x, true);					
			return false;
		}
*/

/*
		
		var onBackClick = function( event ){
			scroll_api.data('jsp').scrollToX(0, true);
			document.location.hash = '';
			position_x = -1;
			
			return false;
		}
		*/
		
		
		var onCollectionItemMouseEnter = function( event ){
			
			$(this).addClass('hover');
			var description_height = $('.overlay-info-text', this).height();
			var info_height = $(this).height();
			
			$('.overlay-info .overlay-info-text', this).css({
				
				width: $(this).closest('.ngg-gallery-thumbnail-box').innerWidth(),
				top: ($(this).closest('.ngg-gallery-thumbnail-box').innerHeight() - description_height)/2
			});
			
		}
		
		var onCollectionItemMouseLeave = function( event ){
			$(this).removeClass('hover');
		}
		/*
		var onCollectionItemClick = function( event ){
			var x = $(this).closest('.ngg-gallery-thumbnail-box').position().left+$('#header').outerWidth();
			scroll_api.data('jsp').scrollToX(x, true);
			position_x = $(this).closest('.ngg-gallery-thumbnail-box').index();
			document.location.hash = '#'+position_x;
			return false;
		}
		
		var showNextPrev = function(event, scrollPositionX, isAtLeft, isAtRight){
			
			if(position_x >= 0)
				$prev_item.fadeIn();
			else
				$prev_item.fadeOut();

			if(position_x < max_items-1)
				$next_item.fadeIn();
			else
				$next_item.fadeOut();
	
		}*/

        plugin.init();

    }

    $.fn.girlsMustHavePress = function(options) {

        return this.each(function() {
            if (undefined == $(this).data('girlsMustHavePress')) {
                var plugin = new $.girlsMustHavePress(this, options);
                $(this).data('girlsMustHavePress', plugin);
            }
        });

    }

})(jQuery);
