/**
* Continous slide gallery
* Author: James Marquez
* Email: jamesmarquez@gmail.com
* URL: http://www.jamesmarquez.com/
**/
(function($) {
 
	$.fn.quesksgallery = function(settings) {
		var defaults = {
			transition		:	'slide', 				// animation transition effects values are fade or slide
			transitionTime	:	0.6,					// the transition effects timer
			interval		:	9,						// animation in seconds
			slideOffset		:	0,						// width offsetting when doing slide transition
			navHolder		:	'',						// element of the gallery navigation for active state
			showArrow		: 	false
		};
		
		var opts = $.extend(defaults, settings);
		var $me = $(this);
				
		this.each(function() {		
						   
			var placeholder = '#'+$(this).attr('id');
			var li_items 	= $(placeholder + ' li').length;
			var li_width 	= $(placeholder + ' li:first').width();
			var play 		= new Array();
		
			opts.transitionTime = opts.transitionTime * 1000;
		
			switch(opts.transition) {
				case 'slide':						
					// adjust ul width
					$(placeholder + ' ul').width( (li_items+1) * parseInt(li_width) );		
					$(placeholder + ' li').css({
						display: 'inline',
						float: 'left',
						width: li_width
					});
					
					playSlide = function(placeholder){
						// set active state
						if ( $(placeholder + ' li.active').length==0 ) {
							$(placeholder + ' li:first').next().addClass('active');
						}
						
						var $next = $(placeholder + ' li.active').next();
						if ($next.length==0) {								
							// add temporary item
							$temp = $(placeholder + ' li:first').clone()
							$temp.addClass('li_temp');
							$(placeholder + ' ul').append($temp);
							$next = $(placeholder + ' li.li_temp');
						}														

						// fade out active state
						$(placeholder + ' ul').animate({
							marginLeft: -($(placeholder + ' li.active').index() * (parseInt(li_width)+opts.offset))
						}, opts.transitionTime, function(){
							if ($(this).children('li.active').hasClass('li_temp')) {
								$(placeholder + ' ul').css('margin-left', 0);
								$(placeholder + ' li.li_temp').remove();
								$(placeholder + ' li:first').next().addClass('active');
							} else {
								$(placeholder + ' li.active').removeClass('active');
								$next.addClass('active');
							}
						});
						
						if (opts.navHolder!='') {
							updateNav();
						}
					}
					
					break;
				default: // fade
					$(placeholder + ' li').css({
						left: 0,
						position: 'absolute',
						top: 0
					});	
										
					$(placeholder + ' li').each(function(i){	
						$(this).css('z-index', li_items - (i+1));	   
					});
					
					$(placeholder + ' li').css('opacity', 0);
					$(placeholder + ' li.active').css('opacity', 1);
					
					playSlide = function(placeholder){
						
						// set active state
						if ( $(placeholder + ' li.active').length==0 ) {
							$(placeholder + ' li:first').addClass('active');
						}
						
						var $next = $(placeholder + ' li.active').next();
						
						if ($next.length==0) {								
							$next = $(placeholder + ' li:first');
						}	

						// fade out active state
						$(placeholder + ' li.active').animate({opacity: 0}, opts.transitionTime, function(){
							$(placeholder + ' li.active').removeClass('active');	
							$next.addClass('active');
							
							// re-order stacks
							$(placeholder + ' li').each(function(i){	
								if ( $(this).hasClass('active')==false ) {
									$(this).css('z-index', li_items - (i+1));	
								} else {
									$(this).css('z-index', li_items);	
								}
							});
						});
						
						$next.animate({opacity: 1}, opts.transitionTime);
						
						if (opts.navHolder!='') {
							updateNav();
						}
					}
			}
			
			
			// show navigation arrow
			if (opts.showArrow) {
				$(placeholder).append('<a href="#" class="arrowNavLeft"><span class="hide">&lt;</span></a><a href="#" class="arrowNavRight"><span class="hide">&gt;</span></a>');			
			}
			
			// apply navigation active state
			updateNav = function() {
				$(opts.navHolder+' li').removeClass('active');
				if ($(placeholder + ' li.active').index()+1<li_items) {
					$(opts.navHolder+' li').eq($(placeholder + ' li.active').index()+1).addClass('active');	
				} else {
					$(opts.navHolder+' li:first').addClass('active');	
				}
			}
			
			// nav click
			$(opts.navHolder+' li a').live('click', function(){
				clearInterval(play[$me]);
				var index = $(this).parent().index();
				$(placeholder + ' li.active').removeClass('active');
				if (index>0) {
					$(placeholder + ' li').eq(index-1).addClass('active');
				} else {
					$(placeholder + ' li:last').addClass('active');
				}				
				$(placeholder + ' li').css('opacity', 0);
				playSlide(placeholder);
				play[$me] = setInterval(function(){ playSlide(placeholder); }, opts.interval*1000);	
			});
			
			
			
			// play slide on load			
			play[$me] = setInterval(function(){ playSlide(placeholder); }, opts.interval*1000);
			
			
			// pause animation 
			var switch_key = '';
			$(placeholder).hover(
				function(){
					clearInterval(play[$me]);
					$(placeholder + " .arrowNavLeft").show();
					$(placeholder + " .arrowNavRight").show();
				},
				function() {
					clearInterval(play[$me]);
					play[$me] = setInterval(function(){ playSlide(placeholder); }, opts.interval*1000);	
					$(placeholder + " .arrowNavLeft").hide();
					$(placeholder + " .arrowNavRight").hide();
				}
			);	
			
			
			// arrow click
			$(".arrowNavLeft").live('click', function(){
				clearInterval(play[$me]);
				var placeholder = '#' + $(this).parent().attr('id');
				var $active = $(placeholder + ' li.active');
				
				if ($active.prev().prev().length>0 ) {
					$active.prev().prev().addClass('active');	
				} else if ($active.prev().length>0 ) {
					$(placeholder + ' li:last').addClass('active');	
				} else {
					$(placeholder + ' li:last').prev().addClass('active');	
				}
				
				$active.removeClass('active');
				$(placeholder + ' li').css('opacity', 0);
				playSlide(placeholder);
				return false;										  
			});

			$(".arrowNavRight").live('click', function(){
				var placeholder = '#' + $(this).parent().attr('id');
				clearInterval(play[$me]);
				$(placeholder + ' li').css('opacity', 0);
				playSlide(placeholder);
				return false;										  
			});


		});
		
		return this;	
	};
 
 })(jQuery);
