

$.fn.kBackground = function( ops ){

		if( !ops ) { var ops = {}; }
		
		var option = {};
		
		option.step = 0;
		option.count = 0;
		option.flag = 0;
		option.obj = $(this);
		option.topZero = 1;
		option.type = ops['type'] || "full";
		option.auto = ops['type'] || "true";
		option.loader = ops['loader'] || "true";
		option.delay = ops['delay'] || 10000;
		option.speed = ops['speed'] || 500;
		option.images = '';
		option.timed;
		option.newImagesFlag = 0;
		option.toggle = 0;
		option.slideShow = $("#slideShow");
		option.perScroll = 0;
		option.timeout = '';
		option.click = "false"; // custom by Tarvi Raun
		option.no_loader == "true";

		
		/* Count the images */
			option.count_images = function( obj ){
				
				option.images = option.obj.find("img");
				option.thumbnails = '';
				option.images.each(function(){
										
					if( $(this).attr("class") !== "active" ){				 
						option.count++;
						if( $(this).attr("class") == "newImage" ){
							$(this).removeClass("newImage");
						};
						$(this).attr("rel", option.count);
						
						option.thumbnails+='<div class="item" rel="'+option.count+'"><img src="'+$(this).attr("thumb")+'" /></div>';
					}
					else{
						$(this).removeAttr("rel");
					};
				});
				
				
				option.slideShow.find(".scroller").html(option.thumbnails).css("width", option.count*55);
				
				option.perScroll = 430/ (option.count-7);
				if( option.count < 8 ){
					
					option.slideShow.find("#drag").css({
						width: "100%",
						left:0
					});
				}
				else{
					option.slideShow.find("#drag").css({
						width: option.perScroll,
						left:0
						
					});
				}
				
				option.scrollClicks();
				
			};
			
			option.removeOldImages = function(){
				option.images.each(function(){
					var classy = $(this).attr("class");
					if( classy !== "active" && classy !== "newImages" ){ $(this).remove();}
				});
			};
		
		/*  To maximize full screen layer  */
			option.do_resize = function( obj ){
				
				obj.css({
										
					height: $(window).height(),
					width: $(window).width()
											
				});
			
			}
			
			
		/* To maximize the image */
			option.img_size = function( obj ){
				var o = {};
				o.winW = $(window).width();
				o.winH = $(window).height();
				o.imgW = obj.width();
				o.imgH = obj.height();
				
				
				if((o.winW/o.winH) >= (o.imgW/o.imgH)){
					
					
					obj.css({
						width: "auto",
						height: "auto"
					});
					obj.css({
						width: o.winW,
						top: 0,
						left: 0
					});
					
					return false;
					obj.css({width:o.winW+"px", height:o.winW/(o.imgW/o.imgH)+"px"});
					o.top = (o.winH-(o.winW/(o.imgW/o.imgH)))/2;
					if( option.topZero == 1){
						o.top = 0;
					};
					obj.css({left:"0px", top:o.top+"px"});
				}
				
				else{
					obj.css({
						width: "auto",
						height: "auto"
					});
					obj.css({
						height: o.winH
					});
					obj.css({
						left: (obj.width()-$(window).width())/2 *(-1)
					});
					
					return false;
					obj.css({height:o.winH+"px", width:o.winH/(o.imgH/o.imgW)+"px"});
					o.left = (o.winW-(o.winH/(o.imgH/o.imgW)))/2;
					if( option.topZero == 1){
						o.top = 0;
					};
					obj.css({top:"0px", left:o.left+"px"});
				}
			}
		
		
			if( option.type == "full" ){
				
				option.do_resize( option.obj );
				
				$(window).resize(function(){
												  
					option.do_resize(	option.obj );
					
					option.obj.find("img").each(function(){
																	 
						option.img_size( $(this) );
					
					});
												  
				});
				
			}
		
		/* Drag movement */
			option.dragMovement = function(){
				var drag = $("#drag");
				var scrollBar = $("#scrollBar .padding");
				var scroller = $("#slideShow .scroller");
				
				var scrollBarWidth = scrollBar.width();
				
				drag.bind("mousedown", function(e){
					if( option.count < 8 ){ return false;}
					e.preventDefault();
					var dragWidth = drag.outerWidth();
					var perPixel = (scroller.outerWidth() - 440) / (scrollBarWidth - dragWidth);
					
					var offsetLeft = scrollBar.offset().left;
					var hotSpot = e.pageX - drag.offset().left;
					
					$(document).bind("mousemove", function(e){
						clearTimeout( option.timer );
						var positionLeft = e.pageX - offsetLeft - hotSpot;
						
						if( positionLeft < 0 ){ positionLeft = 0; }
						else if( positionLeft + dragWidth >=  scrollBarWidth ){
							
							positionLeft = scrollBarWidth - dragWidth;	
						};
						
						drag.css({
							left: positionLeft 
						});
						
						scroller.css({
							left: positionLeft*perPixel*(-1)
						});
						
					});
					
					$(document).bind("mouseup", function(){
						$(this).unbind("mousemove mouseup");
					});
					
				});
				
				/*
				$("#slideShow").bind("mousewheel", function (event, delta){
					if( option.count < 8 ){ return false;}
					var remove = option.count
					var currentLeft = scroller.position().left / 55*(-1);
					var perScroll = (430-drag.width())/(option.count-8);
					currentLeft = currentLeft - delta;
					if( currentLeft < 0 ){ currentLeft = 0; }
					else if( currentLeft+8 >= option.count ){currentLeft = option.count - 8;}
					var scrollLeft = currentLeft * 55;
					
					scroller.css({
						left: scrollLeft*(-1)
					});
					drag.css({
						left: currentLeft*perScroll
					});
					
				});
				*/
				var userAgent = navigator.userAgent.toLowerCase();

				$("#slideShow").bind("mousewheel", function (event, delta){
					if( option.count < 8 ){ return false;}
					
					if( jQuery.browser.webkit || userAgent.indexOf("mac") !== -1 ){
						delta = delta*(-1)
					};
					var main = $("#slideShow");
					var scroller = main.find(".scroller");
					var track = main.find(".padding");
					var drag = main.find("#drag");
					var pixels = track.width() - drag.width();
					var perPixel = (scroller.width()-440) / pixels;
					
					if( delta < 0 ){ delta = track.width() /(track.width() / drag.width()*(-1));}
					else{delta = track.width() /(track.width() / drag.width());}
					
					
					var left = Math.floor(drag.position().left+(delta/5));
					
					if( left < 0 ){ left = 0;}
					else if( left > 430 - drag.width() ){ left = 430-drag.width(); };
					
					drag.css({
						left : left
					});
					
					scroller.css({
						left: left*perPixel*(-1)
					});
					
				});
				
			};
			
		/* Keypresses */
			option.keypress = function(){
				$(document).bind("keypress", function(e){
					var code = e.keyCode ? e.keyCode : e.which;
					if( code == 37 ){ option.navi("prev");}
					else if( code == 39 ){ option.navi("next"); }
	
				});
				
				$("#newImages").unbind("click").bind("click", function(e){
					$("#slideShow .scroller").css("left", 0);
					if( option.flag == 1 ){
					
						option.timeout = setTimeout(function(){
							$("#newImages").trigger("click");
						}, option.speed);
						
						return false;
					}else{
						option.newImages();
					};
				});
				
			};

			option.scrollClicks = function(){
				var items = option.slideShow.find(".item");
				
				items.bind("click", function(e){
					e.preventDefault();
					if( option.flag == 1 ) {return false; }
					if( $(this).attr("rel") == option.step ){ return false;}
					
					if( option.no_loader == "true" ){
						option.click = "false";
					} else {
						option.click = "true";
					}

					option.step = $(this).attr("rel");
					option.swap( option.step );
					items.removeClass("active");
					$(this).addClass("active");
				});
				
			};
			
		/* After you have appended new images */
			option.newImages = function(){
				
	
				
				option.count = 0;
				option.newImagesFlag = 1;
				option.removeOldImages();
				option.step = 0;
				option.count_images();
				
				option.navi("next");
				
			};
			
		
		/* To Navigate between pages */
			option.navi = function( obj ){
				
				var step = option.step;
				if( option.flag == 1 ) {return false; }
				
				if( obj == "next" || !obj ){
					step++;
				}
				else if( obj == "prev" ){
					step--;
				}
				else{
					step = obj;
				}
				if( parseInt(step) > parseInt(option.count) ){
					
					step = 1;	
					
					
				}
				else if( parseInt(step) < 1 ){
					
					step = option.count;
				
				}
	
				option.slideShow.find(".item[rel='"+step+"']").trigger("click");	
				
			}
			
		/* To swap the image */
			option.swap = function(){
				
				clearTimeout( option.timer ); option.timer = '';
				
				if( option.loader == "true" && option.click == "true") { $(".loader").show();}
				option.click = "false";
				option.no_loader = "true";
				option.flag = 1;
				
				var o = {};
				o.img = new Image();
				
				o.img.onload = function(){
					
					option.process( option.step );
					
				};
				o.img.onerror = function(){
					option.flag = 0;
					option.navi("next");	
				};
				
				o.img.src = option.obj.find("img[rel='"+option.step+"']").attr("src2");
			}
		
		/* Timeout bug fix */
			var fire_navi = function(){
				option.no_loader = "true";
				option.navi();
				
			}
		
		/* To process the swap */
		
			option.process = function( obj ){
				
				if( option.loader == "true") { $(".loader").hide();}
				
				
				if( option.count > 8 ){
					

					var step = option.step-1;
					if( step <= 4 ){ step = 0;}
					else if( step+4 >= option.count ){ step = option.count-8;}
					else{ step = step-4;}
					
					option.slideShow.find(".scroller").animate({left: (step)*55*(-1)}, {duration:200, queue:false});
					
					var drag = option.slideShow.find("#drag");
					var perPix = (430-drag.width())/(option.count-8);
										
					drag.animate({left:  (step)*perPix}, {duration:200, queue:false});
					
				};
				
				var object = option.obj.find("img[rel='"+option.step+"']");
				
				option.obj.find(".active").fadeOut( option.speed );
				
				object.attr("src", object.attr("src2")).addClass("active");
				
				option.img_size( object );
				
				object.fadeIn( option.speed , function(){
				
					$("#background img").each(function(){
						if( !$(this).attr("rel") ){ $(this).remove();}	
					});
					
					option.flag = 0;
					if( option.count == 1 ){
						return false;
					};
					if( option.auto == "true" ){
						option.timer = setTimeout(fire_navi, option.delay);
						
					}
					
				});
			
			}
		
		option.count_images( $(this) );
		option.navi();
		option.keypress();
		option.dragMovement();
		
		$("#arrowLeft").bind("click", function(e){
			e.preventDefault();
			option.no_loader = "false";
			option.navi("prev");
		});
		
		$("#arrowRight").bind("click", function(e){
			e.preventDefault();
			option.no_loader = "false";
			option.navi("next");
		});

		
}

