// JavaScript Document

(function($) {
	
	
	

	
	debug = function($obj) {
		if (window.console && window.console.log)
			window.console.log($obj);
	};
		
	
	
	$.BUDeGALLERY = function(element, options) {
		var defaults = {
			autoRotate: true,
			interval: 5,
			nextClass: 'gallery_next',
			prevClass: 'gallery_prev',
			usePlaylist: true,
			playlistDiv: null,
			allowPlaylistClick: true,
			galleryWidth:640,
			animate:'tween',
			tweenDirection:'left',
			animateSpeed:1500,
			onInit:null,
			onBuildComplete:null,
		}
		
		var opts;
		var currentItemNo;
		var totalItems;
		var element;
		var timer;
		var $this;
		var id;
		//expose the functions via $(element).data('BUDeGALLERY').function();
		element.data('BUDeGALLERY', this);
		
		
		var resetTimer = function() {
			var timer = setTimeout(function() {jumpToItem(($this.currentItemNo + 1))}, (1000 * $this.opts.interval) );
			return timer;
		}
	
	
		var jumpToItem = function($itemNo) {
			if(!$this) return;
			if($this.timer) {clearTimeout($this.timer); };	
			if($itemNo >= $this.totalItems) {
				$itemNo = 0;
			} 
		
			$this.currentItemNo = $itemNo;
		
			if($this.opts.usePlaylist) {
				if(!$this.opts.playlistDiv) {
					
					$this.element.children('.budegallery_playlist').children('.budegallery_playlist_item').removeClass('active');
					$this.element.children('.budegallery_playlist').children('.budegallery_playlist_item').each(function(index, value){
						if((index) == $this.currentItemNo) {
							$(this).addClass('active');
						}
					});
				} else {
					$($this.opts.playlistDiv).children('.budegallery_playlist_item').removeClass('active');
					$($this.opts.playlistDiv).children('.budegallery_playlist_item').each(function(index, value){
						if((index) == $this.currentItemNo) {
							$(this).addClass('active');
						}
					});
					
				}
			}
			//transition gallery
			
			if($this.opts.animate == 'tween') {
				var distance = $this.opts.galleryWidth - (($this.currentItemNo + 1) * $this.opts.galleryWidth);
				
				var dir = $this.opts.tweenDirection;
				
				if(dir == 'left')
					$this.element.children('.budegallery_container').animate({ left : distance }, $this.opts.animateSpeed, function() {
						if(!$this) return;
						
						$this.timer = resetTimer(); 
							
					});
				else if(dir == 'right')
					this.element.children('.budegallery_container').animate({ right : distance }, $this.opts.animateSpeed, function() {
						if(!$this) return;
						$this.timer = resetTimer(); 	
					});
				else if(dir == 'top')
					this.element.children('.budegallery_container').animate({ top : distance }, $this.opts.animateSpeed, function() {
						if(!$this) return;
						 $this.timer = resetTimer(); 	
					});
				else if(dir == 'bottom')
					$this.element.children('.budegallery_container').animate({ bottom : distance }, $this.opts.animateSpeed, function() {
						if(!$this) return;
						$this.timer = resetTimer(); 	
					});
					
			} else if($this.opts.animate == 'fade') {
				$this.element.children('.budegallery_container').children('.budegallery_item:visible').fadeOut($this.opts.animateSpeed);
				$this.element.children('.budegallery_container').children('.budegallery_item:eq(' + ($this.currentItemNo) + ')').fadeIn($this.opts.animateSpeed, function() {
						if(!$this) return;
						$this.timer = resetTimer();	
					});
			}
		}
		
		
		this.destroy = function() {
			if(!$this) return;
			if($this.timer)
				clearTimeout($this.timer);
			$this.element = null;
			$this = null;
		}
		
		
		
		this.init = function(element, options) {
			$this = this;
			$this.id = Math.random() * 100;
			
			opts = $.extend(defaults, options);
			$this.opts = opts;
			
			$this.element = element;
			//trigger onInit function
			if($this.opts.onInit) $this.opts.onInit();
			
			$this.currentItemNo = 0;
			
			$this.totalItems = $(element).children('.budegallery_container').children('.budegallery_item').length;
			
			var padding = $(element).children('.budegallery_container').css('padding-left');
			if(padding) {spl = padding.split('px'); padding = parseInt(spl[0]); }
			if($this.opts.animate == 'tween' && $this.opts.tweenDirection == 'left') {
				$(element).children('.budegallery_container').children('.budegallery_item').each(function(i, v) {
					if(padding) 
						$(this).css('left', ($this.opts.galleryWidth * i) + padding);
					else
						$(this).css('left', $this.opts.galleryWidth * i);
				});
			} else if ($this.opts.animate == 'tween' && $this.opts.tweenDirection == 'right') {
				$(element).children('.budegallery_container').children('.budegallery_item').each(function(i, v) {
					if(padding) 
						$(this).css('right', ($this.opts.galleryWidth * i) + padding);
					else
						$(this).css('right', $this.opts.galleryWidth * i);
				});
			} else if($this.opts.animate == 'fade') {
				$(element).children('.budegallery_container').children('.budegallery_item').hide();
				$(element).children('.budegallery_container').children('.budegallery_item:eq(' + ($this.currentItemNo) + ')').fadeIn($this.opts.animateSpeed);
			}
			
			if($this.opts.usePlaylist) {
				
				$(element).children($this.opts.nextClass).click(function(e) {
					e.preventDefault();
					currentItemNo +=1;
					jumpToItem($this.currentItemNo);
				});
				$(element).children($this.opts.prevClass).click(function(e) {
					e.preventDefault();
					currentItemNo -=1;
					jumpToItem($this.currentItemNo);
				});
				
				if(!$this.opts.playlistDiv) {
				
					$this.children('.budegallery_playlist').children('.budegallery_playlist_item :first').addClass('active');
					
					if($this.opts.allowPlaylistClick) {
						$this.children('.budegallery_playlist').children('.budegallery_playlist_item').click(function(e) {
							e.preventDefault();
							
							jumpToItem( parseInt($(this).attr('id')));
							
						});
					}
				} else {
					$($this.opts.playlistDiv).children('.budegallery_playlist_item :first').addClass('active');
					
					if($this.opts.allowPlaylistClick) {
						$($this.opts.playlistDiv).children('.budegallery_playlist_item').click(function(e) {
							e.preventDefault();
							
							jumpToItem(parseInt($(this).attr('id')));
							
						});
					}
					
				}
			}
			
			if($this.opts.autoRotate) {
				$this.timer = resetTimer();
			}
			//trigger onInit function
			if($this.opts.onBuildComplete) $this.opts.onBuildComplete();	
		
			
			
		}
		
		this.init(element, options);
	}

	
	$.fn.BUDeGALLERY = function(options)
	{
		
		return this.each(function() {
			(new $.BUDeGALLERY($(this), options));
		});
		
	};
	
	
	
	
})(jQuery);


