(function($) {

    $.girlsMustHave = function(element, options) {

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

        var plugin = this;

        plugin.settings = {}

        var $element = $(element),
             element = element;
        
		
	    var $collection_wrapper	= $('#scroll-content', element);     
		var $collection	= $('.ngg-galleryoverview', element);
        var $back_item	= $('#btn_back', $collection);
		var $next_item	= $('#btn_next');
		var $prev_item	= $('#btn_prev');
		var $intro_item = $('#main');
		
        
        //PROPERTIES
        var scroll_acceleration	= 40;
		var position_x = -1;
		var max_items = $('.ngg-galleryoverview > .ngg-gallery-thumbnail-box').size();
		
        var collection_x		= 0;
        var collection_animated = false;
        var scroll_api;

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

        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', checkScrollPosition);
			
            $back_item.bind('click', onBackClick);
			$next_item.bind('click', onNextClick);
			$prev_item.bind('click', onPrevClick);
			
			$('.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 onLocationHash = function(event){
			if(window.location.hash){
				var hash = window.location.hash.substring(1);
				var x = $('.ngg-gallery-thumbnail-box:eq('+hash+')').position().left+$('#header').outerWidth();
				position_x = hash;
				scroll_api.data('jsp').scrollToX(x, true)
			}
		}
		
		var onDocumentScroll = function(event, delta, deltaX, deltaY){
			if( deltaY ){
				scroll_api.data('jsp').scrollByX(-deltaY * scroll_acceleration);
				//$('#header').css( {'height': '100%'} );
			}
		}
		
		
		
		var onDocumentResize = function( event ){	

			$intro_item.css({ width: $(window).width()/3});
			
			var pw = $('#page').innerWidth();		
			var collection_width = 0;
			var collection_height = $collection.outerHeight(true);
						
			$('.item-image').each(function(){
				
				var ratio = $(this).attr('width')/$(this).attr('height');
				
				$(this).css({
					height: $(window).height(),
					width: Math.floor($(window).height()*ratio)
				})
				collection_width += Math.floor($(window).height()*ratio);
								
			});
					
			
			$('#btn_back').css({'height': $(window).height()});
			
			var w = Math.ceil(collection_width)+$back_item.outerWidth();
			$collection.css({ width: w });
			
			scroll_api.data('jsp').reinitialise();
		 }
		 
		 
		 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 checkScrollPosition = function(event, scrollPositionX, isAtLeft, isAtRight){
			
			if(scrollPositionX >= $('#header').outerWidth()){
				
				$prev_item.fadeIn();
				
				$('.ngg-gallery-thumbnail-box').each(function(){
					var offset = $(this).offset();
					
					if(offset.left < 100 && offset.left > -100){
							position_x = $(this).closest('.ngg-gallery-thumbnail-box').index();
							document.location.hash = '#'+position_x;
					}
				})
			
			}else if(scrollPositionX < $('#header').outerWidth()){
			
				$prev_item.fadeOut();
				position_x = -1;
				document.location.hash = '';
			
			}
			
	
			if(position_x < max_items-1)
				$next_item.fadeIn();
			else
				$next_item.fadeOut();
				
	
			
			if(isAtRight == true){
				position_x = max_items-1;
				document.location.hash = '#'+position_x;
			}
		}

        plugin.init();

    }

    $.fn.girlsMustHave = function(options) {

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

    }

})(jQuery);
