(function($) {

	$.JOSlider = function(el, options) {

		// To avoid scope issues, use 'base' instead of 'this'
		// to reference this class from internal events and functions.
		var base = this;

		// Wraps the ul in the necessary divs and then gives Access to jQuery element
		base.$el = $(el).addClass('JOSliderBase');
		
		// Add a reverse reference to the DOM object
		base.$el.data("JOSlider", base);
		
		base.init = function(){

			base.options = $.extend({}, $.JOSlider.defaultSliderConfig, options);
			base.images = [];
			base.currency =0;
			base.size = 0;
			base.timer = null;
			base.big_image_contaner = $(base.options.big_image_contaner);
			base.options.big_image_contaner = base.options.big_image_contaner.substr(1,base.options.big_image_contaner.length);
			base.big_image_path = base.options.big_image_contaner + '_big_image';
			
			$('li', base.$el).each(function(i){
				base.images[i] = $('li:eq(' + i + ')',base.$el);
				$('li:eq(' + i + ') a',base.$el).click(function(){ 
					base.clearTimer();
					base.changeImage(i);
					return false;
				});
				base.size++;
			}); 
			
			if( base.size == 0 ) {
				return;
			} else if( base.size == 1 ) {
				base.options.auto = false;
			} else if(!base.big_image_contaner) {
				return;
			}
			
			if(base.images[base.options.start]) {
				base.changeImage(base.options.start);
			} else {
				base.changeImage(base.currency);
			}
			
			if(base.options.auto === true && !base.timer) {
				base.timer = setInterval(function() {
						base.nav(-1);
					}, (base.options.interval*1000));
			}

			$(base.options.prev).click(function(){
				base.clearTimer();
				base.nav(1);
				return false;
			});

			$(base.options.next).click(function(){
				base.clearTimer();
				base.nav(-1);
				return false;
			});
		};
		
		base.scrollThumbsTop = function(number) {
			base.parent_height = base.$el.parent().outerHeight(true);
			base.image_contaner_height = base.$el.height();
			base.this_element = $('li:eq(' + number + ')', base.$el);
			base.this_offsetHeight = base.this_element.outerHeight(true);

			base.per_page = parseInt(base.parent_height / base.this_offsetHeight);
			if(/^\d$/.test(base.per_page/2)) {
				base.center = parseInt(base.per_page/2);
			} else {
				base.center = Math.ceil(base.per_page/2);
			}

			base.move = ((base.this_offsetHeight*number) - (base.center*base.this_offsetHeight)) + base.this_offsetHeight;
			
			if(base.move < 0) { base.move = 0; }
			if((base.size - base.per_page) <= (number-base.center)) {
				base.move = base.this_offsetHeight*(base.size - base.per_page);
			}

			//if((base.image_contaner_height - base.parent_height) < base.move) { base.move = (base.image_contaner_height - base.parent_height); }

			base.$el.animate({
			  'top' : -base.move
			}, base.options.speed);
		};
		
		base.scrollThumbsLeft = function(number) {
			base.parent_width = base.$el.parent().outerWidth(true);
			base.image_contaner_width = base.$el.width();
			base.this_element = $('li:eq(' + number + ')', base.$el);
			base.this_offsetWidth = base.this_element.outerWidth(true);

			base.per_page = parseInt(base.parent_width / base.this_offsetWidth);
			if(/^\d$/.test(base.per_page/2)) {
				base.center = parseInt(base.per_page/2);
			} else {
				base.center = Math.ceil(base.per_page/2);
			} 

			base.move = ((base.this_offsetWidth*number) - (base.center*base.this_offsetWidth)) + base.this_offsetWidth;
			
			if(base.move < 0) { base.move = 0; }
			if((base.size - base.per_page) <= (number-base.center)) {
				base.move = base.this_offsetWidth*(base.size - base.per_page);
			}

			//if((base.image_contaner_height - base.parent_height) < base.move) { base.move = (base.image_contaner_height - base.parent_height); }

			base.$el.animate({
			  'left' : -base.move
			}, base.options.speed);
		};
		
		base.nav = function(number) {
			switch(number) {
				case 1:
					base.num = (base.currency - 1);
					if(base.num < 0) {
						base.num = (base.size -1);
					}
				break;
				case -1:
					base.num = (base.currency + 1);
					if(base.num > (base.size-1)) {
						base.num = 0;
					}
				break;
			}
			base.changeImage(base.num);
		}
		
		base.changeImage = function(number) {
			/*if(base.currency == number) { 
				return; 
			}*/
			base.image = base.images[number].find('a').attr('href');
			base.big_image_contaner.html(base.createImage(base.image));
			
			if(base.options.direction == 'left') {
				base.scrollThumbsLeft(number);
			} else if(base.options.direction == 'top') {
				base.scrollThumbsTop(number);
			}
			
			$(base.$el).find('a').removeClass('selected');
			$(base.$el).find('a:eq(' + number + ')').addClass('selected');
			base.currency = number;
		};
		
		base.createImage = function(image) {
			return $('<img id="' + base.big_image_path + '">').css({'position':'absolute','display':'none'}).attr('src',image).fadeIn(base.options.fadeIn);
		};
		
		base.clearTimer = function(){
			// Clear the timer only if it is set
			if (base.timer) { 
				window.clearInterval(base.timer); 
			}
		};
		
		$.JOSlider.defaultSliderConfig = {
			'direction' 	: 'top',
			'interval'	: 5,
			'start'		: 0,
			'fadeIn'	: 500,
			'prev'		: false,   // '.prev'
			'next'		: false,   // '.next'
			'speed'		: 500,
			'auto'		: true,
			'big_image_contaner' : false
		};
		
		// Trigger the initialization
		base.init();
	
	};
	
	$.fn.JOSlider = function(options) {

		return this.each(function(i){ 
			var JOSlide = $(this).data('JOSlider');

			// initialize the slider but prevent multiple initializations
			if ((typeof(options)).match('object|undefined')){
				if (!JOSlide) {
					(new $.JOSlider(this, options));
				}
			// If options is a number, process as an external link to page #: $(element).JOSlider(#)
			} else if (/\d/.test(options) && !isNaN(options) && JOSlide) {
				var page = (typeof(options) == "number") ? options : parseInt($.trim(options),10); // accepts "  2  "
				// ignore out of bound pages
				if ( page >= 1 && page <= JOSlide.currency ) {
					JOSlide.changeImage(page);
				}
			}
		});
	};
	
	$.fn.toppos = function() {
		if($(this).offset()) {
			return $(this).offset().top;
		} else {
			return 0;
		}
    };
	
	$.fn.leftpos = function() {
		if($(this).offset()) {
			return $(this).offset().left;
		} else {
			return 0;
		}
    };
	
})(jQuery);


/*
//example
$(document).ready(function(){
  $('#thumbs-large-slider').JOSlider({
	'direction' 	: 'top',
	'big_image_contaner' : '#large-slider-image',
	'interval'	: 5,
	'prev'		: '.btnPrevVert',
	'next'		: '.btnNextVert'
    });
});


$(document).ready(function(){
  $('#thumbs-large-slider1').JOSlider({
	'direction' 	: 'top',
	'big_image_contaner' : '#large-slider-image1',
	'interval'	: 3,
	'prev'		: '.btnPrevVert1',
	'next'		: '.btnNextVert1'
    });
});
*/
