//REEL
$(document).ready(function(){

	//Get total
	totalReel = $('#slides ul li').size();
	
	//Set random, have to +1 so we don't get 0
	currentReel = 1+(Math.floor(Math.random()*totalReel));
	
	//Find xPos with random num, -1 to account for the levoiously mentioned +1
	xPos = 0 - ((currentReel-1)*600);
	
	//Set new position and show (Otherwise we get a flash of content)
	$('#slides ul').show().css({left:xPos});
	
	//Uncomment to remove randomness
	//xPos = 0;
	//currentReel = 1;
	moose = true;
	autoMoose = true;
	reelDelay = 4000;
	
	$('#sipky a').show().css({opacity:0.6});
	
	$('#sipky .pravo a').click(function(){
		if(moose==true && totalReel>currentReel){
			xPos = xPos - 600;
			currentReel = currentReel + 1;
			slideTo(xPos);
		}else if(moose==true){
			currentReel = 1;
			infiniteLoop('right');
		}
		return false;
	});
	
	$('#sipky .levo a').click(function(){
		if(moose==true && currentReel>1){
			xPos = xPos + 600;
			currentReel = currentReel - 1;
			slideTo(xPos);
		}else if(moose==true){
			currentReel = totalReel;
			infiniteLoop('left');
		}
		return false;
	});
	
	function infiniteLoop(dir){
		
		moose = false;
		
		totalX = 600 - (770 * totalReel);
		
		if(dir == 'left'){
			$('#slides ul').prepend('<li>'+$('#slides ul li:last').html()+'</li>').css({left:-600}).animate({left:0},600,'easeInOutExpo',function(){
				$('#slides ul').css({left:totalX});
				$('#slides ul li:first').remove();
				moose = true;
			});
			xPos = totalX;
		}else if(dir == 'right'){
			$('#slides ul').append('<li>'+$('#slides ul li:first').html()+'</li>').animate({left:totalX-600},600,'easeInOutExpo',function(){
				$('#slides ul').css({left:0});
				$('#slides ul li:last').remove();
				moose = true;
			});
			xPos = 0;
		}
	}
	
	$('#intro').hover(function(){
		$('#sipky a').stop([]).animate({opacity:1},300);
		autoMoose = false;
		$(this).stop([]);
	},function(){
		$('#sipky a').stop([]).animate({opacity:0.6},300);
		autoMoose = true;
		$(this).stop([]);
		autoMagical();
	});
	
	function autoMagical(){
		$('#intro').animate({opacity:1},reelDelay,function(){
			if(autoMoose == true){
				$('#sipky .pravo a').click();
			}
			autoMagical();
		});
	}
	
	autoMagical();
	
	function slideTo(newX){
		moose = false;
		$('#slides ul').animate({left:newX},600,'easeInOutExpo',function(){
			moose = true;
		})
	}	

//END REEL
});

//SIMPLE PAUSE FUNCTION

$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
		var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});
};

$.fn.clearQueue = $.fn.unpause = function(type) {
	return this.each(function(){
		type = type || "fx";
		if(this.queue && this.queue[type]) {
			this.queue[type].length = 0;
		}
	});
};

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});

