(function($) {
	$.fn.sliderBox = $.fn.sliderbox = function(speed){
		initSliderBox = function(el, speed)
		{
			el.content = $(el).children("ul");
			el.first = el.currentbox = 0;
			el.last = el.content.children("li").size() - 1;
			el.speed = speed;
			
			$(el).css("width", "100%").css("overflow", "hidden").css("position", "relative");

			buildNav(el);
						
			el.content.prepend("<li></li>")
			el.content.children("li").css("width", "5%").css("float", "left").css("list-style", "none").css("display","block");;
			el.content.css("width", "2000%").css("position", "relative").css("z-index", "1").css("display","block").css("padding-left", "0").css("left", '-100%');
			

			
			
			el.status_bar.css('width', '100px');
			el.status_bar.children().css('width', '100px');

			bar_anim(el);
			$(el).hover(function(){el.status_bar.children('div').stop(true, false);},function(){bar_anim(el);});
		}
		
		buildNav = function(el)
		{
			var children = el.content.children("li");
			var output_text = '<div class=\"sliderboxNav\"><div class="sliderboxNavMain">'
			output_text += '<a href="javascript:goNav($(\'#'+$(el).attr('id') +'\')[0] , \'left\')">&laquo;</a> '
			$.each(children, function(index, box){
				output_text += '<a href="javascript:showBox($(\'#'+$(el).attr('id') +'\')[0] , \''+ index + '\')" class="direct_buttons">'+ (index + 1) +'</a> '
			});
			output_text += '<a href="javascript:goNav($(\'#'+$(el).attr('id') +'\')[0] , \'right\')">&raquo;</a></div><div class = "progress_box"><div class="progress_bar">&nbsp;</div></div></div> '
			$(el).append(output_text);
			$(el).children(".sliderboxNav").children('.sliderboxNavMain').children('.direct_buttons').filter(":eq(0)").addClass('active_icon');
			el.status_bar = $(el).children('.sliderboxNav').children('.progress_box');
		}
		
		bar_anim = function(el)
		{
			var rem_width = parseInt(el.status_bar.children('div').width());
			el.status_bar.children('div').animate({"width" : "2px"},
				{"duration": (rem_width * 100), "complete": function(){
					setTimeout(function(){goNav(el, 'right');}, 10);
			}});
		}
		
		goNav = function(el, direction)
		{
			if (direction == 'left'){
				if (el.currentbox > el.first){
					showBox(el, el.currentbox - 1);
				}else{
					showBox(el, el.last);
				}
			} else {
				if (el.currentbox < el.last){
					showBox(el, el.currentbox + 1);
				}else{
					showBox(el, el.first);
				}
			}
		}

		showBox = function(el,number)
		{	
			el.status_bar.children('div').stop(true, false);
			anim_string = (number == 0) ? '-100%' : ("-" + ((number * 100)+100) + "%");
			el.content.animate({"left": anim_string}, {"easing": 'swing', "duration": el.speed, 'complete': function(){
				el.status_bar.children('div').css('width', '100px');
				bar_anim(el);
			}});

			el.currentbox = parseInt(number);
			
			$(el).children(".sliderboxNav").children('.sliderboxNavMain').children().removeClass('active_icon');
			$(el).children(".sliderboxNav").children('.sliderboxNavMain').children('.direct_buttons').filter(":eq("+(number)+")").addClass('active_icon');
			
		}
		this.each(
			function()
			{
				if(this.nodeName.toLowerCase()!= "div") return;
				initSliderBox(this, speed);
			}
		)
		return this;
		
	}
})(jQuery);