jQuery.fn.serckIndustries = function(options){
	var options = jQuery.extend({
		values: [],
		delay: 10000,
		wrapperWidth: 272,
		colour: ''
	},options);
	
	return this.each(function(){
	
		var transitioning = false;
		var playing = true;
		var current = 0;
	
		//Get the Case Studies
		var casestudies = jQuery(this).children('.industry-study');
		
		var total = casestudies.length;
		
		//Create array to store new case study elements in
		var studies = [];
		
		//Loop through the content and put into casestudy wrapper and insert elements into array
		casestudies.each(function(u){
			var casewrapper = jQuery('<div class="casestudy"></div>'); 
			jQuery(this).contents().appendTo(casewrapper);	
			studies.push(casewrapper);
			jQuery(this).remove();
		});
		
		//Create elements for the loop display
		var homeindustry = jQuery('<div id="home-industry"></div>');
		jQuery(this).children('h3').after(homeindustry);
		
		var slideWrapper = jQuery('<div id="home-industry-wrapper"></div>');
		slideWrapper.appendTo(homeindustry);
		
		var controls = jQuery('<ul id="home-industry-controls"></ul>');
		var prev = jQuery('<li class="prev"><a href="#">Previous</a></li>');
		var next = jQuery('<li class="next"><a href="#">Next</a></li>');
		var playpause = jQuery('<li id="controls"><a class="pause" href="#play">Pause</a></li>');
		var info = jQuery('<li class="info">x of y</li>');
		playpause.appendTo(controls);
		prev.appendTo(controls);
		next.appendTo(controls);
		info.appendTo(controls);
		controls.appendTo(homeindustry);
		
		//Add click events
		prev.click(function(e){
			e.preventDefault();
			if(transitioning == false){
				prevItem();
				if(playing == true){
					pause();				
				}
			}
		});

		next.click(function(e){
			e.preventDefault();
			if(transitioning == false){
				nextItem();
				if(playing == true){
					pause();				
				}
			}
		});
		
		playpause.click(function(e){
			e.preventDefault();
			play_pause();
		});
		
		//Insert the first element
		studies[0].prependTo(slideWrapper);
		updateInfo();

		//Update info

		function updateInfo(){
			var information = (current+1)+" of "+total;
			info.text(information);
		}
		
		//*********Next Prev
		
		function increment(dir){
			current+=dir;
			if(current < 0){
				current = total-1;
			}else if(current >= total){
				current = 0;
			}else{
				current = current;
			}
		}
		
		function getNext(){
			var n = current+1;
			if(n >= total){
				n = 0;
			}
			return n;
		}
		
		function nextItem(){
			increment(1);
			transition('next');
		}
		
		function prevItem(){
			increment(-1);		
			transition('prev');
		}
		
		function transition(dir){
			transitioning = true;
			if(dir == 'next'){
				studies[current].appendTo(slideWrapper);
				slideWrapper.css({'width': options.wrapperWidth*2});				
				slideWrapper.animate({'marginLeft':-272},500, 'linear',
					function(){
						jQuery('.casestudy').first().remove();
						slideWrapper.css({'width':272,'marginLeft':0});
						transitioning = false;
						updateInfo();						
					}
				);
			}
			
			if(dir == 'prev'){
				studies[current].prependTo(slideWrapper);
				slideWrapper.css({'width': options.wrapperWidth*2,'marginLeft':-272});				
				slideWrapper.animate({'marginLeft':0},500, 'linear',
					function(){
						jQuery('.casestudy').last().remove();
						slideWrapper.css({'width':272,'marginLeft':0});
						transitioning = false;
						updateInfo();									
					}
				);
			}
		}
		
		//*************Timer
		
		var myTimer;
		
		startTimer();

		function startTimer(){
			myTimer = setInterval(nextItem,options.delay);
		}
		
		function stopTimer(){
			clearInterval(myTimer);
		}
		
		
		
		
		//************Play Pause Controls
		function play_pause(){
			if(playing == true){
				pause();
			}else{
				play();
			}
		}
		
		function pause(){
			playing = false;
			playpause.children('a').text('Play');
			playpause.children('a').removeClass('pause');
			playpause.children('a').addClass('play');
			stopTimer();
			//Stop Timer
		}
		
		function play(){
			playing = true;
			playpause.children('a').text('Pause');
			playpause.children('a').removeClass('play');
			playpause.children('a').addClass('pause');
			startTimer();
			//Start Timer
		}
		
	})
};
