/*

	GalleryView - jQuery Content Gallery Plugin
	Author: 		Jack Anderson
	Version:		1.1 (April 5, 2009)
	Documentation: 	http://www.spaceforaname.com/jquery/galleryview/


	Customizations
	--------------
	Author: 		Nicholas Chua
	Version:		1.1 Modification to suite aesthetics design (6 May, 2010)

	Author: 		Nicholas Chua
	Version:		1.1 Further Modification to suite logic changes requirements (9 Jun, 2010)

	
*/


(function($){
	$.fn.galleryView = function(options) {
		var opts = $.extend($.fn.galleryView.defaults,options);
		
		var id;
		var iterator = 0;
		var gallery_width;
		var gallery_height;
		var frame_margin = 4;
		var strip_width;
		var wrapper_width;
		var item_count = 0;
		var slide_method;
		var img_path;
		var paused = false;
		var frame_caption_size = 20;
		var frame_margin_top = 11;
		var pointer_width = 2;
		
		//Define jQuery objects for reuse
		var j_gallery;
		var j_filmstrip;
		var j_frames;
		var j_panels;
		var j_pointer;

		
/************************************************/
/*	Plugin Methods								*/
/************************************************/	
		//20100512: nicholas: overlay hiding when no overlay text is available
		function showPanelOverlay(i) {
			if(has_panels) {
			
				//Setting of Item Number in X of X info
				$('.info-overlay-item')[i%item_count].innerHTML = i%item_count+1;

			
					//alert(i%item_count)
					//alert($('.panel-overlay')[xxx-1]);
					//alert($('.panel-overlay').length);
					if($('.panel-overlay')[i%item_count].innerHTML.toLowerCase().replace(' ', '') == ''	|| $('.panel-overlay')[i%item_count].innerHTML.toLowerCase().replace(' ', '') == '<p></p>')
					{	
						$('.overlay',j_panels).css({
							'visibility':'hidden'
						});
					}
					else{
						$('.overlay',j_panels).css({
							'visibility':'visible'
						});
					}
					
					//nicholas: 20100609: pause at end of slide show
					if(i%item_count+1 >= item_count){
						$(document).oneTime(100,"animation_pause",function(){
							$(document).stopTime("transition");
							paused=true;
							
							slideshow_pause();
							});
					}
			}
		};
		
		function showItem(i) {
			//alert(i)
			showPanelOverlay(i);
			
			//nicholas:20100609
			$('img.nav-forward').unbind('click');
			$('img.nav-backward').unbind('click');

			//Disable next/prev buttons until transition is complete
			$('img.nav-next').unbind('click');
			$('img.nav-prev').unbind('click');
			j_frames.unbind('click');
			if(has_panels) {
				if(opts.fade_panels) {
					//Fade out all panels and fade in target panel
					j_panels.fadeOut(opts.transition_speed).eq(i%item_count).fadeIn(opts.transition_speed,function(){
						if(!has_filmstrip) {
							//nicholas:20100609
							$('img.nav-forward').click(showNextItemSet);
							$('img.nav-backward').click(showPrevItemSet);

							$('img.nav-prev').click(showPrevItem);
							$('img.nav-next').click(showNextItem);		
						}
					});
				} 
			}
			
			if(has_filmstrip) {
				//Slide either pointer or filmstrip, depending on transition method
				if(slide_method=='strip') {
					//Stop filmstrip if it's currently in motion
					j_filmstrip.stop();
					
					//Determine distance between pointer (eventual destination) and target frame
					var distance = getPos(j_frames[i]).left - (getPos(j_pointer[0]).left+2);
					var leftstr = (distance>=0?'-=':'+=')+Math.abs(distance)+'px';
					
					//Animate filmstrip and slide target frame under pointer
					//If target frame is a duplicate, jump back to 'original' frame
					j_filmstrip.animate({
						'left':leftstr
					},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
						if(i>item_count) {
							i = i%item_count;
							iterator = i;
							j_filmstrip.css('left','-'+((opts.frame_width+frame_margin)*i)+'px');
						} else if (i<=(item_count-strip_size)) {
							i = (i%item_count)+item_count;
							iterator = i;
							j_filmstrip.css('left','-'+((opts.frame_width+frame_margin)*i)+'px');
						}
						
						if(!opts.fade_panels) {
							j_panels.hide().eq(i%item_count).show();
						}

						//nicholas:20100609
						$('img.nav-forward').click(showNextItemSet);
						$('img.nav-backward').click(showPrevItemSet);

						$('img.nav-prev').click(showPrevItem);
						$('img.nav-next').click(showNextItem);
						enableFrameClicking();
					});
				} else if(slide_method=='pointer') {
					//Stop pointer if it's currently in motion
					j_pointer.stop();
					//Get position of target frame
					var pos = getPos(j_frames[i]);
					//Slide the pointer over the target frame
					j_pointer.animate({
						//nicholas:20100609:ie8 check addition
						'left':(pos.left-2+'px')
					},opts.transition_speed,opts.easing,function(){	
						if(!opts.fade_panels) {
							j_panels.hide().eq(i%item_count).show();
						}	

						//nicholas:20100609
						$('img.nav-forward').click(showNextItemSet);
						$('img.nav-backward').click(showPrevItemSet);

						$('img.nav-prev').click(showPrevItem);
						$('img.nav-next').click(showNextItem);
						enableFrameClicking();
					});
				}
			
				if($('a',j_frames[i])[0]) {
					j_pointer.unbind('click').click(function(){
						var a = $('a',j_frames[i]).eq(0);
						if(a.attr('target')=='_blank') {window.open(a.attr('href'));}
						else {location.href = a.attr('href');}
					});
				}
			}
		};
		function showNextItem() {
			$(document).stopTime("transition");
			if(++iterator==j_frames.length) {iterator=0;}
			showItem(iterator);
				$(document).everyTime(opts.transition_interval,"transition",function(){
					showNextItem();
			});
		};
		function showPrevItem() {
			$(document).stopTime("transition");
			if(--iterator<0) {iterator = item_count-1;}
			//alert(iterator);
			showItem(iterator);
			$(document).everyTime(opts.transition_interval,"transition",function(){
				showNextItem();
			});
		};
		
		
		//nicholas: 20100607
		function showNextItemSet() {
			if(item_count<=5)
				return;
				
			$(document).stopTime("transition");
			iterator = iterator + 5;
			if(iterator==j_frames.length) {iterator=0;}
			showItem(iterator);
			if(!paused)//nicholas:20100611
			{
				$(document).everyTime(opts.transition_interval,"transition",function(){
					showNextItem();
				});
			}
		};
		//nicholas: 20100607
		function showPrevItemSet() {
			if(item_count<=5)
				return;

			$(document).stopTime("transition");
			iterator = iterator - 5;
			if(iterator<0) {iterator = 0;}
			//alert(iterator);
			showItem(iterator);
			if(!paused)//nicholas:20100611
			{
				$(document).everyTime(opts.transition_interval,"transition",function(){
					showNextItem();
				});
			}
		};
		//nicholas: 20100609
		function slideshow_replay() {
			showItem(0);
			if(item_count<=5)
				iterator = 0;

		}
		function slideshow_toggle() {
			if(opts.pause_on_hover)
				return;
			
			if(paused){
				if(iterator%item_count<item_count-1){
					$(document).everyTime(opts.transition_interval,"transition",function(){
						showNextItem();
					});
					slideshow_play();
				}
				else{
					slideshow_replay();
					$(document).everyTime(opts.transition_interval,"transition",function(){
						showNextItem();
					});
					slideshow_play();
				}
				paused = false;
			}
			else{
				$(document).oneTime(100,"animation_pause",function(){
					$(document).stopTime("transition");
					paused=true;
					slideshow_pause();
				});
			}
		}
		function slideshow_play() {
			if($('.nav-play').attr('src').indexOf('hover.') >= 0){
				$('.nav-play').attr('src',img_path+opts.nav_theme+'/pause_hover.gif');
			}
			else{
				$('.nav-play').attr('src',img_path+opts.nav_theme+'/pause.gif');
			}
				
			$('.nav-play').attr('alt','Pause');
			$('.nav-play').attr('title','Pause');
		}
		function slideshow_pause() {
			if($('.nav-play').attr('src').indexOf('hover.') >= 0){
				$('.nav-play').attr('src',img_path+opts.nav_theme+'/play_hover.gif');
			}
			else{
				$('.nav-play').attr('src',img_path+opts.nav_theme+'/play.gif');
			}
			
			$('.nav-play').attr('alt','Play');
			$('.nav-play').attr('title','Play');
		}

		function slideshow_mouseover_Play() {
			//alert($('.nav-play').attr('src').indexOf('pause') >= 0);
			if($('.nav-play').attr('src').indexOf('pause.') >= 0){
					$('.nav-play').attr('src',img_path+opts.nav_theme+'/pause_hover.gif');
					//alert('pause');
				}
			else if($('.nav-play').attr('src').indexOf('play.') >= 0){
					$('.nav-play').attr('src',img_path+opts.nav_theme+'/play_hover.gif');
					//alert('play');
				}
		}
		function slideshow_mouseout_Play() {
			//alert($('.nav-play').attr('src').indexOf('pause') >= 0);
			if($('.nav-play').attr('src').indexOf('pause_hover.') >= 0){
					//alert('pause_hover');
					$('.nav-play').attr('src',img_path+opts.nav_theme+'/pause.gif');
				}
			else if($('.nav-play').attr('src').indexOf('play_hover.') >= 0){
					//alert('play_hover');
					$('.nav-play').attr('src',img_path+opts.nav_theme+'/play.gif');
				}
		}
		function slideshow_mouseover_Replay() {
			//alert("Hover Replay");
		}
		function slideshow_mouseover_NextItem() {
			//alert("Hover next item");
		}
		function slideshow_mouseover_PrevItem() {
			//alert("Hover prev item");
		}
		function slideshow_mouseover_NextItemSet() {
			$('.nav-forward').attr('src',img_path+opts.nav_theme+'/forward_hover.gif');
			//alert("Hover next item set");
		}
		function slideshow_mouseout_NextItemSet() {
			$('.nav-forward').attr('src',img_path+opts.nav_theme+'/forward.gif');
			//alert("Out next item set");
		}
		function slideshow_mouseover_PrevItemSet() {
			$('.nav-backward').attr('src',img_path+opts.nav_theme+'/backward_hover.gif');
			//alert("Hover prev item set");
		}
		function slideshow_mouseout_PrevItemSet() {
			$('.nav-backward').attr('src',img_path+opts.nav_theme+'/backward.gif');
			//alert("Out next item set");
		}
		
		
		function getPos(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 == id) {return {'left':left,'top':top};}
			//Otherwise, get position of element relative to gallery
			else {
				var gPos = getPos(j_gallery[0]);
				var gLeft = gPos.left;
				var gTop = gPos.top;
				
				return {'left':left-gLeft,'top':top-gTop};
			}
		};
		function enableFrameClicking() {
			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(){
						$(document).stopTime("transition");
						showItem(i);
						iterator = i;
						if(!paused)//nicholas:20100611
						{
							$(document).everyTime(opts.transition_interval,"transition",function(){
								showNextItem();
							});
						}
					});
				}
			});
		};
		
		function buildPanels() {
			//If there are panel captions, add overlay divs
			if($('.panel-overlay').length>0) {
				j_panels.append('<div class="overlay"></div>');

				//nicholas: 20100607: info overlay addition
				j_panels.append('<div class="info-overlay"></div>');
				$('.info-overlay').append('<div class="info-overlay-wrapper"></div>')
				//
				$('.info-overlay-wrapper').append('<img class="nav-replay" alt="Replay" title="Replay" src="'+img_path+opts.nav_theme+'/replay.gif'+'" />');
				$('.info-overlay-wrapper').append('<div class="info-overlay-item-wrapper"></div>')
				$('.info-overlay-item-wrapper').append('Image<span class="info-overlay-item">x</span>of<span class="info-overlay-item-count">'+item_count+'</span>');
				$('.info-overlay-wrapper').append('<img class="nav-play" src="" />');
				slideshow_play();

				//
				$('.info-overlay',j_panels).css({
					'position':'absolute',
					'zIndex':'1999',
					'width':opts.panel_width+'px',
					'height':'20px',
					'top':opts.panel_height-20+'px',
					'left':'0px',
					'background':'#8e8d8d',
					'color':'white',
					'text-align':'right',
					'opacity':1,
					'padding':'0px 0px 0px 0px'
				});
				$('.info-overlay-wrapper',j_panels).css({
					'width':'260px',
					'float':'right',
					'border':'solid 0px blue'
				});
				$('.info-overlay-item-wrapper',j_panels).css({
					'width':'160px',
					'float':'left',
					'border':'solid 0px green',
					'padding':'1px 5px 0px 5px',
					'background':'none'
				});
				$('.info-overlay-item',j_panels).css({
					'padding':'0px 3px 0px 3px',
					'border':'solid 0px red',
					'background':'none'
				});
				$('.info-overlay-item-count',j_panels).css({
					'padding':'0px 3px 0px 3px',
					'border':'solid 0px red',
					'background':'none'
				});
				$('.nav-replay').css({
					'display':'none',
					'width':'16px',
					'height':'18px',
					'cursor':'pointer',
					'padding':'1px 5px 0px 15px',
					'float':'left'
				}).click(slideshow_replay).mouseover(slideshow_mouseover_Replay);
				$('.nav-play').css({
					'width':'40px',
					'height':'14px',
					'padding':'3px 15px 0px 15px',
					'float':'left'
				}).click(slideshow_toggle).mouseover(slideshow_mouseover_Play).mouseout(slideshow_mouseout_Play);
				if(!opts.pause_on_hover){
					$('.nav-play').css({
						'cursor':'pointer'
					});
				}
				
				//20100806: nicholas: moving img into a table cell for vertical alignment
				$(j_panels).each(function(i){
					//alert(this.innerHTML);
					//
					//find the target image
					var myImg = $('img', this).eq(0);
					//create table with image
					$('<table cellspacing="0" cellpadding="0" border="0" style="' +
							'position: relative; top: 0; left: 0; text-align: center;' +
							'border: solid 0px red; padding: 0px; margin: 0px;' + 
							'width: ' + opts.panel_width + 'px;' +
							'height: ' + opts.panel_height + 'px;' +
							'"><tr><td><img border="0" src="' + myImg.attr('src') + '" /></td></tr></table>'
						).insertBefore(myImg);
					//hide original image
					$(myImg).css({'display':'none'});
				});
				
				showPanelOverlay(0)
			}
			
			if(!has_filmstrip) {
				//nicholas: 20100607:
				//Add navigation buttons - Info Overlay
				$('<img />').addClass('nav-forward').attr('src',img_path+opts.nav_theme+'/forward.gif').appendTo(j_gallery).css({
					'position':'absolute',
					'zIndex':'1101',
					'cursor':'pointer',
					'top':((opts.panel_height-42)/2)+'px',
					'right':'10px',
					'display':'none'
				}).click(showNextItemSet);
				$('<img />').addClass('nav-backward').attr('src',img_path+opts.nav_theme+'/backward.gif').appendTo(j_gallery).css({
					'position':'absolute',
					'zIndex':'1101',
					'cursor':'pointer',
					'top':((opts.panel_height-42)/2)+'px',
					'left':'10px',
					'display':'none'
				}).click(showPrevItemSet);


				//Add navigation buttons
				$('<img />').addClass('nav-next').attr('src',img_path+opts.nav_theme+'/next.gif').appendTo(j_gallery).css({
					'position':'absolute',
					'zIndex':'1100',
					'cursor':'pointer',
					'top':((opts.panel_height-22)/2)+'px',
					'right':'10px',
					'display':'none'
				}).click(showNextItem);
				$('<img />').addClass('nav-prev').attr('src',img_path+opts.nav_theme+'/prev.gif').appendTo(j_gallery).css({
					'position':'absolute',
					'zIndex':'1100',
					'cursor':'pointer',
					'top':((opts.panel_height-22)/2)+'px',
					'left':'10px',
					'display':'none'
				}).click(showPrevItem);
				
				$('<img />').addClass('nav-overlay').attr('src',img_path+opts.nav_theme+'/panel-nav-next.gif').appendTo(j_gallery).css({
					'position':'absolute',
					'zIndex':'1099',
					'top':((opts.panel_height-22)/2)-10+'px',
					'right':'0',
					'display':'none'
				});
				
				$('<img />').addClass('nav-overlay').attr('src',img_path+opts.nav_theme+'/panel-nav-prev.gif').appendTo(j_gallery).css({
					'position':'absolute',
					'zIndex':'1099',
					'top':((opts.panel_height-22)/2)-10+'px',
					'left':'0',
					'display':'none'
				});
				
			}
			j_panels.css({
				'width':(opts.panel_width-parseInt(j_panels.css('paddingLeft').split('px')[0],10)-parseInt(j_panels.css('paddingRight').split('px')[0],10))+'px',
				'height':(opts.panel_height-parseInt(j_panels.css('paddingTop').split('px')[0],10)-parseInt(j_panels.css('paddingBottom').split('px')[0],10))+'px',
				'position':'absolute',
				'top':(opts.filmstrip_position=='top'?(opts.frame_height+frame_margin_top+(opts.show_captions?frame_caption_size:frame_margin_top))+'px':'0px'),
				'left':'0px',
				'overflow':'hidden',
				'background':opts.panel_background_color,
				'textAlign':'center',
				'border':'solid 0px purple',
				'display':'none'
			});
			$('.panel-overlay',j_panels).css({
				'position':'absolute',
				'zIndex':'999',
				'width':(opts.panel_width-20)+'px',
				'height':opts.overlay_height+'px',
				'top':(opts.overlay_position=='top'?'0':opts.panel_height-opts.overlay_height+'px'),
				'left':'0',
				'padding':'10px 10px',
				'color':opts.overlay_text_color,
				'textAlign':'left',
				'fontSize':opts.overlay_font_size
			});
			$('.panel-overlay a',j_panels).css({
				'color':opts.overlay_text_color,
				'textDecoration':'underline',
				'fontWeight':'bold'
			});
			$('.overlay',j_panels).css({
				'position':'absolute',
				'zIndex':'998',
				'width':opts.panel_width+'px',
				'height':opts.overlay_height+'px',
				'top':(opts.overlay_position=='top'?'0':opts.panel_height-opts.overlay_height+'px'),
				'left':'0',
				'background':opts.overlay_color,
				'opacity':opts.overlay_opacity
			});
			$('.panel iframe',j_panels).css({
				'width':opts.panel_width+'px',
				'height':(opts.panel_height-opts.overlay_height)+'px',
				'border':'0'
			});
		};
		
		function buildFilmstrip() {
			//Add wrapper to filmstrip to hide extra frames
			j_filmstrip.wrap('<div class="strip_wrapper"></div>');

			if(slide_method=='strip') {
				j_frames.clone().appendTo(j_filmstrip);
				j_frames.clone().appendTo(j_filmstrip);
				j_frames = $('li',j_filmstrip);
			}
			//If captions are enabled, add caption divs and fill with the image titles
			if(opts.show_captions) {
				j_frames.append('<div class="caption"></div>').each(function(i){
					$(this).find('.caption').html($(this).find('img').attr('title'));			   
				});
			}
			
			j_filmstrip.css({
				'listStyle':'none',
				'margin':'0',
				'padding':'0',
				'width':strip_width+'px',
				'position':'absolute',
				'zIndex':'900',
				'top':'0',
				'left':'0',
				'border':'solid 0px pink',
				'height':(opts.frame_height+10)+'px',
				'background':opts.background_color
			});
			j_frames.css({
				'float':'left',
				'position':'relative',
				'height':opts.frame_height+'px',
				'width':opts.frame_width+'px',
				'overflow':'hidden',
				'zIndex':'901',
				'marginTop':frame_margin_top+'px',
				'marginBottom':'0px',
				'marginRight':frame_margin+'px',
				'padding':'0',
				'textAlign':'center',
				'cursor':'pointer',
				'border':'solid 0px pink',
				'background':opts.frame_background_color
			});
			$('img',j_frames).css({
				'border':'none'
			});
			$('.strip_wrapper',j_gallery).css({
				'padding':'0',
				'position':'absolute',
				'top':(opts.filmstrip_position=='top'?'0px':opts.panel_height+'px'),
				'left':((gallery_width-wrapper_width)/2)+'px',
				'width':wrapper_width+'px',
				'height':(opts.frame_height+frame_margin_top+(opts.show_captions?frame_caption_size:frame_margin_top))+'px',
				'overflow':'hidden'
			});
			$('.caption',j_gallery).css({
				'position':'absolute',
				'top':opts.frame_height+'px',
				'left':'0',
				'margin':'0',
				'width':opts.frame_width+'px',
				'padding':'0',
				'color':opts.caption_text_color,
				'textAlign':'center',
				'fontSize':'10px',
				'height':frame_caption_size+'px',
				
				'lineHeight':frame_caption_size+'px'
			});
			var pointer = $('<div></div>');
			
			pointer.attr('id','pointer').appendTo(j_gallery).css({
				 'position':'absolute',
				 'zIndex':'1000',
				 'cursor':'pointer',
				 'top':getPos(j_frames[0]).top-(pointer_width/2)+'px',
				 'left':getPos(j_frames[0]).left-(pointer_width/2)+'px',
				 'height':opts.frame_height-1+'px',
				 'width':opts.frame_width-1+'px',
				 'border':(has_panels?pointer_width+'px solid '+(opts.nav_theme=='dark'?'black':'white'):'none')
			});
			
			//
			j_pointer = $('#pointer',j_gallery);
			if(has_panels) {
				var pointerArrow = $('<img />');
				pointerArrow.attr('src',img_path+opts.nav_theme+'/pointer'+(opts.filmstrip_position=='top'?'-down':'')+'.gif').appendTo($('#pointer')).css({
					'position':'absolute',
					'zIndex':'1001',
					'top':(opts.filmstrip_position=='bottom'?'-'+(9+pointer_width)+'px':opts.frame_height+'px'),
					'left':((opts.frame_width/2)-10)+'px'
				});
			}
			
			//If the filmstrip is animating, move the strip to the middle third
			if(slide_method=='strip') {
				j_filmstrip.css('left','-'+((opts.frame_width+frame_margin)*item_count)+'px');
				iterator = item_count;
			}
			//If there's a link under the pointer, enable clicking on the pointer
			if($('a',j_frames[iterator])[0]) {
				j_pointer.click(function(){
					var a = $('a',j_frames[iterator]).eq(0);
					if(a.attr('target')=='_blank') {window.open(a.attr('href'));}
					else {location.href = a.attr('href');}
				});
			}


			//nicholas: 20100607:
			//Add navigation buttons - Info Overlay
			$('<img title="Next 5" alt="Next 5" />').addClass('nav-forward').attr('src',img_path+opts.nav_theme+'/forward.gif').appendTo(j_gallery).css({
				'position':'absolute',
				'cursor':'pointer',
				'width':'16px',
				'height':'16px',
				'zIndex':'3000',
				'top':'480px',
				'padding':'1px 0px 0px 0px',
				'left':'630px'
			}).click(showNextItemSet).mouseover(slideshow_mouseover_NextItemSet).mouseout(slideshow_mouseout_NextItemSet);
			$('<img title="Previous 5" alt="Previous 5" />').addClass('nav-backward').attr('src',img_path+opts.nav_theme+'/backward.gif').appendTo(j_gallery).css({
				'position':'absolute',
				'cursor':'pointer',
				'width':'16px',
				'height':'16px',
				'zIndex':'3000',
				'top':'480px',
				'padding':'1px 0px 0px 0px',
				'left':'10px'
			}).click(showPrevItemSet).mouseover(slideshow_mouseover_PrevItemSet).mouseout(slideshow_mouseout_PrevItemSet);

			
			//Add navigation buttons
			$('<img title="Next" alt="Next" />').addClass('nav-next').attr('src',img_path+opts.nav_theme+'/next.gif').appendTo(j_gallery).css({
				'display':'none',
				'position':'absolute',
				'cursor':'pointer',
				'width':'14px',
				'height':'22px',
				'top':(opts.filmstrip_position=='top'?0:opts.panel_height)+frame_margin_top+((opts.frame_height-22)/2)+'px',
				'right':(gallery_width/2)-(wrapper_width/2)-10-15+'px'
			}).click(showNextItem).mouseover(slideshow_mouseover_NextItem);
			$('<img title="Previous" alt="Previous" />').addClass('nav-prev').attr('src',img_path+opts.nav_theme+'/prev.gif').appendTo(j_gallery).css({
				'display':'none',
				'position':'absolute',
				'cursor':'pointer',
				'width':'14px',
				'height':'22px',
				'top':(opts.filmstrip_position=='top'?0:opts.panel_height)+frame_margin_top+((opts.frame_height-22)/2)+'px',
				'left':(gallery_width/2)-(wrapper_width/2)-10-17+'px'
			}).click(showPrevItem).mouseover(slideshow_mouseover_PrevItem);

			//nicholas: 20100609: nav arrows hiding
			if(item_count<=5){
				$('.nav-backward').css({
					'display':'none'
				});
				$('.nav-forward').css({
					'display':'none'
				});
				$('.nav-prev').css({
					'display':'none'
				});
				$('.nav-next').css({
					'display':'none'
				});
			}


			//20100806: nicholas: moving img into a table cell for vertical alignment
			$(j_frames).each(function(i){
				//alert(this.innerHTML);
				//
				//find the target image
				var myImg = $('img', this).eq(0);
				//alert($(myImg).attr('src'));
				//create table with image
				$('<table cellspacing="0" cellpadding="0" border="0" style="' +
						'position: relative; top: 0; left: 0; text-align: center;' +
						'border: solid 0px red; padding: 0px; margin: 0px;' + 
						'width: ' + opts.frame_width + 'px;' +
						'height: ' + opts.frame_height + 'px;' +
						'"><tr><td><img border="0" src="' + myImg.attr('src') + '" /></td></tr></table>'
					).insertBefore(myImg);
				//hide original image
				$(myImg).css({'display':'none'});
			});

		};
		
		//Check mouse to see if it is within the borders of the panel
		//More reliable than 'mouseover' event when elements overlay the panel
		function mouseIsOverPanels(x,y) {		
			var pos = getPos(j_gallery[0]);
			var top = pos.top;
			var left = pos.left;
			
			return x > left && x < left+opts.panel_width && y > top && y < top+opts.panel_height
				//nicholas:20100609: extended some +height to make mouse over region lower
				+10;
		};
		
/************************************************/
/*	Main Plugin Code							*/
/************************************************/
		return this.each(function() {
			j_gallery = $(this);
			//Determine path between current page and filmstrip 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/)){
					img_path = s.attr('src').split('jquery.galleryview')[0]+'themes/';	
				}
			});
			
			//Hide gallery to prevent Flash of Unstyled Content (FoUC) in IE
			j_gallery.css('visibility','hidden');
			
			//Assign elements to variables for reuse
			j_filmstrip = $('.filmstrip',j_gallery);
			j_frames = $('li',j_filmstrip);
			j_panels = $('.panel',j_gallery);
			
			id = j_gallery.attr('id');
			
			has_panels = j_panels.length > 0;
			has_filmstrip = j_frames.length > 0;
			
			if(!has_panels) opts.panel_height = 0;
			
			//Number of frames in filmstrip
			item_count = has_panels?j_panels.length:j_frames.length;
			
			//Number of frames that can display within the screen's width
			//64 = width of block for navigation button * 2
			//5 = minimum frame margin
			strip_size = has_panels?Math.floor((opts.panel_width-64)/(opts.frame_width+frame_margin)):Math.min(item_count,opts.filmstrip_size); 
			
			
			/************************************************/
			/*	Determine transition method for filmstrip	*/
			/************************************************/
					//If more items than strip size, slide filmstrip
					//Otherwise, slide pointer
					if(strip_size >= item_count) {
						slide_method = 'pointer';
						strip_size = item_count;
					}
					else {slide_method = 'strip';}
			
			
			/************************************************/
			/*	Determine dimensions of various elements	*/
			/************************************************/
					
					//Width of gallery block
					gallery_width = has_panels?opts.panel_width:(strip_size*(opts.frame_width+frame_margin))-frame_margin+64;
					
					//Height of gallery block = screen + filmstrip + captions (optional)
					gallery_height = (has_panels?opts.panel_height:0)+(has_filmstrip?opts.frame_height+frame_margin_top+(opts.show_captions?frame_caption_size:frame_margin_top):0);
					
					//Width of filmstrip
					if(slide_method == 'pointer') {strip_width = (opts.frame_width*item_count)+(frame_margin*(item_count));}
					else {strip_width = (opts.frame_width*item_count*3)+(frame_margin*(item_count*3));}
					
					//Width of filmstrip wrapper (to hide overflow)
					wrapper_width = ((strip_size*opts.frame_width)+((strip_size-1)*frame_margin));
			
			/************************************************/
			/*	Apply CSS Styles							*/
			/************************************************/
					j_gallery.css({
						'position':'relative',
						'border':opts.border,
						'margin':'0',
						'background':opts.background_color,
						'width':gallery_width+'px',
						'height':gallery_height+'px'
					});

					//20100618: nicholas: customized border
					//left
					$('<div></div>').appendTo(j_gallery).css({
						'font-size':'0px',
						'position':'absolute',
						'top':'0px',
						'left':'-5px',
						'width': '0px',
						'height': gallery_height+'px',
						'border-left-width':'5px',
						'border-color':'#d9d9d9',
						'border-style':'solid'
					})
					//right
					$('<div></div>').appendTo(j_gallery).css({
						'font-size':'0px',
						'position':'absolute',
						'top':'0px',
						'left':gallery_width+'px',
						'width': '0px',
						'height': gallery_height+'px',
						'border-left-width':'5px',
						'border-color':'#d9d9d9',
						'border-style':'solid'
					})
					//top
					$('<div></div>').appendTo(j_gallery).css({
						'font-size':'0px',
						'position':'absolute',
						'top':'-5px',
						'left':'-5px',
						'width': (gallery_width+10)+'px',
						'height': '0px',
						'border-top-width':'5px',
						'border-color':'#d9d9d9',
						'border-style':'solid'
					})
					//bottom
					$('<div></div>').appendTo(j_gallery).css({
						'line-height':'0px',
						'font-size':'0px',
						'position':'absolute',
						'top':(gallery_height)+'px',
						'left':'-5px',
						'width': (gallery_width+10)+'px',
						'height': '0px',
						'border-bottom-width':'5px',
						'border-color':'#d9d9d9',
						'border-style':'solid'
					})
			
			/************************************************/
			/*	Build filmstrip and/or panels				*/
			/************************************************/
					if(has_filmstrip) {
						buildFilmstrip();
					}
					if(has_panels) {
						buildPanels();
					}

			
			/************************************************/
			/*	Add events to various elements				*/
			/************************************************/
					if(has_filmstrip) enableFrameClicking();
					
						
						
						$().mousemove(function(e){							
							if(mouseIsOverPanels(e.pageX,e.pageY)) {
								if(opts.pause_on_hover) {
									$(document).oneTime(100,"animation_pause",function(){
										$(document).stopTime("transition");
										paused=true;
										
										//nicholas: 20100609: pause
										slideshow_pause();
										
									});
								}
								if(has_panels && !has_filmstrip) {
									$('.nav-overlay').fadeIn('fast');
									$('.nav-next').fadeIn('fast');
									$('.nav-prev').fadeIn('fast');
								}
							} else {
								if(opts.pause_on_hover) {
									$(document).stopTime("animation_pause");
									if(paused) {
										$(document).everyTime(opts.transition_interval,"transition",function(){
											showNextItem();
										});
										paused = false;

										//nicholas: 20100609: play
										slideshow_play();
									}
								}
								if(has_panels && !has_filmstrip) {
									$('.nav-overlay').fadeOut('fast');
									$('.nav-next').fadeOut('fast');
									$('.nav-prev').fadeOut('fast');
								}
							}
						});
			
			
			/************************************************/
			/*	Initiate Automated Animation				*/
			/************************************************/
					//Show the first panel
					j_panels.eq(0).show();

					//If we have more than one item, begin automated transitions
					if(item_count > 1) {
						$(document).everyTime(opts.transition_interval,"transition",function(){
							showNextItem();
						});
					}
					
					//Make gallery visible now that work is complete
					j_gallery.css('visibility','visible');
		});
	};
	
	$.fn.galleryView.defaults = {
		panel_width: 656,
		panel_height: 435,
		frame_width: 114,
		frame_height: 76,
		panel_background_color: '#000000',
		frame_background_color: '#000000', // Thumbnail background color
		filmstrip_size: 3,
		overlay_height: 90,
		overlay_font_size: '1em',
		transition_speed: 500,
		transition_interval: 5000,
		overlay_opacity: 0.6,
		overlay_color: '#6d6d6d',
		background_color: '#6d6d6d', // Frame background color
		overlay_text_color: 'white',
		caption_text_color: 'white',
		border: '0px solid #d9d9d9',
		nav_theme: 'light',
		easing: 'swing',
		filmstrip_position: 'bottom',
		overlay_position: 'bottom',
		show_captions: false,
		fade_panels: true,
		pause_on_hover: false
	};
})(jQuery);
