(function($){
	$.preloadImage = function(image, callback) {
		var self = this;
		this.file = image;
		this.callback = callback;
		this.loaded = false;
		this.aborted = false;
		this.error = false;
		this.image = new Image();
		
		this.cancelEvent = function() {
			if (this.image && this.image.onload) {
				this.image.onload = null;
			}
			if (this.image && this.image.onabort) {
				this.image.onabort = null;
			}
			if (this.image && this.image.onerror) {
				this.image.onerror = null;
			}
		};
		
		this.on_load = function() {
			self.loaded = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, ["loaded"]);
			}
			self.cancelEvent();
		};
		
		this.on_error = function() {
			self.error = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, ["error"]);
			}
			self.cancelEvent();
		};
		
		this.on_abort = function() {
			self.aborted = true;
			if (self.callback && typeof self.callback == "function") {
				self.callback.apply(self, ["aborted"]);
			}
			self.cancelEvent();
		};
		
		this.image.src = this.file;
		
		this.autoCheckIntervalId = 0;
		this.autoCheck = function() {
			if (self.loaded || self.aborted || self.error) {
				window.clearInterval(self.autoCheckIntervalId);
				return;
			}
			
			if (self.image && self.image.complete) {
				window.clearInterval(self.autoCheckIntervalId);
				self.loaded = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self, ["loaded"]);
				}
				self.cancelEvent();
				return;
			}
			
			if (self.image && (self.image.width || self.image.height)) {
				window.clearInterval(self.autoCheckIntervalId);
				self.loaded = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self, ["loaded"]);
				}
				self.cancelEvent();
				return;
			}
		};
		
		this.autoCheckIntervalId = window.setInterval( self.autoCheck, 500 );
	};
	
	$.preloadImages = function(images, callback) {
		if (typeof images == "string") {
			return new $.preloadImage(images, callback);
		}
		
		var self = this;
		this.images = images;
		this.callback = callback;
		this.all_complete = false;
		this.preloader = [];
		
		this.checkComplete = function() {
			if (self.preloader.length < self.images.length) return;
			if (this.all_complete) return;
			
			var all_complete = true;
			var j = 0;
			for (var i=0, n = self.preloader.length; i < n; i++) {
				if ( self.preloader[i] && self.preloader[i].loaded ) {
					j++;
				} else {
					all_complete = false;
				}
			}
			
			if (all_complete) {
				this.all_complete = true;
				if (self.callback && typeof self.callback == "function") {
					self.callback.apply(self);
				}
			}
		};
		
		for (var i=0, n = images.length; i < n; i++) {
			this.preloader[i] = new $.preloadImage(this.images[i], function(){
				self.checkComplete();
			});
		}
		
		this.autoCheckIntervalId = 0;
		this.autoCheck = function() {
			if (this.all_complete) {
				window.clearInterval(self.autoCheckIntervalId);
			}
			
			self.checkComplete();
		};
		
		this.autoCheckIntervalId = window.setInterval( self.autoCheck, 500 );
	};
	
	$.fn.carousel = function(options) {
		var isMethodCall = (typeof options == "string") || false;
		var args = arguments;
		
		$(this).each(function() {
			var carousel = $(this).data("carousel");
			if (carousel && isMethodCall) {
				if (carousel[options] && $.isFunction(carousel[options])) {
					carousel[options].apply(carousel, $.makeArray(args).slice(1));
				}
			} else if ( !isMethodCall && !carousel ) {
				carousel = new $.carousel(this, options);
				$(this).data("carousel", carousel);
			}
		});
		
		return this;
	};
	
	$.carousel = function(elem, options) {
		this.elem = elem;
		this.options = $.extend( {}, $.carousel.defaults, options );
		this.init();
	};
	
	$.extend( $.carousel, {
		defaults: {
			duration:1000,
			autoMode:1,
			autoMode_waitBeforeStart:5000,
			autoMode_delay:15000
		},
		prototype: {
			init:function(){
				var self = this;
				this.ready = false;
				
				this.slides = $(".carousel_contents ul li", this.elem);
				if (!this.slides.length) return;
				
				this.baseurl = $('script[src*=javascripts/carousel.js]:first').attr('src');
				this.baseurl = this.baseurl.substring(0, this.baseurl.indexOf("javascripts/carousel.js"));
				
				this.loader = $(".carousel_loader", this.elem);
				
				this.autoModeTimeout = 0;
				
				this.preloadImages();
			},
			preloadImages: function(){
				var to_preload = [];
				var self = this;
				
				$(".carousel_contents img", this.elem).each(function(){
					to_preload.push(this.src);
				});
				
				var preloader = new $.preloadImages(to_preload, function() {
					self.loaded.apply(self,[]);
				});
				
			},
			loaded: function(){
				var self = this;
				
				$(".carousel_loader", this.elem).animate({opacity:0},{duration:500,queue:false,complete:function(){
					$(this).hide().css("opacity","");
				}});
				$(".carousel_container", this.elem).css({opacity:0,display:"block"}).animate({opacity:1},{duration:750,queue:false,complete:function(){
					$(this).css("opacity","");
				}});
				
				this.slide_width = $(this.slides[0]).innerWidth();
				this.slide_height = $(this.slides[0]).innerHeight();
				
				this.slideContainer = $(".carousel_contents ul", this.elem);
				this.slideContainer.css({width: (this.slide_width * this.slides.length)+"px", height: this.slide_height+"px"});
				this.slideContainer.parent().css({height: this.slide_height+"px"});
				this.menu = $(".carousel_menu a", this.elem);
				$(this.menu).each(function(){
					$(this).bind("click", function(){
						self.resetAutoMode();
						self.moveTo.apply(self, [this]);
						return false;
					});
				});
				
				this.slideContainer.css({left:0});
				this.menu.removeClass("selected").filter(":eq(0)").addClass("selected");
				
				this.resetAutoMode();
			},
			moveTo: function(a) {
				if ($(a).is(".selected")) return;
				if (a.hash && a.hash.replace('#','') != "") {
					var target = $(a.hash);
					if (target.length) {
						var target_idx = this.slides.index(target);
						if (target_idx >= 0) {
							this.slideContainer.stop();
							this.slideContainer.animate({left: -(target_idx * this.slide_width)+"px"},{
								duration:this.options.duration,
								easing:"easeInOutExpo",
								queue:false
							});
						}
					}
				}
					
				this.menu.not(a).removeClass("selected");
				$(a).addClass("selected");
			},
			clearAutoMode: function(){
				if (this.autoModeTimeout > 0) {
					window.clearTimeout(this.autoModeTimeout);
				}
				this.autoModeTimeout = 0;
			},
			resetAutoMode: function(){
				var self = this;
				this.clearAutoMode();
				this.autoModeTimeout = window.setTimeout(function(){
					self.autoNext();
				}, this.options.autoMode_waitBeforeStart);
			},
			autoNext: function(){
				var cur, cur_next, self = this;
				
				this.clearAutoMode();
				cur = $(".carousel_menu a.selected", this.elem);
				if (!cur.length) {
					cur = $(".carousel_menu a:eq(0)", this.elem);
				}
				
				if (cur) {
					cur = $(cur[0]);
					cur_next = $(cur).parents("li").next();
					if (!cur_next.length) {
						cur_next = $(cur).parents("ul").find("li:eq(0)");
					}
				}
				
				if (!cur_next) return;
				
				cur_next = $($("a", cur_next[0])[0]);
				
				if (cur) {
					cur.removeClass("selected");
				}
				
				this.moveTo($(cur_next).get(0));
				this.autoModeTimeout = window.setTimeout(function(){
					self.autoNext();
				}, this.options.autoMode_delay);
			}
		}
	});
	
	$().ready(function(){
		$('.carousel').carousel();
	});
})(jQuery);