/*
	jQuery produk1Slider v2.0 - http://www.ndoherty.biz/produk1slider
	Copyright (c) 2009 Niall Doherty
	This plugin available for use in all personal or commercial projects under both MIT and GPL licenses.
*/

$(function(){
	// Remove the produk1slider-no-js class from the body
	$("body").removeClass("produk1slider-no-js");
	// Preloader
	$(".produk1slider").children('.panelproduk1').hide().end().prepend('<p class="loading">Harap Tunggu...<br /><img src="images/ajax-loader.gif" alt="Harap Tunggu..." /></p>');
});

var sliderCount = 1;

$.fn.produk1codaSlider = function(settings) {

	settings = $.extend({
		autoHeight: true,
		autoHeightEaseDuration: 1000,
		autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: true,
		autoSlideInterval: 3500,
		autoSlideStopWhenClicked: true,
		crossLinking: false,
		dynamicArrows: false,
		dynamicArrowLeftText: "&#171; |",
		dynamicArrowRightText: "| &#187;",
		dynamicTabs: false,
		dynamicTabsAlign: "left",
		dynamicTabsPosition: "top",
		externalTriggerSelector: "a.xtrig",
		firstpanelproduk1ToLoad: 1,
		panelproduk1TitleSelector: "div.title",
		slideEaseDuration: 1000,
		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="produk1nav-left" id="produk1nav-left-' + sliderCount + '"><a href="#">' + settings.dynamicArrowLeftText + '</a></div>');
			slider.after('<div class="produk1nav-right" id="produk1nav-right-' + sliderCount + '"><a href="#">' + settings.dynamicArrowRightText + '</a></div>');
		};
		
		var panelproduk1Width = slider.find(".panelproduk1").width();
		var panelproduk1Count = slider.find(".panelproduk1").size();
		var panelproduk1ContainerWidth = panelproduk1Width*panelproduk1Count;
		var navClicks = 0; // Used if autoSlideStopWhenClicked = true
		
		// Surround the collection of panelproduk1 divs with a container div (wide enough for all panelproduk1s to be lined up end-to-end)
		$('.panelproduk1', slider).wrapAll('<div class="panelproduk1-container"></div>');
		// Specify the width of the container div (wide enough for all panelproduk1s to be lined up end-to-end)
		$(".panelproduk1-container", slider).css({ width: panelproduk1ContainerWidth });
		
		// Specify the current panelproduk1.
		// 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)) <= panelproduk1Count) {
			var currentpanelproduk1 = parseInt(location.hash.slice(1));
			var offset = - (panelproduk1Width*(currentpanelproduk1 - 1));
			$('.panelproduk1-container', slider).css({ marginLeft: offset });
		// If that's not the case, check to see if we're supposed to load a panelproduk1 other than panelproduk1 1 initially...
		} else if (settings.firstpanelproduk1ToLoad != 1 && settings.firstpanelproduk1ToLoad <= panelproduk1Count) { 
			var currentpanelproduk1 = settings.firstpanelproduk1ToLoad;
			var offset = - (panelproduk1Width*(currentpanelproduk1 - 1));
			$('.panelproduk1-container', slider).css({ marginLeft: offset });
		// Otherwise, we'll just set the current panelproduk1 to 1...
		} else { 
			var currentpanelproduk1 = 1;
		};
			
		// Left arrow click
		$("#produk1nav-left-" + sliderCount + " a").click(function(){
			navClicks++;
			if (currentpanelproduk1 == 1) {
				offset = - (panelproduk1Width*(panelproduk1Count - 1));
				alterpanelproduk1Height(panelproduk1Count - 1);
				currentpanelproduk1 = panelproduk1Count;
				slider.siblings('.produk1nav').find('a.current').removeClass('current').parents('ul').find('li:last a').addClass('current');
			} else {
				currentpanelproduk1 -= 1;
				alterpanelproduk1Height(currentpanelproduk1 - 1);
				offset = - (panelproduk1Width*(currentpanelproduk1 - 1));
				slider.siblings('.produk1nav').find('a.current').removeClass('current').parent().prev().find('a').addClass('current');
			};
			$('.panelproduk1-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			if (settings.crossLinking) { location.hash = currentpanelproduk1 }; // Change the URL hash (cross-linking)
			return false;
		});
			
		// Right arrow click
		$('#produk1nav-right-' + sliderCount + ' a').click(function(){
			navClicks++;
			if (currentpanelproduk1 == panelproduk1Count) {
				offset = 0;
				currentpanelproduk1 = 1;
				alterpanelproduk1Height(0);
				slider.siblings('.produk1nav').find('a.current').removeClass('current').parents('ul').find('a:eq(0)').addClass('current');
			} else {
				offset = - (panelproduk1Width*currentpanelproduk1);
				alterpanelproduk1Height(currentpanelproduk1);
				currentpanelproduk1 += 1;
				slider.siblings('.produk1nav').find('a.current').removeClass('current').parent().next().find('a').addClass('current');
			};
			$('.panelproduk1-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
			if (settings.crossLinking) { location.hash = currentpanelproduk1 }; // Change the URL hash (cross-linking)
			return false;
		});
		
		// If we need a dynamic menu
		if (settings.dynamicTabs) {
			var dynamicTabs = '<div class="produk1nav" id="produk1nav-' + sliderCount + '"><ul></ul></div>';
			switch (settings.dynamicTabsPosition) {
				case "bottom":
					slider.parent().append(dynamicTabs);
					break;
				default:
					slider.parent().prepend(dynamicTabs);
					break;
			};
			ul = $('#produk1nav-' + sliderCount + ' ul');
			// Create the nav items
			$('.panelproduk1', slider).each(function(n) {
				ul.append('<li class="tab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(settings.panelproduk1TitleSelector).text() + '</a></li>');												
			});
			navContainerWidth = slider.width() + slider.siblings('.produk1nav-left').width() + slider.siblings('.produk1nav-right').width();
			ul.parent().css({ width: navContainerWidth });
			switch (settings.dynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * panelproduk1Count });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#produk1nav-' + sliderCount + ' a').each(function(z) {
			// 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 = - (panelproduk1Width*z);
				alterpanelproduk1Height(z);
				currentpanelproduk1 = z + 1;
				$('.panelproduk1-container', slider).animate({ marginLeft: 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++;
					targetpanelproduk1 = parseInt($(this).attr("href").slice(1));
					offset = - (panelproduk1Width*(targetpanelproduk1 - 1));
					alterpanelproduk1Height(targetpanelproduk1 - 1);
					currentpanelproduk1 = targetpanelproduk1;
					// Switch the current tab:
					slider.siblings('.produk1nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (targetpanelproduk1 - 1) + ') a').addClass('current');
					// Slide
					$('.panelproduk1-container', slider).animate({ marginLeft: 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)) <= panelproduk1Count) {
			$("#produk1nav-" + 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 panelproduk1 other than panelproduk1 1 initially...
		} else if (settings.firstpanelproduk1ToLoad != 1 && settings.firstpanelproduk1ToLoad <= panelproduk1Count) {
			$("#produk1nav-" + sliderCount + " a:eq(" + (settings.firstpanelproduk1ToLoad - 1) + ")").addClass("current");
		// Otherwise we must be loading panelproduk1 1, so make the first tab the current one.
		} else {
			$("#produk1nav-" + sliderCount + " a:eq(0)").addClass("current");
		};
		
		// Set the height of the first panelproduk1
		if (settings.autoHeight) {
			panelproduk1Height = $('.panelproduk1:eq(' + (currentpanelproduk1 - 1) + ')', slider).height();
			slider.css({ height: panelproduk1Height });
		};
		
		// Trigger autoSlide
		if (settings.autoSlide) {
			slider.ready(function() {
				setTimeout(autoSlide,settings.autoSlideInterval);
			});
		};
		
		function alterpanelproduk1Height(x) {
			if (settings.autoHeight) {
				panelproduk1Height = $('.panelproduk1:eq(' + x + ')', slider).height()
				slider.animate({ height: panelproduk1Height }, settings.autoHeightEaseDuration, settings.autoHeightEaseFunction);
			};
		};
		
		function autoSlide() {
			if (navClicks == 0 || !settings.autoSlideStopWhenClicked) {
				if (currentpanelproduk1 == panelproduk1Count) {
					var offset = 0;
					currentpanelproduk1 = 1;
				} else {
					var offset = - (panelproduk1Width*currentpanelproduk1);
					currentpanelproduk1 += 1;
				};
				alterpanelproduk1Height(currentpanelproduk1 - 1);
				// Switch the current tab:
				slider.siblings('.produk1nav').find('a').removeClass('current').parents('ul').find('li:eq(' + (currentpanelproduk1 - 1) + ') a').addClass('current');
				// Slide:
				$('.panelproduk1-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				setTimeout(autoSlide,settings.autoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.panelproduk1', slider).show().end().find("p.loading").remove();
		slider.removeClass("preload");
		
		sliderCount++;
		
	});
};
