var gg_collections = function() {
	/*
	#
	# Private variables
	#
	*/
	var _imagePrefix				= "garygraham_";
	var _selectedYear 				= "menu_year_2012";
	var _selectedCollection			= "2012_spring";
	var _selectedCollectionArray 	= null;
	var _currentImage				= 0;
	var _buttonsEnabled				= false;

	/*
	#
	# Private functions
	#
	*/
	var _setMenuStyles = function() {
		// clear styles
		jQuery('.menuCollectionsVersionList').children('a').removeClass('active');
		jQuery('.sub_menus').children('div').css({'display':'none'});
		jQuery('.sub_menus').children('div').find('a').removeClass('activeCollections');
		// set styles
		jQuery('#'+_selectedYear).addClass('active');
		jQuery('#sub_'+_selectedYear).css({'display':'block'});
		jQuery('#'+_selectedCollection).addClass('activeCollections');
	};
	
	var _switchYear = function(p_newYear) {
		_selectedYear = p_newYear;
		_selectedCollection = jQuery('#sub_'+p_newYear).children(':first-child').children('a').attr('id');
		_setMenuStyles();
		_loadCollection();
	};

	var _switchCollection = function(p_newCollection) {
		_selectedCollection = p_newCollection;
		_setMenuStyles();
		_loadCollection();
	};
	
	var _openSlideshow = function(p_imageNumber) {
		// position overlay
		jQuery('#collections_white_overlay').css('position', 'relative');
		jQuery('html,body').css('overflow','hidden');
		//
		jQuery('#collections_image_next').hide();
		jQuery('#collections_image_prev').hide();
		// fade in
		jQuery('#collections_full_screen_holder').fadeIn();
		// load to clicked image
		_currentImage = p_imageNumber-1;
		_loadImage();
		_onWindowResize();
		_enableButtons();
	};
	
	var _closeSlideshow = function() {
		jQuery('html,body').css('overflow','auto');
		jQuery('#collections_full_screen_holder').fadeOut();
		_disableButtons();
	};

	var _getNextImage = function() {
		var l_nextImage;
		if (_currentImage == _selectedCollectionArray.length-1) {
			l_nextImage = 0;
		} else {
			l_nextImage = _currentImage + 1;
		}
		return l_nextImage;
	};

	var _getPrevImage = function() {
		var l_prevImage;
		if (_currentImage == 0) {
			l_prevImage = _selectedCollectionArray.length-1;
		} else {
			l_prevImage = _currentImage - 1;
		}
		return l_prevImage;
	};
	
	var _prevImage = function() {
		_currentImage = _getPrevImage();
		_loadImage();
	};

	var _nextImage = function() {
		_currentImage = _getNextImage();
		_loadImage();
	};
	
	var _loadImage = function() {
		var l_directory = jQuery('#collection_'+_selectedCollection).attr('rel');
		var l_imageFile = _selectedCollectionArray[_currentImage].number;
		var l_nextImageFile = _selectedCollectionArray[_getNextImage()].number;
		var l_prevImageFile = _selectedCollectionArray[_getPrevImage()].number;
		// load and show current image
		jQuery('#collections_image_current').css('opacity','0');
		jQuery('#collections_image_current').html('<img>');
		jQuery('#collections_image_current').children('img').attr('src',l_directory+'/large/'+_imagePrefix+_selectedCollection+'_'+l_imageFile+'.jpg');
		jQuery('#collections_image_current').children('img').attr('alt','');
		jQuery('#collections_info').html('<strong>'+_selectedCollectionArray[_currentImage].number+'</strong><br/>' + _selectedCollectionArray[_currentImage].description);
		jQuery('#collections_image_current').stop().fadeTo(500,1, function(){
			// try to preload next/prev images
			jQuery('#collections_image_prev').html('<img src="'+l_directory+'/large/'+_imagePrefix+_selectedCollection+'_'+l_prevImageFile+'.jpg'+'">');
			jQuery('#collections_image_next').html('<img src="'+l_directory+'/large/'+_imagePrefix+_selectedCollection+'_'+l_nextImageFile+'.jpg'+'">');
			_onWindowResize();
		});
		//resize
		_onWindowResize();
	};
	
	var _enableButtons = function() {
		// close button
		jQuery('#collections_close,#collections_white_overlay').bind('click',function(){
			_closeSlideshow();
		});
		// next button
		jQuery('#collections_right').bind('click',function(){
			_nextImage();
		});
		// previous button
		jQuery('#collections_left').bind('click',function(){
			_prevImage();
		});
	};

	var _disableButtons = function() {
		jQuery('#collections_close,#collections_white_overlay').unbind('click');
		jQuery('#collections_right,#collections_image_current').unbind('click');
		jQuery('#collections_left').unbind('click');
	};
	
	var _onWindowResize = function() {
		// move overlay
		jQuery('#collections_full_screen_holder').css('top', jQuery(window).scrollTop());
		jQuery('#collections_full_screen_holder').css('left', jQuery(window).scrollLeft());
		// position arrows
		var l_newTop = jQuery('#collections_full_screen_holder').height() / 2 - jQuery('#collections_left').height() / 2;
		jQuery('#collections_left').css('top',l_newTop);
		jQuery('#collections_right').css('top',l_newTop);
		// set height of image holders
		jQuery('#collections_image_current,#collections_image_next,#collections_image_prev').height(jQuery(window).height());
		jQuery('#collections_image_current,#collections_image_next,#collections_image_prev').children('img').height(jQuery(window).height());
	};
	
	var _loadCollection = function() {
		// define files directory
		var l_directory = jQuery('#collection_'+_selectedCollection).attr('rel');
		// reset holder and slideshow array
		jQuery('#collections_thumb_holder').html('<ul rel='+l_directory+'></ul>');
		_selectedCollectionArray = new Array();
		// draw thumbnails and load array
		jQuery('#collection_'+_selectedCollection).find('div').each(function(){
			var l_rel = jQuery(this).attr('rel');
			var l_description = jQuery(this).html();
			var l_obj = [];
			l_obj.number = l_rel;
			l_obj.description = l_description;
			// add to array
			_selectedCollectionArray.push(l_obj);
			// draw thumbnail
			jQuery('#collections_thumb_holder').children('ul').append('<li rel="'+l_rel+'"><img src="'+l_directory+'/thumbs/'+_imagePrefix+_selectedCollection+'_'+jQuery(this).attr('rel')+'_thumb.jpg" alt="" /></li>')
		});
		// add click listener to thumbnails
		jQuery('#collections_thumb_holder').find('li').each(function(){
			jQuery(this).bind('mouseover',function(){
				jQuery(this).stop().fadeTo(500,0.6);
			});
			jQuery(this).bind('mouseout',function(){
				jQuery(this).stop().fadeTo(500,1);
			});
			jQuery(this).bind('click',function(){
				jQuery(this).stop().fadeTo(100,1);
				_openSlideshow(jQuery(this).attr('rel'));
			});
		});
	};
	
	return {
		/*
		#
		# Public functions
		#
		*/
		init: function() {
			// add listeners for year selectors
			jQuery('.menuCollectionsVersionList').children('a').bind('click',function(){
				_switchYear(this.id)
			});
			// add listeners for collection selectors
			jQuery('.menuCollectionsSecondMList').children('a').bind('click',function(){
				_switchCollection(this.id)
			});
			// resize listener
			jQuery(window).resize(function() {
				_onWindowResize();
			});
			// orientation listener
			jQuery(window).bind('onorientationchange', function() {
				_onWindowResize();
			});
			// scroll listener
			jQuery(window).scroll(function(){
				_onWindowResize();
			});

			// init overlay slideshow
			jQuery('#collections_white_overlay').show().css('opacity','.96');
			// button rollover states
			jQuery('#collections_left,#collections_right,#collections_close').bind('mouseover',function(){
				jQuery(this).css('opacity','.2');
			});
			jQuery('#collections_left,#collections_right,#collections_close').bind('mouseout',function(){
				jQuery(this).css('opacity','1');
			});
			_setMenuStyles();
			_loadCollection();
			_onWindowResize();
		}
	};
}();

jQuery(document).ready(function() {
	//buildCollections
	jQuery('body').append('<div id="collections_full_screen_holder" style="display:none"><div id="collections_white_overlay"></div><div class="overlay_blocker"><div id="collections_image_next"></div><div id="collections_image_prev"></div><div id="collections_image_current"></div></div><div id="collections_close"></div><div id="collections_left"></div><div id="collections_right"></div><div id="collections_info"></div></div>');
	//init collections slideshow
	gg_collections.init();
});
