 //make $ surely available inside the PI as jQuery shortcut
(function($) {
	//call teh method with options arguments
	$.fn.xSlider = function(options) {
		//extend to defaults
		var defaults = $.extend({}, $.fn.xSlider.defaults, options);
			//Initilaize any instance looping on the match element
			return this.each(function(){
				//set local variables and extend to the metadata plugin if loaded
				var $obj = $(this);
				var $fields = $obj.children();
				var total = $fields.length - 1;
				var o = $.meta ? $.extend({}, defaults, $obj.data()) : defaults;
				//set o.display in js 0 count
				o.display = o.display - 1;
				var $nextPrev = $('.nextPrev');
				//Insert command strings
				//$('<span class="prev xSlider">'+ o.prevText + '</span>').appendTo($nextPrev);
				//$('<span class="next xSlider">' + o.nextText + '</span>').appendTo($nextPrev);
				//Insert command strings
				$('<img class="next xSlider" src="../../img/library/9/btn_next.gif" alt="Next">').appendTo($('.nextPrev'));
				$('<img class="prev xSlider" src="../../img/library/9/btn_prev.gif" alt="Prev" />').appendTo($('.nextPrev'));
				
				//hide the command element if needed
					if(o.display == 0){
						$('.prev').hide()
					}
					if(o.display == total){
						$('.next').hide()
					}
				//Hide the element exept the first
					$fields.each(function(i){
						if(i != o.display){
			 				noDisplay(this);
			  			}
					});
					//bind events
					$('.xSlider').click(function(){
						if ($(this).is('.next')){
							o.display = o.display + 1;
								$fields.each(function(i){
			  						if(i == o.display){
			  						 displayNext(this);
			  				 		}
			 					});
						}else{
							o.display = o.display - 1;
							$fields.each(function(i){
			  					if(i == o.display){
			  					 displayPrev($(this));
			  				 	}
			 				});
							
						}	
					});
			//recursive functions	
			function noDisplay(field){
				$(field).hide();		
			}	
			function displayNext(field){
				$field = $(field);
					if(o.effect == 'fade'){						
						$field.prev().fadeOut(function(){
							$field.fadeIn()
						});
					}
					else
					{
						$field.slideDown();
						$field.prev().slideUp();
					}
					
					if(o.display == total){
						$('.next').hide();
					}
					if(o.display != 0)
					{
				  	 $('.prev').show();
					}
			}
			function displayPrev(field){
				$field = $(field);
						if(o.effect == 'fade'){
							$field.next().fadeOut(function(){
								$field.fadeIn();				
							});
						}	
						else
						{
								$field.slideDown();
								$field.next().slideUp();		
						}			
						
						if(o.display != total){
							$('.next').show();
						}
						if(o.display == 0)
						{
				   			$('.prev').hide();
						}
			}
		});
	}	
})(jQuery);

$.fn.xSlider.defaults = {
	prevText : 'Prev',
	nextText : 'Next',
	display : 1,
	effect : 'slide'
};		
		
	
