/*
 *  adapted by by Chris Pollock
 *	based on code by by Alen Grakalic	

 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
/*
 *	markup example for $("#images").easySlider();
 *	
 * 	<div id="images">
 *		<ul>
 *			<li><img src="images/01.jpg" alt="" /></li>
 *			<li><img src="images/02.jpg" alt="" /></li>
 *			<li><img src="images/03.jpg" alt="" /></li>
 *			<li><img src="images/04.jpg" alt="" /></li>
 *			<li><img src="images/05.jpg" alt="" /></li>
 *		</ul>
 *	</div>
 *
 */

(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800,
			fadeSpeed:      500,
			hover: 			true,
			activatesIFR: 	true,
			preloadNavBg:   true,
			scrollDown: 	true,
			scrollSpeed: 	800,
			buildPagination:true,
			fadeAndGrow: 	false,
			growMinHeight:  293
		}; 
		
		var options = $.extend(defaults, options);  
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li.carousel-panel", obj).length;			
			var w = obj.width(); 
			var h = obj.height(); 
			var ts = s-1;
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			
			$(".carousel-content li:eq(0)").fadeIn();
			
			//$(obj).after('<span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span> <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>');		
			
			// add a page button for each li
			if (options.buildPagination)
			{
				if ($('.pagination').length)
				{
					var x = 0;
					$("li", obj).each(function(){
						$(".pagination").append('<li><a href="javascript:void(0);" class="text-replace" id="pagination_' + x + '">' + (x + 1) + '</a></li>');
						
						$("#pagination_"+x).click(function(){		
							goto($(this).html());
						});	
						x++;
					});
				}
			}
			else // if there is still a pagination, then find it and attach
			{
				if ($(".pagination").length)
				{
			
					$(".pagination a" ).click(function(){	
						parentId = $(this).parent().attr("id");
						parentSplit = parentId.split("_");
						goto((parentSplit[1]*1)+1);
					});	
					
					$(".pagination a" ).attr("href", "javascript:void(0)");
				}
			}
			
			
			$("#"+options.prevId).attr("href", 'javascript:void(0);');
			$("#"+options.nextId).attr("href", 'javascript:void(0);');
			
			$("#"+options.nextId).click(function(){		
				if (options.scrollDown)
					scrollTo('#project-viewer');
				animate("next");
				setLocation();
			});
					
			$("#"+options.prevId).click(function(){	
				if (options.scrollDown)
					scrollTo('#project-viewer');
				animate("prev");
				setLocation();
			});
					
			if (options.hover == true)
			{
				if ($.browser.msie)
				{
					$("#"+options.nextId).hover(function(){ $(".hoverRight").show(); }, 
					function(){ $(".hoverRight").hide();  });
					
					$("#"+options.prevId).hover(function(){ $(".hoverLeft").show(); }, 
					function(){ $(".hoverLeft").hide();  });	
				} 
				else
				{
					$("#"+options.nextId).hover(function(){ $(".hoverRight").fadeIn(); }, 
						function(){ $(".hoverRight").fadeOut();  });
						
					$("#"+options.prevId).hover(function(){ $(".hoverLeft").fadeIn(); }, 
						function(){ $(".hoverLeft").fadeOut();  });	
				}
			}
			
			if (options.preloadNavBg)
			{
				jQuery("<img>").attr("src", "/images/arrow-right-hover.png");
				jQuery("<img>").attr("src", "/images/arrow-left-hover.png");
			}
						
			function animate(dir){
				
				
				
				if (dir != undefined)
				{
					if(dir == "next"){
						t = (t>=ts) ? 0 : t+1;	
					} else {
						t = (t<=0) ? ts : t-1;
					};
				}								
				
				if(!vertical) {
					p = (t*w*-1);
					//$("li:eq(" + t + ") .panel-details", obj).hide();
					//$(".panel-details", obj).fadeOut(500, function(){
					
						var newHeight =  $("li.carousel-panel:eq(" + t + ")", obj).height() + 38 + 38;
					
						if (options.fadeAndGrow && (obj.height() != newHeight))
						{
						
							obj.animate(
								{ height: newHeight }, 
								(options.speed / 2),
								function()
								{
									$("ul",obj).animate(
										{ marginLeft: p }, 
										options.speed 
									);
								}
							);
							
						}
						else 
						{ 
							// just do the regular animation
							$("ul",obj).animate(
								{ marginLeft: p }, 
								options.speed 
							);
						}	
						
						$(".carousel-content li:visible").fadeOut(options.fadeSpeed, function(){
							$(".carousel-content li:eq(" + t + ")").fadeIn(function(){
								
							});
							if (options.activatesIFR && !($.browser.msie))
							{
								if ($(".carousel-content li:eq(" + t + ") .sifr-loaded").length == 0)
								{
									sIFR.replace(archer_bold, {
									      selector: '#body-work h2.sifr',
									      css: [
									      '.sIFR-root {color:#3b3c3f;text-transform: uppercase; font-size:18px; line-height:24px; text-align:left; width:auto; padding:0; margin:0;'
									      ],
									      wmode: 'transparent'
									});
									$(".carousel-content li:eq(" + t + ") h2").addClass("sifr-loaded");
								}
							}
						});
					//});
								
					
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
				
				//setLocation();
			};
			
			function goto(i){
				t = (i-1);
				animate();
				/*p = (t*w*-1);
				$("ul",obj).animate(
					{ marginLeft: p }, 
					options.speed
				);*/			
				
				setLocation();
			}
			
			function jump(i)
			{
				t = (i-1);
				p = (t*w*-1);
				$("ul",obj).css({ marginLeft: p });
				
				setLocation();
			}
			
			function setLocation()
			{
				
				
				
				$(".pagination a").removeClass("active");
				$(".pagination li").removeClass("active");
				
				$(".pagination li:eq(" + t + ")").addClass("active");
				
				// setup hover 
				
				var left = t-1;
				var right = t+1;
				
				if (left<0) { left = ts }
				if (right>ts) { right = 0 }
				
				var hoverSrc = $("li:eq(" + left + ")", obj).attr("rel");
				$(".hoverLeft").html('<img src="' + hoverSrc + '" />');
				
				hoverSrc = $("li:eq(" + right + ")", obj).attr("rel");
				$(".hoverRight").html('<img src="' + hoverSrc + '" />');
			
				
			}
			
			function scrollTo(selector) {
				if (! $('body').hasClass("noscroll"))
				{
		        	var targetOffset = $(selector).offset().top;
		        	$('html,body').animate({scrollTop: targetOffset}, options.scrollSpeed);
		        	$('body').addClass("noscroll");
		        }
		    }
			
			
			// initial call
			setLocation();
			
			// check the hash
			if (location.hash != "" && location.hash != "#")
			{
				var h = location.hash;
				var i = h.replace("#", "");
				jump(i);
			}
		});
	  
	};

})(jQuery);