/*!
 * mCarousel jQuery Plugin v1.0
 * Copyright 2011, pit69
 */
(function($){
	$.fn.mcarousel = function() {
		var mc = this;
		var actIs = false;
		var opt = {
			name: 'mcarousel',
			wWrap: 0,
			hWrap: 520,
			wZoom: 500,
			hZoom: 500,
			wElem: 230,
			hElem: 230,
			mTime: 250,
			zTime: 100
		}
		
		if (mc.children('li').length > 1) {
			mcInit();
			$(window).bind('resize', mcSetPos);
		}
		
		function mcInit() {
			mc.attr('id', opt.name)
				.wrap('<div id="'+opt.name+'-wrap"></div>')
				.append('<div id="'+opt.name+'-prev"></div><div id="'+opt.name+'-next"></div>');
			
			mc.parent().height(opt.hWrap);
			mc.children('li').attr('class', opt.name+'-elem').css({'width': opt.wElem, 'height': opt.hElem});
			
			mc.children('li:eq(0)').attr('id', opt.name+'-left').css('z-index', 2);
			mc.children('li:eq(1)').attr('id', opt.name+'-center').css('z-index', 3)
			mc.children('li:eq(2)').attr('id', opt.name+'-right').css('z-index', 2);
			
			$('#'+opt.name+'-prev').bind('click', mcPrev);
			$('#'+opt.name+'-next').bind('click', mcNext);
			
			mcSetPos();
		}
		
		function mcSetPos() {
			opt.wWrap = $('#'+opt.name+'-wrap').width();
			
			$('.'+opt.name+'-elem').css({
				'left': opt.wWrap,
				'top': parseInt(0.5*(opt.hWrap - opt.hElem)) + 10
			});
			
			$('#'+opt.name+'-left').css({
				'left': parseInt(0.5*(opt.wWrap - 3*opt.wElem)) - 170
			});
			
			$('#'+opt.name+'-center').css({
				'width': opt.wZoom,
				'height': opt.hZoom,
				'left': parseInt(0.5*(opt.wWrap - opt.wZoom)),
				'top': parseInt(0.5*(opt.hWrap - opt.hZoom))
			});
			
			$('#'+opt.name+'-right').css({
				'left': parseInt(0.5*(opt.wWrap + opt.wElem)) + 170
			});
			
			$('#'+opt.name+'-prev').css({
				'left': 50,
				'top': parseInt(0.5*opt.hZoom)
			});
			
			$('#'+opt.name+'-next').css({
				'left': parseInt(0.5*opt.wWrap + opt.wZoom)+40,
				'top': parseInt(0.5*opt.hZoom)
			});
		}
		
		function mcPrev() {
			if (!actIs) {
				actIs = true;
				
				$('#'+opt.name+'-center')
					.animate({
						'width': opt.wElem,
						'height': opt.hElem,
						'left': parseInt(0.5*(opt.wWrap - opt.wElem)),
						'top': parseInt(0.5*(opt.hWrap - opt.hElem)) + 10
					}, opt.zTime, 'swing', function(){
						
						mc.children('li').last()
							.insertBefore(mc.children('li').first())
							.css({ 'left': -opt.wElem, 'z-index': 2 })
							.animate({
								'left': parseInt(0.5*(opt.wWrap - 3*opt.wElem)) - 170
							}, opt.mTime, 'swing', function(){ $(this).attr('id', opt.name+'-left'); });
						
						$('#'+opt.name+'-left')
							.css('z-index', 3)
							.animate({
								'left': parseInt(0.5*(opt.wWrap - opt.wElem))
							}, opt.mTime, 'swing', function(){ 
								$(this)
									.attr('id', opt.name+'-center')
									.animate({
										'width': opt.wZoom,
										'height': opt.hZoom,
										'left': parseInt(0.5*(opt.wWrap - opt.wZoom)),
										'top': parseInt(0.5*(opt.hWrap - opt.hZoom))
									}, opt.zTime, 'swing', function(){ actIs = false; });
							})
						
						$(this)
							.css('z-index', 2)
							.animate({
								'left': parseInt(0.5*(opt.wWrap + opt.wElem)) + 170
							}, opt.mTime, 'swing', function(){ $(this).attr('id', opt.name+'-right'); })
						
						$('#'+opt.name+'-right')
							.css('z-index', 1)
							.animate({
								'left': opt.wWrap + 170
							}, opt.mTime, 'swing', function(){ $(this).removeAttr('id'); });
						
					})
			}
		}
		
		function mcNext() {
			if (!actIs) {
				actIs = true;
				
				$('#'+opt.name+'-center')
					.animate({
						'width': opt.wElem,
						'height': opt.hElem,
						'left': parseInt(0.5*(opt.wWrap - opt.wElem)),
						'top': parseInt(0.5*(opt.hWrap - opt.hElem)) + 10
					}, opt.zTime, 'swing', function(){
						
						$('#'+opt.name+'-right').next()
							.css({ 'left': opt.wWrap, 'z-index': 2 })
							.animate({
								'left': parseInt(0.5*(opt.wWrap + opt.wElem)) + 170
							}, opt.mTime, 'swing', function(){ $(this).attr('id', opt.name+'-right'); });
						
						$('#'+opt.name+'-right')
							.css('z-index', 3)
							.animate({
								'left': parseInt(0.5*(opt.wWrap - opt.wElem))
							}, opt.mTime, 'swing', function(){ 
								$(this)
									.attr('id', opt.name+'-center')
									.animate({
										'width': opt.wZoom,
										'height': opt.hZoom,
										'left': parseInt(0.5*(opt.wWrap - opt.wZoom)),
										'top': parseInt(0.5*(opt.hWrap - opt.hZoom))
									}, opt.zTime, 'swing', function(){ actIs = false; });
							})
						
						$(this)
							.css('z-index', 2)
							.animate({
								'left': parseInt(0.5*(opt.wWrap - 3*opt.wElem)) - 170
							}, opt.mTime, 'swing', function(){ $(this).attr('id', opt.name+'-left'); })
						
						$('#'+opt.name+'-left')
							.css('z-index', 1)
							.animate({
								'left': -opt.wElem - 170
							}, opt.mTime, 'swing', function(){ $(this).removeAttr('id'); })
							.insertAfter(mc.children('li').last());
						
					})
			}
		}
	}
})(jQuery);
