//became fodaSlider
//Clocker 2010/03 Leonardo Beal

 
$(function(){
	// Remove the coda-slider-no-js class from the body
	$("body").removeClass("coda-slider-no-js");
	// Preloader
	$(".coda-slider").children('.item').hide().end().prepend('<p class="loading">Loading...<br /><img src="images/ajax-loader.gif" alt="loading..." /></p>');
});

var sliderCount = 1;

$.fn.phodaSlider = function(settings) {

	settings = $.extend({
		autoHeight: false,
		autoHeightEaseDuration: 1000,
		autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: false,
		autoSlideInterval: 10000,
		autoSlideStopWhenClicked: false,
		crossLinking: false,
		dynamicArrows: false,
		dynamicArrowLeftText: "&#171; left",
		dynamicArrowRightText: "right &#187;",
		dynamicTabs: true,
		dynamicTabsAlign: "center",
		dynamicTabsPosition: "bottom",
		externalTriggerSelector: "a.xtrig",
		firstPanelToLoad: 1,
		panelTitleSelector: ".imagem",
		slideEaseDuration: 500,
		slideEaseFunction: "easeInOutExpo"
	}, settings);
	
	return this.each(function(){
		
		// Uncomment the line below to test your preloader
		// alert("Testing preloader");
		
		var slider = $(this);
		
		/*
		// If we need arrows
		if (settings.dynamicArrows) {
			slider.parent().addClass("arrows");
			slider.before('<div class="coda-nav-left" id="coda-nav-left-' + sliderCount + '"><a href="#">' + settings.dynamicArrowLeftText + '</a></div>');
			slider.after('<div class="coda-nav-right" id="coda-nav-right-' + sliderCount + '"><a href="#">' + settings.dynamicArrowRightText + '</a></div>');
		};
		*/
		var panelWidth = slider.find(".item").width();
		var panelCount = slider.find(".item").size();
		var panelContainerWidth = panelWidth*panelCount+1000;
		var navClicks = 0; // Used if autoSlideStopWhenClicked = true
		
		// Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
		$('.item', slider).wrapAll('<div class="container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".container", slider).css({ width: panelContainerWidth });
		$(".container").append('<div class="clear"></div>');
		
		// Specify the current panel.
		// If the loaded URL has a hash (cross-linking), we're going to use that hash to give the slider a specific starting position...
		/*
		if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			var currentPanel = parseInt(location.hash.slice(1));
			var offset = - (panelWidth*(currentPanel - 1));
			$('.container', slider).css({ left: offset });
		// If that's not the case, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) { 
			var currentPanel = settings.firstPanelToLoad;
			var offset = - (panelWidth*(currentPanel - 1));
			$('.container', slider).css({ left: offset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
		*/
			var currentPanel = 1;
		//};
			
		
		// If we need a dynamic menu
		/*
		if (settings.dynamicTabs) {
			var dynamicTabs = '<div class="coda-nav" id="coda-nav-1"><ul class="navigation"></ul></div>';
			switch (settings.dynamicTabsPosition) {
				case "bottom":
					$('.coda-slider-wrapper').parent().append(dynamicTabs);
					break;
				default:
					$('.coda-slider-wrapper').parent().append(dynamicTabs);
					break;
			};*/
			$('#coda-nav-1 ul').html('');
			ul = $('#coda-nav-1 ul');
			// Create the nav items
			 ul.append('<li class="coda-nav-left" id="coda-nav-left-1"><img src="imagens/ant.gif" class="left" style="margin-right:10px; margin-top:-5px;" /></li>');
			$('.item', slider).each(function(n) {
				if($(this).hasClass('first')){
					ul.append('<li class="tab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).attr('title') + '</a></li>');	
				};
			});
			ul.append('<li class="coda-nav-right" id="coda-nav-right-1"><img src="imagens/prox.gif" class="right" style="margin-left:10px; margin-top:-5px;" /></li>	');
			navContainerWidth = slider.width() + slider.siblings('.coda-nav-left').width() + slider.siblings('.coda-nav-right').width();
			//ul.parent().css({ width: navContainerWidth });

				var tamanho = 0;
					var lis = $('.coda-nav .navigation li');
					for(i=0;i<lis.length;i++){
						tamanho += lis[i].offsetWidth; 
					}
					
					ul.css({ width: tamanho+100});
					$(".coda-nav").css('width',tamanho+100);
					$(".coda-nav").css('padding-left','70px');

		
		
		// Left arrow click
		$("#bt_prev").click(function(){
			
			navClicks++;
			if (currentPanel == 1) {
				atual = panelCount;
				offset2 = $('#'+atual).position();
				offset = - offset2.left;
				
				alterPanelHeight(panelCount - 1);
				currentPanel = panelCount;
				

			} else {
				atual = currentPanel - 1;
				offset2 = $('#'+atual).position();
				offset = - offset2.left;
				currentPanel -= 1;
				alterPanelHeight(currentPanel - 1);
				
				
			};
			$('.container', slider).animate({ left: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)


			
			return false;
		});
			
		// Right arrow click
		$('#bt_next').click(function(){
			navClicks++;
			if (currentPanel == panelCount) {
				offset = 0;
				currentPanel = 1;
				alterPanelHeight(0);
				slider.siblings('.coda-nav').find('a.current').removeClass('current').parents('ul').find('a:eq(0)').addClass('current');
			} else {
				//offset = - (panelWidth*currentPanel);
				atual = currentPanel+1;
				offset2 = $('#'+atual).position();
				offset = - offset2.left;
				currentPanel += 1;
				
				//slider.siblings('.coda-nav').find('a.current').removeClass('current').parent().next().find('a').addClass('current');
			};
			$('.container', slider).animate({ left: offset }, settings.slideEaseDuration, settings.slideEaseFunction);

			return false;
		});
		
		
			
		// If we need a tabbed nav
		$('#coda-nav-1 a').each(function(e) {
			// What happens when a nav link is clicked
			$(this).bind("click", function() {
				navClicks++;
				$(this).addClass('current').parents('ul').find('a').not($(this)).removeClass('current');
				//offset = - (panelWidth*z);
				z = parseInt($(this).attr("href").slice(1));
				zz=z;
				offset2 = $('#'+zz).position();
				offset = - offset2.left;
				alterPanelHeight(z);
				currentPanel = z;
				$('.container', slider).animate({ left: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
				 
			});
		});
		
		// External triggers (anywhere on the page)
		$(settings.externalTriggerSelector).each(function() {
			// Make sure this only affects the targeted slider
			if (sliderCount == parseInt($(this).attr("rel").slice(12))) {
				$(this).bind("click", function() {
					navClicks++;
					targetPanel = parseInt($(this).attr("href").slice(1));
					//offset = - (panelWidth*(targetPanel - 1));
					offset2 = $('#'+targetPanel-1).position();
					offset = - offset2.left;
					alterPanelHeight(targetPanel - 1);
					currentPanel = targetPanel;
					// Switch the current tab:
					slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (targetPanel - 1) + ') a').addClass('current');
					// Slide
					$('.container', slider).animate({ left: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
					if (!settings.crossLinking) { return false }; // Don't change the URL hash unless cross-linking is specified
				});
			};
		});
			
		// Specify which tab is initially set to "current". Depends on if the loaded URL had a hash or not (cross-linking).
		if (settings.crossLinking && location.hash && parseInt(location.hash.slice(1)) <= panelCount) {
			$("#coda-nav-" + sliderCount + " a:eq(" + (location.hash.slice(1) - 1) + ")").addClass("current");
		// If there's no cross-linking, check to see if we're supposed to load a panel other than Panel 1 initially...
		} else if (settings.firstPanelToLoad != 1 && settings.firstPanelToLoad <= panelCount) {
			$("#coda-nav-" + sliderCount + " a:eq(" + (settings.firstPanelToLoad - 1) + ")").addClass("current");
		// Otherwise we must be loading Panel 1, so make the first tab the current one.
		} else {
			$("#coda-nav-" + sliderCount + " a:eq(0)").addClass("current");
		};
		
		// Set the height of the first panel
		if (settings.autoHeight) {
			panelHeight = $('.item:eq(' + (currentPanel - 1) + ')', slider).height();
			slider.css({ height: panelHeight });
		};
		
		// Trigger autoSlide
		if (settings.autoSlide) {
			slider.ready(function() {
				setTimeout(autoSlide,settings.autoSlideInterval);
			});
		};
		
		function alterPanelHeight(x) {
			if (settings.autoHeight) {
				panelHeight = $('.item:eq(' + x + ')', slider).height()
				slider.animate({ height: panelHeight }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
			};
		};
		
		function autoSlide() {
			if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
				if (currentPanel == panelCount) {
					var offset = 0;
					currentPanel = 1;
				} else {
					var offset = - (panelWidth*currentPanel);
					currentPanel += 1;
				};
				alterPanelHeight(currentPanel - 1);
				// Switch the current tab:
				slider.siblings('.coda-nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (currentPanel - 1) + ') a').addClass('current');
				// Slide:
				$('.container', slider).animate({ left: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				setTimeout(autoSlide,settings.autoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.item', slider).show().end().find("p.loading").remove();
		slider.removeClass("preload");
		
		sliderCount++;
		
		
	});
};
