(function(LK) {
	
	LK.config['carousel'] = {
		crud:"<div><div class='items'></div></div>",
		controls:
			"<div class='controls'><div>"+
				"<a class='btn prev'><span><span>Previous</span></span></a>"+
				"<div class='navi'></div>"+
				"<a class='btn next'><span><span>Next</span></span></a>"+
			"</div></div>"
	}

	LK.jQ.fn.carousel = function(options) {
		var $carousel = this,
			o = jQuery.extend({
				circular:true,
				unitSelector:"p",
				cfig:LK.config.carousel
			}, options);

		function initialize() {
			$carousel
				.find(o.unitSelector)
					.wrapAll(o.cfig.crud)
					.end()
					
				.append(o.cfig.controls)
				.find("div:first")
					.scrollable(o)
					.navigator()

				.parent()
					.addClass("carousel-ready");

			LK.util.log("carousel initialized");
			return $carousel;
		}

		return initialize();	
	}

	LK.config['video'] = {
		controls: 
			// Using only natively inline elements 
			// to be able to append into <p>s
			"<span class='controls'>"+
				"<span class='play-pause pane'>"+
					"<a class='play btn'><span></span></a>"+
				"</span>"+
				"<span class='time pane'>"+
					"<span class='current'></span> / "+
					"<span class='total'></span>"+
				"</span>"+
				"<span class='progress pane'>"+
					"<span class='current'></span>"+
					"<span class='loaded'></span>"+
				"</span>"+
				"<span class='fullscreen pane'>"+
					"<a class='fs-off btn'><span></span></a>"+
				"</span>"+
				"<span class='volume pane'>"+
					"<a class='mute btn'><span></span></a>"+
					"<span class='markers'>"+
						"<span></span><span></span><span></span><span></span><span></span><span></span>"+
					"</span>"+
				"</span>"+
			"</span>"
	}
	
	LK.jQ.fn.video = function(options) {
		var $v = this, _v = this.get(0), $controls = null,
			_isControlbarVisible = false,
			_isFullscreen = false,
			_isPlaying = false,
			_pctLoaded = 0.0,
			o = jQuery.extend({
				useNativeControls:false,
				defaultVolume:0.85
			}, options),
			_isCustomControlbar = !o.useNativeControls;
		
		function initialize() {
			if (typeof _v.nodeName === "function" && _v.nodeName.toUpperCase() !== "VIDEO") {
				LK.util.log("Aborted initializing the video because this element is not a video", "warn");
				return false;
			}
			if (!LK.util.checkForVideoSupport()) {
				LK.util.log("HTML5 Video not supported", "warn");
				return false;
			}
			if (LK.util.checkForTouchDevice()) {
				_isCustomControlbar = false;
			}
			if (typeof _v.hasAttribute === "function" && _v.hasAttribute("preload")) {
				_v.autobuffer = true;
			}
			
			$v.parent().addClass("video-container");
			$v.append('<span class="notification feature-fallback">This browser does not support <a href="http://en.wikipedia.org/wiki/HTML5_video">HTML5 video</a>.</span>');
			
			_buildControls();
			_setupEvents();
			
			LK.util.log("video player initialized");
			return $v;
		}
		
		$v.cleanup = function() {
			$v.unbind("loadeddata");
			$v.unbind("mouseenter");
			$v.unbind("mouseleave");
			if (_isCustomControlbar) {
				
			}
		}
		
		function _buildControls() {
			if (!_isCustomControlbar) {
				_v.controls = true;
				return false;
			}
			_v.controls = false;
			$controls = LK.jQ(LK.config.video.controls).hide();
			$v.after($controls);
			$controls.css("opacity", 0.0).show().animate({"opacity":1.0}, 300);
		}
		
		function _setupEvents() {
			$v.bind("loadeddata", _readyHandler);
			$v.bind("mouseenter", _mouseEnterHandler);
			$v.bind("mouseleave", _mouseLeaveHandler);
			if (_isCustomControlbar) {
				
			}
		}
				
		function _readyHandler(event) {
			
		}
		
		function _mouseEnterHandler(event) {
			
		}
		
		function _mouseLeaveHandler(event) {
			
		}
		
		function _update() {
			
		}
		
		function _enterFullscreen() {
			_isFullscreen = true;
		}
		
		function _exitFullscreen() {
			_isFullscreen = false;
		}
		
		$v.play = function() {
			_v.play();
			_isPlaying = true;
		}
		
		$v.pause = function() {
			_v.pause();
			_isPlaying = false;
		}
		
		$v.toggleFullscreen = function() {
			if (_isFullscreen) {
				_exitFullscreen();
			} else {
				_enterFullscreen();
			}
		}
		
		$v.isPlayable = function() {
			$v.find("source").each(function() {
				if (_v.canPlayType(this.type) === "probably" || _v.canPlayType(this.type) === "maybe") {
					return true;
				}
			});
			return false;
		}
		
		return initialize();
	}
		
	LK.jQ.fn.topBeam = function(options) {
		var $tBeam = this,
			o = jQuery.extend({
				fadeOpacity:0.8
			}, options);
		
		function initialize() {
			LK.jQ(document).bind("scroll", _docScrollHandler);
			$tBeam.bind("mouseover", _beamMouseoverHandler).bind("mouseleave", _beamMouseleaveHandler);
			$tBeam.find(".dropdown > a").bind("click", _dropdownClickHandler);
			$tBeam.find(".dropdown").bind("mouseleave", _dropdownMouseleaveHandler);
			
			__setOpacity();
			
			LK.util.log("top beam initialized");
			return $tBeam;
		}
				
		$tBeam.cleanup = function() {
			LK.jQ(document).unbind("scroll");
			$tBeam.unbind("mouseover mouseleave");
			$tBeam.find(".dropdown > a").unbind("click", _dropdownClickHandler);
			$tBeam.find(".dropdown").unbind("mouseleave", _dropdownMouseleaveHandler);
		}
		
		function _dropdownClickHandler(event) {
			var $_hLI = LK.jQ(this).parent();
			event.preventDefault();
			$_hLI.toggleClass("active");
			$_hLI.find("ul").toggle();
		}
		
		function _dropdownMouseleaveHandler(event) {
			LK.jQ(this).removeClass("active");
			LK.jQ(this).find("ul").hide();
		}
		
		function _docScrollHandler(event) {
			__setOpacity();
		}
		
		function _beamMouseoverHandler(event) {
			$tBeam.css("opacity",1);
		}
		
		function _beamMouseleaveHandler(event) {
			__setOpacity();
		}
		
		function __setOpacity() {
			if (LK.jQ(document).scrollTop() > 5) {
				$tBeam.css("opacity",o.fadeOpacity);
			} else {
				$tBeam.css("opacity",1);
			}
		}
		
		return initialize();
	}
	
	LK.jQ.fn.searchForm = function(options) {
		var $form = this, $input, $btn, blurColor,
			o = jQuery.extend({
				selectors:{
					btn:".btn-wrapper"
				},
				focusColor:"#faffbd"
			}, options);
			
		function initialize() {
			$input = $form.find("input[type='text']");
			$btn = $form.find(o.selectors.btn);
			
			blurColor = $input.blur().css("background-color");
			__setBlurredButtonBG();
			
			$btn.bind("click", _btnClickHandler);
			$input.bind("focus", _inputFocusHandler).bind("blur", _inputBlurHandler);
			
			LK.util.log("search form initialized");
			return $form;
		}
		
		$form.cleanup = function() {
			$btn.unbind("click");
			$input.unbind("focus blur");
		}
			
		function _btnClickHandler(event) {
			$form.submit();
		}
		
		function _inputFocusHandler(event) {
			$btn.css("background-color",o.focusColor);
		}
		
		function _inputBlurHandler(event) {
			__setBlurredButtonBG();
		}
		
		function __setBlurredButtonBG() {
			$btn.css("background-color",blurColor);
		}
		
		return initialize();	
	}
	
	LK.jQ.fn.anchorTabs = function(options) {	
		var $container = this, $tabs, 
			o = jQuery.extend({
				selectors:{
					tabs:".tabs-nav",
					panels:".tabs-panel"
				},
				selectedClass:"selected"
			}, options);
		
		function initialize() {
			$tabs = $container.find(o.selectors.tabs);
			$tabs.find("li a").live("click", _navClickHandler);
			
			if ($tabs.find("li."+o.selectedClass).length === 0) {
				$tabs.find("li:first").addClass(o.selectedClass);
			}
			
			selectPanel($tabs.find("li."+o.selectedClass).children('a').attr('href'));
			
			LK.util.log("anchorTabs widget initialized");
			return $container;
		}
		
		$container.cleanup = function() {
			$tabs.find("li a").die("click");
		}
		
		function _navClickHandler(event) {
			event.preventDefault();
			var _$t = LK.jQ(event.target);
			
			$tabs.find("li").removeClass(o.selectedClass);
			_$t.parent().addClass(o.selectedClass);
			
			selectPanel(_$t.attr('href'));
		}
		
		function selectPanel(selector) {
			$container.find(o.selectors.panels).removeClass(o.selectedClass).hide();
			$container.find(selector).addClass(o.selectedClass).show();
		}
		
		return initialize();
	}
	
}(LK));