(function($){
		var isOn = false;
		var obj;
		var wrapper;
		var images;
		var animatedIn = false;
		var totalWidth = 0;
		var imageIndex = 0;
		var loadIndex = 0;
		var lastImageIndex = -1;
		var options;
		$.fn.iwsSlideshowB = function( method ) {
			if ( methods[method] ) {
			  return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
			} else if ( typeof method === 'object' || ! method ) {
			  return methods.init.apply( this, arguments );
			} else {
			  $.error( 'Method ' +  method + ' does not exist on iwsSlideshow' );
			}
		};
		
		var methods = {
			
		init : function(imageList,options) {  
		var defaults = {
		  type: "link",
		  defaultLinkTarget: "_self",
		  classExt: "",
		  width:685,
		  height:440,
		  speed:5000,
		  wrapperClass: null,
		  prependLabel: "",
		  loadingIndicator: null,
		  prepend:"",
		  preload:1,
		  nav: null,
		  nextBtn: null,
		  prevBtn: null,
		  fillLabel: null,
		  maxHeight: null,
		  slide: true,
		  input: null,
		  onSelect: null,
		  onHover: null,
		  onOut: null,
		  onOpen:null,
		  onClose:null,
		  circular:true
		};
		options = $.extend(defaults, options);
		
		$(this).data('options',options);
		$(this).data("methods",methods);
		var api = $(this).data("methods");
		
		if(options.speed > 0 && imageList.length > 1)
			$(this).everyTime(3000,methods["Next"],5000);

		return this.each(function() {
			if(options.loadingIndicator)
				loadingIndicator = $(options.loadingIndicator);
				
			if(options.nextBtn)
				options.nextBtn.click(methods["Next"]);
				
			if(options.prevBtn)
				options.prevBtn.click(methods["Prev"]);
				
			images = imageList;
			for(var i = 0;i < images.length;i++) {
				images[i].image = options.prepend + images[i].image;
			}
			/*images = [{image:'/content/slides/slideshow_home_1.jpg', duration:'5'}, 
    	 				{image:'/content/slides/slideshow_home_2.jpg', duration:'5'},
						{image:'/content/slides/slideshow_home_3.jpg', duration:'5'}
					];*/
					
			obj = $(this);
			
			var body = obj.html();
			
			if(options.wrapperClass)
				body += '<div class="'+options.wrapperClass+'">';
			
			body += '<div class="iwsSlideshowWrapper" style="width:'+options.width+'px;height:'+options.height+'px;">'
			body += '<div class="iwsSlideshowSlider">'
			
			if(options.nav)
				nav = $(options.nav).html();
				
			for(var i = 0;i < images.length;i++) {
				body += "<div class='iwsSlideshowImageContainer'><img class='iwsSlideshowImage' id='iwsSlideshowImage"+i+"' /></div>";
				
				if(options.nav) {
					nav += '<a class="iwsSlideshowNavItem" href="#" rel="'+i+'">'+ (i+1) + "</a>";
				}
			}
			
			body += '</div>';
			body += '</div>';
			
			if(options.wrapperClass)
				body += "</div>";

			if(options.nav)
			{
				$(options.nav).html(nav);
				$(".iwsSlideshowNavItem",options.nav).each(function() {
					$(this).click(handleNavClick);	
				});
			}
				
			obj.html(body);
			wrapper = $(".iwsSlideshowWrapper",obj);
			methods["LoadImage"]();
		});
		},
		
		setNavState : function (i) {
		//	options = $(this).data('options');
		//	$(".iwsSlideshowNavItem",options.nav).removeClass("iwsSlideshowNavItemOn");
		//	$(".iwsSlideshowNavItem[rel='"+i+"']",options.nav).addClass("iwsSlideshowNavItemOn");
		},
		
		handleNavClick : function() {
			imageIndex = $(this).attr("rel");
			options = $(this).data('options');
			if(options.loadingIndicator)
			{
				loadingIndicator.css("display","none");
				if(loadIndex >= imageIndex)
					loadingIndicator.css("display","none");
				else loadingIndicator.css("display","block");
			}
				
			if($("#iwsSlideshowImage"+imageIndex,obj).attr("src") != undefined)
				animateToIndex();
		},
		
		LoadImage : function() {
			loadIndex = 0;
			for(var i = 0;i < images.length;i++) {
				if(i == 0)
					$("#iwsSlideshowImage"+i).attr("src",images[i].image).load(methods["handleImageLoaded"])
				else $("#iwsSlideshowImage"+i).attr("src",images[i].image).load();
			}
			methods["animateToIndex"]();
			/*if($("#iwsSlideshowImage"+loadIndex,obj).attr("src") == undefined || $("#iwsSlideshowImage"+loadIndex,obj).attr("src") == '')
			{
				$("#iwsSlideshowImage"+loadIndex).attr("src",images[loadIndex].image).load(methods["handleImageLoaded"]).error(function() {alert("Error");});
				methods["animateToIndex"]();
			}
			else {
				$("#iwsSlideshowImage"+loadIndex).attr("src",images[loadIndex].image).load(methods["handleImageLoaded"]).error(function() {alert("Error");});
				methods["animateToIndex"]();
				//if(options.loadingIndicator)
					//loadingIndicator.css("display","block");
			}*/
		},
		
		animateToIndex_slide : function() {
			options = $(this).data('options');
			if(options.onChange)
				options.onChange($("#iwsSlideshowImage"+imageIndex))
				
			setNavState(imageIndex);
			$(".iwsSlideshowSlider",obj).stop().animate(
				{"left":"-"+$("#iwsSlideshowImage"+imageIndex,obj).attr("data-scrollX")+"px"},500);
		},
		animateToIndex : function () {
			//if(options.onChange)
			//	options.onChange($("#iwsSlideshowImage"+imageIndex))	
			methods["setNavState"](imageIndex);
			$("#iwsSlideshowImage"+imageIndex,obj).css("opacity","0");
			$("#iwsSlideshowImage"+imageIndex,obj).css("left","0px");
			$("#iwsSlideshowImage"+imageIndex,obj).stop().animate(
				{opacity:1},500
			);
			$("#iwsSlideshowImage"+lastImageIndex,obj).stop().delay(200).animate(
				{opacity:0},500
			);
		},
		
		
		handleImageLoaded : function () {
			
			/*options = $(this).data('options');

			if(options.loadingIndicator)
				if(loadIndex >= imageIndex)
					loadingIndicator.css("display","none");
				else loadingIndicator.css("display","block");
				
			$(this).css("opacity",1);
			var cx = (options.width - parseInt($(this).css("width")))/2;*/
			var cx = 0;
			//alert(cx);
			
			$(this).css("left",totalWidth+"px");
			$(this).attr("data-scrollX",totalWidth-cx);
			totalWidth += parseInt($(this).css("width"));
			
			methods["checkPreload"]();
			if(!animatedIn) {
				animatedIn = true;
				methods["animateToIndex"]();
			}
			//if(parseInt(loadIndex) >= (parseInt(imageIndex) + parseInt(options.preload))) {
				//animateToIndex();
			//}
		},
		
		checkPreload : function () {
			options = $(this).data('options');
			//if((loadIndex + options.preload) >= imageIndex)
			{
				loadIndex++;
				if(loadIndex > images.length-1)
					return;
				
				if(!$("#iwsSlideshowImage"+loadIndex,obj).attr("src") != undefined) {
					$("#iwsSlideshowImage"+loadIndex,obj).attr("src",images[loadIndex].image).load(methods["handleImageLoaded"]).error(function() {alert("Error");});
				}
				
			}
		},

		Next : function (el) {
			options = $(this).data('options');
			
			lastImageIndex = imageIndex;
			imageIndex++;
			
			if(options.loadingIndicator)
				if(loadIndex > imageIndex)
					loadingIndicator.css("display","none");
				else loadingIndicator.css("display","block");
				
			if(images) {
				if(imageIndex > images.length-1)
					if(options.circular)
						imageIndex = 0;
					else imageIndex = images.length-1;	
			}
			
			if($("#iwsSlideshowImage"+imageIndex,$(this)).attr("src") != undefined)
				methods["animateToIndex"]();
		},
		
		Prev : function () {
			options = $(this).data('options');
			imageIndex--;
			if(imageIndex < 0)
				imageIndex = 0;
			if($("#iwsSlideshowImage"+imageIndex,obj).attr("src") != undefined)
				animateToIndex();
		},
		
		Goto : function(index) {
			if(imageIndex == index)
				return;
				
			lastImageIndex = imageIndex;
			imageIndex = index;
			methods["animateToIndex"]();
		},
		
		executeCallback : function (func) {
			if(func)
				func();	
		}
		
};
})(jQuery);

