(function($){
	//console.log("yeah");
	
	$.fn.mslide = function(options) {
		return this.each(function() {
			var $this = $(this);
				
			// get the slides
			var slides = $(".slide", this);
			var slidecount = slides.length;
			
			$this.append('<ol class="pager"></ol>');
			
			slides.each(function() {
				$(".pager").append('<li><a href="#">' + $(this).index() + '</a></li>');
			})
						
			var isplaying = false;
			playpause();
			var t;
			
			var animated = false;
			
			var currentslide = 0;
			var currentslide = 0;
			
			var images = $("img", this);
			var texts = $(".text", this);
			

			
			$this.append('<div class="imgwrap"></div>');
			$this.append('<div class="textwrap"></div>')
			
			
			images.each(function() {
				$(this).css({
					left: 640
				})
				$(this).appendTo(".imgwrap");
			})
			
			images.eq(0).css({left:0}).addClass("currentslide");
			$(".pager li").eq(0).addClass("current");
			
			
			texts.each(function() {
				$(this).css({
					left: 300
				}).appendTo(".textwrap");
			})
			
			texts.eq(0).css({left: 30}).addClass("currentslide");
			
			
			
			function gotoSlide(nextslide) {
				if(!animated) {
					
					if(isplaying) {
						isplaying = false;
						clearInterval(t);
						playpause();
					}
					
					animated = true;
						if(nextslide<currentslide) {
							//going left
							var previmg = (currentslide==0) ? slidecount-1 : nextslide;


							images.eq(currentslide).animate({
								left: 640
							}, 1200, function() {
								$(this).removeClass("currentslide");
							});

							images.eq(previmg).css({left:-640}).animate({
								left: 0
							}, 1200, function() {
								$(this).addClass("currentslide");
								currentslide = $(this).index();
							});

							texts.eq(currentslide).delay(400).animate({
								left: 300,
								opacity: 0
							}, 1000, function() {
								$(this).css({opacity: 1}).removeClass("currentslide");
							})

							texts.eq(previmg).css({left:-300}).delay(450).animate({
								left: 30
							}, 1000, function() {
								$(this).addClass("currentslide");
								$(".pager .current").removeClass("current");
								$(".pager li").eq(currentslide).addClass("current");
								animated = false;
							})


						}

						else if(nextslide>currentslide) {
							//going right
							var nextimg = (currentslide+1==slidecount) ? 0 : nextslide;

							images.eq(currentslide).animate({
								left: -640
							}, 1200, function() {
								$(this).css({left:640}).removeClass("currentslide");
							})

							images.eq(nextimg).animate({
								left: 0
							}, 1200, function() {
								$(this).addClass("currentslide");
								currentslide = $(this).index();
							});

							texts.eq(currentslide).delay(400).animate({
								left: -300,
								opacity: 0
							}, 1000, function() {
								$(this).css({left:300, opacity: 1}).removeClass("currentslide");
							})

							texts.eq(nextimg).delay(450).animate({
								left: 30
							}, 1000, function() {
								$(this).addClass("currentslide");
								$(".pager .current").removeClass("current");
								$(".pager li").eq(currentslide).addClass("current");
								animated = false;
							})
						}
				}
			

				
			}
			
			function nextSlide() {
				var num = $(".currentslide").index()
				gotoSlide(num+1);
				
			}
			
			function prevSlide() {
				var num = $(".currentslide").index();
				gotoSlide(num-1);
			}
			
			
			
			
			$(".pager li").click(function(e) {
				e.preventDefault();
				gotoSlide($(this).index());
			})
			
			$(document).bind("keyup", function(e) {
				if(e.keyCode==37) {
					prevSlide();
				}
				if(e.keyCode==39) {
					nextSlide();
				}
				if(e.keyCode==80) {
					playpause();
				}
			});
			
			
			function playpause() {
				if(isplaying) {
					isplaying = false;
					clearInterval(t);
				}
				else {					
					isplaying = true;
					t = setInterval(function() {
						nextSlide()
					}, 10000);
				}
				
			}

		});
	}
})(jQuery)
