(function($) {
	
/*
This code is licenced under LGPL v3

Author : Alexandre Stanislawski
*/
/*global window : false, jQuery : false*/
/**
fadeImages

Simple jQuery plugin that can transform a div containing images into a slideshow with a fade as a transition.

In : 
- params : object containing the attributes :  
	- timeout : time before next transition
	- fadeDuration : transition duration
	- initDelay : time before first transition
*/
	$.fn.fadeImages = function(params){
		var config = {
			timeout: 4000,
			fadeDuration: 500
		};
		if (params){
			jQuery.extend(config, params);
		}
		if (!config.initDelay){
		  config.initDelay=config.timeout;
		}

		function faderCreator(imgSet){
			var current = 0,
			  total = imgSet.size(),
			  fader = function(){
				var nextIter = current + 1;
				
				if (nextIter >= total){
					nextIter = 0;
				}

				jQuery(imgSet[current]).animate({
					opacity: 0
				}, config.fadeDuration);

				jQuery(imgSet[nextIter]).animate({
					opacity: 1
				}, config.fadeDuration, function(){
					window.setTimeout(fader, config.timeout);
				});

				current = nextIter;
			};
			
			return fader;
		}

		this.each(
		function(){
			var jQthis = jQuery(this),
				imgSet = jQthis.find("img");

			jQthis.css({
				position: "relative",
				overflow: "hidden"
			});

			imgSet.css({
				opacity: 0,
				position: "absolute",
				top: 0,
				left: 0
			});

			imgSet.first().css({
				opacity: 1
			});

			window.setTimeout(faderCreator(imgSet), config.initDelay);
		});
		return this;
	};
	
	$.fn.vaultPromo = function(p){
		var d={};
		var o=$.extend(d,p);
		this.each(function(){
			var h=$(this),i='';
			//for (var x in o) i+='<h1><img src="'+o[x]['img'].toString()+'" alt="'+o[x]['alt'].toString()+'" border="0" /></h1>\n';
			for (var x in o) i+=o[x]+'\n';
			i+='<div class="clear"></div>\n';
			h.html(i).fadeImages({timeout:8000,fadeDuration:500})
		});
		return this
	};
	
/*
 * 	Easy Slider 1.5 - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/4004/easy-slider-15-the-easiest-jquery-plugin-for-sliding
 *
 *	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 $("#slider").easySlider();
 *	
 * 	<div id="slider">
 *		<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>
 *
 */

	$.fn.easySlider = function(options){
	  
		// default configuration properties
		var defaults = {			
			prevId: 		'prevBtn',
			prevText: 		'Previous',
			nextId: 		'nextBtn',	
			nextText: 		'Next',
			pauseId:		'pauseId',
			controlsShow:	false,
			controlsBefore:	'',
			controlsAfter:	'',	
			controlsFade:	true,
			firstId: 		'firstBtn',
			firstText: 		'First',
			firstShow:		false,
			lastId: 		'lastBtn',	
			lastText: 		'Last',
			lastShow:		false,				
			vertical:		true,
			speed: 			800,
			auto:			true,
			pause:			8000,
			continuous:		true
		}; 
		
		var options = $.extend(defaults, options);  
				
		this.each(function() {  
			var obj = $(this); 				
			var s = $("li", obj).length;
			var w = $("li", obj).width();
			//var h = $("li", obj).height();
			var h = 52;
//			obj.width(w); 
//			obj.height(h); 
//			obj.css("overflow","hidden");
			var ts = s-1;
			var t = 0;
			//$("ul", obj).css('width',(s+1)*w);			
			$("ul", obj).css('height',(s+1)*h);			
			if(!options.vertical) $("li", obj).css('float','left');
			
			if(options.controlsShow) {
				var html = options.controlsBefore;
				if(options.firstShow) html += '<span id="'+ options.firstId +'"><a href=\"javascript:void(0);\">'+ options.firstText +'</a></span>';
				html += ' <span id="'+ options.prevId +'"><a href=\"javascript:void(0);\">'+ options.prevText +'</a></span>';
				html += ' <span id="'+ options.nextId +'"><a href=\"javascript:void(0);\">'+ options.nextText +'</a></span>';
				if(options.lastShow) html += ' <span id="'+ options.lastId +'"><a href=\"javascript:void(0);\">'+ options.lastText +'</a></span>';
				html += options.controlsAfter;						
				$(obj).after(html);										
			};
	
			$("#"+options.nextId).click(function(){		
				animate("next",true);
			});
			$("#"+options.prevId).click(function(){		
				animate("prev",true);				
			});	
			$("#"+options.firstId).click(function(){		
				animate("first",true);
			});				
			$("#"+options.lastId).click(function(){		
				animate("last",true);				
			});
			$('#'+options.pauseId).click(function(){
				if ($(this).hasClass('play')) {
					$(this).removeClass('play');
					options.continuous = true;
					timeout = setTimeout(function() { animate('next', false); }, options.speed+options.pause);
				}
				else {
					$(this).addClass('play');
					options.continuous = false;
					animate('pause', true);
				}
			});
			
			function animate(dir,clicked){
				var ot = t;				
				switch(dir){
					case "next":
						t = (ot>=ts) ? (options.continuous ? 0 : ts) : t+1;						
						break; 
					case "prev":
						t = (t<=0) ? (options.continuous ? ts : 0) : t-1;
						break; 
					case "first":
						t = 0;
						break; 
					case "last":
						t = ts;
						break; 
					default:
						break; 
				};	
				
				var diff = Math.abs(ot-t);
				var speed = diff*options.speed;						
				if(!options.vertical) {
					p = (t*w*-1);
					$("ul",obj).animate(
						{ marginLeft: p }, 
						speed
					);				
				} else {
					p = (t*h*-1);
					$("ul",obj).animate(
						{ marginTop: p }, 
						speed
					);					
				};
				
				if(clicked) clearTimeout(timeout);
				if(options.auto && dir=="next" && !clicked){;
					timeout = setTimeout(function(){
						animate("next",false);
					},diff*options.speed+options.pause);
				};
			};
			// init
			var timeout;
			window.sliderTimeoutFn = setTimeout(function(){
					animate("next",false);
				},options.pause);

			if(options.auto){;
				timeout = window.sliderTimeoutFn;
			};		
		
			window.sliderTimeoutHandle = timeout;
		});
	};
	
	/***************************************************
	 ***************************************************/
	$('div.slider').easySlider({
		prevId: 'ctrl-prev',
		nextId: 'ctrl-next',
		pauseId: 'ctrl-pause',
		continuous: true
	});
})(jQuery);

