$.noConflict();
jQuery(document).ready(function($) {
	
		$.fn.jh_slider = function(options) {

			// Create some defaults, extending them with any options that were provided
		    var settings = $.extend( {
		      "animation":"scroll",
		      "duration" :700,
		      "continuous":true,
		      "static_item_count":true,
		      "per_page":3,
		      "is_auto":false
		    }, options);
			
			var object = this.each(function(slide_index) {
				
				// Setting varibles 		
				var slider = $(this).find("ul")
					total_items = slider.children("li").length,
					item_width = 750,
					current_index = 0,
					next_index = settings.per_page,
					prev_index = 1;
					highest_next_index = total_items,
					lowest_prev_index = 1;
					
				settings.per_page = total_items;
				
				// E.g. there are 5 slide items but 4 per age along with continious scrolling
				if(settings.static_item_count === false){
					$(slider.find('li')).each(function(item_index){
						$(this).attr("id", 'slide_'+slide_index+'_item_'+(item_index+1));
					});				
				};
				
				
				
				// Set up		
				slider.addClass("slider");				
				
				// Button click event handler
				$(this).find(".btn").each(function(button_index) {
    				
    				var button = $(this);
    				
    				button.click(function(e){
    					e.preventDefault();
    					
    					if(button.hasClass("next")){
    						current_index++;
    						object[slide_index].startSlide("next");
    					}else{
    						current_index--;
    						object[slide_index].startSlide("prev");
    					};
    					
    				});
    				
				});
				
				this.startSlide = function(action){
					if(settings.static_item_count === false){
						object[slide_index].cloneItems(action);
					}

					if(action === "next"){
						slider.animate({"left":"-="+item_width+"px"}, settings.duration);
					}else{
						slider.animate({"left":"+="+item_width+"px"}, settings.duration);
					};
					
				};
				
				this.cloneItems = function(action){
				
					var clone_index,
						clone_object;
					
					if(action === "next"){							
						next_index++;
						prev_index++;
						
						clone_index = next_index - total_items;
						clone_object = $('#slide_'+slide_index+'_item_'+clone_index).clone();
						
						if(next_index > highest_next_index){
							slider.append(clone_object.attr("id", 'slide_'+slide_index+'_item_'+next_index));
							highest_next_index++;
						};
					}else{
						prev_index--;
						next_index--;
						
						clone_index = prev_index + total_items;
						clone_object = $('#slide_'+slide_index+'_item_'+clone_index).clone();
						
						if(prev_index < lowest_prev_index){
							slider.prepend(clone_object.attr("id", 'slide_'+slide_index+'_item_'+prev_index));
							slider.animate({"left":"-="+item_width+"px"}, 0);
							lowest_prev_index--;
						};
					};	
					
				}	
				
				//TODO - Ask Dave for help with running this (object = undefined)
				if(settings.is_auto){					
					window.setInterval(function(){
						object[slide_index].startSlide("next");
					}, 5500);
				}
				
				/*this.autoSlide = function(){
					window.setInterval(function(){
						object[slide_index].startSlide("next");
					}, 5000);					
				}*/	
				
			});
			
			return this;

		};
	
	$(".slider-container").jh_slider({
		"static_item_count":false,
		"is_auto":true
	});

});
