/* ** ** GalleryView - jQuery Content Gallery Plugin ** Author: Jack Anderson ** Version: 2.1 (March 14, 2010) ** ** Please use this development script if you intend to make changes to the ** plugin code. For production sites, please use jquery.galleryview-2.1-pack.js. ** ** See README.txt for instructions on how to markup your HTML ** ** 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); var id; var iterator = 0; // INT - Currently visible panel/frame var item_count = 0; // INT - Total number of panels/frames var slide_method; // STRING - indicator to slide entire filmstrip or just the pointer ('strip','pointer') var theme_path; // STRING - relative path to theme directory var paused = false; // BOOLEAN - flag to indicate whether automated transitions are active // Element dimensions var gallery_width; var gallery_height; var pointer_height; var pointer_width; var strip_width; var strip_height; var wrapper_width; var f_frame_width; var f_frame_height; var frame_caption_size = 25; var gallery_padding; var filmstrip_margin; var filmstrip_orientation; // Arrays used to scale frames and panels var frame_img_scale = {}; var panel_img_scale = {}; var img_h = {}; var img_w = {}; // Flag indicating whether to scale panel images var scale_panel_images = true; var panel_nav_displayed = false; // Define jQuery objects for reuse var j_gallery; var j_filmstrip; var j_frames; var j_frame_img_wrappers; var j_panels; var j_pointer; /* ** Plugin Functions */ /* ** showItem(int) ** Transition from current frame to frame i (1-based index) */ function showItem(i) { // Disable next/prev buttons until transition is complete // This prevents overlapping of animations $('.nav-next-overlay',j_gallery).unbind('click'); $('.nav-prev-overlay',j_gallery).unbind('click'); $('.nav-next',j_gallery).unbind('click'); $('.nav-prev',j_gallery).unbind('click'); j_frames.unbind('click'); if(opts.show_filmstrip) { // Fade out all frames j_frames.removeClass('current').find('img').stop().animate({ 'opacity':opts.frame_opacity },opts.transition_speed); // Fade in target frame j_frames.eq(i).addClass('current').find('img').stop().animate({ 'opacity':1.0 },opts.transition_speed); } //If necessary, fade out all panels while fading in target panel if(opts.show_panels && opts.fade_panels) { j_panels.fadeOut(opts.transition_speed).eq(i%item_count).fadeIn(opts.transition_speed,function(){ //If no filmstrip exists, re-bind click events to navigation buttons if(!opts.show_filmstrip) { $('.nav-prev-overlay',j_gallery).click(showPrevItem); $('.nav-next-overlay',j_gallery).click(showNextItem); $('.nav-prev',j_gallery).click(showPrevItem); $('.nav-next',j_gallery).click(showNextItem); } }); } // If gallery has a filmstrip, handle animation of frames if(opts.show_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(); var distance; var diststr; if(filmstrip_orientation=='horizontal') { // Determine distance between pointer (eventual destination) and target frame distance = getPos(j_frames[i]).left - (getPos(j_pointer[0]).left+(pointer_width/2)-(f_frame_width/2)); diststr = (distance>=0?'-=':'+=')+Math.abs(distance)+'px'; // Animate filmstrip and slide target frame under pointer j_filmstrip.animate({ 'left':diststr },opts.transition_speed,opts.easing,function(){ var old_i = i; // After transition is complete, shift filmstrip so that a sufficient number of frames // remain on either side of the visible filmstrip if(i>item_count) { i = i%item_count; iterator = i; j_filmstrip.css('left','-'+((f_frame_width+opts.frame_gap)*i)+'px'); } else if (i<=(item_count-strip_size)) { i = (i%item_count)+item_count; iterator = i; j_filmstrip.css('left','-'+((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) { j_frames.eq(old_i).removeClass('current').find('img').css({ 'opacity':opts.frame_opacity }); j_frames.eq(i).addClass('current').find('img').css({ 'opacity':1.0 }); } // If panels are not set to fade in/out, simply hide all panels and show the target panel if(!opts.fade_panels) { j_panels.hide().eq(i%item_count).show(); } // Once animation is complete, re-bind click events to navigation buttons $('.nav-prev-overlay',j_gallery).click(showPrevItem); $('.nav-next-overlay',j_gallery).click(showNextItem); $('.nav-prev',j_gallery).click(showPrevItem); $('.nav-next',j_gallery).click(showNextItem); enableFrameClicking(); }); } else { // filmstrip_orientation == 'vertical' //Determine distance between pointer (eventual destination) and target frame distance = getPos(j_frames[i]).top - (getPos(j_pointer[0]).top+(pointer_height)-(f_frame_height/2)); diststr = (distance>=0?'-=':'+=')+Math.abs(distance)+'px'; // Animate filmstrip and slide target frame under pointer j_filmstrip.animate({ 'top':diststr },opts.transition_speed,opts.easing,function(){ // After transition is complete, shift filmstrip so that a sufficient number of frames // remain on either side of the visible filmstrip var old_i = i; if(i>item_count) { i = i%item_count; iterator = i; j_filmstrip.css('top','-'+((f_frame_height+opts.frame_gap)*i)+'px'); } else if (i<=(item_count-strip_size)) { i = (i%item_count)+item_count; iterator = i; j_filmstrip.css('top','-'+((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) { j_frames.eq(old_i).removeClass('current').find('img').css({ 'opacity':opts.frame_opacity }); j_frames.eq(i).addClass('current').find('img').css({ 'opacity':1.0 }); } // If panels are not set to fade in/out, simply hide all panels and show the target panel if(!opts.fade_panels) { j_panels.hide().eq(i%item_count).show(); } // Once animation is complete, re-bind click events to navigation buttons $('.nav-prev-overlay',j_gallery).click(showPrevItem); $('.nav-next-overlay',j_gallery).click(showNextItem); $('.nav-prev',j_gallery).click(showPrevItem); $('.nav-next',j_gallery).click(showNextItem); enableFrameClicking(); }); } } else if(slide_method=='pointer') { // Stop pointer if it's currently in motion j_pointer.stop(); // Get screen position of target frame var pos = getPos(j_frames[i]); if(filmstrip_orientation=='horizontal') { // Slide the pointer over the target frame j_pointer.animate({ 'left':(pos.left+(f_frame_width/2)-(pointer_width/2)+'px') },opts.transition_speed,opts.easing,function(){ if(!opts.fade_panels) { j_panels.hide().eq(i%item_count).show(); } $('.nav-prev-overlay',j_gallery).click(showPrevItem); $('.nav-next-overlay',j_gallery).click(showNextItem); $('.nav-prev',j_gallery).click(showPrevItem); $('.nav-next',j_gallery).click(showNextItem); enableFrameClicking(); }); } else { // Slide the pointer over the target frame j_pointer.animate({ 'top':(pos.top+(f_frame_height/2)-(pointer_height)+'px') },opts.transition_speed,opts.easing,function(){ if(!opts.fade_panels) { j_panels.hide().eq(i%item_count).show(); } $('.nav-prev-overlay',j_gallery).click(showPrevItem); $('.nav-next-overlay',j_gallery).click(showNextItem); $('.nav-prev',j_gallery).click(showPrevItem); $('.nav-next',j_gallery).click(showNextItem); enableFrameClicking(); }); } } } }; /* ** extraWidth(jQuery element) ** Return the combined width of the border and padding to the elements left and right. ** If the border is non-numerical, assume zero (not ideal, will fix later) ** RETURNS - int */ 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; }; /* ** extraHeight(jQuery element) ** Return the combined height of the border and padding above and below the element ** If the border is non-numerical, assume zero (not ideal, will fix later) ** RETURN - int */ 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; }; /* ** showNextItem() ** Transition from current frame to next frame */ function showNextItem() { // Cancel any transition timers until we have completed this function $(document).stopTime("transition"); if(++iterator==j_frames.length) {iterator=0;} // We've already written the code to transition to an arbitrary panel/frame, so use it showItem(iterator); // If automated transitions haven't been cancelled by an option or paused on hover, re-enable them if(!paused) { $(document).everyTime(opts.transition_interval,"transition",function(){ showNextItem(); }); } }; /* ** showPrevItem() ** Transition from current frame to previous frame */ function showPrevItem() { // Cancel any transition timers until we have completed this function $(document).stopTime("transition"); if(--iterator<0) {iterator = item_count-1;} // We've already written the code to transition to an arbitrary panel/frame, so use it showItem(iterator); // If automated transitions haven't been cancelled by an option or paused on hover, re-enable them if(!paused) { $(document).everyTime(opts.transition_interval,"transition",function(){ showNextItem(); }); } }; /* ** getPos(jQuery element ** Calculate position of an element relative to top/left corner of gallery ** If the gallery bounding box itself is passed to the function, calculate position of gallery relative to top/left corner of browser window ** RETURNS - JSON {left: int, top: int} */ 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}; } }; /* ** enableFrameClicking() ** Add an onclick event handler to each frame ** Exception: if a frame has an anchor tag, do not add an onlick handler */ function enableFrameClicking() { j_frames.each(function(i){ if($('a',this).length==0) { $(this).click(function(){ // Prevent transitioning to the current frame (unnecessary) if(iterator!=i) { $(document).stopTime("transition"); showItem(i); iterator = i; if(!paused) { $(document).everyTime(opts.transition_interval,"transition",function(){ showNextItem(); }); } } }); } }); }; /* ** buildPanels() ** Construct gallery panels from
elements ** NOTE - These DIVs are generated automatically from the content of the UL passed to the plugin */ function buildPanels() { // If panel overlay content exists, add the necessary overlay background DIV // The overlay content and background are separate elements so the background's opacity isn't inherited by the content j_panels.each(function(i){ if($('.panel-overlay',this).length>0) { $(this).append('
'); } }); // If there is no filmstrip in this gallery, add navigation buttons to the panel itself /*if(!opts.show_filmstrip) { $('').addClass('nav-next').attr('src',theme_path+opts.nav_theme+'/next.gif').appendTo(j_gallery).css({ 'position':'absolute', 'zIndex':'1100', 'cursor':'pointer', 'top':((opts.panel_height-22)/2)+gallery_padding+'px', 'right':'10px', 'display':'none' }).click(showNextItem); $('').addClass('nav-prev').attr('src',theme_path+opts.nav_theme+'/prev.gif').appendTo(j_gallery).css({ 'position':'absolute', 'zIndex':'1100', 'cursor':'pointer', 'top':((opts.panel_height-22)/2)+gallery_padding+'px', 'left':'10px', 'display':'none' }).click(showPrevItem); $('').addClass('nav-next-overlay').attr('src',theme_path+opts.nav_theme+'/panel-nav-next.gif').appendTo(j_gallery).css({ 'position':'absolute', 'zIndex':'1099', 'top':((opts.panel_height-22)/2)+gallery_padding-10+'px', 'right':'0', 'display':'none', 'cursor':'pointer', 'opacity':0.75 }).click(showNextItem); $('').addClass('nav-prev-overlay').attr('src',theme_path+opts.nav_theme+'/panel-nav-prev.gif').appendTo(j_gallery).css({ 'position':'absolute', 'zIndex':'1099', 'top':((opts.panel_height-22)/2)+gallery_padding-10+'px', 'left':'0', 'display':'none', 'cursor':'pointer', 'opacity':0.75 }).click(showPrevItem); }*/ // Set the height and width of each panel, and position it appropriately within the gallery j_panels.each(function(i){ $(this).css({ 'width':(opts.panel_width-extraWidth(j_panels))+'px', 'height':(opts.panel_height-extraHeight(j_panels))+'px', 'position':'absolute', 'overflow':'hidden', 'display':'none' }); switch(opts.filmstrip_position) { case 'top': $(this).css({ 'top':strip_height+Math.max(gallery_padding,filmstrip_margin)+'px', 'left':gallery_padding+'px' }); break; case 'left': $(this).css({ 'top':gallery_padding+'px', 'left':strip_width+Math.max(gallery_padding,filmstrip_margin)+'px' }); break; default: $(this).css({'top':gallery_padding+'px','left':gallery_padding+'px'}); break; } }); // Position each panel overlay within panel $('.panel-overlay',j_panels).css({ 'position':'absolute', 'cursor':'pointer', 'zIndex':'999', 'width':(opts.panel_width-extraWidth($('.panel-overlay',j_panels)))+'px', 'left':'0' }); $('.overlay-background',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',j_panels).css('top',0); $('.overlay-background',j_panels).css('top',0); } else { $('.panel-overlay',j_panels).css('bottom',0); $('.overlay-background',j_panels).css('bottom',0); } $('.panel iframe',j_panels).css({ 'width':opts.panel_width+'px', 'height':opts.panel_height+'px', 'border':'0' }); // If panel images have to be scaled to fit within frame, do so and position them accordingly if(scale_panel_images) { $('img',j_panels).each(function(i){ $(this).css({ 'height':panel_img_scale[i%item_count]*img_h[i%item_count], 'width':panel_img_scale[i%item_count]*img_w[i%item_count], 'position':'relative', 'top':(opts.panel_height-(panel_img_scale[i%item_count]*img_h[i%item_count]))/2+'px', 'left':(opts.panel_width-(panel_img_scale[i%item_count]*img_w[i%item_count]))/2+'px' }); }); } }; /* ** buildFilmstrip() ** Construct filmstrip from