/**
 * jQuery jqGalScroll Plugin
 * Examples and documentation at: http://benjaminsterling.com/jquery-jqgalscroll-photo-gallery/
 *
 * @author: Benjamin Sterling
 * @version: 2.1
 * @copyright (c) 2007 Benjamin Sterling, KenzoMedia
 * @extendThanks Koesmanto Bong http://www.koesbong.com/
 *		Koes put a fire under my butt to improve this plugin
 *		and when I took too long he took what I had and added
 *		the horizontal scroll and in turn I ripped it from his
 *		hands and made it better :)
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *   
 * @requires jQuery v1.2.1 or later
 * @optional jQuery Easing v1.2
 *
 * @name jqGalScroll
 * @example $('ul').jqGalScroll();
 * 
 * @Semantic requirements:
 * 				The structure fairly simple; the structure should consist
 * 				of a ul > li > img structure.
 * 
 * 	<ul>
 *		<li><img src="common/img/dsc_0003.thumbnail.JPG"/></li>
 *		<li><img src="common/img/dsc_0012.thumbnail.JPG"/></li>
 *	</ul>
 *
 * @param String ease
 *					refer to http://gsgd.co.uk/sandbox/jquery.easing.php for values
 * 
 * @example $('#gallery').jqGalScroll({speed:1000});
 
 * @param String speed
 * 					fast, slow, 1000, ext..
 * 
 * @example $('#gallery').jqGalScroll({speed:1000});
 * 
 * @param String height
 * 					the default height of your wrapper
 * 
 * @example $('#gallery').jqGalScroll({height:490});
 * 
 * @param String titleOpacity
 * 					the opacity of your title bar (if present)
 * 
 * @example $('#gallery').jqGalScroll({titleOpacity:.70});
 * 
 * @param String direction 
 *					vertical horizontal diagonal
 * 
 * @example $('#gallery').jqGalScroll({direction:'vertical'});
 * 			
 */
(function($) {
	$.fn.jqGalScroll = function(options){
		return this.each(function(i){
			var el = this
			el.curImage = 0;
			el.jqthis = $(this).css({position:'relative'});
			el.jqchildren = el.jqthis.children();
			el.opts = $.extend({}, jqGalScroll, options);
			el.index = i;
			el.totalChildren = el.jqchildren.size();
			var width,height;

			var go = new galOptions(el, i)
			
			go.calcDimensions();
			go.buildContainer();
			
			go.createVCRButtons();
      el.jqchildren.each(function(j){
				var gi = new galImage(el)
				gi.placeImage(j, this);

        // Handle clicking on the image
				var sImage = gi.jqImg;

				sImage.click(function(){
        go.next();
				});
        
        gi.placeLoaderAndTitle();
        
				var image = new Image();
				image.onload = function(){
					image.onload = null;
					gi.loader.fadeOut();
					sImage.css({marginLeft:-image.width*.5,marginTop:-image.height*.5,position:'absolute',left:'50%',top:'50%'}).fadeIn();
					var alt = sImage.attr('alt');
					if(typeof alt != 'undefined'){
						gi.titleHolder.text(alt).fadeIn();
					}
				};
				image.src = gi.jqImg.attr('src');
			});
  		go.changeImage(0);
		}); // end : this.each(function()
	};  // end : $.fn.jqGalScroll
	jqGalScroll = {
		ease: null,
		speed:500,
		height: 500,
		width: 500,
		titleOpacity : .60,
		direction : 'horizontal' // vertical horizontal diagonal
	};
	function galOptions(element, currentIndex) {
	  this.el = element;
	  this.currentIndex = currentIndex;
	  this.maxIndex = $(element).children().size();
	}
	
	galOptions.prototype = {
	  calcDimensions: function() {
			switch(this.el.opts.direction){
				case 'horizontal':
					this.width = this.el.totalChildren * this.el.opts.width;
					this.height = this.el.opts.height;
					break;
				case 'vertical':
					this.width = this.el.opts.width;
					this.height = this.el.totalChildren * this.el.opts.height;
					break;
				default:
					this.width = this.el.totalChildren * this.el.opts.width;
					this.height = this.el.totalChildren * this.el.opts.height;
					break;
			};
	  },
	  
	  buildContainer: function() {
			this.el.container = $('<div id="jqGS'+this.currentIndex+'" class="jqGSContainer">').css({position:'relative'});
			this.el.ImgContainer = $('<div class="jqGSImgContainer" style="height:'+this.el.opts.height+'px;position:relative;overflow:hidden">')
								.css({height:this.el.opts.height,width:this.el.opts.width,position:'relative',overflow:'hidden'});
			this.el.jqthis.css({height:this.height,width:this.width});
			
			this.el.jqthis.wrap(this.el.container);
			this.el.jqthis.wrap(this.el.ImgContainer);
	  },
	  
	  buildPaginationContainer: function() {
			this.el.pagination = $('<div class="jqGSPagination">');
			this.el.jqthis.parent().parent().append(this.el.pagination);
			this.jqul = $('<ul>').appendTo(this.el.pagination);
	  },
	  
	  reselect: function(which) {
			this.el.pagination.find('.selected').removeClass('selected');
			$(which).addClass('selected');
	  },
	  
	  next: function() {
	    this.currentIndex = (this.currentIndex + 1) % this.maxIndex;
	    this.changeImage(this.currentIndex);
	  },
	  
	  previous: function() {
	    this.currentIndex = Math.max(this.currentIndex - 1, 0);
	    this.changeImage(this.currentIndex);
	  },
	  
	  changeImage: function(newImage) {
			var params = {};
			switch(this.el.opts.direction) {
			  case 'diagonal':
			    params = {right:(this.el.opts.width*newImage),bottom:(this.el.opts.height*newImage)};
			    break;
			  case 'vertical':
				  params = {bottom:(this.el.opts.height*newImage)};
				  break;
				case 'horizontal':
					params = {right:(this.el.opts.width*newImage)};
					break;
				default:
				  throw('undefined direction set in options');
			}
			this.currentIndex = newImage;
			
			this.el.jqthis.stop().animate(params, this.el.opts.speed, this.el.opts.ease);
			currentObjectSet = $('.jqGSImgContainer > ul > li > img');
			currentImage = $(currentObjectSet[newImage]);
      $(this.el).trigger('next', currentImage.attr('name'));  // Use name attribute instead of title.
	  },

	  createVCRButtons: function() {
	    _th = this;
	    
			this.el.pagination = $('<div class="jqGSPagination">');
			this.el.jqthis.parent().parent().append(this.el.pagination);
      this.el.pagination.html('<ul><li id="previous"><img src="/images/spacer.gif" width=19 height=20 /></li>' +
                              '<li id="next"><img src="/images/spacer.gif" width=19 height=20 /></li></ul>');
      $('ul li#next').click(function() {
        _th.next();
      });
      
      $('ul li#previous').click(function() {
        _th.previous();
      });
	  },
	  
	  createPaginationButton: function(buttonNumber, selected) {
	    return;
	    
	    var selected = ''
	    if(buttonNumber == 0) selected = 'selected';
	    
	    _th = this;
			var $a = $('<a href="#'+(buttonNumber)+'" class="'+selected+'">'+(buttonNumber+1)+'</a>').click(function(){
				var href = this.index;
				_th.reselect(this);
				_th.changeImage(href);
				this.index = href;
				return false;
			});

			this.n = $a.get(0);

			this.n.index = buttonNumber;
			
			$('<li>').appendTo(this.jqul).append($a).append('</li>');
	  }
  }
	
	function galImage(element) {
	  this.el = element;
	  this.pos = {x:0, y:0};
	}
	
	galImage.prototype = {
 	  placeImage: function(imageNumber, which) {
			switch(this.el.opts.direction) {
			  case 'diagonal':
					this.pos.x = imageNumber * this.el.opts.width;
					this.pos.y = imageNumber * this.el.opts.height;
			    break;
			  case 'horizontal':
				  this.pos.x = imageNumber * this.el.opts.width;
			    break;
			  case 'vertical':
				  this.pos.y = imageNumber * this.el.opts.height;
			    break;
			  default:
			    throw('undefined direction set in options');
			}

			this.jqChild = $(which).css({height:this.el.opts.height,width:this.el.opts.width,position:'absolute',left:this.pos.x, top:this.pos.y});

			this.jqImg = this.jqChild.find('img').hide()

			if(this.jqImg.parent().is('a')){
				var p = this.jqImg.parent();
				this.jqImg.get(0).linkHref = p.attr('href');
				p.remove();
				this.jqImg.appendTo(this.jqChild);
			};
	  },

	  placeLoaderAndTitle: function() {
			this.loader = $('<div class="jqGSLoader">').appendTo(this.jqChild);
			this.titleHolder = $('<div class="jqGSTitle">').appendTo(this.jqChild).css({opacity:this.el.opts.titleOpacity}).hide();
	  }
	}
})(jQuery);