/*

	GalleryView - jQuery Content Gallery Plugin
	Author: 		Jack Anderson
	Version:		2.0 (May 5, 2009)
	Documentation: 	http://www.spaceforaname.com/galleryview/
	
	Please use this development script if you intend to make changes to the
	plugin code.  For production sites, please use jquery.galleryview-2.0-pack.js.
	
	See CHANGELOG.txt for a review of changes and LICENSE.txt for the applicable
	licensing information.


*/

//Global variable to check if window is already loaded
//Used for calling GalleryView after page has loaded
var window_loaded = false;

(function($){
	$.fn.galleryView = function(options) {
		var opts = $.extend($.fn.galleryView.defaults,options);
		
		
/************************************************/
/*	Plugin Methods								*/
/************************************************/

	//Transition from current item to item 'i'
		function showItem(obj, i) {
			opts = obj.opts;
			
			//Disable next/prev buttons until transition is complete
			$('.nav-next-overlay',obj.j_gallery).unbind('click');
			$('.nav-prev-overlay',obj.j_gallery).unbind('click');
			$('.nav-next',obj.j_gallery).unbind('click');
			$('.nav-prev',obj.j_gallery).unbind('click');
			obj.j_frames.unbind('click');
			
			//Fade out all frames while fading in target frame
			if(opts.show_filmstrip) {
				obj.j_frames.removeClass('current').find('img').stop().animate({
					'opacity':opts.frame_opacity
				},opts.transition_speed);
				obj.j_frames.eq(i).addClass('current').find('img').stop().animate({
					'opacity':1.0
				},opts.transition_speed);
			}
			
			//If the gallery has panels and the panels should fade, fade out all panels while fading in target panel
			if(opts.show_panels && opts.fade_panels) {
				obj.j_panels.fadeOut(opts.transition_speed).eq(i%obj.item_count).fadeIn(opts.transition_speed,function(){
					if(!opts.show_filmstrip) {
						$('.nav-prev-overlay',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
						$('.nav-next-overlay',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
						$('.nav-prev',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
						$('.nav-next',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
					}
				});
				$(".overlay-background",obj.j_panels.eq(i%obj.item_count)).css('height', $(".panel-overlay",obj.j_panels.eq(i%obj.item_count)).height());
			}
			
			//If gallery has a filmstrip, handle animation of frames
			if(opts.show_filmstrip) {
				//Slide either pointer or filmstrip, depending on transition method
				if(obj.slide_method=='strip') {
					//Stop filmstrip if it's currently in motion
					obj.j_filmstrip.stop();
					
					if(obj.filmstrip_orientation=='horizontal') {
						//Determine distance between pointer (eventual destination) and target frame
						var distance = getPos(obj, obj.j_frames[i]).left - (getPos(obj, obj.j_pointer[0]).left+(obj.pointer_width/2)-(obj.f_frame_width/2));
						var diststr = (distance>=0?'-=':'+=')+Math.abs(distance)+'px';
						
						//Animate filmstrip and slide target frame under pointer
						obj.j_filmstrip.animate({
							'left':diststr
						},opts.transition_speed,opts.easing,function(){
							//Always ensure that there are a sufficient number of hidden frames on either
							//side of the filmstrip to avoid empty frames
							var old_i = i;
							if(i>obj.item_count) {
								i = i%obj.item_count;
								obj.iterator = i;
								obj.j_filmstrip.css('left','-'+((obj.f_frame_width+opts.frame_gap)*i)+'px');
							} else if (i<=(obj.item_count-strip_size)) {
								i = (i%obj.item_count)+obj.item_count;
								obj.iterator = i;
								obj.j_filmstrip.css('left','-'+((obj.f_frame_width+opts.frame_gap)*i)+'px');
							}
							//If the target frame has changed due to filmstrip shifting,
							//Make sure new target frame has 'current' class and correct size/opacity settings
							if(old_i != i) {
								obj.j_frames.eq(old_i).removeClass('current').find('img').css({
									'opacity':opts.frame_opacity
								});
								obj.j_frames.eq(i).addClass('current').find('img').css({
									'opacity':1.0
								});
							}
							if(!opts.fade_panels) {
								obj.j_panels.hide().eq(i%obj.item_count).show();
								$(".overlay-background",obj.j_panels.eq(i%obj.item_count)).css('height', $(".panel-overlay",obj.j_panels.eq(i%obj.item_count)).height());
							}
							
							//Enable navigation now that animation is complete
							$('.nav-prev-overlay',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next-overlay',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							$('.nav-prev',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							enableFrameClicking(obj);
						});
					} else {
						//Determine distance between pointer (eventual destination) and target frame
						var distance = getPos(obj, obj.j_frames[i]).top - (getPos(obj, obj.j_pointer[0]).top+(obj.pointer_height)-(obj.f_frame_height/2));
						var diststr = (distance>=0?'-=':'+=')+Math.abs(distance)+'px';
						
						//Animate filmstrip and slide target frame under pointer
						obj.j_filmstrip.animate({
							'top':diststr
						},opts.transition_speed,opts.easing,function(){
							//Always ensure that there are a sufficient number of hidden frames on either
							//side of the filmstrip to avoid empty frames
							var old_i = i;
							if(i>obj.item_count) {
								i = i%obj.item_count;
								obj.iterator = i;
								obj.j_filmstrip.css('top','-'+((obj.f_frame_height+opts.frame_gap)*i)+'px');
							} else if (i<=(obj.item_count-strip_size)) {
								i = (i%obj.item_count)+obj.item_count;
								obj.iterator = i;
								obj.j_filmstrip.css('top','-'+((obj.f_frame_height+opts.frame_gap)*i)+'px');
							}
							//If the target frame has changed due to filmstrip shifting,
							//Make sure new target frame has 'current' class and correct size/opacity settings
							if(old_i != i) {
								obj.j_frames.eq(old_i).removeClass('current').find('img').css({
									'opacity':opts.frame_opacity
								});
								obj.j_frames.eq(i).addClass('current').find('img').css({
									'opacity':1.0
								});
							}
							if(!opts.fade_panels) {
								obj.j_panels.hide().eq(i%obj.item_count).show();
								$(".overlay-background",obj.j_panels.eq(i%obj.item_count)).css('height', $(".panel-overlay",obj.j_panels.eq(i%obj.item_count)).height());
							}
							
							//Enable navigation now that animation is complete
							$('.nav-prev-overlay',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next-overlay',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							$('.nav-prev',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							enableFrameClicking(obj);
						});
					}
				} else if(obj.slide_method=='pointer') {
					//Stop pointer if it's currently in motion
					obj.j_pointer.stop();
					//Get position of target frame
					var pos = getPos(obj, obj.j_frames[i]);
					
					if(obj.filmstrip_orientation=='horizontal') {
						//Slide the pointer over the target frame
						obj.j_pointer.animate({
							'left':(pos.left+(obj.f_frame_width/2)-(obj.pointer_width/2)+'px')
						},opts.transition_speed,opts.easing,function(){	
							if(!opts.fade_panels) {
								obj.j_panels.hide().eq(i%obj.item_count).show();
								$(".overlay-background",obj.j_panels.eq(i%obj.item_count)).css('height', $(".panel-overlay",obj.j_panels.eq(i%obj.item_count)).height());
							}
							$('.nav-prev-overlay',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next-overlay',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							$('.nav-prev',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							enableFrameClicking(obj);
						});
					} else {//Slide the pointer over the target frame
						obj.j_pointer.animate({
							'top':(pos.top+(obj.f_frame_height/2)-(obj.pointer_height)+'px')
						},opts.transition_speed,opts.easing,function(){
							if(!opts.fade_panels) {
								obj.j_panels.hide().eq(i%obj.item_count).show();
								$(".overlay-background",obj.j_panels.eq(i%obj.item_count)).css('height', $(".panel-overlay",obj.j_panels.eq(i%obj.item_count)).height());
							}
							$('.nav-prev-overlay',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next-overlay',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							$('.nav-prev',obj.j_gallery).bind("click", {obj: obj}, showPrevItem);
							$('.nav-next',obj.j_gallery).bind("click", {obj: obj}, showNextItem);
							enableFrameClicking(obj);
						});
					}
				}
			
			}
		};
		
	//Find padding and border widths applied to element
	//If border is non-numerical ('thin','medium', etc) set to zero
		function extraWidth(el) {
			if(!el) return 0;
			if(el.length==0) return 0;
			el = el.eq(0);
			var ew = 0;
			ew += getInt(el.css('paddingLeft'));
			ew += getInt(el.css('paddingRight'));
			ew += getInt(el.css('borderLeftWidth'));
			ew += getInt(el.css('borderRightWidth'));
			return ew;
		}
	//Find padding and border heights applied to element
	//If border is non-numerical ('thin','medium', etc) set to zero
		function extraHeight(el) {
			if(!el) return 0;
			if(el.length==0) return 0;
			el = el.eq(0);
			var eh = 0;
			eh += getInt(el.css('paddingTop'));
			eh += getInt(el.css('paddingBottom'));
			eh += getInt(el.css('borderTopWidth'));
			eh += getInt(el.css('borderBottomWidth'));
			return eh;
		}
		
	//Halt transition timer, move to next item, restart timer
		function showNextItem(e) {
			obj = e.data.obj;
			
			$("#" + obj.id).stopTime(obj.id + "-transition");
			if(++obj.iterator==obj.j_frames.length) {obj.iterator=0;}
			showItem(obj, obj.iterator);
			if(!obj.paused) {
				$("#" + obj.id).everyTime(obj.opts.transition_interval,obj.id + "-transition",function(){
					showNextItem({data: {obj: obj}});
				});
			}
		};
		
	//Halt transition timer, move to previous item, restart timer
		function showPrevItem(e) {
			obj = e.data.obj;
			
			$("#" + obj.id).stopTime(obj.id + "-transition");
			if(--obj.iterator<0) {obj.iterator = obj.item_count-1;}
			showItem(obj, obj.iterator);
			if(!obj.paused) {
				$("#" + obj.id).everyTime(obj.opts.transition_interval,obj.id + "-transition",function(){
					showNextItem({data: {obj: obj}});
				});
			}
		};
		
	//Get absolute position of element in relation to top-left corner of gallery
	//If el=gallery, return position of gallery within browser viewport
		function getPos(obj, el) {
			var left = 0, top = 0;
			var el_id = el.id;
			if(el.offsetParent) {
				do {
					left += el.offsetLeft;
					top += el.offsetTop;
				} while(el = el.offsetParent);
			}
			//If we want the position of the gallery itself, return it
			if(el_id == obj.id) {return {'left':left,'top':top};}
			//Otherwise, get position of element relative to gallery
			else {
				var gPos = getPos(obj, obj.j_gallery[0]);
				var gLeft = gPos.left;
				var gTop = gPos.top;
				
				return {'left':left-gLeft,'top':top-gTop};
			}
		};
		
	//Add onclick event to each frame
		function enableFrameClicking(obj) {
			obj.j_frames.each(function(i){
				//If there isn't a link in this frame, set up frame to slide on click
				//Frames with links will handle themselves
				if($('a',this).length==0) {
					$(this).click(function(){
						if(obj.iterator!=i) {
							$("#" + obj.id).stopTime(obj.id + "-transition");
							showItem(obj, i);
							obj.iterator = i;
							if(!obj.paused) {
								$("#" + obj.id).everyTime(obj.opts.transition_interval,obj.id + "-transition",function(){
									showNextItem({data: {obj: obj}});
								});
							}
						}
					});
				}
			});
		};
		
	//Construct gallery panels from '.panel' <div>s
		function buildPanels(obj) {
			opts = obj.opts;
			
			//If there are panel captions, add overlay divs
			obj.j_panels.each(function(i){
				if($('.panel-overlay',this).length>0) {
					$(this).append('<div class="overlay-background"></div>');
				}
			});
			if(!opts.show_filmstrip) {
				//Add navigation buttons
				$('<img />').addClass('nav-next').attr('src',obj.theme_path+opts.nav_theme+'/next.png').appendTo(obj.j_gallery).css({
					'position':'absolute',
					'zIndex':'1100',
					'cursor':'pointer',
					'top':((opts.panel_height-22)/2)+obj.gallery_padding+'px',
					'right':'10px',
					'display':'none'
				}).bind("click", {obj: obj}, showNextItem);
				$('<img />').addClass('nav-prev').attr('src',obj.theme_path+opts.nav_theme+'/prev.png').appendTo(obj.j_gallery).css({
					'position':'absolute',
					'zIndex':'1100',
					'cursor':'pointer',
					'top':((opts.panel_height-22)/2)+obj.gallery_padding+'px',
					'left':'10px',
					'display':'none'
				}).bind("click", {obj: obj}, showPrevItem);
				
				$('<img />').addClass('nav-next-overlay').attr('src',obj.theme_path+opts.nav_theme+'/panel-nav-next.png').appendTo(obj.j_gallery).css({
					'position':'absolute',
					'zIndex':'1099',
					'top':((opts.panel_height-22)/2)+obj.gallery_padding-10+'px',
					'right':'0',
					'display':'none',
					'cursor':'pointer',
					'opacity':0.75
				}).bind("click", {obj: obj}, showNextItem);
				
				$('<img />').addClass('nav-prev-overlay').attr('src',obj.theme_path+opts.nav_theme+'/panel-nav-prev.png').appendTo(obj.j_gallery).css({
					'position':'absolute',
					'zIndex':'1099',
					'top':((opts.panel_height-22)/2)+obj.gallery_padding-10+'px',
					'left':'0',
					'display':'none',
					'cursor':'pointer',
					'opacity':0.75
				}).bind("click", {obj: obj}, showPrevItem);
			}
			obj.j_panels.each(function(i){
				$(this).css({
					'width':(opts.panel_width-extraWidth(obj.j_panels))+'px',
					'height':(opts.panel_height-extraHeight(obj.j_panels))+'px',
					'position':'absolute',
					'overflow':'hidden',
					'display':'none'
				});
				switch(opts.filmstrip_position) {
					case 'top': $(this).css({
									'top':obj.strip_height+Math.max(obj.gallery_padding,obj.filmstrip_margin)+'px',
									'left':obj.gallery_padding+'px'
								}); break;
					case 'left': $(this).css({
								 	'top':obj.gallery_padding+'px',
									'left':obj.strip_width+Math.max(obj.gallery_padding,obj.filmstrip_margin)+'px'
								 }); break;
					default: $(this).css({'top':obj.gallery_padding+'px','left':obj.gallery_padding+'px'}); break;
				}
			});
			$('.panel-overlay',obj.j_panels).css({
				'position':'absolute',
				'zIndex':'999',
				'width':(opts.panel_width-extraWidth($('.panel-overlay',obj.j_panels)))+'px',
				'left':'0'
			});
			$('.overlay-background',obj.j_panels).css({
				'position':'absolute',
				'zIndex':'998',
				'width':opts.panel_width+'px',
				'left':'0',
				'opacity':opts.overlay_opacity
			});
			if(opts.overlay_position=='top') {
				$('.panel-overlay',obj.j_panels).css('top',0);
				$('.overlay-background',obj.j_panels).css('top',0);
			} else {
				$('.panel-overlay',obj.j_panels).css('bottom',0);
				$('.overlay-background',obj.j_panels).css('bottom',0);
			}
			
			$('.panel iframe',obj.j_panels).css({
				'width':opts.panel_width+'px',
				'height':opts.panel_height+'px',
				'border':'0'
			});
			
			if(obj.scale_panel_images) {
				$('img',obj.j_panels).each(function(i){
					$(this).css({
						'height':obj.panel_img_scale[i%obj.item_count]*obj.img_h[i%obj.item_count],
						'width':obj.panel_img_scale[i%obj.item_count]*obj.img_w[i%obj.item_count],
						'position':'relative',
						'top':(opts.panel_height-(obj.panel_img_scale[i%obj.item_count]*obj.img_h[i%obj.item_count]))/2+'px',
						'left':(opts.panel_width-(obj.panel_img_scale[i%obj.item_count]*obj.img_w[i%obj.item_count]))/2+'px'
					});
				});
			}
		};
		
	//Construct filmstrip from '.filmstrip' <ul>
		function buildFilmstrip(obj) {
			opts = obj.opts
			//Add wrapper to filmstrip to hide extra frames
			obj.j_filmstrip.wrap('<div class="strip_wrapper"></div>');
			if(obj.slide_method=='strip') {
				obj.j_frames.clone().appendTo(obj.j_filmstrip);
				obj.j_frames.clone().appendTo(obj.j_filmstrip);
				obj.j_frames = $('li',obj.j_filmstrip);
			}
			//If captions are enabled, add caption divs and fill with the image titles
			if(opts.show_captions) {
				obj.j_frames.append('<div class="caption"></div>').each(function(i){
					$(this).find('.caption').html($(this).find('img').attr('title'));
					//$(this).find('.caption').html(i);
				});
			}
			obj.j_filmstrip.css({
				'listStyle':'none',
				'margin':'0',
				'padding':'0',
				'width':obj.strip_width+'px',
				'position':'absolute',
				'zIndex':'900',
				'top':(obj.filmstrip_orientation=='vertical' && obj.slide_method=='strip'?-((obj.f_frame_height+opts.frame_gap)*obj.iterator):0)+'px',
				'left':(obj.filmstrip_orientation=='horizontal' && obj.slide_method=='strip'?-((obj.f_frame_width+opts.frame_gap)*obj.iterator):0)+'px',
				'height':obj.strip_height+'px'
			});
			obj.j_frames.css({
				'float':'left',
				'position':'relative',
				'height':obj.f_frame_height+'px',
				'width':obj.f_frame_width+'px',
				'zIndex':'901',
				'padding':'0',
				'margin':'0',
				'cursor':'pointer'
			});
			switch(opts.filmstrip_position) {
				case 'top': obj.j_frames.css({
								'marginBottom':obj.filmstrip_margin+'px',
								'marginRight':opts.frame_gap+'px'
							}); break;
				case 'bottom': obj.j_frames.css({
								'marginTop':obj.filmstrip_margin+'px',
								'marginRight':opts.frame_gap+'px'
							}); break;
				case 'left': obj.j_frames.css({
								'marginRight':obj.filmstrip_margin+'px',
								'marginBottom':opts.frame_gap+'px'
							}); break;
				case 'right': obj.j_frames.css({
								'marginLeft':obj.filmstrip_margin+'px',
								'marginBottom':opts.frame_gap+'px'
							}); break;
			}
			$('.img_wrap',obj.j_frames).each(function(i) {
				$(this).css({
					'height':Math.min(opts.frame_height,obj.img_h[i%obj.item_count]*obj.frame_img_scale[i%obj.item_count])+'px',
					'width':Math.min(opts.frame_width,obj.img_w[i%obj.item_count]*obj.frame_img_scale[i%obj.item_count])+'px',
					'position':'relative',
					'top':Math.max(0,(opts.frame_height-(obj.frame_img_scale[i%obj.item_count]*obj.img_h[i%obj.item_count]))/2)+'px',
					'left':Math.max(0,(opts.frame_width-(obj.frame_img_scale[i%obj.item_count]*obj.img_w[i%obj.item_count]))/2)+'px',
					'overflow':'hidden'
				});
			});
			$('img',obj.j_frames).each(function(i){
				$(this).css({
					'opacity':opts.frame_opacity,
					'height':obj.img_h[i%obj.item_count]*obj.frame_img_scale[i%obj.item_count]+'px',
					'width':obj.img_w[i%obj.item_count]*obj.frame_img_scale[i%obj.item_count]+'px',
					'position':'relative',
					'top':Math.min(0,(opts.frame_height-(obj.frame_img_scale[i%obj.item_count]*obj.img_h[i%obj.item_count]))/2)+'px',
					'left':Math.min(0,(opts.frame_width-(obj.frame_img_scale[i%obj.item_count]*obj.img_w[i%obj.item_count]))/2)+'px'
				}).mouseover(function(){
					$(this).stop().animate({'opacity':1.0},300);
				}).mouseout(function(){
					//Don't fade out current frame on mouseout
					if(!$(this).parent().parent().hasClass('current')) $(this).stop().animate({'opacity':opts.frame_opacity},300);
				});
			});
			$('.strip_wrapper',obj.j_gallery).css({
				'position':'absolute',
				'overflow':'hidden'
			});
			if(obj.filmstrip_orientation=='horizontal') {
				$('.strip_wrapper',obj.j_gallery).css({
					'top':(opts.filmstrip_position=='top'?Math.max(obj.gallery_padding,obj.filmstrip_margin)+'px':opts.panel_height+obj.gallery_padding+'px'),
					'left':((obj.gallery_width-obj.wrapper_width)/2)+obj.gallery_padding+'px',
					'width':obj.wrapper_width+'px',
					'height':obj.strip_height+'px'
				});
			} else {
				$('.strip_wrapper',obj.j_gallery).css({
					'left':(opts.filmstrip_position=='left'?Math.max(obj.gallery_padding,obj.filmstrip_margin)+'px':opts.panel_width+obj.gallery_padding+'px'),
					'top':'0px',
					'width':obj.strip_width+'px',
					'height':wrapper_height+'px'
				});
			}
			$('.caption',obj.j_gallery).css({
				'position':'absolute',
				'top':(opts.filmstrip_position=='bottom'?obj.f_frame_height:0)+'px',
				'left':'0',
				'margin':'0',
				'width':obj.f_frame_width+'px',
				'padding':'0',
				'padding-top':opts.frame_caption_padding+'px',
				'height':Math.floor(opts.frame_caption_size*1.25)+'px',
				'font-size':opts.frame_caption_size+'px',
				'overflow':'hidden',
				'lineHeight':1,
			});
			var pointer = $('<div></div>');
			pointer.addClass('pointer').appendTo(obj.j_gallery).css({
				 'position':'absolute',
				 'zIndex':'1000',
				 'width':'0px',
				 'fontSize':'0px',
				 'lineHeight':'0%',
				 'borderTopWidth':obj.pointer_height+'px',
				 'borderRightWidth':(obj.pointer_width/2)+'px',
				 'borderBottomWidth':obj.pointer_height+'px',
				 'borderLeftWidth':(obj.pointer_width/2)+'px',
				 'borderStyle':'solid'
			});
			
			//For IE6, use predefined color string in place of transparent (see stylesheet)
			var transColor = $.browser.msie && $.browser.version.substr(0,1)=='6' ? 'pink' : 'transparent'
			
			if(!opts.show_panels) { pointer.css('borderColor',transColor); }
			
			switch(opts.filmstrip_position) {
				case 'top': pointer.css({
								'bottom':(opts.panel_height-(obj.pointer_height*2)+obj.gallery_padding+obj.filmstrip_margin)+'px',
								'left':((obj.gallery_width-obj.wrapper_width)/2)+(obj.slide_method=='strip'?0:((obj.f_frame_width+opts.frame_gap)*obj.iterator))+((obj.f_frame_width/2)-(obj.pointer_width/2))+obj.gallery_padding+'px',
								'borderBottomColor':transColor,
								'borderRightColor':transColor,
								'borderLeftColor':transColor
							}); break;
				case 'bottom': pointer.css({
									'top':(opts.panel_height-(obj.pointer_height*2)+obj.gallery_padding+obj.filmstrip_margin)+'px',
									'left':((obj.gallery_width-obj.wrapper_width)/2)+(obj.slide_method=='strip'?0:((obj.f_frame_width+opts.frame_gap)*obj.iterator))+((obj.f_frame_width/2)-(obj.pointer_width/2))+obj.gallery_padding+'px',
									'borderTopColor':transColor,
									'borderRightColor':transColor,
									'borderLeftColor':transColor
								}); break;
				case 'left': pointer.css({
								'right':(opts.panel_width-obj.pointer_width+obj.gallery_padding+obj.filmstrip_margin)+'px',
								'top':(obj.f_frame_height/2)-(obj.pointer_height)+(obj.slide_method=='strip'?0:((obj.f_frame_height+opts.frame_gap)*obj.iterator))+obj.gallery_padding+'px',
								'borderBottomColor':transColor,
								'borderRightColor':transColor,
								'borderTopColor':transColor
							}); break;
				case 'right': pointer.css({
								'left':(opts.panel_width-obj.pointer_width+obj.gallery_padding+obj.filmstrip_margin)+'px',
								'top':(obj.f_frame_height/2)-(obj.pointer_height)+(obj.slide_method=='strip'?0:((obj.f_frame_height+opts.frame_gap)*obj.iterator))+obj.gallery_padding+'px',
								'borderBottomColor':transColor,
								'borderLeftColor':transColor,
								'borderTopColor':transColor
							}); break;
			}
			
			obj.j_pointer = $('.pointer',obj.j_gallery);
			
			//Add navigation buttons
			var navNext = $('<img />');
			navNext.addClass('nav-next').attr('src',obj.theme_path+opts.nav_theme+'/next.png').appendTo(obj.j_gallery).css({
				'position':'absolute',
				'cursor':'pointer'
			}).bind("click", {obj: obj}, showNextItem);
			var navPrev = $('<img />');
			navPrev.addClass('nav-prev').attr('src',obj.theme_path+opts.nav_theme+'/prev.png').appendTo(obj.j_gallery).css({
				'position':'absolute',
				'cursor':'pointer'
			}).bind("click", {obj: obj}, showPrevItem);
			if(obj.filmstrip_orientation=='horizontal') {
				var filmstrip_top_padding = 0;
				if((opts.filmstrip_position=='top')&&(opts.show_captions==true)) {
					filmstrip_top_padding = obj.frame_caption_height;
				}
				
				navNext.css({
					'top':(opts.filmstrip_position=='top'?Math.max(obj.gallery_padding,obj.filmstrip_margin):opts.panel_height+obj.filmstrip_margin+obj.gallery_padding)+((obj.f_frame_height-22)/2)+filmstrip_top_padding+'px',
					'right':((obj.gallery_width+(obj.gallery_padding*2))/2)-(obj.wrapper_width/2)-opts.frame_gap-22+'px'
				});
				navPrev.css({
					'top':(opts.filmstrip_position=='top'?Math.max(obj.gallery_padding,obj.filmstrip_margin):opts.panel_height+obj.filmstrip_margin+obj.gallery_padding)+((obj.f_frame_height-22)/2)+filmstrip_top_padding+'px',
					'left':((obj.gallery_width+(obj.gallery_padding*2))/2)-(obj.wrapper_width/2)-opts.frame_gap-22+'px'
				 });
			} else {
				navNext.css({
					'left':(opts.filmstrip_position=='left'?Math.max(obj.gallery_padding,obj.filmstrip_margin):opts.panel_width+obj.filmstrip_margin+obj.gallery_padding)+((obj.f_frame_width-22)/2)+13+'px',
					'top':wrapper_height+(Math.max(obj.gallery_padding,opts.frame_gap)*2)+'px'
				});
				navPrev.css({
					'left':(opts.filmstrip_position=='left'?Math.max(obj.gallery_padding,obj.filmstrip_margin):opts.panel_width+obj.filmstrip_margin+obj.gallery_padding)+((obj.f_frame_width-22)/2)-13+'px',
					'top':wrapper_height+(Math.max(obj.gallery_padding,opts.frame_gap)*2)+'px'
				});
			}
		};
		
	//Check mouse to see if it is within the borders of the panel
	//More reliable than 'mouseover' event when elements overlay the panel
		function mouseIsOverGallery(obj, x, y) {
			var pos = getPos(obj, obj.j_gallery[0]);
			var top = pos.top;
			var left = pos.left;
			return x > left && x < left+obj.gallery_width+(obj.filmstrip_orientation=='horizontal'?(obj.gallery_padding*2):obj.gallery_padding+Math.max(obj.gallery_padding,obj.filmstrip_margin)) && y > top && y < top+obj.gallery_height+(obj.filmstrip_orientation=='vertical'?(obj.gallery_padding*2):obj.gallery_padding+Math.max(obj.gallery_padding,obj.filmstrip_margin));
		};
		
		function getInt(i) {
			i = parseInt(i,10);
			if(isNaN(i)) { i = 0; }
			return i;
		}
		
		function buildGallery(obj) {
			var gallery_images = opts.show_filmstrip?$('img',obj.j_frames):$('img',obj.j_panels);
			gallery_images.each(function(i){
				obj.img_h[i] = this.height;
				obj.img_w[i] = this.width;
				if(opts.frame_scale=='nocrop') {
					obj.frame_img_scale[i] = Math.min(opts.frame_height/obj.img_h[i],opts.frame_width/obj.img_w[i]);
				} else {
					obj.frame_img_scale[i] = Math.max(opts.frame_height/obj.img_h[i],opts.frame_width/obj.img_w[i]);
				}
				
				if(opts.panel_scale=='nocrop') {
					obj.panel_img_scale[i] = Math.min(opts.panel_height/obj.img_h[i],opts.panel_width/obj.img_w[i]);
				} else {
					obj.panel_img_scale[i] = Math.max(opts.panel_height/obj.img_h[i],opts.panel_width/obj.img_w[i]);
				}
			});
			
	/************************************************/
	/*	Apply CSS Styles							*/
	/************************************************/
			obj.j_gallery.css({
				'position':'relative',
				'width':obj.gallery_width+(obj.filmstrip_orientation=='horizontal'?(obj.gallery_padding*2):obj.gallery_padding)+'px',
				'height':obj.gallery_height+(obj.filmstrip_orientation=='vertical'?(obj.gallery_padding*2):obj.gallery_padding)+'px'
			});
			
	/************************************************/
	/*	Build filmstrip and/or panels				*/
	/************************************************/
			if(opts.show_filmstrip) {
				buildFilmstrip(obj);
				enableFrameClicking(obj);
			}
			if(opts.show_panels) {
				buildPanels(obj);
			}
			
	/************************************************/
	/*	Add events to various elements				*/
	/************************************************/
			if(opts.pause_on_hover || (opts.show_panels && !opts.show_filmstrip)) {
				$().mousemove(function(e){
					if(mouseIsOverGallery(obj, e.pageX, e.pageY)) {
						if(opts.pause_on_hover) {
							if(!obj.paused) {
								$("#" + obj.id).oneTime(500,obj.id + "-animation_pause",function(){
									$("#" + obj.id).stopTime(obj.id + "-transition");
									obj.paused=true;
								});
							}
						}
						if(opts.show_panels && !opts.show_filmstrip && !obj.panel_nav_displayed) {
							$('.nav-next-overlay', obj.j_gallery).fadeIn('fast');
							$('.nav-prev-overlay', obj.j_gallery).fadeIn('fast');
							$('.nav-next',obj.j_gallery).fadeIn('fast');
							$('.nav-prev',obj.j_gallery).fadeIn('fast');
							obj.panel_nav_displayed = true;
						}
					} else {
						if(opts.pause_on_hover) {
							$("#" + obj.id).stopTime(obj.id + "-animation_pause");
							if(obj.paused) {
								$("#" + obj.id).everyTime(opts.transition_interval,obj.id + "-transition",function(){
									showNextItem({data: {obj: obj}});
								});
								obj.paused = false;
							}
						}
						if(opts.show_panels && !opts.show_filmstrip && obj.panel_nav_displayed) {
							$('.nav-next-overlay', obj.j_gallery).fadeOut('fast');
							$('.nav-prev-overlay', obj.j_gallery).fadeOut('fast');
							$('.nav-next',obj.j_gallery).fadeOut('fast');
							$('.nav-prev',obj.j_gallery).fadeOut('fast');
							obj.panel_nav_displayed = false;
						}
					}
				});
			}
			
			
	/****************************************************************/
	/*	Initiate Automated Animation								*/
	/****************************************************************/
			
			//Hide loading box
			obj.j_filmstrip.css('visibility','visible');
			obj.j_gallery.css('visibility','visible');
			$('.loader',obj.j_gallery).fadeOut('1000',function(){
				//Show the 'first' panel
				showItem(obj, obj.iterator);
				//If we have more than one item, begin automated transitions
				if(obj.item_count > 1) {
					$("#" + obj.id).everyTime(opts.transition_interval,obj.id + "-transition",function(){
						showNextItem({data: {obj: obj}});
					});
				}	
			});	
		}
		
/************************************************/
/*	Main Plugin Code							*/
/************************************************/
		return this.each(function() {
			//Hide <ul>
			$(this).css('visibility','hidden');
			
			// Current object var
			var obj = this;
			
			this.opts = new Object();
			for(var i in opts) {
				this.opts[i] = opts[i];
			}
			
			obj.paused = false;
			obj.item_count = 0;
			obj.frame_img_scale = new Object();
			obj.panel_img_scale = new Object();
			obj.img_h = new Object();
			obj.img_w = new Object();
			obj.scale_panel_images = true;
			obj.panel_nav_displayed = false;
			
			//Wrap <ul> in <div> and transfer ID to container <div>
			//Assign filmstrip class to <ul>
			$(this).wrap("<div></div>");
			obj.j_gallery = $(this).parent();
			obj.j_gallery.css('visibility','hidden').attr('id',$(this).attr('id')).addClass('gallery');
			$(this).removeAttr('id').addClass('filmstrip');
			
			obj.id = obj.j_gallery.attr('id');
			
			$("#" + obj.id).stopTime(obj.id + "-transition");
			$("#" + obj.id).stopTime(obj.id + "-animation_pause");
			
			//If there is no defined panel content, we will scale panel images
			obj.scale_panel_images = $('.panel-content',obj.j_gallery).length==0;
			
			//Define dimensions of pointer <div>
			obj.pointer_height = opts.pointer_size;
			obj.pointer_width = opts.pointer_size*2;
			
			//Determine filmstrip orientation (vertical or horizontal)
			//Do not show captions on vertical filmstrips
			obj.filmstrip_orientation = (opts.filmstrip_position=='top'||opts.filmstrip_position=='bottom'?'horizontal':'vertical');
			if(obj.filmstrip_orientation=='vertical') opts.show_captions = false;
			
			//Determine path between current page and plugin images
			//Scan script tags and look for path to GalleryView plugin
			$('script').each(function(i){
				var s = $(this);
				if(s.attr('src') && s.attr('src').match(/jquery\.galleryview/)){
					obj.theme_path = s.attr('src').split('jquery.galleryview')[0]+'themes/';
				}
			});
			
			obj.j_filmstrip = $('.filmstrip',obj.j_gallery);
			obj.j_frames = $('li',obj.j_filmstrip);
			obj.j_frames.addClass('frame');
			
			//If the user wants panels, generate them using the filmstrip images
			if(opts.show_panels) {
				for(i=obj.j_frames.length-1;i>=0;i--) {
					if(obj.j_frames.eq(i).find('.panel-content').length>0) {
						obj.j_frames.eq(i).find('.panel-content').remove().prependTo(obj.j_gallery).addClass('panel');
					} else {
						p = $('<div>');
						p.addClass('panel');
						im = $('<img />');
						im.attr('src',obj.j_frames.eq(i).find('img').eq(0).attr('src')).appendTo(p);
						p.prependTo(obj.j_gallery);
						obj.j_frames.eq(i).find('.panel-overlay').remove().appendTo(p);
					}
				}
			} else {
				$('.panel-overlay',obj.j_frames).remove();
				$('.panel-content',obj.j_frames).remove();
			}
			
			//If the user doesn't want a filmstrip, delete it
			if(!opts.show_filmstrip) { obj.j_filmstrip.remove(); }
			else {
				//Wrap the frame images (and links, if applicable) in container divs
				//These divs will handle cropping and zooming of the images
				obj.j_frames.each(function(i){
					if($(this).find('a').length>0) {
						$(this).find('a').wrap('<div class="img_wrap"></div>');
					} else {
						$(this).find('img').wrap('<div class="img_wrap"></div>');
					}
				});
				obj.j_frame_img_wrappers = $('.img_wrap',obj.j_frames);
			}
			
			obj.j_panels = $('.panel',obj.j_gallery);
			
			if(!opts.show_panels) {
				opts.panel_height = 0;
				opts.panel_width = 0;
			}
			
			
			//Determine final frame dimensions, accounting for user-added padding and border
			obj.f_frame_width = opts.frame_width+extraWidth(obj.j_frame_img_wrappers);
			obj.f_frame_height = opts.frame_height+extraHeight(obj.j_frame_img_wrappers);
			
			//Number of frames in filmstrip
			obj.item_count = opts.show_panels?obj.j_panels.length:obj.j_frames.length;
			
			//Number of frames that can display within the gallery block
			//64 = width of block for navigation button * 2 + 20
			if(obj.filmstrip_orientation=='horizontal') {
				strip_size = opts.show_panels?Math.floor((opts.panel_width-((opts.frame_gap+22)*2))/(obj.f_frame_width+opts.frame_gap)):Math.min(obj.item_count,opts.filmstrip_size);
			} else {
				strip_size = opts.show_panels?Math.floor((opts.panel_height-(opts.frame_gap+22))/(obj.f_frame_height+opts.frame_gap)):Math.min(obj.item_count,opts.filmstrip_size);
			}
			
			/************************************************/
			/*	Determine transition method for filmstrip	*/
			/************************************************/
					//If more items than strip size, slide filmstrip
					//Otherwise, slide pointer
					if(strip_size >= obj.item_count) {
						obj.slide_method = 'pointer';
						strip_size = obj.item_count;
					}
					else {obj.slide_method = 'strip';}
					
					obj.iterator = (strip_size<obj.item_count?obj.item_count:0)+opts.start_frame-1;
			
			/************************************************/
			/*	Determine dimensions of various elements	*/
			/************************************************/
					
					obj.filmstrip_margin = (opts.show_panels?getInt(obj.j_filmstrip.css('marginTop')):0);
					obj.j_filmstrip.css('margin','0px');
					
					if(obj.filmstrip_orientation=='horizontal') {
						if(opts.show_filmstrip && opts.show_captions) {
							obj.frame_caption_height = opts.frame_caption_padding + Math.floor(opts.frame_caption_size*1.25);
						}
						
						//Width of gallery block
						obj.gallery_width = opts.show_panels?opts.panel_width:(strip_size*(obj.f_frame_width+opts.frame_gap))+44+opts.frame_gap;
						
						//Height of gallery block = screen + filmstrip + captions (optional)
						obj.gallery_height = (opts.show_panels?opts.panel_height:0)+(opts.show_filmstrip?obj.f_frame_height+obj.filmstrip_margin+obj.frame_caption_height:0);
					} else {
						//Width of gallery block
						obj.gallery_height = opts.show_panels?opts.panel_height:(strip_size*(obj.f_frame_height+opts.frame_gap))+22;
						
						//Height of gallery block = screen + filmstrip + captions (optional)
						obj.gallery_width = (opts.show_panels?opts.panel_width:0)+(opts.show_filmstrip?obj.f_frame_width+obj.filmstrip_margin:0);
					}
					
					
					//Width of filmstrip
					if(obj.filmstrip_orientation=='horizontal') {
						if(obj.slide_method == 'pointer') {obj.strip_width = (obj.f_frame_width*obj.item_count)+(opts.frame_gap*(obj.item_count));}
						else {obj.strip_width = (obj.f_frame_width*obj.item_count*3)+(opts.frame_gap*(obj.item_count*3));}
					} else {
						obj.strip_width = (obj.f_frame_width+obj.filmstrip_margin);
					}
					
					if(obj.filmstrip_orientation=='horizontal') {
						obj.strip_height = obj.f_frame_height+obj.filmstrip_margin+obj.frame_caption_height;
					} else {
						if(obj.slide_method == 'pointer') {obj.strip_height = (obj.f_frame_height*obj.item_count+opts.frame_gap*(obj.item_count));}
						else {obj.strip_height = (obj.f_frame_height*obj.item_count*3)+(opts.frame_gap*(obj.item_count*3));}
					}
					
					//Width of filmstrip wrapper (to hide overflow)
					obj.wrapper_width = ((strip_size*obj.f_frame_width)+((strip_size-1)*opts.frame_gap));
					wrapper_height = ((strip_size*obj.f_frame_height)+((strip_size-1)*opts.frame_gap));
					
					obj.gallery_padding = getInt(obj.j_gallery.css('paddingTop'));
					obj.j_gallery.css('padding','0px');
			/********************************************************/
			/*	PLACE LOADING BOX OVER GALLERY UNTIL IMAGES LOAD	*/
			/********************************************************/
					galleryPos = getPos(obj, obj.j_gallery[0]);
					$('<div>').addClass('loader').css({
						'position':'absolute',
						'zIndex':'32666',
						'opacity':1,
						'top':'0px',
						'left':'0px',
						'width':obj.gallery_width+(obj.filmstrip_orientation=='horizontal'?(obj.gallery_padding*2):obj.gallery_padding)+'px',
						'height':obj.gallery_height+(obj.filmstrip_orientation=='vertical'?(obj.gallery_padding*2):obj.gallery_padding)+'px'
					}).appendTo(obj.j_gallery);
					
			
			if(!window_loaded) {
				$(window).load(function(){
					window_loaded = true;
					buildGallery(obj);
					
					var gallery = $(obj.j_gallery);
				});
			} else {
				buildGallery(obj);
			}
		});
	};
	
	$.fn.galleryView.defaults = {
		
		show_panels: true,
		show_filmstrip: true,
		
		panel_width: 600,
		panel_height: 400,
		frame_width: 60,
		frame_height: 40,
		
		start_frame: 1,
		filmstrip_size: 3,
		transition_speed: 800,
		transition_interval: 4000,
		
		overlay_opacity: 0.7,
		frame_opacity: 0.3,
		
		pointer_size: 8,
		
		nav_theme: 'dark',
		easing: 'swing',
		
		filmstrip_position: 'bottom',
		overlay_position: 'bottom',
		
		panel_scale: 'nocrop',
		frame_scale: 'crop',
		
		frame_gap: 5,
		frame_caption_size: 12,
		frame_caption_padding: 2,
		
		show_captions: false,
		fade_panels: true,
		pause_on_hover: false
	};
})(jQuery);
