(function($) {

	$.fn.cubeSlider = function(options) {
	
		var currentslide = 1;
		var cubescount = 0;
		var dimx = 0;
		var dimy = 0;
		var links=[];
		var alts=[];
		var sliderwidth = 0;
		var sliderheight = 0;
		var effects= new Array();
		var animtrigger = false;
		var maininterval;
			
		settings = jQuery.extend({
			cubescountx:5,
			cubescounty:3,
			timer:5000,
			animtimer:500,
			effects:'0,1,2, 3, 4',
			lag:300,
		}, options);
		
		
		var slider = $(this);
		
		effects=settings.effects.split(',');
		
		for (key in effects) {
			effects[key]=parseInt(effects[key]);
		}
		
		sliderwidth=$('img:first',slider).width();
		sliderheight=$('img:first',slider).height();
		
		$('img', slider).each(
			function(){ 
				links.push($(this).attr('src'));
				alts.push($(this).attr('alt'));

				if ($(this).width()<sliderwidth) {sliderwidth=$(this).width()}
				if ($(this).height()<sliderheight) {sliderheight=$(this).height();}
				$(this).hide();
			}
		);
		
		slider.width(sliderwidth);
		slider.height(sliderheight);

		dimx = Math.round(sliderwidth/settings.cubescountx+1);
		dimy = Math.round(sliderheight/settings.cubescounty+1);		
		
		if(dimy*settings.cubescounty>sliderheight){settings.cubescounty=Math.round(sliderheight/dimy); if(dimy*settings.cubescounty<sliderheight){ settings.cubescounty++; }}
		if(dimx*settings.cubescountx>sliderwidth){settings.cubescountx=Math.round(sliderwidth/dimx); if(dimx*settings.cubescountx<sliderwidth){ settings.cubescountx++; }}
		
		cubescount=settings.cubescountx*settings.cubescounty;
		
		
		
		slider.append($('<div class="cubeslidertext"></div>').css({'width':sliderwidth-10}));
		

		
		slider.hover(function(){ $('.cubeslidernext',slider).animate({'opacity':'0.7'}) },function(){ $('.cubeslidernext',slider).animate({'opacity':'0.0'}) });
		slider.hover(function(){ $('.cubesliderprev',slider).animate({'opacity':'0.7'}) },function(){ $('.cubesliderprev',slider).animate({'opacity':'0.0'}) });
		slider.hover(function(){ $('.cubesliderinfo',slider).animate({'opacity':'0.6'}) },function(){ $('.cubesliderinfo',slider).animate({'opacity':'0.0'}) });
		
		
		for(i=0; i<settings.cubescounty; i++){
			for(j=0; j<settings.cubescountx; j++){
				slider.append($('<div class="cubeslidercube ' +'i'+i+'j'+j+'"></div>').css({
					left:j*dimx, 
					top: i*dimy, 
					background: 'url('+links[0]+') no-repeat',
					backgroundPosition: -j*dimx+'px '+ -i*dimy +'px', width: dimx, height: dimy
				}));
			}
		}
		
		$('.cubeslidertext', slider).html(alts[0]);
		if (alts[0]=="") {$('.cubeslidertext', slider).hide()}
		slider.css({background: 'url('+links[1]+') no-repeat top left'});

		if ((settings.lag*cubescount+settings.animtimer)>settings.timer-500) { settings.timer=settings.lag*cubescount+settings.animtimer+settings.timer}

		maininterval = setInterval(function(){anim(Math.floor(Math.random()*effects.length))},settings.timer);
		
		$('.cubeslidernext', slider).live('click', 
			function(){
				if(animtrigger==false){
					clearInterval(maininterval);
					anim(Math.floor(Math.random()*effects.length));
					maininterval = setInterval(function(){anim(Math.floor(Math.random()*effects.length))},settings.timer);
				}
			}
		);
		
		$('.cubesliderprev', slider).live('click', 
			function(){
				if(animtrigger==false){
					clearInterval(maininterval);
					
					currentslide=currentslide-2;
					if(currentslide==-1){currentslide=links.length-1}
					if(currentslide==-2){currentslide=links.length-2}
					slider.css({background: 'url('+links[currentslide]+') no-repeat top left'});
					
					anim(Math.floor(Math.random()*effects.length));
					maininterval = setInterval(function(){anim(Math.floor(Math.random()*effects.length))},settings.timer);
				}
			}
		);
		
		function initcubes(link, prev){
			for(i=0; i<settings.cubescounty; i++){
				for(j=0; j<settings.cubescountx; j++){
					$('.i'+i+'j'+j,slider).css({
						left:j*dimx, 
						top: i*dimy, 
						background: 'url('+links[link]+') no-repeat',
						backgroundPosition: -j*dimx+'px '+ -i*dimy +'px', 
						width: dimx, 
						height: dimy, 
						opacity:1,
						zIndex:2
					});
				}
			}
			if (link==links.length-1){
				slider.css({background: 'url('+links[0]+') no-repeat top left'});
				currentslide=0;
			} else {
				slider.css({background: 'url('+links[link+1]+') no-repeat top left'})
				currentslide=link+1;
			}
		}
				
		function anim(effect){
			var i=0;
			var j=0;
			animtrigger=true;
			$('.cubeslidertext', slider).slideUp();
			$('.cubesliderinfo', slider).html((currentslide+1)+'/'+links.length);
			var effectfunction= {};
			var interval = setInterval(function(){
				if (effects[effect]==0) { effectfunction={'opacity': '0'} } else
				if (effects[effect]==1) { effectfunction={'width': '0px'} } else
				if (effects[effect]==2) { effectfunction={'height':'0px'} } else
				if (effects[effect]==3) { effectfunction={'width':'0px', 'opacity': '0'} } else
				if (effects[effect]==4) { effectfunction={'height':'0px', 'opacity': '0'} }
				$('.i'+i+'j'+j,slider).animate(effectfunction, settings.animtimer, 
					function() {
						if($(this).hasClass('i'+(settings.cubescounty-1)+'j'+(settings.cubescountx-1))){ 
							if(alts[currentslide]!=''){$('.cubeslidertext', slider).html(alts[currentslide]).slideToggle(); }
							initcubes(currentslide,false);
							animtrigger=false;
						}
					}
				);
			j++;	
			if (j==settings.cubescountx) {j=0; i++; if(i==settings.cubescounty){clearInterval(interval)}}
			}, settings.lag)  
		}
		
		return this;
	};

	$.fn.reverse = [].reverse;

})(jQuery);

