(function($) {

$.fn.mCycle = function(options) {
			
	function initCycle() {
		
		opts.elps = $(context).children().get();
		
		if(opts.elps.length<=1)
			return;
		
		opts.curSlide = 0;
		
		removeFilter(opts.elps[0], opts);
		
		if($(context).css("position") == "static")
			$(context).css("position", "relative");
			
		var mWidth = (opts.width == "auto")? (($(context).width())? 0 : $(context).width()) : opts.width;
		var mHeight = (opts.height == "auto")? (($(context).height())? 0 : $(context).height()) : opts.height;
		
		var str="";
		
		$(opts.elps).css({position: 'absolute', top:0, left:0}).hide().each(function(i) {
			
			z = opts.elps.length +i;			
			//$(this).css('z-index', z);
				
			if(mWidth<$(this).width())
				mWidth = $(this).width();			
				
			if(mHeight<$(this).height())
				mHeight = $(this).height();

		});		
		
		if(opts.width == "auto")		
			$(context).width(mWidth);
		else if(opts.width)
			$(context).width(opts.width);
			
		if(opts.height == "auto")		
			$(context).height(mHeight);
		else if(opts.height)
			$(context).height(opts.height);

		
		if(opts.prevButton)
			$(opts.prevButton).bind("click", function(){
			
				opts.autoPlay = false;
				
				if(!opts.busy)				
					Play(opts,0);
				
				return false;
			
			}).show();
		
		if(opts.nextButton)
			$(opts.nextButton).bind("click", function(){
			
				opts.autoPlay = false;
				
				if(!opts.busy)				
					Play(opts,1);
				
				return false;
			
			}).show();
		
		$(opts.elps[opts.curSlide]).show();
		
		if(opts.center)
			autoCenter(opts, opts.curSlide);
		
		if(opts.autoPlay)
			opts.Timer = setTimeout(function(){Play(opts, 1);}, opts.delay);
	}	
	
	function Play(opts, fwd){
		
		fwd = (fwd == 0)? fwd : 1;
		
		if(fwd == 1) {
			if(opts.curSlide<opts.elps.length-1)
				opts.nextSlide = opts.curSlide+1;
			else
				opts.nextSlide = 0;		
		} else if(fwd == 0) {
		
			if((opts.curSlide-1)>=0){
				opts.nextSlide = opts.curSlide-1;
			} else {
				opts.nextSlide = opts.elps.length-1;		
			}
		}
		
		if(opts.center)
			autoCenter(opts, opts.nextSlide);
			
		opts.busy = true;
		
		$(opts.elps[opts.curSlide]).animate({opacity:0}, opts.speed, null).hide();
		$(opts.elps[opts.nextSlide]).css({opacity:0}).show().animate({opacity:1}, opts.speed, function(){opts.busy = false;  
			
			if(!$.support.opacity)
				removeFilter(opts.elps[opts.nextSlide], opts);
		});
		
		opts.curSlide = opts.nextSlide;
		
		ButtonUpdate(opts);	

		if(opts.autoPlay && opts.Timer){
		
			opts.Timer = setTimeout(function(){Play(opts, 1)}, opts.delay);
		
		} else if(opts.Timer){
		
			clearTimeout(opts.Timer);
			
			opts.Timer = false;		
		}
	}	
	
	function ButtonUpdate(opts) 
	{	
		if(opts.curSlide<=0) {
		
			$(opts.prevButton).attr("class","PrevArrowN");		
		} 
		else
		{
			$(opts.prevButton).attr("class","PrevArrow");	
		}
		
		if(opts.curSlide==(opts.elps.length-1))
		{
			$(opts.nextButton).attr("class","NextArrowN");
		}
		else
		{
			$(opts.nextButton).attr("class","NextArrow");
		}
	}
	
	function autoCenter(opts, slide)
	{
		if($(opts.elps[slide]).width() == 0 || $(opts.elps[slide]).height() == 0)
			return;
			
		var hCont = $(context).height();
		var wCont = $(context).width();
		
		var LP = ((wCont - $(opts.elps[slide]).width())/2);
		var TP = ((hCont - $(opts.elps[slide]).height())/2);
		
		if(LP>0)
			$(opts.elps[slide]).css("left", LP);
			
		if(TP>0)
			$(opts.elps[slide]).css("top", TP);
	}
	
	function removeFilter(el, opts) {		
		
		if (!$.support.opacity && el.style.filter && opts.clearType) {
			try {el.style.removeAttribute('filter');}
			catch(smother) {}		
		}
	}

	var defaults = {
		
		width:"auto",
		height:"auto",
		clearType:false,
		center:false,
		speed:600, 
		delay:6000,
		nextButton:"#narr",
		prevButton:"#parr",
		autoPlay:true
	};

	var context = $(this);
	
	var opts = $.extend(defaults, options);
	
	if ($.support == undefined) {
		$.support = {opacity: !($.browser.msie)}
	}
	
	if(document.readyState != "complete")		
		return setTimeout(function(){$(context).mCycle(opts)}, 100);
		
		initCycle();
}
})(jQuery);
