(function($) {
	
	$.fn.panelViewer = function(options) {
		
		var defaults = {
			btnClose: '.pnlClose',
			btnNext: '.pnlNext',
			btnPrev: '.pnlPrev',
			panel: '.pnlPanel',
			teaser: '.pnlTeaser',
			content: '.pnlContent',
			useIntent: false
		};
		
		var _o = $.extend(defaults, options),
			_btnClose = $(_o.btnClose, this),
			_btnNext = $(_o.btnNext, this),
			_btnPrev = $(_o.btnPrev, this),
			_view = $(_o.view, this),
			_panels = $(_o.panel, this),
			_nextPanel = null,
			_currPanel = null,
			_prevPanel = null,
			_firstPanel = _panels.first(),
			_lastPanel = _panels.last(),
			_buttons = [_btnClose, _btnNext, _btnPrev];
			
		function changePanel(panel){
			
			$.each(_buttons, function(i,el){ el.show(); });
			
			var inPanel = _view.find('div').filter(function(){ 
			  return $(this).data('c') == panel.find(_o.teaser).data('t');
			});
			var outPanel = _view.find('div').filter(function(){ 
			  return $(this).data('c') == (_currPanel == null ? 0 : _currPanel.find(_o.teaser).data('t'));
			});
			
			if (inPanel.data('c') != outPanel.data('c')) {
			
				_currPanel = panel;			
				_panels.not(_currPanel).find(_o.teaser).removeClass('active'); //.fadeTo(100,0.3);
				_currPanel.find(_o.teaser).addClass('active'); //.fadeTo(100,1);
				
				var next = _currPanel.next(_o.panel),
					prev = _currPanel.prev(_o.panel);
				_nextPanel = next.length == 0 ? _firstPanel : next;
				_prevPanel = prev.length == 0 ? _lastPanel : prev;
				
				outPanel.fadeOut(100);
				inPanel.fadeIn(200);
				
			}

		};
		
		function resetPanels(){
		
			$.each(_buttons, function(i,el){ el.hide(); });

			var outPanel = _view.find('div').filter(function(){ 
			  return $(this).data('c') == _currPanel.find(_o.teaser).data('t');
			});
			var inPanel = _view.find('div').filter(function(){ 
			  return $(this).data('c') == 0;
			});

			outPanel.fadeOut(100);
			inPanel.fadeIn(200);
			
			_panels.find(_o.teaser).fadeTo(100,1);
			
			_currPanel = null;
		};
		
		function init(){
			
			_view.wrapInner('<div />');
			_view.find('div:first').show().data('c', 0);
			_panels.each(function(i){ 
				++i;
				$(this).find(_o.content).detach().appendTo(_view).addClass('c' + i).data('c', i).hide();
				$(this).find(_o.teaser).data('t', i)
			});
			
		};
		
		return this.each(function(){
			
			init();
							
			$(_o.teaser).find('a').bind('click', function(e){
				//changePanel($(e.target).closest(_o.panel));
				//e.preventDefault();
			});
							
			if (_o.useIntent){
							
				$(_o.teaser).hoverIntent({
					sensitivity: 7,
					interval: 100,
					timeout: 0,
					over: function(e){
						changePanel($(e.target).closest(_o.panel));
					},
					out: function(){}
				});
							
			};
							
			_btnClose.bind('click', function(e){
				resetPanels();
				e.preventDefault();
			});
						
			_btnNext.bind('click', function(e){
				changePanel(_nextPanel);
				e.preventDefault();
			});
						
			_btnPrev.bind('click', function(e){
				changePanel(_prevPanel);
				e.preventDefault();
			});
								
		});	
			
	};

})(jQuery);
