(function($){
$.fn.mCarousel = function(config) {
    // set o
    var defaults = {
        width: 700,
        height: 500,
        next: false,
        prev: false,
		pause: false,
		play: false,
        vertical: false,
        auto: false,
        fade: false,
        current: 0,
        items: 0,
        slidespeed: 600,
        visible: 1,
        pagination: false
    };
    var o = $.extend(defaults, config);
	var speedS = o.auto;
    
    // Configure carousel ul and li
    var ul = $(this);
    var li = ul.children('li');
    
    o.items = li.length;
    
    var height = o.height;
    var width = o.width;
    if(o.visible>1) {
        if(o.vertical)
            height = height*o.visible;
        else
            width = width*o.visible;
    }
    
    ul.wrap('<div class="mcarouselCntr" style="width:'+width+'px;height:'+height+'px;overflow:hidden">');
    var container = ul.parent('.mcarouselCntr');
    if(!o.vertical) {
        ul.width(o.items*o.width);
        ul.height(o.height);
    } else {
        ul.width(o.width);
        ul.height(o.items*o.height);
    }
    ul.css('overflow','hidden');
    
    li.each(function(i,item) {
        $(item).width(o.width);
        $(item).height(o.height);
        if(!o.vertical)
            $(item).css('float','left');
    });
    
    // function for sliding the carousel
    var slide = function(dir, click) {
        if(typeof click == "undefined" & o.auto==false)
            return;
    
        if(dir=="next") {
            o.current += o.visible;
            if(o.current>=o.items)
                o.current = 0;
        } else if(dir=="prev") {
            o.current -= o.visible;
            if(o.current<0)
                o.current = (o.visible==1) ? o.items-1 : o.items-o.visible+(o.visible-(o.items%o.visible));
        }else if(dir=="pause") {
            o.current == o.visible;
        } else {
            o.current = dir;
        }
        
        // set pagination
        if(o.pagination != false) {
            container.next('.pagination').find('.mcarouselPag').find('li').removeClass('mcarouselPagActive')
            container.next('.pagination').find('.mcarouselPag').find('li:nth-child('+(o.current+1)+')').addClass('mcarouselPagActive');
        }
        
        // fade
        if(o.fade!=false) {
            ul.fadeOut(o.fade, function() {
                ul.css({marginLeft: -1.0*o.current*o.width});
                ul.fadeIn(o.fade);
            });
            
        // slide
        } else {
            if(!o.vertical)
                ul.animate( {marginLeft: -1.0*o.current*o.width}, o.slidespeed );
            else
                ul.animate( {marginTop: -1.0*o.current*o.height}, o.slidespeed );
        }
        
        if(typeof click != "undefined")
            o.auto = false;
        
        if(o.auto!=false)
            setTimeout(function() {
                slide('next');
            }, o.auto);
    }
    
    // include pagination
    if(o.pagination != false) {
        container.after('<ul class="pagination"><li class="prev"></li><li class="mainPag"><ul class="mcarouselPag"></ul></li><li class="pause"></li><li class="next"></li></ul>');
        var pagination = container.next('.pagination').find('.mcarouselPag');
        for(var i=0;i<o.items;i++) {
            if(i==0)
                pagination.append('<li class="mcarouselPagActive"></li>');
            else
                pagination.append('<li></li>');
        }
        
        pagination.find('li').each(function(index, item) {
            $(this).click(function() {
                slide(index,true);
            });
        });
    }
        
    // set event handler for next and prev
    if(o.next!=false){
        o.next.live('click',function() {
            slide('next',true);
        });
        }
        
    if(o.prev!=false){
        o.prev.live('click',function() {
            slide('prev',true);
        });
		}
    if(o.pause!=false){
        $(o.pause).live('click',function() {
		$(this).removeClass('pause').addClass('play');
			o.auto = false;
        });
		}
	if(o.play!=false){
        $(o.play).live('click',function() {
			$(this).removeClass('play').addClass('pause');
			o.auto = speedS;
				setTimeout(function() {
					slide('next');
				}, o.auto);
			});
		}
		
    // start auto sliding
    if(o.auto!=false){
        setTimeout(function() {
            slide('next');
        }, o.auto);
	}

}
})(jQuery);
