//alert("working");
(function( $ ){
	
	var version = 'beta';
	var pluginName = 'mnxtSlider';
	
	
	
	// if $.support is not defined (pre jQuery 1.3) add what I need
	if ($.support == undefined) {
		$.support = {
			opacity: !($.browser.msie)
		};
	}
	
	$.fn.mnxtSlider = function( method ){
		
		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.tooltip' );
	    }  
		
	};

	
	
	var methods = {
			    init: function( options ) {

			    	var $this = this;
			    	var opts = $.extend({}, $defs, options);
					this.data("opts", opts);
			    	
					var slides = this.children(opts.filterBy);
					
					if (slides.length < 2) {
						log('terminating; too few slides: ' + slides.length);
						return;
					}
					
					
					//Fila de seletores usando o plugin ao mesmo tempo, para controle de focus e blur da pagina
					if ($controllers.selectors == null){
						$controllers.selectors = new Array();
					}
					$controllers.selectors.push(this.selector);
					
					
					vars = this.data('vars');
					if (!vars){
						vars = this.data( "vars", { qtdSlides: slides.length, actualSlide: 0, timer: null, selector: this.selector });
					}
					vars = this.data('vars');
					
					
					if (opts.navSelector.length > 0){
						var cont = opts.navSelector;
						var ul = cont+" ul";
						var li = ul+" li";
						var link = li+" a";
						
						$(cont).html(navigatorHtml(vars.qtdSlides, vars.actualSlide));
						$w = ($(li).width()*1.2)*(vars.qtdSlides);
						$(ul).width($w);
						$(link).bind("click",function(){
							slideAt = $(this).attr("data-id");
							methods.navTo.apply( $this, [slideAt] );
							return false;
						});
						
					}
					
					window.onfocus = $.fn.mnxtSlider.onPageFocus;
					window.onblur = $.fn.mnxtSlider.onPageBlur;
					
					methods.start.apply(this);
					
			    },
			    
			    
			    start: function(){
			    	var vars = this.data("vars");
			    	var opts = this.data("opts");
			    	if (!opts.slideshow) return;
			    	
					vars.timer = setTimeout( function (){ 
						$(vars.selector).mnxtSlider('next');
						}, opts.interval
					);
				},
				
				
				stop: function(){
					this.data("vars").timer = clearTimeout(this.data("vars").timer);
				},
			    
			    
			    nav: function(dir){
					dir = (dir) ? 1 : -1;
					var vars = this.data("vars");
					vars.actualSlide += dir;
					bounds(vars);
					go.apply(this);
				},
			    
			    
			    
			    navTo: function (index){
			    	//alert("navTo: "+index);
			    	methods.stop.apply(this);
			    	var vars = this.data("vars");
					vars.actualSlide = parseInt(index);
					bounds(vars);
					go.apply(this);
				},
			    
			    
			    next: function(){
		    		methods.nav.apply( this, [1] );
				},
				
				
			    prev: function(){
		    		methods.nav.apply( this, [0] );
				}
	};
	
	
	

	$.fn.mnxtSlider.onPageFocus = function (){
		sel = $controllers.selectors;
		for (i=0; i<sel.length; i++){
			$(sel[i]).mnxtSlider('start');
		}
	};
	
	$.fn.mnxtSlider.onPageBlur = function (){
		sel = $controllers.selectors;
		for (i=0; i<sel.length; i++){
			$(sel[i]).mnxtSlider('stop');
		}
	};
	
	
	
	function bounds(vars){
		if (vars.actualSlide  < 0) {
			vars.actualSlide = vars.qtdSlides-1;
		} else if (vars.actualSlide == vars.qtdSlides) {
			vars.actualSlide = 0;
		}
		return vars.actualSlide;
	}
	
	
	
	
	function go(){
		//alert("go");
		var opts = this.data("opts");
		var vars = this.data("vars");
		var $this = this;
		
		methods.stop.apply(this);
		var actual = vars.actualSlide;
		
		var child = this.children(opts.filterBy).eq(actual);
		
		height = child.height();
		width = child.width();
		
		var scroll = opts.fixedScroll;
		
		var command = {};
		switch(opts.orientation){
			case "top":
				if (scroll == false)
					scroll = height;
				pos = opts.direction*(actual*(scroll));
				command = {"top": pos};
				break;
				
			case "left":
				if (scroll == false)
					scroll = width;
				pos = opts.direction*(actual*(scroll));
				command = {"left": pos};
				break;
				
			case "margin-top":
				if (scroll == false)
					scroll = height;
				pos = opts.direction*(actual*(scroll));
				command = {"margin-top": pos};
				break;
			
			case "margin-left":
				if (scroll == false)
					scroll = width;
				pos = opts.direction*(actual*(scroll));
				command = {"margin-left": pos};
				break;
		}
		
		
		
		this.animate(command, opts.speed, opts.easing, function(){});
		if (opts.navSelector.length > 0){
			$(opts.navSelector+" ul li.active").removeClass("active");
			$(opts.navSelector+" ul li").eq(actual).addClass("active");
		}
		
		methods.start.apply(this);

	}


	function navigatorHtml(qtd, actual) {
		var html = "<ul class=\"inline\">";
		var clss = "";
		var i;
		
		for( i = 0; i < qtd; i++){
			clss = (i == actual) ? "active" : "";
			html += "<li class=\""+clss+"\"><a href=\"#\" data-id=\""+i+"\"></a></li>";
		}
		html += "</ul>";
		return html;
	}	
	
	
	
	
	
	
	
	
	
	
	
	function debug(s) {
		$.fn.mnxtSlider.debug && log(s);
	}		
	function log() {
		window.console && console.log && console.log('['+pluginName+'] ' + Array.prototype.join.call(arguments,' '));
	}
	
	$.fn.mnxtSlider.version = function() { return version; };
	
	
	
	
	
	$.fn.mnxtSlider.controllers = {
		selectors: null //Array of already loaded selectors
	};
	
	$.fn.mnxtSlider.defaults = {
		interval		: 5000,
		maxSlides 		: 5,
		speed			: 800,
		easing			: 'easeInOutQuart',
		slideshow		: true,
		orientation		: 'top',
		direction		: -1,
		fixedScroll		: false,
		filterBy		: "li",
		navSelector		: ""
	};
	
	
	
	
	//SHORTCUTS
	var $fn = $.fn.mnxtSlider;
	var $controllers = $fn.controllers;
	var $defs = $fn.defaults;
	
})( jQuery );

