/*
 * axSlide - jQuery Plugin
 *
 * $Date: 2010-04-20 (Tue, 20 Apr 2010) $
 * $version: 0.1
 *
 * Updated Slide Show Plugin for AX OpenForum
 * 
 * Usage:
 * Apply to any 'new style' slideshow gallery markup. Gallery container MUST HAVE UNIQUE ID!
 * 
 * $('#Gallery1').axSlide({
 * 		speed: INT - value for transition speed, default is 600 ms
 * 		auto: BOOL - true/false for use of autoplay, default is false
 * 		timer: INT - valuse for timer speed, default is 6000 ms (6.0 s)
 * });
 * 
 * Features:
 * 	- Control button injection
 * 	- Multiple galleries per page
 * 	- Adjustable transition speed
 * 	- Autoplay w/ adjustable timer
 * 	- Error reporting to browser console
 * 
 *
*/
(function($){
	jQuery.fn.axSlide = function(options)
		{
			var slideSettings = jQuery.extend({
				speed: 600,
				auto: false,
				timer: 6000
			}, options);
			
			return this.each(
				function(){
					if($(this).attr("id") && $(this).length == 1){				
						var parent = $(this),
						pid = $(this).attr("id"),
						images = $('#'+pid+' > ul').children(),
						timerId = 0;

						var Slide = {
							controls: [
							'<div class="pager '+pid+'">\n<ul>\n<li class="prev '+pid+'"><a href="#">Prev</a></li>\n<li class="next '+pid+'"><a href="#">Next</a></li>\n</ul>\n</div>\n'
							],
							init: function(){
								try{
									$('#'+pid+' > ul').addClass(pid); //changed from: $('#'+pid+' > ul').css({'width': 706+'px'}).addClass(pid);
									
									$(parent).append(Slide.controls[0]);
									var i = 1,
									tile;
									while(i <= images.length){
										if(i == 1){
											tile = '<li class="slide_'+i+' active"><a href="#slide_'+i+'">Slide '+i+'</a></li>';
										} else {
											tile = '<li class="slide_'+i+'"><a href="#slide_'+i+'">Slide '+i+'</a></li>';
										}

										$('#'+pid+' li.next').before(tile);
										i++;
									}
																				
									if(images.length >= 3){
										$('.pager.'+pid+' ul').css({'width': 22+(images.length * 18)+'px'});

										if(jQuery.browser.version == "6.0"){
											$('.pager.'+pid+' ul').css({'width': 26+(images.length * 18)+'px'});
										}
									}
																		
									$.each(images, function(i,image){
										$(image).addClass('slide_'+(i+1));
									});
									
									$('.viewer.'+pid+' li.active').css({'opacity': 1});
									$('.viewer.'+pid+' li:not(.active)').css({'opacity': 0});
									
									$('.pager.'+pid+' ul li > a').bind('click', Slide.next);
									
									if(slideSettings.auto === true){
										Slide.autoplay();
									}
									
								} catch(e){
									var msg = 'There was an error initializing the jquery.axSlide plugin.\n'+e;
									if(window.console && window.console.log){
										console.debug(msg);
									}
								}
							}, 
							next: function(){
								$(this).unbind('click').bind('click', function(){return false;});

								var object = $(this),
								dir = $(this).attr('href').split('#'),
	                            slideNum, next;

								if(dir[1]){
									next = $('.viewer.'+pid+' li.'+dir[1]);
									if(timerId !== 0){
										clearTimeout(timerId);
									}
								} else {
									if($(this).parent().hasClass('prev')) {
										if($('.viewer.'+pid+' li.active').is(':first-child')){
											next = $('.viewer.'+pid+' li:last-child');
										} else {
											next = $('.viewer.'+pid+' li.active').prev();
										}
									} else if($('.viewer.'+pid+' li.active').is(':last-child')) {
										next = $('.viewer.'+pid+' li:first-child');
									} else {
										next = $('.viewer.'+pid+' li.active').next();
									}	
								}
								
								$('.viewer.'+pid+' li.active').animate({'opacity': 0}, slideSettings.speed).removeClass('active');
								
								slideNum = $(next).attr('class');
								
								
								if(jQuery.browser.version == "6.0"){
									$('.pager.'+pid+' ul li.active').removeClass('active').css({backgroundPosition: '-13px 0px'});	
									$('.pager.'+pid+' ul li.'+slideNum).css({backgroundPosition: '-32px 0px'}).addClass('active');
								} else {
									$('.pager.'+pid+' ul li.active').removeClass('active');								
									$('.pager.'+pid+' ul li.'+slideNum).addClass('active');	
								}

								$(next).addClass('active').animate(
									{'opacity': 1}, 
									slideSettings.speed, 
									function(){
										$(this).unbind('click');
										$(object).bind('click', Slide.next);
									}
								);
								
								return false;
							},
							autoplay: function(){
								var func = function(){
									$('.pager.'+pid+' ul li.next a').trigger('click');
								};
								timerId = setInterval(func, slideSettings.timer);
							}
						};
						return Slide.init();
					} else {
						var msg = 'There was an error loading the jquery.axSlide plugin.';
						if(window.console && window.console.log){
							console.log(msg);
						}
					}
				});
		};
})(jQuery);
