/*
 * 	Easy Slider - jQuery plugin
 *	written 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
 *
 */
 
(function($) {

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			orientation:	'', //  'vertical' is optional;
			speed: 			800,
			distance: 		0,
			visible:		6
		}; 
		
		var options = $.extend(defaults, options);  
		
		// Lenker fra thumbnails 
		$("#galleryLink:not(.empty)").click(function() {showGallery();})
		
		// lukk-hover
		var captionTemp;
		var captionClose = $("span", "#galleryClose").eq(0).html();
		$("#galleryClose").html("");
		
		$("#galleryClose").hover(
				function(){
					captionTemp = $("#galleryCaption").html();
					$("#galleryCaption").html("<span>" + captionClose + "</span>");
				},
				function(){
					$("#galleryCaption").html(captionTemp);
				}				
		);
		
		
		return this.each(function() {  
			obj = $(this); 				
			var s = $("li", obj).length;
			
			var w = (options.distance > 0) ? options.distance : obj.width(); 
			
			var h = obj.height(); 
			var ts = s - options.visible;
			
			var t = 0;
			var vertical = (options.orientation == 'vertical');
			$("ul", obj).css('width',s*w);			
			if(!vertical) $("li", obj).css('float','left');
			
			var $images = obj.find('img');

			$images.each(function(i, n) {
				var $img = $(this);
				$img.wrap('<a />');
			});
			
			// lenke til bilde
			$("a", this).each(function (i, a) {
				
				var $link = $(this);
				var $img = $("img", this).eq(0);
				
				$img.click(function() {
					
					if ($(this).parent().hasClass('activeThumb'))
						return;
					
					// setter css
					$(".activeThumb", obj).removeClass('activeThumb').stop().animate({'marginTop' : 0}, 200);
					$("a", obj).removeClass('activeThumb');
					$link.addClass('activeThumb');					

					setSlide(i);

					// viser bildetekst
					var caption = new String($(this).attr("title"));
					if (caption.length > 0)
						$("#galleryCaption").html("<span>" + caption + "</span>");
					else
						$("#galleryCaption").html("");
					
					// scroller dersom bildet er helt til høyre eller venstre
					if (s > options.visible && i > 0 && i - t == 0)
						animate("prev");
					else if (s > options.visible && i >= (options.visible - 1) && (options.visible - 1) + t == i)
						animate("next");
					
				});
			});
			
			
			// hover-effekt
			$("a", this).hover(
				function() {
					if (!$(this).hasClass('activeThumb'))
						$(this).stop().animate({'marginTop' : -5}, 200);
				}, 
			    
			    function() {
					if (!$(this).hasClass('activeThumb'))
						$(this).stop().animate({'marginTop' : 0}, 200);
				}
			);

			// Piler
			$("#"+options.prevId).fadeTo(100, 0);
			$("#"+options.nextId).fadeTo(100, 0);
			
			$("#"+options.nextId).click(function()
			{		
				animate("next");
			});
			
			$("#"+options.prevId).click(function()
			{		
				animate("prev");
			});	
			
			function animate(dir)
			{
				if(dir == "next"){
					if (t>=ts) $("#"+options.nextId).fadeTo(200, 0);
					$("#"+options.prevId).fadeTo(200, 1);
					t = (t>=ts) ? ts : t+1;	
				} else {
					t = (t<=0) ? 0 : t-1;
					if (t<=0) $("#"+options.prevId).fadeTo(200, 0);
					$("#"+options.nextId).fadeTo(200, 1);
				};								
				if(!vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						options.speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						options.speed
					);					
				}
			};
			
			if(s>options.visible) $("#"+options.nextId).fadeTo(1, 1);	
			
		});
	  
	};

})(jQuery);
