/*
	jQuery slider bare gallery plugin. 
	Version 0.5 alpha
	By Ian Hoar (http://www.ianhoar.com)
*/

(function($) {

/*
  var methods = {
    init : function( options ) {  },
    show : function( ) {    },
		hide : function( ) {  },
		update : function( content ) {  }
};
*/ 
	$.fn.baregallery = function(opts) {

		var thisGallery = $(this); // reference to this gallery
		var t = parseInt($('img', this).size());
		
		// create totalGalleryWidth div
		$('img', this).wrapAll('<div id="totalGalleryWidth"></div>');
		
		$(thisGallery).css({
			'position' : 'relative',
			'overflow' : 'hidden',
			'height'   : 'auto'
		});	

var w;
var h;
				
	function resizeImage() {
	
		// check if this is a full screen slider
			if(opts.fullScreen == true) {		
				$(thisGallery).css({
					'position' : 'fixed'
				});

				w = $(window).width();
				h = $(window).height();
		
				$(thisGallery).css({
					'width' : w,
					'height' : h
				});
				
				var ratio = $('img', thisGallery).height() / $('img', thisGallery).width();
				
				// Scale the image
				if ((h/w) > ratio){
				    $('img', thisGallery).height(h);
				    $('img', thisGallery).width(h / ratio);
				} else {
				    $('img', thisGallery).width(w);
				    $('img', thisGallery).height(w * ratio);
				}
				// Center the image
				if(opts.transitionType == 'fade') {
					$('img', thisGallery).css({'position' : 'absolute', 'left' : (w - $('img', thisGallery).width())/2});
					$('img', thisGallery).css({'position' : 'absolute', 'top' : (h - $('img', thisGallery).height())/2});
				}
			}
	
			else {
	
				// This needs to be fixed
				w = $('img', thisGallery).width();
				h = $('img', thisGallery).height();	
			}
	}
				// this should be in else above




resizeImage();

$(window).resize(function() {
	resizeImage();
});



		// Set Defaults
		var defaults = {
			skin: null,
			fullScreen: false,
			speed: 600,
			imageWidth : w,
			imageHeight : h,
			galleryHeight : h,
			totalImages : t,
			galleryWidth : w,
			totalGalleryWidth : t * w,
			currentImage : 0, // this is also the start image
			imageNextPrev : true, // image divided in half for Next and Previous clicks
			imageNext : false,
			imagePrev : false,
			teasers : false, // teaser, set id for teaser container
			captionType : false, // title, div
			rewind : false, // should images rewind at end or constant slide forwards
			cycle : true, // should images cycle
			cycleSpeed : 4000,
			onHoverStop : true,
			pager : false,
			pagerType : 'numbers', // false, div, numbers, images
			transitionType : 'slide',
			descTrigger : '#' // trigger for a class id, if you need to use # at the start of your descriptions you could change the trigger here.
		};





// Get default options	
		var opts = $.extend(defaults, opts);

		// set effects
		
		if(opts.transitionType == 'slide') {
			$('img', thisGallery).css({'float' : 'left'});
		} else {
			$('img', thisGallery).css({'float' : 'left'});
		}

		$(thisGallery).width(opts.galleryWidth);
		$('#totalGalleryWidth').width(opts.totalGalleryWidth);
		
		
		return this.each(function() {
		
		// Image cycle
		if(opts.cycle == true){
			var refreshIntervalId = setInterval(nextImage, opts.cycleSpeed); // rotate images
			if(opts.onHoverStop == true) {
				$(thisGallery).hover(function () { // stop and resume rotate on rollover of main image. 
					clearInterval(refreshIntervalId);
				}, function () {
					refreshIntervalId = setInterval(nextImage, opts.cycleSpeed);
				});
			}
		}

		// Rewind images 
		if(opts.rewinds == true) {
			
		}
		
		$(thisGallery).css({
			'width' : opts.imageWidth,
			'height' : opts.galleryHeight
		});

		$('img', this).css({
			'display' : 'block'
		});
		
		$('ul, li', this).css({
			'float' : 'left',
			'margin' : 0,
			'padding' : 0,
			'list-style' : 'none'
		});
		
		if(opts.pager) {
			pager();
		}
		
		if(opts.imageNextPrev == true) {
			// Create Gallery Previous and next
				var galleryPrevious = '<div id="gallery-prev"></div>';
	   		var galleryNext = '<div id="gallery-next"></div>';
	   		$(thisGallery).append(galleryPrevious + galleryNext)
	   			
			// Postion Gallery Previous and Next
				
			$('#gallery-prev, #gallery-next', this).css({
				'position' : 'absolute',
				'zIndex' : 1,
				'height' : opts.galleryHeight,	
				'width' : (opts.imageWidth / 2), // prev and next take up half the image
				'cursor' : 'pointer'
				});
				
			$('#gallery-prev', this).css({
				'left' : 0
			});
			$('#gallery-next', this).css({
				'right' : 0
			});
		}
		// Hover actions
		if(opts.imageNext) {$('#gallery-next').hover(function() { $(this).addClass(opts.imageNext)},function() {$(this).removeClass(opts.imageNext)});}
		if(opts.imagePrev) {$('#gallery-prev').hover(function() { $(this).addClass(opts.imagePrev)},function() {$(this).removeClass(opts.imagePrev)});}
		// Clicks actions
		$('#gallery-prev', this).click(function() {prevImage();});
		$('#gallery-next', this).click(function() {nextImage();});
		$('#pager-prev').live('click', function() {prevImage();});
		$('#pager-next').live('click', function() {nextImage();});
		$('#pager .dot a').live('click', function() {
			var skipToImage = $(this).text(); 
			imageLocation = opts.imageWidth*skipToImage-opts.imageWidth;
			currentImage = skipToImage-1; // subtract 1 for image index 0
			transition(opts.transitionType, imageLocation, currentImage);
		});
		$('#skip-to-image li').mouseenter(function() { // manual skip to image list
			skipToImage = $(this).index();
			opts.currentImage = skipToImage;
			imageLocation = opts.imageWidth*(skipToImage+1)-opts.imageWidth;
			transition(opts.transitionType, imageLocation, skipToImage);
		});
		
		// Gallery captions
		if(opts.captionType != false) {
			if(opts.captionType.charAt(0) == '.') { // if custom class captions are used add active class
					$(opts.captionType).hide();
					$(opts.captionType).eq(opts.currentImage).show();
			} else { // do typical caption
				$('li', this).each(function(n) {
					captionTitle = $('img').eq(n).attr('title');
					captionContent = '<div class="caption-content">'+captionTitle+'</div>';
					caption = '<div class="caption"></div>';
					captionContentHeight = $('li .caption-content').height();
					$('li').eq(n).append(caption);
					$('li').eq(n).append(captionContent);
				});
	   			$('.caption-content').css({
	   				'color' : '#fff',
	   				'zIndex' : 3,
	   				'fontFamily' : 'arial'
	   			});
	   			$('.caption, .caption-content').css({
	   				'position' : 'absolute',
	   				'bottom' : 0,
	   				'padding' : 5
	   			});
	   			
	   			$('.caption').css({
	   				'background' : '#000',
	   				'opacity' : .4,
	   				'width' : imageWidth-10,
	   				'height' : captionContentHeight,
	   				'zIndex' : 2
	   			});
	   		}
		}
			
		$(window).keypress(function (event) {
			var keyCode = event.keyCode || event.which, 
				arrow = {left: 37, right: 39 };
				clearInterval(refreshIntervalId);
			switch (keyCode) {
				case arrow.left:
				prevImage();
			break;
				case arrow.right:
				nextImage();
			break;
			}
		});
	});

	function prevImage() {
		if(!$('div:first', thisGallery).is(':animated')) {
			opts.currentImage -= 1;
			if(opts.currentImage >= 0) {
				imageLocation = opts.currentImage*opts.imageWidth;
			} else {
				imageLocation = opts.imageWidth*opts.totalImages-opts.imageWidth;
				opts.currentImage = opts.totalImages-1;
			}
			transition(opts.transitionType, imageLocation, opts.currentImage,  prev = 'prev');
		}
		if(opts.pager) {
			pager(true);
		}
	}
	function nextImage() {
			if(!$('div:first', thisGallery).is(':animated')) {
				opts.currentImage += 1;
				if(opts.currentImage < opts.totalImages) {
					imageLocation = opts.currentImage*opts.imageWidth;
				} else {
					imageLocation = 0;
					opts.currentImage = 0;
				}
				transition(opts.transitionType, imageLocation, opts.currentImage, next = 'next');
		}
	}
	function transition(transitionType, imageLocation, currentImage, action) {
		if(opts.transitionType == 'fade' && action == 'next') {
			$('img:last-child', thisGallery).stop(true, true).fadeOut(opts.speed, function(){ // wait for fadeout to complete before detach is preformed.
					$('img:last-child', thisGallery).detach().prependTo('#totalGalleryWidth', thisGallery);
					$('img', thisGallery).show();
			});
			if(opts.captionType != false) {
				if(opts.captionType.charAt(0) == '.') {
					$(opts.captionType).hide();
					$(opts.captionType).eq(currentImage).show();
				}
			}
		} else if(opts.transitionType == 'fade' && action == 'prev') {
			if(opts.transitionType == 'fade') {
				$('img:first-child', thisGallery).hide().detach().appendTo('#totalGalleryWidth', thisGallery);
				$('img:last-child', thisGallery).stop(true, true).fadeIn(opts.speed);
			}
		} else {
			$('div:first', thisGallery).stop(true, false).animate({marginLeft:-imageLocation}, opts.speed);
			if(opts.teasers != false) {
				$(opts.teasers).removeClass('active');
				$(opts.teasers).eq(currentImage).addClass('active');
			}
			if(opts.captionType != false) {
				if(opts.captionType.charAt(0) == '.') {
					$(opts.captionType).fadeOut();
					$(opts.captionType).eq(currentImage).stop(true, true).fadeIn();
				}
			}
		}
		pager();
	}

	function pager() {
		$('#pager-dots, #pager-next, #pager-prev').remove();
		i=0;
		var pager = '<div id="pager-prev"></div><div id="pager-dots">';
			while(i < opts.totalImages) { // build pager
				if(i == opts.currentImage) {
					pager = pager + '<div class="dot active"><a href="#'+i+'">'+(opts.pagerType == 'numbers' ? i+1 : "")+'</a><\/div>';
				} else {
					pager = pager + '<div class="dot"><a href="#'+i+'">'+(opts.pagerType == 'numbers' ? i+1 : "")+'</a><\/div>';
				}
				i++;
			}
		pager = pager + '<\/div><div id="pager-next"></div>';
		$(opts.pager).append(pager);
	}


}
})(jQuery);
