(function($){


//$(document).ready(function(){
	
	var timer = 0;
	
	
   	$.fn.loadFlow = function() {
		$(document).ready(function(){$(window).theFlow();});
	}
	
	$.fn.theFlow = function() {
	
	$('#divFlow .item').each(function(index){
   		
   		//Add in the appropriate styles
   		switch(index){
   			case 0:
   				//$(this).css({ width: '240px', height: '101px', top: '50px', left: '0px', zIndex: '600', display: 'block' });
   				$(this).addClass('level3Left');
   			break;
   			case 1:
   				//$(this).css({ width: '410px', height: '173px', top: '68px', left: '34px', zIndex: '800', display: 'block' });
   				$(this).addClass('level2Left');
   			break;
   			case 2:
   				//$(this).css({ width: '711px', height: '300px', top: '84px', left: '115px', zIndex: '1000', display: 'block' });
   				$(this).addClass('level1');
   			break;
   			case 3:
   				//$(this).css({ width: '410px', height: '173px', top: '68px', left: '500px', zIndex: '800', display: 'block' });
   				$(this).addClass('level2Right');
   			break;
   			case 4:
   				//$(this).css({ width: '240px', height: '101px', top: '50px', left: '710px', zIndex: '600', display: 'block' });
   				$(this).addClass('level3Right');
   			break;
   			case 5:
   				//$(this).css({ width: '240px', height: '101px', top: '50px', left: '710px', zIndex: '600', display: 'none' });
   				$(this).addClass('level4');
   			break;
   			
   		}
   		
   	});
	
	
	//Initiate the timer
	setInMotion();
	
	//Add link functionality
	addLinks();
   	
   	/*
   	* Initial Functionality for the left and right buttons, all the non-active items
   	*/
	function addLinks(){
		$('.moveleft').bind('click',function(){
			killlinks();
			moveLeft(1);
			clearInterval(timer);
			setInMotion();
		});
		//
		$('.moveright').bind('click',function(){
			killlinks();
			moveRight(1);
			clearInterval(timer);
			setInMotion();
		});
		//
		$('.level3Left').bind('click',function(){
			killlinks();
			moveRight(2);
			clearInterval(timer);
			setInMotion();
			return false;
		});
		//
		$('.level2Left').bind('click',function(){
			killlinks();
			moveRight(1);
			clearInterval(timer);
			setInMotion();
			return false;
		});
		//
		$('.level2Right').bind('click',function(){
			killlinks();
			moveLeft(1);
			clearInterval(timer);
			setInMotion();
			return false;
		});
		//
		$('.level3Right').bind('click',function(){
			killlinks();
			moveLeft(2);
			clearInterval(timer);
			setInMotion();
			return false;
		});
	}
   	
   	/*
	* Kill all links for over zealous users
	*/
	function killlinks(){
		$('#divFlow .item').each(function(){
			$(this).unbind();
		});
		$('.moveleft').unbind();
		$('.moveright').unbind();
	}
	
	/*
	* Add in timer to move flow every 5 seconds
	*/
	function setInMotion(){
		timer = setInterval(function(){
			killlinks();
			moveRight(1);
		},5000);
	}
   	
   	/*
   	* Move everything left
   	*/
   	function moveLeft(moves){
   		//Moving left... remove the item on the left
   		$('.level3Left').animate({opacity: 0}, 100, function(){
   			$(this).css({ width: '240px', height: '101px', top: '25px', left: '710px' });
   			$(this).removeClass('level3Left');
   			$(this).addClass('level4');
   			$(this).css({zIndex:199});
   			//$(this).unbind('click');
   		});
   		
   		//level2Left -> level3Left
   		$('.level2Left').animate({left:0, top:25, width:240, height:101}, 150, function(){
   			$(this).removeClass('level2Left');
   			$(this).addClass('level3Left');
   			$(this).css({zIndex:200});
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				moveRight(2);
   				return false;
   			});
			*/
   		});
   		
   		//level1 -> level2Left
   		$('.level1').animate({left:34, top:43, width:410, height:173}, 150, function(){
   			$(this).removeClass('level1');
   			$(this).addClass('level2Left');
   			$(this).css({zIndex:300});
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				moveRight(1);
   				return false;
   			});
			*/
   			//Check if the reg button exists
   			if($(this).find('.reg').length>0){
   				//hide it
   				$('.reg').css('display','none');
   			}
   		});
   		
   		//level2Right -> level1
   		$('.level2Right').css({zIndex:400});
   		$('.level2Right').animate({left:115, top:59, width:711, height:300}, 450, function(){
   			$(this).removeClass('level2Right');
   			$(this).addClass('level1');
   			/*$(this).unbind('click');
			$(this).click(function(){
   				return true;
   			});
			*/
   			//Check if the reg button exists
   			if($(this).find('.reg').length>0){
   				//display it
   				$('.reg').css('display','block');
   			}
   		});
   		
   		//level3Right -> level2Right
   		$('.level3Right').animate({left:500, top:43, width:410, height:173}, 450, function(){
   			$(this).removeClass('level3Right');
   			$(this).addClass('level2Right');
   			$(this).css({zIndex:300});
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				moveLeft(1);
   				return false;
   			});
			*/
   		});
   		
   		//level4 -> level3Right
   		$('.level4').animate({opacity:1, left:710, top:25, width:240, height:101}, 600, function(){
   			$(this).removeClass('level4');
   			$(this).addClass('level3Right');
   			$(this).css({zIndex:200});
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				moveLeft(2);
   				return false;
   			});
			*/
   			if(moves == 2){
   				moveLeft(1);
   			}else{
				//Rebind clicks
				addLinks();
			}
   		});
   	}
   	
   	/*
   	* Move everything right
   	*/
   	function moveRight(moves){
   		//Moving right... remove the item on the right
   		$('.level3Right').animate({opacity: 0}, 100, function(){
   			$(this).css({ width: '240px', height: '101px', top: '25px', left: '710px' });
   			$(this).removeClass('level3Right');
   			$(this).addClass('level4');
   			$(this).css({zIndex:199});
   			//$(this).unbind('click');
   		});
   		
   		//level2Right -> level3Right
   		$('.level2Right').animate({left:710, top:25, width:240, height:101}, 150, function(){
   			$(this).removeClass('level2Right');
   			$(this).addClass('level3Right');
   			$(this).css({zIndex:200});
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				moveLeft(2);
   				return false;
   			});
			*/
   		});
   		
   		//level1 -> level2Right
   		$('.level1').animate({left:500, top:43, width:410, height:173}, 150, function(){
   			$(this).removeClass('level1');
   			$(this).addClass('level2Right');
   			$(this).css({zIndex:300});
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				moveLeft(1);
   				return false;
   			});
			*/
   			//Check if the reg button exists
   			if($(this).find('.reg').length>0){
   				//hide it
   				$('.reg').css('display','none');
   			}
   		});
   		
   		//level2Left -> level1
   		$('.level2Left').css({zIndex:400});
   		$('.level2Left').animate({left:115, top:59, width:711, height:300}, 450, function(){
   			$(this).removeClass('level2Left');
   			$(this).addClass('level1');
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				return true;
   			});
			*/
   			//Check if the reg button exists
   			if($(this).find('.reg').length>0){
   				//display it
   				$('.reg').css('display','block');
   			}
   		});
   		
   		//level3Left -> level2Left
   		$('.level3Left').animate({left:34, top:43, width:410, height:173}, 550, function(){
   			$(this).removeClass('level3Left');
   			$(this).addClass('level2Left');
   			$(this).css({zIndex:300});
   			//$(this).unbind('click');
			/*
   			$(this).click(function(){
   				moveRight(1);
   				return false;
   			});
			*/
   		});
   		
   		//level4 -> level3Left
   		$('.level4').css({left:0, top:25, width:240, height:101});
   		$('.level4').animate({opacity:1}, 600, function(){
   			$(this).removeClass('level4');
   			$(this).addClass('level3Left');
   			$(this).css({zIndex:200});
   			//$(this).unbind('click');
   			/*
			$(this).click(function(){
   				moveRight(2);
   				return false;
   			});
			*/
   			if(moves == 2){
   				moveRight(1);
   			}else{
				//Rebind clicks
				addLinks();
			}
   		});
   	}
	
}
   	
//});

})(jQuery);
