/* dotGallery.js
 * Version: 0.2
 * Stupid little plugin to practice writing plugins
 * Date: 7/8/11
 * Author: Jack Lenehan, jacklenehan.com
 */

;(function($){
	
	$.fn.dotGallery = function(options, method) {
	
		var $self=this,
			navcontroldiv,
			navlinks=$(),
			imgs=$(),
			tempvar,
			timer,
			settings = {
				"addlinks" 	: 	true,
				"delay"		:	4000
			},
			methods = {
		
				changeImg : function (i, link) {
					//console.log("changing to " + i);
					tempvar=$(imgs[i]);
					//console.log(" - fading all out")
					imgs.fadeOut("slow");
					//console.log(" - fading in " + i)
					tempvar.fadeIn("slow", function() {
						//console.log(" - fading in " + i + " complete")
						(i == imgs.length-1) ? i=0 : i++;
						if(settings.addlinks) {
							navlinks.removeClass("active");
							link.addClass("active");
							timer=setTimeout(function() {
								//console.log("fire!");
								methods.changeImg(i, navcontroldiv.find("a[data-section='" + i + "']"));
							}, settings.delay)
						} else {
							timer=setTimeout(function() {
								//console.log("fire!");
								methods.changeImg(i);
							}, settings.delay);
						}
					});
				
			},
				init : function () {
					return this.each(function() {
											
						if(options) {
							$.extend(settings, options)
						}
						
						var $this = $(this);
						var count=0;
						
						$this.find("img").each(function() {
							tempvar=$(this).css({
								display: "none",
								position: "absolute",
								"z-index": 1000-count
							}).addClass("dotgallery")
							.attr("data-section",count);
							imgs=imgs.add(tempvar);
							count++;
						});
											
						imgs.first().show();
						
						//console.log(imgs.length);
									
						
						if(settings.addlinks) {
							navcontroldiv = $("<div id='gallery_control'></div>").appendTo($this);
							
							for(var i=0; i <= imgs.length-1; i++) {
								navlinks=navlinks.add($("<a data-section='" + i + "'></a>"))
								navcontroldiv.append(navlinks[i]);
							}
							
							navlinks.first().addClass("active");
							
							navlinks.click(function() {
								if($(this).attr("data-section")!=$this.find("img:visible").attr("data-section")) {
									methods.changeImg($(this).attr("data-section"), $(this));
									clearTimeout(timer);
								}
							});
						}
						
						if(settings.addlinks) {
							timer=setTimeout(function() {
								//console.log("fire!");
								methods.changeImg(1, navcontroldiv.find("a[data-section='1']"));
							}, settings.delay)
						} else {
							timer=setTimeout(function() {
								//console.log("fire!");
								methods.changeImg(1);
							}, settings.delay);
						}
					});	
	
				}		
		}
		
		// Method calling logic
		if ( methods[method] ) {
			return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.dotGallery' );
		}
			
	}
		
})(jQuery);


