(function($) {
$.tmnSlider = function(el, options) {
    //global params
	var me = this;
    me.el = $(el);
	me.timer = null;
	me.pause = false;
	
    me.init = function() {
        me.options = $.extend({},$.tmnSlider.defaults, options);
		//set slide elems
		me.singleSlide  = me.el.children('div');
		me.preview = me.singleSlide.children('.preview');
		me.view = me.singleSlide.children('.view');
		//init single slides
		me.neededSlide = me.options.openSlide - 1;
		me.singleSlide.eq(me.neededSlide).addClass('open').children('.preview').width(0);
		me.singleSlide.filter(':last').addClass('last');
		//set in-line width for slide elems
		me.setViewWidth();
		//add events
		me.el.hover(function(){me.pause = true},function(){me.pause = false});
		me.preview.hover(
			function(){
				$(this).find('.title').stop(true,true).fadeOut(me.options.slideTime/2).siblings('.expand').stop(true,true).fadeIn(0).stop(true,true).animate({right: 0},me.options.slideTime/3)
			},
			function(){
				$(this).find('.title').stop(true,true).fadeIn(me.options.slideTime/2).siblings('.expand').stop(true,true).fadeOut(me.options.slideTime/2).css({right:me.options.previewWidth,display:'block'})
			}
		);
		me.preview.click(function(){me.slide($(this).parent().index())});
		//autoplay
		if(me.options.playing)	me.autoPlay()
    }	
	me.slide = function(index){
		if(me.isAnimated(me.view)){
			
			me.setOpen(index);
			
			me.singleSlide.not('.open').stop(true,true).animate({width: me.options.previewWidth},me.options.slideTime);
			me.singleSlide.filter('.open').stop(true,true).animate({width: me.options.viewWidth},me.options.slideTime)
		}
	}
	me.setOpen = function(index){
		me.singleSlide.removeClass('open').children('.preview').animate({width:me.options.previewWidth},me.options.slideTime/2);
		me.singleSlide.eq(index).addClass('open').children('.preview').animate({width:0},me.options.slideTime/2)
	}

	me.setViewWidth = function(){
		me.singleSlide.each(function(){
			var _this = $(this);
			_this.width(_this.width())
		})
	}

	me.isAnimated = function(elem){return elem.is(':animated') ? false : true}
	
	me.autoPlay = function(){
		me.timer = window.setInterval(function(){
			if(!me.pause){
				me.neededSlide = me.singleSlide.filter('.open').index();
				if(me.neededSlide >= me.singleSlide.length - 1 || me.neededSlide < 0) me.neededSlide = -1;
				me.singleSlide.eq(me.neededSlide+1).children('.preview').trigger('click')
			}
		},me.options.autoPlayTime);
	}
	me.autoPlayStop = function(){window.clearInterval(me.timer)}
    me.init()
}
$.tmnSlider.defaults = {
	slideTime: 400,
	autoPlayTime: 5000,
	playing: true,
	openSlide: 2,
	previewWidth: 115,
	viewWidth: 587
}
$.fn.tmnSlider = function(options){return this.each(function(){new $.tmnSlider(this, options)})}
})(jQuery);
