/*
 *	jQuery vgallery 1.0.0 - Another jQuery images gallery (with a vertical scrolling navigation)
 * 
 *	Requires : jquery-1.3.2.min.js
 *
 *	Params
 *	--------------------
 *	defaultIndex : Choose which slide to start on
 *	auto : True to run the auto widget, false to disable it
 *	autoTimeout : Auto scroll duration (ms : less is faster). Must be superior to the panelFadeSpeed.
 *	panelFadeSpeed : Fade effect duration (ms)
 *	visible : How much item to show at the same time
 *
 */

(function(jQuery) {
	// jQuery plugin
	jQuery.fn.vgallery = function(params) {

		// Default params
        var defaults = {
			defaultIndex : 0,
			auto : true,
			autoTimeout : 6000,
            panelFadeSpeed: 500,
			visible : 5
			//scroll : 1 // not implemented
        };
		
		// Merge with user params
		settings = jQuery.extend(defaults, params);
		
		return this.each(function(){
			var widget = jQuery(this);
			widget.css("display","block"); //by default, the widget should be hidden via CSS
			
            var panelClassName = "widget-vgallery-panel";
			var selectedClassName = "widget-vgallery-selected";
			var activeClassName = "widget-vgallery-panel-active";
			var oldClassName = "widget-vgallery-panel-old";
			var navClassName = "widget-vgallery-nav";
			var widgetPanels = widget.find(".widget-vgallery-panels>div");	
			var panelCurrentId = 0;
			var panelIndex = 0;
			var currentPanel = "";
			var autoPlay = null;
			var firstTime = false;
			var widgetNavClip = widget.find("."+ navClassName);
			var widgetNav = widgetNavClip.find("ul");
			
			// Adding classes
			widgetPanels.addClass(panelClassName);
			
			// Starting
			if(widgetNav.size() > 0){
				// Nav height			
				var widgetNavItems = widgetNav.find("li");
				var widgetNavItemsSize = widgetNavItems.size();
				var widgetNavHeight = widgetNav.height();
				var widgetNavItemHeight = widgetNavHeight / widgetNavItemsSize;
				var widgetNavClipHeight = settings.visible * widgetNavItemHeight;
				widgetNavClip.css("height",widgetNavClipHeight);
				
				// Nav links handler
				widgetNavItems.mouseover(function(){

					// Stop autoScrolling if active
					if(autoPlay){
						stopAutoScroll();
					}
					
					// Get the current panel index
					panelIndex = jQuery("li", jQuery(this).parent()).index(this);
					currentPanel = panelFinder(panelIndex);
					
					// Switch item (only if hidden)
					if(!currentPanel.hasClass(activeClassName)){				
						widget.find("."+activeClassName).stop(true, true);
						panelSwitch(panelIndex, 0);
					}					
					else return false;					
				});
				
				// Buttons
				var widgetPrevBtn = widget.find(".widget-vgallery-prev a");
				var widgetNextBtn = widget.find(".widget-vgallery-next a");
				var btnActiveClassName = "widget-vgallery-btn-active";
				var btnDisableClassName = "widget-vgallery-btn-disable";
				
				widgetNextBtn.bind("click", function(){
					autoScrollHandler("-=");
					return false;
				});
				widgetPrevBtn.bind("click", function(){
					autoScrollHandler("+=");
					return false;
				});
				
				// Display first item 
				panelSwitch(settings.defaultIndex, true);
				
				// AutoScrolling starter
				if(settings.auto){			
					startAutoScroll();
					
					// Restart autoScrolling when leaving the nav
					widgetNav.mouseleave(function(){
						startAutoScroll();					
					});			
				
					// Stop autoScrolling when reading content
					widget.find(".widget-vgallery-panel").hover(
						function(){if(autoPlay) stopAutoScroll();},
						function(){startAutoScroll();}
					);
					widget.find(".widget-vgallery-btn").hover(
						function(){if(autoPlay) stopAutoScroll();},
						function(){startAutoScroll();}
					);
				}
				
			} else return false;
			
			// Select a panel
			function panelFinder(panelIndex){
				return widgetPanels.eq(panelIndex);
			};
						
			// Switches to the current panel
			function panelSwitch(forceId, firstTime){
				
				// Current panel index
				if(forceId > -1) panelCurrentId = forceId;
				currentPanel = panelFinder(panelCurrentId);
				currentPanel.css({"display":"none"});

				// Highlight selected menu
				widget.find("ul li."+selectedClassName+"").removeClass(selectedClassName);
				widget.find("ul li:eq("+(panelCurrentId)+")").addClass(selectedClassName);
				
				// Fade in new panel
				if(!firstTime){
					// Hiding previous panels
					widget.find("."+oldClassName).removeClass(oldClassName);							
					widget.find("."+activeClassName).removeClass(activeClassName).addClass(oldClassName);
					
					// Showing current (smooth effect)								
					currentPanel.addClass(activeClassName).fadeIn(settings.panelFadeSpeed);
				}
				else{
					currentPanel.addClass(activeClassName).css({"display":"block"});
				}
			};

			// Nav AutoScrolling handler
			function navScroll(scrollValue, scrollDirection){	
				widgetNav.animate({top: scrollDirection+scrollValue}, 1000);	
			};
			
			// Nav AutoScrolling checker
			function navScrollChecker(scrollDirection, curitemIndex){				
				var curitempos = curitemIndex+1;
				widgetNavPos = widgetNav.position();
				widgetNavPosTop = widgetNavPos.top;				
				itemsLeftDown = (widgetNavHeight - Math.abs(widgetNavPosTop) - widgetNavClipHeight) / widgetNavItemHeight;
				itemsLeftUp = Math.abs(widgetNavPosTop)/widgetNavItemHeight;				
				itemPositionFromTop = Math.abs(curitempos - itemsLeftUp);
				
				// Going up
				if(scrollDirection == "+="){
					itemLeft = itemsLeftUp;
					
					// Top is reached => Go to bottom
					if(widgetNavPosTop == 0 && curitemIndex == widgetNavItemsSize-1 && widgetNavItemsSize > settings.visible){				
						scrollUnits = Math.ceil(widgetNavItemsSize / settings.visible);
						scrollUnits--;
						navScroll(scrollUnits * (settings.visible*widgetNavItemHeight), "-=");
					}
					else if(itemPositionFromTop == 0){	
						if(itemLeft >= settings.visible) scrollValue = widgetNavItemHeight * settings.visible;
						else scrollValue = widgetNavItemHeight * itemLeft;
						navScroll(scrollValue, scrollDirection);
					}
				}
				
				// Going down
				if(scrollDirection == "-="){
					itemLeft = itemsLeftDown;
					
					// Bottom is reached => Return to top
					if(curitemIndex == 0){
						navScroll(0, "");
					}
					else if(itemPositionFromTop == settings.visible+1){	
						if(itemLeft >= settings.visible) scrollValue = widgetNavItemHeight * settings.visible;
						else scrollValue = widgetNavItemHeight * itemLeft;
						navScroll(scrollValue, scrollDirection);
					}
				}
				
			};
			/* Not implemented
			function swithButton(btnObject, switchOn){
				if(switchOn){
					btnObject.parent().removeClass(btnDisableClassName);
					btnObject.parent().addClass(btnActiveClassName);
				}
				else{
					btnObject.parent().removeClass(btnActiveClassName);
					btnObject.parent().addClass(btnDisableClassName);
				}
			}*/
			
			// AutoScrolling handler
			function autoScrollHandler(forceDirection){
				// Increment direction
				var scrollDirection = (forceDirection!="") ? forceDirection : "-=";
				
				// New id
				var panelIncrementId = (scrollDirection == "-=") ? panelCurrentId + 1 : panelCurrentId - 1;				
				panelCurrentId = (panelIncrementId == widgetNavItemsSize) ? 0 : (panelIncrementId == -1) ? widgetNavItemsSize-1 : panelIncrementId;				
	
				// Nav scroll handler
				navScrollChecker(scrollDirection, panelCurrentId);
				
				// Switch to the next panel
				panelSwitch(-1, 0);
			};			
			
			function autoScrollLauncher(){
				autoScrollHandler("");
			};
			
			// Start autoScrolling
			function startAutoScroll(){
				autoPlay = setInterval(autoScrollLauncher, settings.autoTimeout);
			};
			
			// Stop autoScrolling
			function stopAutoScroll(){
				clearTimeout(autoPlay);
				autoPlay = null;
			};			
		});
    };
})(jQuery);

/*
// Webkit engine bugfix
if (jQuery.browser.safari && document.readyState != "complete"){
	setTimeout(arguments.callee, 100);
	return;
}*/
