/*
	jQuery Coda-Slider v2.0 - http://www.ndoherty.biz/coda-slider
	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 coda-slider-no-js class from the body
	$("body").removeClass("coda-slider-no-js");
	// Preloader
	$(".coda-slider").children('.panel').hide().end().prepend('<p class="loading">Loading...<br /><img src="images/ajax-loader.gif" alt="loading..." /></p>');
});

var sliderCount = 1;

$.fn.codaSlider = function(settings) {

	settings = $.extend({
		autoHeight: false,
		autoHeightEaseDuration: 1000,
		autoHeightEaseFunction: "easeInOutExpo",
		autoSlide: false,
		autoSlideInterval: 7000,
		autoSlideStopWhenClicked: true,
		crossLinking: false,
		dynamicArrows: true,
		dynamicArrowLeftText: "&#171; left",
		dynamicArrowRightText: "right &#187;",
		dynamicTabs: false,
		dynamicTabsAlign: "center",
		dynamicTabsPosition: "bottom",
		externalTriggerSelector: "a.xtrig", /* */
		firstPanelToLoad: 1,
		panelTitleSelector: "h2.title",
		slideEaseDuration: 1000,
		continuousSlide: false,
		stopSlideOnLast:true,
		bulletsSelectImage:false
		
		/* 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 + '"><div>' + settings.dynamicArrowLeftText + '</div></div>');
			slider.after('<div class="coda-nav-right" id="coda-nav-right-' + sliderCount + '"><div>' + settings.dynamicArrowRightText + '</div></div>');
		};

		var panelWidth = slider.find(".panel").width();
		var navClicks = 0; // Used if autoSlideStopWhenClicked = true
		var panelCount = slider.find(".panel").size();
		var panelContainerWidth = panelWidth*(panelCount+2);
		
		
		// Surround the collection of panel divs with a container div (wide enough for all panels to be lined up end-to-end)
		$('.panel', slider).wrapAll('<div class="panel-container"></div>');
		// Specify the width of the container div (wide enough for all panels to be lined up end-to-end)
		$(".panel-container", slider).css({ width: panelContainerWidth });


/* Coppyed from  */
		if(settings.continuousSlide){
			$(".panel-container", slider).prepend($(".panel:last-child", slider).clone().css("margin-left","-"+ panelWidth +"px")); // 
			$(".panel-container", slider).append($(".panel:nth-child(2)", slider).clone()); // .css({"margin-left": "auto", "background": "#0F3"})
		};
/* Coppyed from  */		




		
		// 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));
			$('.panel-container', slider).css({ marginLeft: 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));
			$('.panel-container', slider).css({ marginLeft: offset });
		// Otherwise, we'll just set the current panel to 1...
		} else { 
			var currentPanel = 1;
			$('.panel-container').children().eq(currentPanel-1).addClass("selected");
		};
		
		
		if(settings.bulletsSelectImage) {
		/*
			$(".project_list_pages").find("img").click(function() {
				alert("O-lo=lo");
			}
		*/
			// slider.parents().eq(2).prepend('<div class=\"project_list_pages2\">11 sssssss 11</div>');
			/*
			$(".project_list_pages").find("img").click(function() {
				// var clickedBullet = $(".project_list_pages img").index(this);
				// var clickedBulletPictureWidth = $(this).parent().parent().find(".panel-container").find(".panel").eq(0).width();
				// var clickedOffcet = -(clickedBulletPictureWidth*clickedBullet);
				// var slideEaseDuration = 1000;
				
				alert("hello");
			});
			*/
			//
			

		} else {
			$(".project_list_pages").hide();
		}
		

		if(settings.stopSlideOnLast) {
			var currentPanelIndex = $('.panel-container').children().index($('.panel-container').children(".selected"));
			if (currentPanelIndex == 0) {
				$(".coda-nav-left").css({'display':'none'});
				$(".coda-nav-right").children().css({'width':'940px'});
			} else { 
				$(".coda-nav-left").css({'display':'block'});
				$(".coda-nav-right").children().css({'width':'350px'});
			}
			if (panelCount == 1) {
				$(".coda-nav-right").css({'display':'none'});
				// $(".coda-nav-left").children().css({'width':'940px', 'border':'solid 3px #0F0'});
			} else {
				$(".coda-nav-right").css({'display':'block'});
				$(".coda-nav-left").children().css({'width':'350px'});
			}
		}

		// Left arrow click
		// var timeout=null;
		$("#coda-nav-left-" + sliderCount).find("div").click(function() {   /* bind("click", leftArrowClick); */
			if(!$("#coda-nav-left-" + sliderCount).hasClass('active')) {
				$("#coda-nav-left-" + sliderCount).addClass('active');
				$("#coda-nav-right-" + sliderCount).addClass('active');
				
				navClicks++; // Clicks on line "Left"
				
				/*
				if($("#coda-nav-left-" + sliderCount).hasClass("active")) {
					// alert("has class active");
				} else {
					alert("not has class")
				}
				*/
			
			// var currentPanel = $(this).parent().parent().find(".panel").index($(this).parent().parent().find(".panel.selected"));
			var currentPanelIndex = $('.panel-container').children().index($('.panel-container').children(".selected"));

			// .css({'border':'solid 2px #96F'});

			$('.panel-container').children().removeClass("selected");
			
			if (currentPanelIndex == 1) {
				// offset = - (panelWidth*(panelCount - 1));
				offset = panelWidth;
				alterPanelHeight(currentPanelIndex - 1);
				// currentPanel = panelCount;
				// slider.siblings('.coda-nav').find('div.current').removeClass('current').parents('ul').find('li:last a').addClass('current');
				new_offset = - (panelWidth*0);
				$('.panel-container', slider).animate({ marginLeft: 0 }, settings.slideEaseDuration, settings.slideEaseFunction, function() {  /* $(this).attr('title', (currentPanelIndex - 1)); .removeClass("") */   
				
					setTimeout(function() {
						$("#coda-nav-left-" + sliderCount).removeClass('active');
						$("#coda-nav-right-" + sliderCount).removeClass('active');
					}, 300);
				
				});
				
				$(this).parent().parent().parent().parent().find('.project_list_pages').find("img").removeClass("selected").eq(currentPanelIndex - 1).addClass("selected");
				
				$('.project_list_pages').delay(480).animate({'left':'690px'}, 470);
				$('.project_list_pages').find('div').delay(490).animate({'margin-left':'0px'}, 300);
				
				// {duration : settings.slideEaseDuration, queue: false}
			} else {
				currentPanel -= 1;
				alterPanelHeight(currentPanelIndex - 1);
				offset = - (panelWidth*(currentPanelIndex - 1));
				slider.siblings('.coda-nav').find('div.current').removeClass('current').parent().prev().find('div').addClass('current');

				$('.panel-container', slider).delay(200).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction, function() {
					setTimeout(function() {
						$("#coda-nav-left-" + sliderCount).removeClass('active');
						$("#coda-nav-right-" + sliderCount).removeClass('active');
					}, 300);
				});
				$('.project_list_pages').delay(200).animate({'left':'445px'}, settings.slideEaseDuration).delay(200).find('div').css({'position':'relative', 'margin-left':'-50%'});
				
				
				$(this).parent().parent().parent().parent().find('.project_list_pages').find("img").removeClass("selected").eq(currentPanelIndex - 1).addClass("selected");

			};
			$('.panel-container').children().eq(currentPanelIndex - 1).addClass("selected");
			// $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);

			if(settings.stopSlideOnLast) {
				if (currentPanelIndex == 1) {
					$(".coda-nav-left").css({'display':'none'});
					$(".coda-nav-right").children().css({'width':'940px'});
				} else {
					$(".coda-nav-left").css({'display':'block'});
				}
				if (currentPanelIndex == panelCount) {
					$(".coda-nav-right").css({'display':'none'});
				} else {
					$(".coda-nav-right").css({'display':'block'});
					$(".coda-nav-left").children().css({'width':'350px'});
				}
			}
			if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
			// setTimeout(bindLeftArrowClick, 700);
			// setTimeout(function() { $(this).bind('click', leftArrowClick) }, 1000);
			
			}
			
			return false;
	
			
	
		});
		
/*		
		function bindLeftArrowClick() {
			$(".coda-nav-left").find("div").bind('click', leftArrowClick);
		}
		function bindRightArrowClick() {
			$(".coda-nav-right").find("div").bind('click', rightArrowClick);
		}		
*/
		
			
		// Right arrow click
		$('#coda-nav-right-' + sliderCount + ' div').click(function() { /*  */ 			/* .bind('click', function() { */
			if(!$("#coda-nav-right-" + sliderCount).hasClass('active')) {
				$("#coda-nav-left-" + sliderCount).addClass('active');
				$("#coda-nav-right-" + sliderCount).addClass('active');
				
		
				navClicks++;
				
				var currentPanelIndex = $('.panel-container').children().index($('.panel-container').children(".selected"));
				
				$(this).parent().parent().parent().parent().find('.project_list_pages').find("img").removeClass("selected").eq(currentPanelIndex + 1).addClass("selected");
				
				$('.panel-container').children().removeClass("selected");
				
				if (currentPanelIndex == (panelCount - 1) ) {
					offset = - (panelWidth*(currentPanelIndex+1));
					// currentPanel = 1;
					alterPanelHeight(0);
					slider.siblings('.coda-nav').find('div.current').removeClass('current').parents('ul').find('div:eq(0)').addClass('current');

					// alert(currentPanel);project_list_pages
					
					new_offset = 0;
					$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction, function() { $(this).css({ marginLeft : new_offset }); 
						setTimeout(function() {
							$("#coda-nav-left-" + sliderCount).removeClass('active');
							$("#coda-nav-right-" + sliderCount).removeClass('active');
						}, 300);
					}); //  .attr('title', currentPanelIndex) , function() { $(this).css({ marginLeft : new_offset }); }
					
					$('.project_list_pages').animate({'left':'690px'}, 500);
					$('.project_list_pages').find('div').animate({'margin-left':'0px'}, 500);
					
					//  
				} else {
					offset = - (panelWidth*(currentPanelIndex+1));
					alterPanelHeight(currentPanelIndex);
					currentPanel += 1;
					slider.siblings('.coda-nav').find('div.current').removeClass('current').parent().next().find('a').addClass('current');
					
					$('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction, function() { 
						setTimeout(function() {
							$("#coda-nav-left-" + sliderCount).removeClass('active');
							$("#coda-nav-right-" + sliderCount).removeClass('active');
						}, 300);
					});
					
					$('.project_list_pages').animate({'left':'445px'}, 500);
					$('.project_list_pages').find('div').animate({'margin-left':'-50%'}, 500);
					
				};
				
				$('.panel-container').children().eq(currentPanelIndex + 1).addClass("selected");


				if(settings.stopSlideOnLast) {
					var currentPanelIndex = $('.panel-container').children().index($('.panel-container').children(".selected"));
					
					if (currentPanelIndex == 0) {
						$(".coda-nav-left").css({'display':'none'});
						$(".coda-nav-right").children().css({'width':'940px'});
					} else { 
						$(".coda-nav-left").css({'display':'block' });
					}
					if (currentPanelIndex == (panelCount - 1) ) {
						$(".coda-nav-right").css({'display':'none'});
					} else {
						$(".coda-nav-left").children().css({'width':'350px'});
						$(".coda-nav-right").children().css({'width':'350px', 'display':'block'});
					}
				}

				// $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				if (settings.crossLinking) { location.hash = currentPanel }; // Change the URL hash (cross-linking)
			
			}
			
			return false;
		});  
		
		// If we need a dynamic menu
		if (settings.dynamicTabs) {
			var dynamicTabs = '<div class="coda-nav" id="coda-nav-' + sliderCount + '"><ul></ul></div>';
			switch (settings.dynamicTabsPosition) {
				case "bottom":
					slider.parent().append(dynamicTabs);
					break;
				default:
					slider.parent().prepend(dynamicTabs);
					break;
			};
			ul = $('#coda-nav-' + sliderCount + ' ul');
			// Create the nav items
			$('.panel', slider).each(function(n) {
				ul.append('<li class="tab' + (n+1) + '"><a href="#' + (n+1) + '">' + $(this).find(settings.panelTitleSelector).text() + '</a></li>');
			});
			navContainerWidth = slider.width() + slider.siblings('.coda-nav-left').width() + slider.siblings('.coda-nav-right').width();
			ul.parent().css({ width: navContainerWidth });
			switch (settings.dynamicTabsAlign) {
				case "center":
					ul.css({ width: ($("li", ul).width() + 2) * panelCount });
					break;
				case "right":
					ul.css({ float: 'right' });
					break;
			};
		};
			
		// If we need a tabbed nav
		$('#coda-nav-' + 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 = - (panelWidth*z);
				alterPanelHeight(z);
				currentPanel = z + 1;
				// $('.panel-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++;
					targetPanel = parseInt($(this).attr("href").slice(1));
					offset = - (panelWidth*(targetPanel - 1));
					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
					// $('.panel-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)) <= 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 = $('.panel: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 = $('.panel: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:
				// $('.panel-container', slider).animate({ marginLeft: offset }, settings.slideEaseDuration, settings.slideEaseFunction);
				setTimeout(autoSlide,settings.autoSlideInterval);
			};
		};
		
		// Kill the preloader
		$('.panel', slider).show().end().find("p.loading").remove();
		slider.removeClass("preload");
		
		// sliderCount++;
		
	});
};
